2015-10-26 15:27:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages what wikis a user has unread notifications on
|
|
|
|
*/
|
|
|
|
class EchoUnreadWikis {
|
2016-01-25 15:33:06 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const DEFAULT_TS = '00000000000000';
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @return EchoUnreadWikis|bool
|
|
|
|
*/
|
|
|
|
public static function newFromUser( User $user ) {
|
2015-12-09 00:54:23 +00:00
|
|
|
$lookup = CentralIdLookup::factory();
|
|
|
|
$id = $lookup->centralIdFromLocalUser( $user, CentralIdLookup::AUDIENCE_RAW );
|
|
|
|
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
|
|
|
|
* @return bool|DatabaseBase
|
|
|
|
*/
|
|
|
|
private function getDB( $index ) {
|
|
|
|
return $this->dbFactory->getSharedDb( $index );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-23 15:21:26 +00:00
|
|
|
* @return array
|
2015-10-26 15:27:31 +00:00
|
|
|
*/
|
|
|
|
public function getUnreadCounts() {
|
|
|
|
$dbr = $this->getDB( DB_SLAVE );
|
|
|
|
if ( $dbr === false ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows = $dbr->select(
|
|
|
|
'echo_unread_wikis',
|
|
|
|
array(
|
|
|
|
'euw_wiki',
|
|
|
|
'euw_alerts', 'euw_alerts_ts',
|
|
|
|
'euw_messages', 'euw_messages_ts',
|
|
|
|
),
|
|
|
|
array( 'euw_user' => $this->id ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
|
|
|
$wikis = array();
|
|
|
|
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;
|
|
|
|
}
|
2015-10-26 15:27:31 +00:00
|
|
|
$wikis[$row->euw_wiki] = array(
|
2015-11-25 04:07:54 +00:00
|
|
|
EchoAttributeManager::ALERT => array(
|
2015-10-26 15:27:31 +00:00
|
|
|
'count' => $row->euw_alerts,
|
|
|
|
'ts' => $row->euw_alerts_ts,
|
|
|
|
),
|
2015-11-25 04:07:54 +00:00
|
|
|
EchoAttributeManager::MESSAGE => array(
|
2015-10-26 15:27:31 +00:00
|
|
|
'count' => $row->euw_messages,
|
|
|
|
'ts' => $row->euw_messages_ts,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $wikis;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $wiki
|
|
|
|
* @param int $alertCount
|
|
|
|
* @param MWTimestamp|bool $alertTime
|
|
|
|
* @param int $msgCount
|
|
|
|
* @param MWTimestamp|bool $msgTime
|
|
|
|
*/
|
|
|
|
public function updateCount( $wiki, $alertCount, $alertTime, $msgCount, $msgTime ) {
|
|
|
|
$dbw = $this->getDB( DB_MASTER );
|
|
|
|
if ( $dbw === false ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-18 20:30:42 +00:00
|
|
|
$conditions = array(
|
|
|
|
'euw_user' => $this->id,
|
|
|
|
'euw_wiki' => $wiki,
|
|
|
|
);
|
|
|
|
|
2016-02-23 15:21:26 +00:00
|
|
|
if ( $alertCount || $msgCount ) {
|
|
|
|
$values = array(
|
|
|
|
'euw_alerts' => $alertCount,
|
|
|
|
'euw_alerts_ts' => $alertCount
|
|
|
|
? $alertTime->getTimestamp( TS_MW )
|
|
|
|
: static::DEFAULT_TS,
|
|
|
|
'euw_messages' => $msgCount,
|
|
|
|
'euw_messages_ts' => $msgCount
|
|
|
|
? $msgTime->getTimestamp( TS_MW )
|
|
|
|
: static::DEFAULT_TS,
|
2015-10-26 15:27:31 +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,
|
2015-10-26 15:27:31 +00:00
|
|
|
array( 'euw_user', 'euw_wiki' ),
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|