2015-10-26 15:27:31 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-24 19:21:49 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MediaWiki\User\UserIdentity;
|
|
|
|
|
2015-10-26 15:27:31 +00:00
|
|
|
/**
|
|
|
|
* Manages what wikis a user has unread notifications on
|
|
|
|
*/
|
|
|
|
class EchoUnreadWikis {
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var MWEchoDbFactory
|
|
|
|
*/
|
|
|
|
private $dbFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id Central user id
|
|
|
|
*/
|
|
|
|
public function __construct( $id ) {
|
|
|
|
$this->id = $id;
|
|
|
|
$this->dbFactory = MWEchoDbFactory::newFromDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return EchoUnreadWikis|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
|
2017-12-22 22:20:13 +00:00
|
|
|
* @return bool|\Wikimedia\Rdbms\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] = [
|
|
|
|
EchoAttributeManager::ALERT => [
|
2015-10-26 15:27:31 +00:00
|
|
|
'count' => $row->euw_alerts,
|
2022-03-23 22:41:16 +00:00
|
|
|
'ts' => $row->euw_alerts_ts === static::DEFAULT_TS_DB ?
|
|
|
|
static::DEFAULT_TS : wfTimestamp( TS_MW, $row->euw_alerts_ts ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
EchoAttributeManager::MESSAGE => [
|
2015-10-26 15:27:31 +00:00
|
|
|
'count' => $row->euw_messages,
|
2022-03-23 22:41:16 +00:00
|
|
|
'ts' => $row->euw_messages_ts === static::DEFAULT_TS_DB ?
|
|
|
|
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
|
2015-10-26 15:27:31 +00:00
|
|
|
$dbw->upsert(
|
|
|
|
'echo_unread_wikis',
|
2016-02-18 20:30:42 +00:00
|
|
|
$conditions + $values,
|
2020-02-19 21:16:58 +00:00
|
|
|
[ [ 'euw_user', 'euw_wiki' ] ],
|
2015-10-26 15:27:31 +00:00
|
|
|
$values,
|
|
|
|
__METHOD__
|
|
|
|
);
|
2016-02-23 15:21:26 +00:00
|
|
|
} else {
|
|
|
|
// No unread notifications, delete the row
|
|
|
|
$dbw->delete(
|
|
|
|
'echo_unread_wikis',
|
|
|
|
$conditions,
|
|
|
|
__METHOD__
|
|
|
|
);
|
2015-10-26 15:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|