2013-05-24 22:51:47 +00:00
|
|
|
<?php
|
2016-12-08 20:50:03 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2013-05-24 22:51:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Entity that represents a notification target user
|
|
|
|
*/
|
|
|
|
class MWEchoNotifUser {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification target user
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
private $mUser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object cache
|
2016-12-08 20:50:03 +00:00
|
|
|
* @var WANObjectCache
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
/**
|
|
|
|
* Notification mapper
|
|
|
|
* @var EchoNotificationMapper
|
|
|
|
*/
|
|
|
|
private $notifMapper;
|
|
|
|
|
2014-08-07 00:07:34 +00:00
|
|
|
/**
|
|
|
|
* Target page mapper
|
|
|
|
* @var EchoTargetPageMapper
|
|
|
|
*/
|
|
|
|
private $targetPageMapper;
|
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
/**
|
|
|
|
* @var EchoForeignNotifications
|
|
|
|
*/
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
private $foreignNotifications = null;
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2016-03-24 15:33:13 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $cached;
|
|
|
|
|
2016-05-29 20:54:15 +00:00
|
|
|
/**
|
|
|
|
* @var array|null
|
|
|
|
*/
|
|
|
|
private $mForeignData = null;
|
|
|
|
|
2016-03-09 04:50:31 +00:00
|
|
|
// The max notification count shown in badge
|
|
|
|
|
|
|
|
// The max number shown in bundled message, eg, <user> and 99+ others <action>.
|
|
|
|
// This is really a totally separate thing, and could be its own constant.
|
|
|
|
|
|
|
|
// WARNING: If you change this, you should also change all references in the
|
|
|
|
// i18n messages (100 and 99) in all repositories using Echo.
|
|
|
|
const MAX_BADGE_COUNT = 99;
|
|
|
|
|
2013-05-24 22:51:47 +00:00
|
|
|
/**
|
2014-08-13 22:00:25 +00:00
|
|
|
* Usually client code doesn't need to initialize the object directly
|
|
|
|
* because it could be obtained from factory method newFromUser()
|
2014-08-07 00:07:34 +00:00
|
|
|
* @param User $user
|
2016-12-08 20:50:03 +00:00
|
|
|
* @param WANObjectCache $cache
|
2014-08-07 00:07:34 +00:00
|
|
|
* @param EchoUserNotificationGateway $userNotifGateway
|
|
|
|
* @param EchoNotificationMapper $notifMapper
|
|
|
|
* @param EchoTargetPageMapper $targetPageMapper
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
2014-08-13 22:00:25 +00:00
|
|
|
public function __construct(
|
|
|
|
User $user,
|
2016-12-08 20:50:03 +00:00
|
|
|
WANObjectCache $cache,
|
2014-08-13 22:00:25 +00:00
|
|
|
EchoUserNotificationGateway $userNotifGateway,
|
2014-08-07 00:07:34 +00:00
|
|
|
EchoNotificationMapper $notifMapper,
|
2016-09-13 20:00:35 +00:00
|
|
|
EchoTargetPageMapper $targetPageMapper
|
2014-08-13 22:00:25 +00:00
|
|
|
) {
|
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;
|
2014-08-13 22:00:25 +00:00
|
|
|
$this->notifMapper = $notifMapper;
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->targetPageMapper = $targetPageMapper;
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param User $user
|
2013-05-24 22:51:47 +00:00
|
|
|
* @throws MWException
|
|
|
|
* @return MWEchoNotifUser
|
|
|
|
*/
|
|
|
|
public static function newFromUser( User $user ) {
|
|
|
|
if ( $user->isAnon() ) {
|
|
|
|
throw new MWException( 'User must be logged in to view notification!' );
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return new MWEchoNotifUser(
|
2015-11-01 09:59:16 +00:00
|
|
|
$user,
|
2016-12-08 20:50:03 +00:00
|
|
|
MediaWikiServices::getInstance()->getMainWANObjectCache(),
|
2014-08-13 22:00:25 +00:00
|
|
|
new EchoUserNotificationGateway( $user, MWEchoDbFactory::newFromDefault() ),
|
2014-08-07 00:07:34 +00:00
|
|
|
new EchoNotificationMapper(),
|
2016-09-13 20:00:35 +00:00
|
|
|
new EchoTargetPageMapper()
|
2014-07-18 03:58:21 +00:00
|
|
|
);
|
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
|
2017-08-09 15:20:55 +00:00
|
|
|
* @return string
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
|
|
|
public function getTalkNotificationCacheKey() {
|
2016-12-01 16:50:18 +00:00
|
|
|
global $wgEchoCacheVersion;
|
|
|
|
return wfMemcKey( 'echo-new-talk-notification', $this->mUser->getId(), $wgEchoCacheVersion );
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the user has more notification count than max count display
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function notifCountHasReachedMax() {
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
if ( $this->getLocalNotificationCount() >= self::MAX_BADGE_COUNT ) {
|
2013-05-24 22:51:47 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
/**
|
|
|
|
* Get message count for this user.
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param bool $cached Set to false to bypass the cache. (Optional. Defaults to true)
|
2017-10-14 13:45:13 +00:00
|
|
|
* @param int $dbSource Use master or slave database to pull count (Optional. Defaults to DB_REPLICA)
|
2015-08-13 00:54:16 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-09-24 05:23:47 +00:00
|
|
|
public function getMessageCount( $cached = true, $dbSource = DB_REPLICA ) {
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
return $this->getNotificationCount( $cached, $dbSource, EchoAttributeManager::MESSAGE );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get alert count for this user.
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param bool $cached Set to false to bypass the cache. (Optional. Defaults to true)
|
2017-10-14 13:45:13 +00:00
|
|
|
* @param int $dbSource Use master or slave database to pull count (Optional. Defaults to DB_REPLICA)
|
2015-08-13 00:54:16 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-09-24 05:23:47 +00:00
|
|
|
public function getAlertCount( $cached = true, $dbSource = DB_REPLICA ) {
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
return $this->getNotificationCount( $cached, $dbSource, EchoAttributeManager::ALERT );
|
|
|
|
}
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2017-09-24 05:23:47 +00:00
|
|
|
public function getLocalNotificationCount( $cached = true, $dbSource = DB_REPLICA, $section = EchoAttributeManager::ALL ) {
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
return $this->getNotificationCount( $cached, $dbSource, $section, false );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 22:51:47 +00:00
|
|
|
/**
|
|
|
|
* Retrieves number of unread notifications that a user has, would return
|
2016-05-13 20:48:03 +00:00
|
|
|
* MWEchoNotifUser::MAX_BADGE_COUNT + 1 at most.
|
|
|
|
*
|
|
|
|
* If $wgEchoCrossWikiNotifications is disabled, the $global parameter is ignored.
|
2013-05-24 22:51:47 +00:00
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param bool $cached Set to false to bypass the cache. (Optional. Defaults to true)
|
2017-10-14 13:45:13 +00:00
|
|
|
* @param int $dbSource Use master or slave database to pull count (Optional. Defaults to DB_REPLICA)
|
2014-08-05 21:50:54 +00:00
|
|
|
* @param string $section Notification section
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
* @param bool|string $global Whether to include foreign notifications. If set to 'preference', uses the user's preference.
|
2014-08-05 21:50:54 +00:00
|
|
|
* @return int
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
2017-09-24 05:23:47 +00:00
|
|
|
public function getNotificationCount( $cached = true, $dbSource = DB_REPLICA, $section = EchoAttributeManager::ALL, $global = 'preference' ) {
|
2013-05-24 22:51:47 +00:00
|
|
|
if ( $this->mUser->isAnon() ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-13 20:48:03 +00:00
|
|
|
global $wgEchoCrossWikiNotifications;
|
|
|
|
if ( !$wgEchoCrossWikiNotifications ) {
|
|
|
|
// Ignore the $global parameter
|
|
|
|
$global = false;
|
|
|
|
}
|
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
if ( $global === 'preference' ) {
|
|
|
|
$global = $this->getForeignNotifications()->isEnabledByUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
$memcKey = $this->getMemcKey( 'echo-notification-count' . ( $section === EchoAttributeManager::ALL ? '' : ( '-' . $section ) ), $global );
|
2015-07-21 21:19:12 +00:00
|
|
|
if ( $cached ) {
|
2016-03-24 15:33:13 +00:00
|
|
|
$data = $this->getFromCache( $memcKey );
|
2015-08-13 00:54:16 +00:00
|
|
|
if ( $data !== false && $data !== null ) {
|
2015-07-21 21:19:12 +00:00
|
|
|
return (int)$data;
|
|
|
|
}
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
2014-08-05 21:50:54 +00:00
|
|
|
if ( $section === EchoAttributeManager::ALL ) {
|
|
|
|
$eventTypesToLoad = $attributeManager->getUserEnabledEvents( $this->mUser, 'web' );
|
|
|
|
} else {
|
2016-12-05 18:51:07 +00:00
|
|
|
$eventTypesToLoad = $attributeManager->getUserEnabledEventsbySections( $this->mUser, 'web', [ $section ] );
|
2014-08-05 21:50:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 19:34:44 +00:00
|
|
|
$count = (int)$this->userNotifGateway->getCappedNotificationCount( $dbSource, $eventTypesToLoad, self::MAX_BADGE_COUNT + 1 );
|
2013-05-24 22:51:47 +00:00
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
if ( $global ) {
|
2016-09-09 22:44:40 +00:00
|
|
|
$count = self::capNotificationCount( $count + $this->getForeignCount( $section ) );
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 23:38:55 +00:00
|
|
|
$this->setInCache( $memcKey, $count, 86400 );
|
2016-03-14 13:37:20 +00:00
|
|
|
return $count;
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
/**
|
2016-07-18 23:46:53 +00:00
|
|
|
* Get the timestamp of the latest unread alert
|
2015-08-13 00:54:16 +00:00
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param bool $cached Set to false to bypass the cache. (Optional. Defaults to true)
|
2017-10-14 13:45:13 +00:00
|
|
|
* @param int $dbSource Use master or slave database to pull count (Optional. Defaults to DB_REPLICA)
|
2016-07-18 23:46:53 +00:00
|
|
|
* @return bool|MWTimestamp Timestamp of latest unread alert, or false if there are no unread alerts.
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
2017-09-24 05:23:47 +00:00
|
|
|
public function getLastUnreadAlertTime( $cached = true, $dbSource = DB_REPLICA ) {
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
return $this->getLastUnreadNotificationTime( $cached, $dbSource, EchoAttributeManager::ALERT );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-18 23:46:53 +00:00
|
|
|
* Get the timestamp of the latest unread message
|
2015-08-13 00:54:16 +00:00
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param bool $cached Set to false to bypass the cache. (Optional. Defaults to true)
|
2017-10-14 13:45:13 +00:00
|
|
|
* @param int $dbSource Use master or slave database to pull count (Optional. Defaults to DB_REPLICA)
|
2015-11-25 04:07:54 +00:00
|
|
|
* @return bool|MWTimestamp
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
2017-09-24 05:23:47 +00:00
|
|
|
public function getLastUnreadMessageTime( $cached = true, $dbSource = DB_REPLICA ) {
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
return $this->getLastUnreadNotificationTime( $cached, $dbSource, EchoAttributeManager::MESSAGE );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 12:08:30 +00:00
|
|
|
/**
|
|
|
|
* Returns the timestamp of the last unread notification.
|
|
|
|
*
|
2016-05-13 20:48:03 +00:00
|
|
|
* If $wgEchoCrossWikiNotifications is disabled, the $global parameter is ignored.
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param bool $cached Set to false to bypass the cache. (Optional. Defaults to true)
|
2017-10-14 13:45:13 +00:00
|
|
|
* @param int $dbSource Use master or slave database to pull count (Optional. Defaults to DB_REPLICA)
|
2015-04-29 12:08:30 +00:00
|
|
|
* @param string $section Notification section
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
* @param bool|string $global Whether to include foreign notifications. If set to 'preference', uses the user's preference.
|
2016-07-18 23:46:53 +00:00
|
|
|
* @return bool|MWTimestamp Timestamp of latest unread message, or false if there are no unread messages.
|
2015-04-29 12:08:30 +00:00
|
|
|
*/
|
2017-09-24 05:23:47 +00:00
|
|
|
public function getLastUnreadNotificationTime( $cached = true, $dbSource = DB_REPLICA, $section = EchoAttributeManager::ALL, $global = 'preference' ) {
|
2015-04-29 12:08:30 +00:00
|
|
|
if ( $this->mUser->isAnon() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-13 20:48:03 +00:00
|
|
|
global $wgEchoCrossWikiNotifications;
|
|
|
|
if ( !$wgEchoCrossWikiNotifications ) {
|
|
|
|
// Ignore the $global parameter
|
|
|
|
$global = false;
|
|
|
|
}
|
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
if ( $global === 'preference' ) {
|
|
|
|
$global = $this->getForeignNotifications()->isEnabledByUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
$memcKey = $this->getMemcKey( 'echo-notification-timestamp' . ( $section === EchoAttributeManager::ALL ? '' : ( '-' . $section ) ), $global );
|
2015-04-29 12:08:30 +00:00
|
|
|
|
|
|
|
// read from cache, if allowed
|
|
|
|
if ( $cached ) {
|
2016-03-24 15:33:13 +00:00
|
|
|
$timestamp = $this->getFromCache( $memcKey );
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
if ( $timestamp === -1 ) {
|
|
|
|
// -1 means the user has no notifications
|
|
|
|
return false;
|
|
|
|
} elseif ( $timestamp !== false ) {
|
2015-04-29 12:08:30 +00:00
|
|
|
return new MWTimestamp( $timestamp );
|
|
|
|
}
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
// else cache miss
|
2015-04-29 12:08:30 +00:00
|
|
|
}
|
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
$timestamp = false;
|
|
|
|
|
|
|
|
// Get timestamp of most recent local notification, if there is one
|
2015-04-29 12:08:30 +00:00
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
if ( $section === EchoAttributeManager::ALL ) {
|
|
|
|
$eventTypesToLoad = $attributeManager->getUserEnabledEvents( $this->mUser, 'web' );
|
|
|
|
} else {
|
2016-12-05 18:51:07 +00:00
|
|
|
$eventTypesToLoad = $attributeManager->getUserEnabledEventsbySections( $this->mUser, 'web', [ $section ] );
|
2015-04-29 12:08:30 +00:00
|
|
|
}
|
2016-06-07 18:48:33 +00:00
|
|
|
$notifications = $this->notifMapper->fetchUnreadByUser( $this->mUser, 1, null, $eventTypesToLoad, null, $dbSource );
|
2015-04-29 12:08:30 +00:00
|
|
|
if ( $notifications ) {
|
|
|
|
$notification = reset( $notifications );
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
$timestamp = new MWTimestamp( $notification->getTimestamp() );
|
|
|
|
}
|
2015-04-29 12:08:30 +00:00
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
// Use timestamp of most recent foreign notification, if it's more recent
|
|
|
|
if ( $global ) {
|
2016-05-29 20:54:15 +00:00
|
|
|
$foreignTime = $this->getForeignTimestamp( $section );
|
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
if (
|
|
|
|
$foreignTime !== false &&
|
|
|
|
// $foreignTime < $timestamp = invert 0
|
|
|
|
// $foreignTime > $timestamp = invert 1
|
|
|
|
( $timestamp === false || $foreignTime->diff( $timestamp )->invert === 1 )
|
|
|
|
) {
|
|
|
|
$timestamp = $foreignTime;
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
if ( $timestamp === false ) {
|
|
|
|
// No notifications, so no timestamp
|
|
|
|
$returnValue = false;
|
|
|
|
$cacheValue = -1;
|
|
|
|
} else {
|
|
|
|
$returnValue = $timestamp;
|
|
|
|
$cacheValue = $timestamp->getTimestamp( TS_MW );
|
2015-04-29 12:08:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 23:38:55 +00:00
|
|
|
$this->setInCache( $memcKey, $cacheValue, 86400 );
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
return $returnValue;
|
2015-04-29 12:08:30 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 22:51:47 +00:00
|
|
|
/**
|
|
|
|
* Mark one or more notifications read for a user.
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param array $eventIds Array of event IDs to mark read
|
2017-10-02 23:07:31 +00:00
|
|
|
* @return bool Returns true when data has been updated in DB, false on
|
|
|
|
* failure, or when there was nothing to update
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
|
|
|
public function markRead( $eventIds ) {
|
|
|
|
$eventIds = array_filter( (array)$eventIds, 'is_numeric' );
|
|
|
|
if ( !$eventIds || wfReadOnly() ) {
|
2014-08-13 22:00:25 +00:00
|
|
|
return false;
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 23:07:31 +00:00
|
|
|
$updated = $this->userNotifGateway->markRead( $eventIds );
|
|
|
|
if ( $updated ) {
|
2014-08-07 00:07:34 +00:00
|
|
|
// Update notification count in cache
|
2014-08-13 22:00:25 +00:00
|
|
|
$this->resetNotificationCount( DB_MASTER );
|
2015-08-15 01:51:11 +00:00
|
|
|
|
|
|
|
// After this 'mark read', is there any unread edit-user-talk
|
|
|
|
// remaining? If not, we should clear the newtalk flag.
|
|
|
|
if ( $this->mUser->getNewtalk() ) {
|
2016-10-13 18:55:34 +00:00
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
$categoryMap = $attributeManager->getEventsByCategory();
|
|
|
|
$usertalkTypes = $categoryMap['edit-user-talk'];
|
|
|
|
$unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser( $this->mUser, 1, null, $usertalkTypes, null, DB_MASTER );
|
2015-08-15 01:51:11 +00:00
|
|
|
if ( count( $unreadEditUserTalk ) === 0 ) {
|
|
|
|
$this->mUser->setNewtalk( false );
|
|
|
|
}
|
|
|
|
}
|
2014-08-13 22:00:25 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2017-10-02 23:07:31 +00:00
|
|
|
return $updated;
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 22:44:22 +00:00
|
|
|
/**
|
|
|
|
* Mark one or more notifications unread for a user.
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param array $eventIds Array of event IDs to mark unread
|
2017-10-02 23:07:31 +00:00
|
|
|
* @return bool Returns true when data has been updated in DB, false on
|
|
|
|
* failure, or when there was nothing to update
|
2016-03-04 22:44:22 +00:00
|
|
|
*/
|
|
|
|
public function markUnRead( $eventIds ) {
|
|
|
|
$eventIds = array_filter( (array)$eventIds, 'is_numeric' );
|
|
|
|
if ( !$eventIds || wfReadOnly() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:07:31 +00:00
|
|
|
$updated = $this->userNotifGateway->markUnRead( $eventIds );
|
|
|
|
if ( $updated ) {
|
2016-03-04 22:44:22 +00:00
|
|
|
// Update notification count in cache
|
|
|
|
$this->resetNotificationCount( DB_MASTER );
|
|
|
|
|
|
|
|
// After this 'mark unread', is there any unread edit-user-talk?
|
|
|
|
// If so, we should add the edit-user-talk flag
|
|
|
|
if ( !$this->mUser->getNewtalk() ) {
|
2016-10-13 18:55:34 +00:00
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
$categoryMap = $attributeManager->getEventsByCategory();
|
|
|
|
$usertalkTypes = $categoryMap['edit-user-talk'];
|
|
|
|
$unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser( $this->mUser, 1, null, $usertalkTypes, null, DB_MASTER );
|
2016-03-04 22:44:22 +00:00
|
|
|
if ( count( $unreadEditUserTalk ) > 0 ) {
|
|
|
|
$this->mUser->setNewtalk( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:07:31 +00:00
|
|
|
return $updated;
|
2016-03-04 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 22:51:47 +00:00
|
|
|
/**
|
2014-08-13 22:00:25 +00:00
|
|
|
* Attempt to mark all or sections of notifications as read, this only
|
|
|
|
* updates up to $wgEchoMaxUpdateCount records per request, see more
|
|
|
|
* detail about this in Echo.php, the other reason is that mediawiki
|
|
|
|
* database interface doesn't support updateJoin() that would update
|
|
|
|
* across multiple tables, we would visit this later
|
|
|
|
*
|
|
|
|
* @param string[] $sections
|
2017-07-26 19:34:44 +00:00
|
|
|
* @return bool
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
public function markAllRead( array $sections = [ EchoAttributeManager::ALL ] ) {
|
2014-08-13 22:00:25 +00:00
|
|
|
if ( wfReadOnly() ) {
|
2013-05-24 22:51:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
global $wgEchoMaxUpdateCount;
|
|
|
|
|
|
|
|
// Mark all sections as read if this is the case
|
|
|
|
if ( in_array( EchoAttributeManager::ALL, $sections ) ) {
|
|
|
|
$sections = EchoAttributeManager::$sections;
|
|
|
|
}
|
|
|
|
|
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
$eventTypes = $attributeManager->getUserEnabledEventsbySections( $this->mUser, 'web', $sections );
|
|
|
|
|
2015-12-09 18:41:40 +00:00
|
|
|
$notifs = $this->notifMapper->fetchUnreadByUser( $this->mUser, $wgEchoMaxUpdateCount, null, $eventTypes );
|
2014-08-07 00:07:34 +00:00
|
|
|
|
|
|
|
$eventIds = array_filter(
|
2015-10-01 13:48:52 +00:00
|
|
|
array_map( function ( EchoNotification $notif ) {
|
2014-08-07 00:07:34 +00:00
|
|
|
// This should not happen at all, but use 0 in
|
2014-08-13 22:00:25 +00:00
|
|
|
// such case so to keep the code running
|
|
|
|
if ( $notif->getEvent() ) {
|
|
|
|
return $notif->getEvent()->getId();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-08-07 00:07:34 +00:00
|
|
|
}, $notifs )
|
|
|
|
);
|
|
|
|
|
2017-10-02 23:07:31 +00:00
|
|
|
$updated = $this->markRead( $eventIds );
|
|
|
|
if ( $updated ) {
|
2014-08-07 00:07:34 +00:00
|
|
|
// Delete records from echo_target_page
|
2016-03-04 19:23:02 +00:00
|
|
|
/**
|
|
|
|
* Keep the 'echo_target_page' records so they can be used for moderation.
|
|
|
|
*/
|
|
|
|
// $this->targetPageMapper->deleteByUserEvents( $this->mUser, $eventIds );
|
2014-08-07 00:07:34 +00:00
|
|
|
if ( count( $notifs ) < $wgEchoMaxUpdateCount ) {
|
|
|
|
$this->flagCacheWithNoTalkNotification();
|
|
|
|
}
|
2014-08-13 22:00:25 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2017-10-02 23:07:31 +00:00
|
|
|
return $updated;
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-08 20:50:03 +00:00
|
|
|
* Invalidate cache and update echo_unread_wikis if x-wiki notifications is enabled
|
|
|
|
* NOTE: Consider calling this function from a deferred update since it may access the db
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param int $dbSource Use master or replica database to pull count
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
2016-12-08 20:50:03 +00:00
|
|
|
public function resetNotificationCount( $dbSource = DB_REPLICA ) {
|
2016-05-13 20:48:03 +00:00
|
|
|
global $wgEchoCrossWikiNotifications;
|
|
|
|
if ( $wgEchoCrossWikiNotifications ) {
|
|
|
|
// Schedule an update to the echo_unread_wikis table
|
2016-12-08 20:50:03 +00:00
|
|
|
$uw = EchoUnreadWikis::newFromUser( $this->mUser );
|
|
|
|
if ( $uw ) {
|
|
|
|
$alertCount = $this->getNotificationCount( false, $dbSource, EchoAttributeManager::ALERT, false );
|
|
|
|
$msgCount = $this->getNotificationCount( false, $dbSource, EchoAttributeManager::MESSAGE, false );
|
|
|
|
$alertUnread = $this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::ALERT, false );
|
|
|
|
$msgUnread = $this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::MESSAGE, false );
|
|
|
|
$uw->updateCount( wfWikiID(), $alertCount, $alertUnread, $msgCount, $msgUnread );
|
|
|
|
}
|
2016-05-13 20:48:03 +00:00
|
|
|
}
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
|
2016-05-13 20:48:03 +00:00
|
|
|
$this->invalidateCache();
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 23:56:07 +00:00
|
|
|
/**
|
|
|
|
* Get the timestamp of the last time the global notification counts/timestamps were updated, if available.
|
|
|
|
*
|
|
|
|
* If the timestamp of the last update is not known, this will return the current timestamp.
|
|
|
|
* If the user is not attached, this will return false.
|
|
|
|
*
|
|
|
|
* @return string|false MW timestamp of the last update, or false if the user is not attached
|
|
|
|
*/
|
|
|
|
public function getGlobalUpdateTime() {
|
|
|
|
$key = $this->getGlobalMemcKey( 'echo-notification-updated' );
|
|
|
|
if ( $key === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-08 20:50:03 +00:00
|
|
|
return wfTimestamp( TS_MW, $this->cache->getCheckKeyTime( $key ) );
|
2016-05-10 23:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidate user caches related to notification counts/timestamps.
|
|
|
|
*
|
|
|
|
* This bumps the local user's touched timestamp as well as the timestamp returned by getGlobalUpdateTime().
|
|
|
|
*/
|
|
|
|
protected function invalidateCache() {
|
|
|
|
// Update the user touched timestamp for the local user
|
|
|
|
$this->mUser->invalidateCache();
|
|
|
|
|
2016-12-08 20:50:03 +00:00
|
|
|
$this->deleteFromCache( $this->getLocalKeys() );
|
|
|
|
|
2016-05-13 20:48:03 +00:00
|
|
|
global $wgEchoCrossWikiNotifications;
|
|
|
|
if ( $wgEchoCrossWikiNotifications ) {
|
2016-12-08 20:50:03 +00:00
|
|
|
$this->deleteFromCache( $this->getGlobalKeys() );
|
|
|
|
|
2016-05-13 20:48:03 +00:00
|
|
|
// Update the global touched timestamp
|
|
|
|
$key = $this->getGlobalMemcKey( 'echo-notification-updated' );
|
|
|
|
if ( $key ) {
|
2016-12-08 20:50:03 +00:00
|
|
|
$this->cache->touchCheckKey( $key );
|
2016-05-13 20:48:03 +00:00
|
|
|
}
|
2016-05-10 23:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-05-05 13:05:03 +00:00
|
|
|
return EchoEmailFormat::PLAIN_TEXT;
|
2013-06-24 00:22:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 23:38:55 +00:00
|
|
|
/**
|
|
|
|
* Get a cache entry from the cache, using a preloaded instance cache.
|
2017-09-01 04:48:56 +00:00
|
|
|
* @param string|false $memcKey Cache key returned by getMemcKey()
|
2016-05-05 23:38:55 +00:00
|
|
|
* @return mixed Cache value
|
|
|
|
*/
|
2016-03-24 15:33:13 +00:00
|
|
|
protected function getFromCache( $memcKey ) {
|
2016-05-05 23:38:55 +00:00
|
|
|
// getMemcKey() can return false
|
|
|
|
if ( $memcKey === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the instance cache
|
2016-03-24 15:33:13 +00:00
|
|
|
if ( $this->cached === null ) {
|
2016-06-17 14:49:31 +00:00
|
|
|
$keys = $this->getPreloadKeys();
|
2016-03-24 15:33:13 +00:00
|
|
|
$this->cached = $this->cache->getMulti( $keys );
|
|
|
|
// also keep track of cache values that couldn't be found (getMulti
|
|
|
|
// omits them...)
|
|
|
|
$this->cached += array_fill_keys( $keys, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $this->cached[$memcKey] ) ) {
|
|
|
|
return $this->cached[$memcKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->cache->get( $memcKey );
|
|
|
|
}
|
|
|
|
|
2016-05-05 23:38:55 +00:00
|
|
|
/**
|
|
|
|
* Set a cache entry both in the cache and in the instance cache.
|
|
|
|
* Use this to write to keys that were loaded with getFromCache().
|
|
|
|
* @param string|false $memcKey Cache key returned by getMemcKey()
|
|
|
|
* @param mixed $value Cache value to set
|
|
|
|
* @param int $expiry Expiry, see BagOStuff::set()
|
|
|
|
*/
|
|
|
|
protected function setInCache( $memcKey, $value, $expiry ) {
|
|
|
|
// getMemcKey() can return false
|
|
|
|
if ( $memcKey === false ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the instance cache if it's already been populated
|
|
|
|
if ( $this->cached !== null ) {
|
|
|
|
$this->cached[$memcKey] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cache->set( $memcKey, $value, $expiry );
|
|
|
|
}
|
|
|
|
|
2016-12-08 20:50:03 +00:00
|
|
|
protected function deleteFromCache( $keys ) {
|
|
|
|
foreach ( $keys as $key ) {
|
|
|
|
// Update the instance cache if it's already been populated
|
|
|
|
if ( $this->cached !== null ) {
|
|
|
|
unset( $this->cached[$key] );
|
|
|
|
}
|
|
|
|
$this->cache->delete( $key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 15:33:13 +00:00
|
|
|
/**
|
|
|
|
* Array of memcached keys to load at once.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-06-17 14:49:31 +00:00
|
|
|
protected function getPreloadKeys() {
|
2016-12-08 20:50:03 +00:00
|
|
|
return array_merge(
|
|
|
|
$this->getLocalKeys(),
|
|
|
|
$this->getGlobalKeys()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getLocalKeys() {
|
|
|
|
return array_filter( array_map( [ $this, 'getMemcKey' ], $this->getKeySeeds() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getGlobalKeys() {
|
|
|
|
return array_filter( array_map( [ $this, 'getGlobalMemcKey' ], $this->getKeySeeds() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getKeySeeds() {
|
|
|
|
return [
|
2016-03-24 15:33:13 +00:00
|
|
|
'echo-notification-timestamp',
|
|
|
|
'echo-notification-timestamp-' . EchoAttributeManager::MESSAGE,
|
|
|
|
'echo-notification-timestamp-' . EchoAttributeManager::ALERT,
|
|
|
|
'echo-notification-count',
|
|
|
|
'echo-notification-count-' . EchoAttributeManager::MESSAGE,
|
|
|
|
'echo-notification-count-' . EchoAttributeManager::ALERT,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-03-24 15:33:13 +00:00
|
|
|
}
|
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
/**
|
|
|
|
* Build a memcached key.
|
|
|
|
* @param string $key Key, typically prefixed with echo-notification-
|
|
|
|
* @param bool $global If true, return a global memc key; if false, return one local to this wiki
|
2016-05-05 23:38:55 +00:00
|
|
|
* @return string|false Memcached key, or false if one could not be generated
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
*/
|
|
|
|
protected function getMemcKey( $key, $global = false ) {
|
2016-12-01 16:50:18 +00:00
|
|
|
global $wgEchoCacheVersion;
|
2016-05-05 23:38:55 +00:00
|
|
|
if ( !$global ) {
|
2016-12-01 16:50:18 +00:00
|
|
|
return wfMemcKey( $key, $this->mUser->getId(), $wgEchoCacheVersion );
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
}
|
2016-05-05 23:38:55 +00:00
|
|
|
|
|
|
|
$lookup = CentralIdLookup::factory();
|
|
|
|
$globalId = $lookup->centralIdFromLocalUser( $this->mUser, CentralIdLookup::AUDIENCE_RAW );
|
|
|
|
if ( !$globalId ) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-01 16:50:18 +00:00
|
|
|
return wfGlobalCacheKey( $key, $globalId, $wgEchoCacheVersion );
|
2016-03-24 15:33:13 +00:00
|
|
|
}
|
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
protected function getGlobalMemcKey( $key ) {
|
|
|
|
return $this->getMemcKey( $key, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lazy-construct an EchoForeignNotifications instance. This instance is force-enabled, so it
|
|
|
|
* returns information about cross-wiki notifications even if the user has them disabled.
|
|
|
|
* @return EchoForeignNotifications
|
|
|
|
*/
|
|
|
|
protected function getForeignNotifications() {
|
|
|
|
if ( !$this->foreignNotifications ) {
|
|
|
|
$this->foreignNotifications = new EchoForeignNotifications( $this->mUser, true );
|
|
|
|
}
|
|
|
|
return $this->foreignNotifications;
|
|
|
|
}
|
2016-05-29 20:54:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get data about foreign notifications from the foreign wikis' APIs.
|
|
|
|
*
|
|
|
|
* This is used when $wgEchoSectionTransition or $wgEchoBundleTransition is enabled,
|
|
|
|
* to deal with untrustworthy echo_unread_wikis entries. This method fetches the list of
|
|
|
|
* wikis that have any unread notifications at all from the echo_unread_wikis table, then
|
|
|
|
* queries their APIs to find the per-section counts and timestamps for those wikis.
|
|
|
|
*
|
|
|
|
* The results of this function are cached in the NotifUser object.
|
|
|
|
* @return array [ (str) wiki => [ (str) section => [ 'count' => (int) count, 'timestamp' => (str) ts ] ] ]
|
|
|
|
*/
|
|
|
|
protected function getForeignData() {
|
|
|
|
if ( $this->mForeignData ) {
|
|
|
|
return $this->mForeignData;
|
|
|
|
}
|
|
|
|
|
|
|
|
$potentialWikis = $this->getForeignNotifications()->getWikis( EchoAttributeManager::ALL );
|
|
|
|
$foreignReq = new EchoForeignWikiRequest(
|
|
|
|
$this->mUser,
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2016-05-29 20:54:15 +00:00
|
|
|
'action' => 'query',
|
|
|
|
'meta' => 'notifications',
|
|
|
|
'notprop' => 'count|list',
|
|
|
|
'notgroupbysection' => '1',
|
|
|
|
'notunreadfirst' => '1',
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-05-29 20:54:15 +00:00
|
|
|
$potentialWikis,
|
|
|
|
'notwikis'
|
|
|
|
);
|
|
|
|
$foreignResults = $foreignReq->execute();
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->mForeignData = [];
|
2016-05-29 20:54:15 +00:00
|
|
|
foreach ( $foreignResults as $wiki => $result ) {
|
|
|
|
if ( !isset( $result['query']['notifications'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$data = $result['query']['notifications'];
|
|
|
|
foreach ( EchoAttributeManager::$sections as $section ) {
|
|
|
|
if ( isset( $data[$section]['rawcount'] ) ) {
|
|
|
|
$this->mForeignData[$wiki][$section]['count'] = $data[$section]['rawcount'];
|
|
|
|
}
|
|
|
|
if ( isset( $data[$section]['list'][0] ) ) {
|
|
|
|
$this->mForeignData[$wiki][$section]['timestamp'] = $data[$section]['list'][0]['timestamp']['mw'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->mForeignData;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getForeignCount( $section = EchoAttributeManager::ALL ) {
|
|
|
|
global $wgEchoSectionTransition, $wgEchoBundleTransition;
|
|
|
|
$count = 0;
|
|
|
|
if (
|
|
|
|
// In section transition mode, we don't trust the individual echo_unread_wikis rows
|
|
|
|
// but we do trust that alert+message=all. In bundle transition mode, we don't trust
|
|
|
|
// that either, but we do trust that wikis with rows in the table have unread notifications
|
|
|
|
// and wikis without rows in the table don't.
|
|
|
|
( $wgEchoSectionTransition && $section !== EchoAttributeManager::ALL ) ||
|
|
|
|
$wgEchoBundleTransition
|
|
|
|
) {
|
|
|
|
$foreignData = $this->getForeignData();
|
|
|
|
foreach ( $foreignData as $data ) {
|
|
|
|
if ( $section === EchoAttributeManager::ALL ) {
|
|
|
|
foreach ( $data as $subData ) {
|
|
|
|
if ( isset( $subData['count'] ) ) {
|
|
|
|
$count += $subData['count'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ( isset( $data[$section]['count'] ) ) {
|
|
|
|
$count += $data[$section]['count'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$count += $this->getForeignNotifications()->getCount( $section );
|
|
|
|
}
|
2016-09-09 22:44:40 +00:00
|
|
|
return self::capNotificationCount( $count );
|
2016-05-29 20:54:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 20:00:35 +00:00
|
|
|
protected function getForeignTimestamp( $section = EchoAttributeManager::ALL ) {
|
2016-06-21 12:00:27 +00:00
|
|
|
global $wgEchoSectionTransition, $wgEchoBundleTransition;
|
|
|
|
|
2016-05-29 20:54:15 +00:00
|
|
|
if (
|
|
|
|
// In section transition mode, we don't trust the individual echo_unread_wikis rows
|
|
|
|
// but we do trust that alert+message=all. In bundle transition mode, we don't trust
|
|
|
|
// that either, but we do trust that wikis with rows in the table have unread notifications
|
|
|
|
// and wikis without rows in the table don't.
|
|
|
|
( $wgEchoSectionTransition && $section !== EchoAttributeManager::ALL ) ||
|
|
|
|
$wgEchoBundleTransition
|
|
|
|
) {
|
|
|
|
$foreignTime = false;
|
|
|
|
$foreignData = $this->getForeignData();
|
2016-09-13 20:00:35 +00:00
|
|
|
foreach ( $foreignData as $data ) {
|
2016-05-29 20:54:15 +00:00
|
|
|
if ( $section === EchoAttributeManager::ALL ) {
|
|
|
|
foreach ( $data as $subData ) {
|
|
|
|
if ( isset( $subData['timestamp'] ) ) {
|
|
|
|
$wikiTime = new MWTimestamp( $data[$section]['timestamp'] );
|
|
|
|
// $wikiTime > $foreignTime = invert 1
|
|
|
|
if ( $foreignTime === false || $wikiTime->diff( $foreignTime )->invert === 1 ) {
|
|
|
|
$foreignTime = $wikiTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ( isset( $data[$section]['timestamp'] ) ) {
|
|
|
|
$wikiTime = new MWTimestamp( $data[$section]['timestamp'] );
|
|
|
|
// $wikiTime > $foreignTime = invert 1
|
|
|
|
if ( $foreignTime === false || $wikiTime->diff( $foreignTime )->invert === 1 ) {
|
|
|
|
$foreignTime = $wikiTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-09-13 20:00:35 +00:00
|
|
|
$foreignTime = $this->getForeignNotifications()->getTimestamp( $section );
|
2016-05-29 20:54:15 +00:00
|
|
|
}
|
|
|
|
return $foreignTime;
|
|
|
|
}
|
2016-09-09 22:44:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to produce the capped number of notifications
|
|
|
|
* based on the value of MWEchoNotifUser::MAX_BADGE_COUNT
|
|
|
|
*
|
|
|
|
* @param int $number Raw notification count to cap
|
|
|
|
* @return int Capped notification count
|
|
|
|
*/
|
|
|
|
public static function capNotificationCount( $number ) {
|
2017-07-26 19:34:44 +00:00
|
|
|
return min( $number, self::MAX_BADGE_COUNT + 1 );
|
2016-09-09 22:44:40 +00:00
|
|
|
}
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|