2015-10-26 15:27:31 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
|
2021-06-24 19:21:49 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\CentralId\CentralIdLookup;
|
2021-06-24 19:21:49 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\Utils\MWTimestamp;
|
2022-11-13 07:48:43 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2021-06-24 19:21:49 +00:00
|
|
|
|
2015-10-26 15:27:31 +00:00
|
|
|
/**
|
|
|
|
* Manages what wikis a user has unread notifications on
|
|
|
|
*/
|
2022-11-13 07:48:43 +00:00
|
|
|
class UnreadWikis {
|
2019-10-23 10:31:35 +00:00
|
|
|
|
2020-09-19 18:55:32 +00:00
|
|
|
private const DEFAULT_TS = '00000000000000';
|
2022-03-23 22:41:16 +00:00
|
|
|
private const DEFAULT_TS_DB = '00010101010101';
|
2016-01-25 15:33:06 +00:00
|
|
|
|
2015-10-26 15:27:31 +00:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
/**
|
2022-11-12 07:19:00 +00:00
|
|
|
* @var DbFactory
|
2015-10-26 15:27:31 +00:00
|
|
|
*/
|
|
|
|
private $dbFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id Central user id
|
|
|
|
*/
|
|
|
|
public function __construct( $id ) {
|
|
|
|
$this->id = $id;
|
2022-11-12 07:19:00 +00:00
|
|
|
$this->dbFactory = DbFactory::newFromDefault();
|
2015-10-26 15:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-09 00:54:23 +00:00
|
|
|
* Use the user id provided by the CentralIdLookup
|
2015-10-26 15:27:31 +00:00
|
|
|
*
|
2021-06-24 19:21:49 +00:00
|
|
|
* @param UserIdentity $user
|
2022-11-13 07:48:43 +00:00
|
|
|
* @return UnreadWikis|false
|
2015-10-26 15:27:31 +00:00
|
|
|
*/
|
2021-06-24 19:21:49 +00:00
|
|
|
public static function newFromUser( UserIdentity $user ) {
|
|
|
|
$id = MediaWikiServices::getInstance()
|
|
|
|
->getCentralIdLookup()
|
|
|
|
->centralIdFromLocalUser( $user, CentralIdLookup::AUDIENCE_RAW );
|
2015-12-09 00:54:23 +00:00
|
|
|
if ( !$id ) {
|
|
|
|
return false;
|
2015-10-26 15:27:31 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 00:54:23 +00:00
|
|
|
return new self( $id );
|
2015-10-26 15:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $index DB_* constant
|
2022-11-13 07:48:43 +00:00
|
|
|
*
|
|
|
|
* @return bool|IDatabase
|
2015-10-26 15:27:31 +00:00
|
|
|
*/
|
|
|
|
private function getDB( $index ) {
|
|
|
|
return $this->dbFactory->getSharedDb( $index );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-07 19:35:03 +00:00
|
|
|
* @return array[][]
|
2015-10-26 15:27:31 +00:00
|
|
|
*/
|
|
|
|
public function getUnreadCounts() {
|
2017-09-24 05:23:47 +00:00
|
|
|
$dbr = $this->getDB( DB_REPLICA );
|
2015-10-26 15:27:31 +00:00
|
|
|
if ( $dbr === false ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2015-10-26 15:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$rows = $dbr->select(
|
|
|
|
'echo_unread_wikis',
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2015-10-26 15:27:31 +00:00
|
|
|
'euw_wiki',
|
|
|
|
'euw_alerts', 'euw_alerts_ts',
|
|
|
|
'euw_messages', 'euw_messages_ts',
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[ 'euw_user' => $this->id ],
|
2015-10-26 15:27:31 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$wikis = [];
|
2015-10-26 15:27:31 +00:00
|
|
|
foreach ( $rows as $row ) {
|
2016-02-23 15:21:26 +00:00
|
|
|
if ( !$row->euw_alerts && !$row->euw_messages ) {
|
|
|
|
// This shouldn't happen, but lets be safe...
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
$wikis[$row->euw_wiki] = [
|
2022-11-12 07:19:00 +00:00
|
|
|
AttributeManager::ALERT => [
|
2015-10-26 15:27:31 +00:00
|
|
|
'count' => $row->euw_alerts,
|
2022-07-15 21:13:27 +00:00
|
|
|
'ts' => wfTimestamp( TS_MW, $row->euw_alerts_ts ) === static::DEFAULT_TS_DB ?
|
2022-03-23 22:41:16 +00:00
|
|
|
static::DEFAULT_TS : wfTimestamp( TS_MW, $row->euw_alerts_ts ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2022-11-12 07:19:00 +00:00
|
|
|
AttributeManager::MESSAGE => [
|
2015-10-26 15:27:31 +00:00
|
|
|
'count' => $row->euw_messages,
|
2022-07-15 21:13:27 +00:00
|
|
|
'ts' => wfTimestamp( TS_MW, $row->euw_messages_ts ) === static::DEFAULT_TS_DB ?
|
2022-03-23 22:41:16 +00:00
|
|
|
static::DEFAULT_TS : wfTimestamp( TS_MW, $row->euw_messages_ts ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2015-10-26 15:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $wikis;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-18 23:46:53 +00:00
|
|
|
* @param string $wiki Wiki code
|
|
|
|
* @param int $alertCount Number of alerts
|
|
|
|
* @param MWTimestamp|bool $alertTime Timestamp of most recent unread alert, or
|
|
|
|
* false meaning no timestamp because there are no unread alerts.
|
|
|
|
* @param int $msgCount Number of messages
|
|
|
|
* @param MWTimestamp|bool $msgTime Timestamp of most recent message, or
|
|
|
|
* false meaning no timestamp because there are no unread messages.
|
2015-10-26 15:27:31 +00:00
|
|
|
*/
|
|
|
|
public function updateCount( $wiki, $alertCount, $alertTime, $msgCount, $msgTime ) {
|
2021-05-03 07:28:02 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
2018-02-22 03:07:45 +00:00
|
|
|
if ( $dbw === false || $dbw->isReadOnly() ) {
|
2015-10-26 15:27:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$conditions = [
|
2016-02-18 20:30:42 +00:00
|
|
|
'euw_user' => $this->id,
|
|
|
|
'euw_wiki' => $wiki,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-02-18 20:30:42 +00:00
|
|
|
|
2016-02-23 15:21:26 +00:00
|
|
|
if ( $alertCount || $msgCount ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$values = [
|
2016-02-23 15:21:26 +00:00
|
|
|
'euw_alerts' => $alertCount,
|
2022-03-23 22:41:16 +00:00
|
|
|
'euw_alerts_ts' => $dbw->timestamp(
|
|
|
|
$alertTime
|
|
|
|
? $alertTime->getTimestamp( TS_MW )
|
|
|
|
: static::DEFAULT_TS_DB
|
|
|
|
),
|
2016-02-23 15:21:26 +00:00
|
|
|
'euw_messages' => $msgCount,
|
2022-03-23 22:41:16 +00:00
|
|
|
'euw_messages_ts' => $dbw->timestamp(
|
|
|
|
$msgTime
|
|
|
|
? $msgTime->getTimestamp( TS_MW )
|
|
|
|
: static::DEFAULT_TS_DB
|
|
|
|
),
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-02-23 15:21:26 +00:00
|
|
|
|
2016-02-18 20:30:42 +00:00
|
|
|
// when there is unread alert(s) and/or message(s), upsert the row
|
2024-04-02 14:38:05 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
|
|
|
->insertInto( 'echo_unread_wikis' )
|
|
|
|
->row( $conditions + $values )
|
|
|
|
->onDuplicateKeyUpdate()
|
|
|
|
->uniqueIndexFields( [ 'euw_user', 'euw_wiki' ] )
|
|
|
|
->set( $values )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
2016-02-23 15:21:26 +00:00
|
|
|
} else {
|
|
|
|
// No unread notifications, delete the row
|
2024-04-12 19:50:43 +00:00
|
|
|
$dbw->newDeleteQueryBuilder()
|
|
|
|
->deleteFrom( 'echo_unread_wikis' )
|
|
|
|
->where( $conditions )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
2015-10-26 15:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|