2013-05-24 22:51:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entity that represents a notification target user
|
|
|
|
*/
|
|
|
|
class MWEchoNotifUser {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification target user
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
private $mUser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object cache
|
|
|
|
* @var BagOStuff
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
2014-07-18 03:58:21 +00:00
|
|
|
* Database access gateway
|
|
|
|
* @var EchoUserNotificationGateway
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
2014-07-18 03:58:21 +00:00
|
|
|
private $userNotifGateway;
|
2013-05-24 22:51:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for initialization
|
2014-07-18 03:58:21 +00:00
|
|
|
* @param User
|
|
|
|
* @param BagOStuff
|
|
|
|
* @param EchoUserNotificationGateway
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
2014-07-18 03:58:21 +00:00
|
|
|
private function __construct( User $user, BagOStuff $cache, EchoUserNotificationGateway $userNotifGateway ) {
|
2013-05-24 22:51:47 +00:00
|
|
|
$this->mUser = $user;
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->userNotifGateway = $userNotifGateway;
|
|
|
|
$this->cache = $cache;
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method
|
|
|
|
* @param $user User
|
|
|
|
* @throws MWException
|
|
|
|
* @return MWEchoNotifUser
|
|
|
|
*/
|
|
|
|
public static function newFromUser( User $user ) {
|
|
|
|
if ( $user->isAnon() ) {
|
|
|
|
throw new MWException( 'User must be logged in to view notification!' );
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
global $wgMemc;
|
|
|
|
return new MWEchoNotifUser(
|
|
|
|
$user, $wgMemc,
|
|
|
|
new EchoUserNotificationGateway( $user, MWEchoDbFactory::newFromDefault() )
|
|
|
|
);
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear talk page notification when users visit their talk pages. This
|
|
|
|
* only resets if the notification count is less than max notification
|
|
|
|
* count. If the user has 99+ notifications, decrementing 1 bundled talk
|
|
|
|
* page notification would not really affect the count
|
|
|
|
*/
|
|
|
|
public function clearTalkNotification() {
|
|
|
|
// There is no new talk notification
|
|
|
|
if ( $this->cache->get( $this->getTalkNotificationCacheKey() ) === '0' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do nothing if the count display meets the max 99+
|
|
|
|
if ( $this->notifCountHasReachedMax() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the talk page notification as read
|
|
|
|
$this->markRead(
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->userNotifGateway->getUnreadNotifications(
|
2013-05-24 22:51:47 +00:00
|
|
|
'edit-user-talk'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->flagCacheWithNoTalkNotification();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag the cache with new talk notification
|
|
|
|
*/
|
|
|
|
public function flagCacheWithNewTalkNotification() {
|
|
|
|
$this->cache->set( $this->getTalkNotificationCacheKey(), '1', 86400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag the cache with no talk notification
|
|
|
|
*/
|
|
|
|
public function flagCacheWithNoTalkNotification() {
|
|
|
|
$this->cache->set( $this->getTalkNotificationCacheKey(), '0', 86400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Memcache key for talk notification
|
|
|
|
*/
|
|
|
|
public function getTalkNotificationCacheKey() {
|
|
|
|
global $wgEchoConfig;
|
|
|
|
|
|
|
|
return wfMemcKey( 'echo-new-talk-notification', $this->mUser->getId(), $wgEchoConfig['version'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the user has more notification count than max count display
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function notifCountHasReachedMax() {
|
|
|
|
global $wgEchoMaxNotificationCount;
|
|
|
|
|
|
|
|
if ( $this->getNotificationCount() > $wgEchoMaxNotificationCount ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves number of unread notifications that a user has, would return
|
|
|
|
* $wgEchoMaxNotificationCount + 1 at most
|
|
|
|
*
|
|
|
|
* @param $cached bool Set to false to bypass the cache.
|
2013-10-08 20:48:54 +00:00
|
|
|
* @param $dbSource int use master or slave database to pull count
|
2013-05-24 22:51:47 +00:00
|
|
|
* @return integer: Number of unread notifications.
|
|
|
|
*/
|
|
|
|
public function getNotificationCount( $cached = true, $dbSource = DB_SLAVE ) {
|
|
|
|
global $wgEchoConfig;
|
|
|
|
|
|
|
|
if ( $this->mUser->isAnon() ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$memcKey = wfMemcKey( 'echo-notification-count', $this->mUser->getId(), $wgEchoConfig['version'] );
|
|
|
|
|
|
|
|
if ( $cached && $this->cache->get( $memcKey ) !== false ) {
|
|
|
|
return (int)$this->cache->get( $memcKey );
|
|
|
|
}
|
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
$eventTypesToLoad = $attributeManager->getUserEnabledEvents( $this->mUser, 'web' );
|
|
|
|
$count = $this->userNotifGateway->getNotificationCount( $dbSource, $eventTypesToLoad );
|
2013-05-24 22:51:47 +00:00
|
|
|
|
|
|
|
$this->cache->set( $memcKey, $count, 86400 );
|
|
|
|
|
|
|
|
return (int)$count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark one or more notifications read for a user.
|
|
|
|
* @param $eventIds Array of event IDs to mark read
|
|
|
|
*/
|
|
|
|
public function markRead( $eventIds ) {
|
|
|
|
$eventIds = array_filter( (array)$eventIds, 'is_numeric' );
|
|
|
|
if ( !$eventIds || wfReadOnly() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->userNotifGateway->markRead( $eventIds );
|
2013-05-24 22:51:47 +00:00
|
|
|
$this->resetNotificationCount( DB_MASTER );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to mark all notifications as read
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function markAllRead() {
|
|
|
|
if ( wfReadOnly() || $this->notifCountHasReachedMax() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only update all the unread notifications if it isn't a huge number.
|
|
|
|
// TODO: Implement batched jobs it's over the maximum.
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->userNotifGateway->markAllRead();
|
2013-05-24 22:51:47 +00:00
|
|
|
$this->resetNotificationCount( DB_MASTER );
|
|
|
|
$this->flagCacheWithNoTalkNotification();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recalculates the number of notifications that a user has.
|
2013-10-08 20:48:54 +00:00
|
|
|
* @param $dbSource int use master or slave database to pull count
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
|
|
|
public function resetNotificationCount( $dbSource = DB_SLAVE ) {
|
|
|
|
$this->getNotificationCount( false, $dbSource );
|
|
|
|
$this->mUser->invalidateCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves formatted number of unread notifications that a user has.
|
|
|
|
* @param $cached bool Set to false to bypass the cache.
|
2013-10-08 20:48:54 +00:00
|
|
|
* @param $dbSource int use master or slave database to pull count
|
2013-05-24 22:51:47 +00:00
|
|
|
* @return String: Number of unread notifications.
|
|
|
|
*/
|
|
|
|
public function getFormattedNotificationCount( $cached = true, $dbSource = DB_SLAVE ) {
|
|
|
|
return EchoNotificationController::formatNotificationCount(
|
|
|
|
$this->getNotificationCount( $cached, $dbSource )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-24 00:22:08 +00:00
|
|
|
/**
|
|
|
|
* Get the user's email notification format
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getEmailFormat() {
|
|
|
|
global $wgAllowHTMLEmail;
|
|
|
|
|
|
|
|
if ( $wgAllowHTMLEmail ) {
|
|
|
|
return $this->mUser->getOption( 'echo-email-format' );
|
|
|
|
} else {
|
|
|
|
return EchoHooks::EMAIL_FORMAT_PLAIN_TEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|