2013-05-24 22:51:47 +00:00
|
|
|
<?php
|
2018-08-13 07:29:32 +00:00
|
|
|
|
2016-12-08 20:50:03 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-01-02 18:40:58 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
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
|
|
|
/**
|
2019-03-01 22:34:38 +00:00
|
|
|
* @var EchoForeignNotifications|null
|
2015-11-25 04:07:54 +00:00
|
|
|
*/
|
2019-03-01 22:34:38 +00:00
|
|
|
private $foreignNotifications;
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2016-03-24 15:33:13 +00:00
|
|
|
/**
|
2018-08-13 07:25:22 +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;
|
|
|
|
|
|
|
|
/**
|
2018-08-13 07:25:22 +00:00
|
|
|
* @var array[]|null
|
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 $globalCountsAndTimestamps;
|
2016-03-24 15:33:13 +00:00
|
|
|
|
2016-05-29 20:54:15 +00:00
|
|
|
/**
|
2018-08-13 07:25:22 +00:00
|
|
|
* @var array[]|null
|
2016-05-29 20:54:15 +00:00
|
|
|
*/
|
2019-03-01 22:34:38 +00:00
|
|
|
private $mForeignData;
|
2016-05-29 20:54:15 +00:00
|
|
|
|
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.
|
2020-09-19 18:55:32 +00:00
|
|
|
public const MAX_BADGE_COUNT = 99;
|
2016-03-09 04:50:31 +00:00
|
|
|
|
2020-09-19 18:55:32 +00:00
|
|
|
private const CACHE_TTL = 86400;
|
|
|
|
private const CACHE_KEY = 'echo-notification-counts';
|
|
|
|
private const CHECK_KEY = 'echo-notification-updated';
|
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
|
|
|
|
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 ) {
|
2020-12-22 15:36:58 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
2013-05-24 22:51:47 +00:00
|
|
|
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(),
|
2019-08-30 20:58:41 +00:00
|
|
|
new EchoUserNotificationGateway(
|
|
|
|
$user,
|
|
|
|
MWEchoDbFactory::newFromDefault(),
|
|
|
|
MediaWikiServices::getInstance()->getMainConfig()
|
|
|
|
),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-31 23:33:23 +00:00
|
|
|
* Mark all edit-user-talk notifications as read. This is called when a user visits their user talk page.
|
2013-05-24 22:51:47 +00:00
|
|
|
*/
|
2018-05-31 23:33:23 +00:00
|
|
|
public function clearUserTalkNotifications() {
|
2013-05-24 22:51:47 +00:00
|
|
|
$this->markRead(
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->userNotifGateway->getUnreadNotifications(
|
2013-05-24 22:51:47 +00:00
|
|
|
'edit-user-talk'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
/**
|
|
|
|
* Get message count for this user.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-05-31 23:11:57 +00:00
|
|
|
public function getMessageCount() {
|
|
|
|
return $this->getNotificationCount( EchoAttributeManager::MESSAGE );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get alert count for this user.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-05-31 23:11:57 +00:00
|
|
|
public function getAlertCount() {
|
|
|
|
return $this->getNotificationCount( EchoAttributeManager::ALERT );
|
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
|
|
|
}
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2018-05-31 23:11:57 +00:00
|
|
|
/**
|
|
|
|
* Get the number of unread local notifications in a given section. This does not include
|
|
|
|
* foreign notifications, even if the user has cross-wiki notifications enabled.
|
|
|
|
*
|
|
|
|
* @param string $section Notification section
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLocalNotificationCount( $section = EchoAttributeManager::ALL ) {
|
|
|
|
return $this->getNotificationCount( $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
|
|
|
*
|
2014-08-05 21:50:54 +00:00
|
|
|
* @param string $section Notification section
|
2020-06-27 10:05:03 +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
|
|
|
*/
|
2018-05-31 23:11:57 +00:00
|
|
|
public function getNotificationCount( $section = EchoAttributeManager::ALL, $global = 'preference' ) {
|
2020-12-22 15:36:58 +00:00
|
|
|
if ( !$this->mUser->isRegistered() ) {
|
2013-05-24 22:51:47 +00:00
|
|
|
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
|
|
|
*
|
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
|
|
|
*/
|
2018-05-31 23:11:57 +00:00
|
|
|
public function getLastUnreadAlertTime() {
|
|
|
|
return $this->getLastUnreadNotificationTime( 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
|
|
|
*
|
2015-11-25 04:07:54 +00:00
|
|
|
* @return bool|MWTimestamp
|
2015-08-13 00:54:16 +00:00
|
|
|
*/
|
2018-05-31 23:11:57 +00:00
|
|
|
public function getLastUnreadMessageTime() {
|
|
|
|
return $this->getLastUnreadNotificationTime( 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.
|
|
|
|
*
|
2015-04-29 12:08:30 +00:00
|
|
|
* @param string $section Notification section
|
2020-06-27 10:05:03 +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
|
|
|
*/
|
2018-05-31 23:11:57 +00:00
|
|
|
public function getLastUnreadNotificationTime( $section = EchoAttributeManager::ALL, $global = 'preference' ) {
|
2020-12-22 15:36:58 +00:00
|
|
|
if ( !$this->mUser->isRegistered() ) {
|
2015-04-29 12:08:30 +00:00
|
|
|
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.
|
2020-05-15 16:58:14 +00:00
|
|
|
$talkPageNotificationManager = MediaWikiServices::getInstance()
|
|
|
|
->getTalkPageNotificationManager();
|
|
|
|
if ( $talkPageNotificationManager->userHasNewMessages( $this->mUser ) ) {
|
2021-02-25 01:55:40 +00:00
|
|
|
$attributeManager = EchoServices::getInstance()->getAttributeManager();
|
2016-10-13 18:55:34 +00:00
|
|
|
$categoryMap = $attributeManager->getEventsByCategory();
|
|
|
|
$usertalkTypes = $categoryMap['edit-user-talk'];
|
2020-06-27 10:05:03 +00:00
|
|
|
$unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser(
|
|
|
|
$this->mUser,
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
$usertalkTypes,
|
|
|
|
null,
|
2021-05-03 07:28:02 +00:00
|
|
|
DB_PRIMARY
|
2020-06-27 10:05:03 +00:00
|
|
|
);
|
2018-06-17 16:59:03 +00:00
|
|
|
if ( $unreadEditUserTalk === [] ) {
|
2020-05-15 16:58:14 +00:00
|
|
|
$talkPageNotificationManager->removeUserHasNewMessages( $this->mUser );
|
2015-08-15 01:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
2020-05-15 16:58:14 +00:00
|
|
|
$talkPageNotificationManager = MediaWikiServices::getInstance()
|
|
|
|
->getTalkPageNotificationManager();
|
|
|
|
if ( !$talkPageNotificationManager->userHasNewMessages( $this->mUser ) ) {
|
2021-02-25 01:55:40 +00:00
|
|
|
$attributeManager = EchoServices::getInstance()->getAttributeManager();
|
2016-10-13 18:55:34 +00:00
|
|
|
$categoryMap = $attributeManager->getEventsByCategory();
|
|
|
|
$usertalkTypes = $categoryMap['edit-user-talk'];
|
2020-06-27 10:05:03 +00:00
|
|
|
$unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser(
|
|
|
|
$this->mUser,
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
$usertalkTypes,
|
|
|
|
null,
|
2021-05-03 07:28:02 +00:00
|
|
|
DB_PRIMARY
|
2020-06-27 10:05:03 +00:00
|
|
|
);
|
2018-06-17 16:59:03 +00:00
|
|
|
if ( $unreadEditUserTalk !== [] ) {
|
2020-05-15 16:58:14 +00:00
|
|
|
$talkPageNotificationManager->setUserHasNewMessages( $this->mUser );
|
2016-03-04 22:44:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-25 01:55:40 +00:00
|
|
|
$attributeManager = EchoServices::getInstance()->getAttributeManager();
|
2014-08-13 22:00:25 +00:00
|
|
|
$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(
|
2021-05-04 16:06:42 +00:00
|
|
|
array_map( static 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();
|
|
|
|
}
|
2019-03-01 22:34:38 +00:00
|
|
|
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-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-09-03 17:01:06 +00:00
|
|
|
/**
|
|
|
|
* Mark one of more notifications as read on a foreign wiki.
|
|
|
|
*
|
|
|
|
* @param int[] $eventIds Event IDs to mark as read
|
|
|
|
* @param string $wiki Wiki name
|
|
|
|
*/
|
|
|
|
public function markReadForeign( array $eventIds, $wiki ) {
|
|
|
|
$foreignReq = new EchoForeignWikiRequest(
|
|
|
|
$this->mUser,
|
|
|
|
[
|
|
|
|
'action' => 'echomarkread',
|
|
|
|
'list' => implode( '|', $eventIds ),
|
|
|
|
],
|
|
|
|
[ $wiki ],
|
|
|
|
'wikis',
|
|
|
|
'csrf'
|
|
|
|
);
|
|
|
|
$foreignReq->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get information about a set of unread notifications on a foreign wiki.
|
|
|
|
*
|
|
|
|
* @param int[] $eventIds Event IDs to look up. Only unread notifications can be found.
|
|
|
|
* @param string $wiki Wiki name
|
|
|
|
* @return array[] Array of notification data as returned by api.php, keyed by event ID
|
|
|
|
*/
|
|
|
|
public function getForeignNotificationInfo( array $eventIds, $wiki ) {
|
|
|
|
$foreignReq = new EchoForeignWikiRequest(
|
|
|
|
$this->mUser,
|
|
|
|
[
|
|
|
|
'action' => 'query',
|
|
|
|
'meta' => 'notifications',
|
|
|
|
'notprop' => 'list',
|
|
|
|
'notfilter' => '!read',
|
|
|
|
'notlimit' => 'max'
|
|
|
|
],
|
|
|
|
[ $wiki ],
|
|
|
|
'notwikis'
|
|
|
|
);
|
|
|
|
$foreignResults = $foreignReq->execute();
|
|
|
|
$list = $foreignResults[$wiki]['query']['notifications']['list'] ?? [];
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
foreach ( $list as $notif ) {
|
|
|
|
if ( in_array( $notif['id'], $eventIds ) ) {
|
|
|
|
$result[$notif['id']] = $notif;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
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
|
2021-05-13 00:04:54 +00:00
|
|
|
* the primary 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
|
2021-05-03 07:28:02 +00:00
|
|
|
$newLocalData = $this->computeLocalCountsAndTimestamps( DB_PRIMARY );
|
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
|
2018-08-13 07:25:22 +00:00
|
|
|
* @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.
|
2021-05-13 00:04:54 +00:00
|
|
|
* @param int $dbSource DB_REPLICA or DB_PRIMARY
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return array[] [ 'alert' => [ 'count' => N, 'timestamp' => TS ], ... ]
|
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
|
|
|
*/
|
|
|
|
protected function computeLocalCountsAndTimestamps( $dbSource = DB_REPLICA ) {
|
2021-02-25 01:55:40 +00:00
|
|
|
$attributeManager = EchoServices::getInstance()->getAttributeManager();
|
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 = [];
|
|
|
|
$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 ) {
|
2020-06-27 10:05:03 +00:00
|
|
|
$eventTypesToLoad = $attributeManager->getUserEnabledEventsbySections(
|
|
|
|
$this->mUser,
|
|
|
|
'web',
|
|
|
|
[ $section ]
|
|
|
|
);
|
2016-12-08 20:50:03 +00:00
|
|
|
|
2020-06-27 10:05:03 +00:00
|
|
|
$count = (int)$this->userNotifGateway->getCappedNotificationCount(
|
|
|
|
$dbSource,
|
|
|
|
$eventTypesToLoad,
|
|
|
|
self::MAX_BADGE_COUNT + 1
|
|
|
|
);
|
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]['count'] = $count;
|
|
|
|
$totals['count'] += $count;
|
|
|
|
|
2020-06-27 10:05:03 +00:00
|
|
|
$notifications = $this->notifMapper->fetchUnreadByUser(
|
|
|
|
$this->mUser,
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
$eventTypesToLoad,
|
|
|
|
null,
|
|
|
|
$dbSource
|
|
|
|
);
|
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
|
|
|
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.
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return array[] [ 'alert' => [ 'count' => N, 'timestamp' => TS ], ... ]
|
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
|
|
|
*/
|
|
|
|
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 );
|
2020-06-27 10:05:03 +00:00
|
|
|
$globalTimestamp = max(
|
|
|
|
$localTimestamp,
|
|
|
|
$foreignTimestamp ? $foreignTimestamp->getTimestamp( TS_MW ) : -1
|
|
|
|
);
|
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'] = $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' );
|
|
|
|
}
|
2019-03-01 22:34:38 +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;
|
2019-04-17 11:59:14 +00:00
|
|
|
return $this->cache->makeKey( $key, $this->mUser->getId(), $wgEchoCacheVersion );
|
2018-05-31 23:01:57 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2019-04-17 11:59:14 +00:00
|
|
|
return $this->cache->makeGlobalKey( $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
|
|
|
|
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 ) {
|
2019-04-18 05:07:14 +00:00
|
|
|
return self::capNotificationCount(
|
|
|
|
$this->getForeignNotifications()->getCount( $section )
|
|
|
|
);
|
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 ) {
|
2019-04-18 05:07:14 +00:00
|
|
|
return $this->getForeignNotifications()->getTimestamp( $section );
|
2016-05-29 20:54:15 +00:00
|
|
|
}
|
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
|
|
|
}
|