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
|
|
|
/**
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
* @var array|null
|
2016-03-24 15:33:13 +00:00
|
|
|
*/
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
private $localCountsAndTimestamps;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array|null
|
|
|
|
*/
|
|
|
|
private $globalCountsAndTimestamps;
|
2016-03-24 15:33:13 +00:00
|
|
|
|
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;
|
|
|
|
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
const CACHE_TTL = 86400;
|
|
|
|
const CACHE_KEY = 'echo-notification-counts';
|
|
|
|
const CHECK_KEY = 'echo-notification-updated';
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
$data = $this->getCountsAndTimestamps( $global );
|
|
|
|
$count = $data[$global ? 'global' : 'local'][$section]['count'];
|
|
|
|
return (int)$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();
|
|
|
|
}
|
|
|
|
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
$data = $this->getCountsAndTimestamps( $global );
|
|
|
|
$timestamp = $data[$global ? 'global' : 'local'][$section]['timestamp'];
|
|
|
|
return $timestamp === -1 ? false : new MWTimestamp( $timestamp );
|
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
|
2018-05-25 17:45:49 +00:00
|
|
|
$this->resetNotificationCount();
|
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
|
2018-05-25 17:45:49 +00:00
|
|
|
$this->resetNotificationCount();
|
2016-03-04 22:44:22 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-25 17:45:49 +00:00
|
|
|
* Invalidate cache and update echo_unread_wikis if x-wiki notifications is enabled.
|
2016-12-08 20:50:03 +00:00
|
|
|
*
|
2018-05-25 17:45:49 +00:00
|
|
|
* This updates the user's touched timestamp, as well as the value returned by getGlobalUpdateTime().
|
|
|
|
*
|
|
|
|
* NOTE: Consider calling this function from a deferred update, since it will read from and write to
|
|
|
|
* the master DB if cross-wiki notifications are enabled.
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
2018-05-30 22:38:51 +00:00
|
|
|
public function resetNotificationCount() {
|
2016-05-13 20:48:03 +00:00
|
|
|
global $wgEchoCrossWikiNotifications;
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
|
|
|
|
// Delete cached local counts and timestamps
|
|
|
|
$localMemcKey = $this->getMemcKey( self::CACHE_KEY );
|
|
|
|
$this->cache->delete( $localMemcKey );
|
|
|
|
|
|
|
|
// Update the user touched timestamp for the local user
|
|
|
|
$this->mUser->invalidateCache();
|
|
|
|
|
2016-05-13 20:48:03 +00:00
|
|
|
if ( $wgEchoCrossWikiNotifications ) {
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
// Delete cached global counts and timestamps
|
|
|
|
$globalMemcKey = $this->getGlobalMemcKey( self::CACHE_KEY );
|
|
|
|
if ( $globalMemcKey !== false ) {
|
|
|
|
$this->cache->delete( $globalMemcKey );
|
|
|
|
}
|
|
|
|
|
2016-12-08 20:50:03 +00:00
|
|
|
$uw = EchoUnreadWikis::newFromUser( $this->mUser );
|
|
|
|
if ( $uw ) {
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
// Immediately compute new local counts and timestamps
|
2018-05-30 22:38:51 +00:00
|
|
|
$newLocalData = $this->computeLocalCountsAndTimestamps( DB_MASTER );
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
// Write the new values to the echo_unread_wikis table
|
|
|
|
$alertTs = $newLocalData[EchoAttributeManager::ALERT]['timestamp'];
|
|
|
|
$messageTs = $newLocalData[EchoAttributeManager::MESSAGE]['timestamp'];
|
|
|
|
$uw->updateCount(
|
|
|
|
wfWikiID(),
|
|
|
|
$newLocalData[EchoAttributeManager::ALERT]['count'],
|
|
|
|
$alertTs === -1 ? false : new MWTimestamp( $alertTs ),
|
|
|
|
$newLocalData[EchoAttributeManager::MESSAGE]['count'],
|
|
|
|
$messageTs === -1 ? false : new MWTimestamp( $messageTs )
|
|
|
|
);
|
|
|
|
// We could set() $newLocalData into the cache here, but we don't because that seems risky;
|
|
|
|
// instead we let it be recomputed on demand
|
2016-12-08 20:50: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
|
|
|
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
// Update the global touched timestamp
|
|
|
|
$checkKey = $this->getGlobalMemcKey( self::CHECK_KEY );
|
|
|
|
if ( $checkKey ) {
|
|
|
|
$this->cache->touchCheckKey( $checkKey );
|
|
|
|
}
|
|
|
|
}
|
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() {
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
$key = $this->getGlobalMemcKey( self::CHECK_KEY );
|
2016-05-10 23:56:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
* Get the number of notifications in each section, and the timestamp of the latest notification in
|
|
|
|
* each section. This returns the raw data structure that is stored in the cache; unless you want
|
|
|
|
* all of this information, you're probably looking for getNotificationCount(),
|
|
|
|
* getLastUnreadNotificationTime() or one of its wrappers.
|
|
|
|
*
|
|
|
|
* The returned data structure looks like:
|
|
|
|
* [
|
|
|
|
* 'local' => [
|
|
|
|
* 'alert' => [ 'count' => N, 'timestamp' => TS ],
|
|
|
|
* 'message' => [ 'count' = N, 'timestamp' => TS ],
|
|
|
|
* 'all' => [ 'count' => N, 'timestamp' => TS ],
|
|
|
|
* ],
|
|
|
|
* 'global' => [
|
|
|
|
* 'alert' => [ 'count' => N, 'timestamp' => TS ],
|
|
|
|
* 'message' => [ 'count' = N, 'timestamp' => TS ],
|
|
|
|
* 'all' => [ 'count' => N, 'timestamp' => TS ],
|
|
|
|
* ],
|
|
|
|
* ]
|
|
|
|
* Where N is a number and TS is a timestamp in TS_MW format or -1. If $includeGlobal is false,
|
|
|
|
* the 'global' key will not be present.
|
2016-05-10 23:56:07 +00:00
|
|
|
*
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
* @param bool $includeGlobal Whether to include cross-wiki notifications as well
|
|
|
|
* @return array
|
2016-05-10 23:56:07 +00:00
|
|
|
*/
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
public function getCountsAndTimestamps( $includeGlobal = false ) {
|
|
|
|
if ( $this->localCountsAndTimestamps === null ) {
|
|
|
|
$this->localCountsAndTimestamps = $this->cache->getWithSetCallback(
|
|
|
|
$this->getMemcKey( self::CACHE_KEY ),
|
|
|
|
self::CACHE_TTL,
|
|
|
|
function ( $oldValue, &$ttl, array &$setOpts ) {
|
|
|
|
$dbr = $this->userNotifGateway->getDB( DB_REPLICA );
|
|
|
|
$setOpts += Database::getCacheSetOptions( $dbr );
|
|
|
|
return $this->computeLocalCountsAndTimestamps();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$result = [ 'local' => $this->localCountsAndTimestamps ];
|
|
|
|
|
|
|
|
if ( $includeGlobal ) {
|
|
|
|
if ( $this->globalCountsAndTimestamps === null ) {
|
|
|
|
$memcKey = $this->getGlobalMemcKey( self::CACHE_KEY );
|
|
|
|
// If getGlobalMemcKey returns false, we don't have a global user ID
|
|
|
|
// In that case, don't compute data that we can't cache or store
|
|
|
|
if ( $memcKey !== false ) {
|
|
|
|
$this->globalCountsAndTimestamps = $this->cache->getWithSetCallback(
|
|
|
|
$memcKey,
|
|
|
|
self::CACHE_TTL,
|
|
|
|
function ( $oldValue, &$ttl, array &$setOpts ) {
|
|
|
|
$dbr = $this->userNotifGateway->getDB( DB_REPLICA );
|
|
|
|
$setOpts += Database::getCacheSetOptions( $dbr );
|
|
|
|
return $this->computeGlobalCountsAndTimestamps();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result['global'] = $this->globalCountsAndTimestamps;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2016-05-10 23:56:07 +00:00
|
|
|
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
/**
|
|
|
|
* Compute the counts and timestamps for the local notifications in each section.
|
|
|
|
* @param int $dbSource DB_REPLICA or DB_MASTER
|
|
|
|
* @return array [ 'alert' => [ 'count' => N, 'timestamp' => TS ], ... ]
|
|
|
|
*/
|
|
|
|
protected function computeLocalCountsAndTimestamps( $dbSource = DB_REPLICA ) {
|
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
$result = [];
|
|
|
|
$totals = [ 'count' => 0, 'timestamp' => -1 ];
|
2016-12-08 20:50:03 +00:00
|
|
|
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
foreach ( EchoAttributeManager::$sections as $section ) {
|
|
|
|
$eventTypesToLoad = $attributeManager->getUserEnabledEventsbySections( $this->mUser, 'web', [ $section ] );
|
2016-12-08 20:50:03 +00:00
|
|
|
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
$count = (int)$this->userNotifGateway->getCappedNotificationCount( $dbSource, $eventTypesToLoad, self::MAX_BADGE_COUNT + 1 );
|
|
|
|
$result[$section]['count'] = $count;
|
|
|
|
$totals['count'] += $count;
|
|
|
|
|
|
|
|
$notifications = $this->notifMapper->fetchUnreadByUser( $this->mUser, 1, null, $eventTypesToLoad, null, $dbSource );
|
|
|
|
if ( $notifications ) {
|
|
|
|
$notification = reset( $notifications );
|
|
|
|
$timestamp = $notification->getTimestamp();
|
|
|
|
} else {
|
|
|
|
$timestamp = -1;
|
2016-05-13 20:48:03 +00:00
|
|
|
}
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
$result[$section]['timestamp'] = $timestamp;
|
|
|
|
$totals['timestamp'] = max( $totals['timestamp'], $timestamp );
|
2016-05-10 23:56:07 +00:00
|
|
|
}
|
NotifUser: Redo caching strategy for multi-DC compatibility
To use WANObjectCache correctly in a multi-DC-safe way, we need to use
getWithSetCallback() to read data, and call delete() when it changes.
NotifUser's caching of notification counts and timestamps relied
heavily on set() calls, and so wasn't multi-DC-safe.
Changes in this commit:
* Rather than caching counts/timestamps in separate cache keys, and
using separate cache keys for each section (alert/message/all), put
all this data in an array and store that in a single cache key.
This reduces the number of cache keys per user per wiki from 6 to 1.
* Similarly, use a single global cache key per user. The global check
key for the last updated timestamp is retained, so we now have
2 global cache keys per user (down from 7)
* Remove preloading using getMulti(), no longer needed
* Move computation of counts and timestamps into separate compute
functions (one for local, one for global), and wrap them with
a getter that uses getWithSetCallback().
* Use TS_MW strings instead of MWTimestamp objects internally, to
simplify comparisons and max() operations.
* Make existing getters wrap around this new getter. They now ignore
their $cached and $dbSource parameters, and we should deprecate/change
these function signatures.
* In resetNotificationCounts(), just delete the cache keys. In global
mode, also recompute the notification counts and put them in the
echo_unread_wikis table. We could also set() the data into the cache
at this point, but don't, because you're not supposed to mix set() and
getWithSetCallback() calls and I don't want to find out what happens
if you do.
Bug: T164860
Change-Id: I4f86aab11d50d20280a33e0504ba8ad0c6c01842
2018-05-25 17:49:07 +00:00
|
|
|
$totals['count'] = self::capNotificationCount( $totals['count'] );
|
|
|
|
$result[EchoAttributeManager::ALL] = $totals;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the global counts and timestamps for each section.
|
|
|
|
*
|
|
|
|
* This calls getCountsAndTimestamps() to get data about local notifications, which may end up
|
|
|
|
* calling computeLocalCountsAndTimestamps() if there's a cache miss.
|
|
|
|
* @return array [ 'alert' => [ 'count' => N, 'timestamp' => TS ], ... ]
|
|
|
|
*/
|
|
|
|
protected function computeGlobalCountsAndTimestamps() {
|
|
|
|
$localData = $this->getCountsAndTimestamps()['local'];
|
|
|
|
$result = [];
|
|
|
|
$totals = [ 'count' => 0, 'timestamp' => -1 ];
|
|
|
|
foreach ( EchoAttributeManager::$sections as $section ) {
|
|
|
|
$localCount = $localData[$section]['count'];
|
|
|
|
$globalCount = self::capNotificationCount( $localCount + $this->getForeignCount( $section ) );
|
|
|
|
$result[$section]['count'] = $globalCount;
|
|
|
|
$totals['count'] += $globalCount;
|
|
|
|
|
|
|
|
$localTimestamp = $localData[$section]['timestamp'];
|
|
|
|
$foreignTimestamp = $this->getForeignTimestamp( $section );
|
|
|
|
$globalTimestamp = max( $localTimestamp, $foreignTimestamp ? $foreignTimestamp->getTimestamp( TS_MW ) : -1 );
|
|
|
|
$result[$section]['timestamp'] = $globalTimestamp;
|
|
|
|
$totals['timestamp'] = max( $totals['timestamp'], $globalTimestamp );
|
|
|
|
}
|
|
|
|
$totals['count'] = self::capNotificationCount( $totals['count'] );
|
|
|
|
$result[EchoAttributeManager::ALL] = $totals;
|
|
|
|
return $result;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2018-05-31 23:01:57 +00:00
|
|
|
* Build a cache key for local use (local to this wiki)
|
|
|
|
*
|
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 string $key Key, typically prefixed with echo-notification-
|
2018-05-31 23:01:57 +00:00
|
|
|
* @return string Cache key
|
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
|
|
|
*/
|
2018-05-31 23:01:57 +00:00
|
|
|
protected function getMemcKey( $key ) {
|
2016-12-01 16:50:18 +00:00
|
|
|
global $wgEchoCacheVersion;
|
2018-05-31 23:01:57 +00:00
|
|
|
return wfMemcKey( $key, $this->mUser->getId(), $wgEchoCacheVersion );
|
|
|
|
}
|
2016-05-05 23:38:55 +00:00
|
|
|
|
2018-05-31 23:01:57 +00:00
|
|
|
/**
|
|
|
|
* Build a cache key for global use
|
|
|
|
*
|
|
|
|
* @param string $key Key, typically prefixed with echo-notification-
|
|
|
|
* @return string|false Memcached key, or false if one could not be generated
|
|
|
|
*/
|
|
|
|
protected function getGlobalMemcKey( $key ) {
|
|
|
|
global $wgEchoCacheVersion;
|
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
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:30:40 +00:00
|
|
|
/**
|
|
|
|
* Get the number of foreign notifications in a given section.
|
|
|
|
* @param string $section One of EchoAttributeManager::$sections
|
|
|
|
* @return int Number of foreign notifications
|
|
|
|
*/
|
2016-05-29 20:54:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-29 23:45:17 +00:00
|
|
|
/**
|
|
|
|
* Get the timestamp of the most recent foreign notification in a given section.
|
|
|
|
* @param string $section One of EchoAttributeManager::$sections
|
|
|
|
* @return MWTimestamp|false Timestamp of the most recent foreign notification, or false if
|
|
|
|
* there aren't any
|
|
|
|
*/
|
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
|
|
|
|
) {
|
2018-05-29 23:45:17 +00:00
|
|
|
$foreignTime = -1;
|
2016-05-29 20:54:15 +00:00
|
|
|
$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'] ) ) {
|
2018-05-29 23:45:17 +00:00
|
|
|
$foreignTime = max( $foreignTime, $subData['timestamp'] );
|
2016-05-29 20:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ( isset( $data[$section]['timestamp'] ) ) {
|
2018-05-29 23:45:17 +00:00
|
|
|
$foreignTime = max( $foreignTime, $data[$section]['timestamp'] );
|
2016-05-29 20:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-29 23:45:17 +00:00
|
|
|
$foreignTime = $foreignTime === -1 ? false : new MWTimestamp( $foreignTime );
|
2016-05-29 20:54:15 +00:00
|
|
|
} 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
|
|
|
}
|