3 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
4 use MediaWiki\Linker\LinkTarget
;
5 use MediaWiki\Revision\RevisionLookup
;
6 use MediaWiki\User\UserIdentity
;
7 use Wikimedia\Assert\Assert
;
8 use Wikimedia\Rdbms\IDatabase
;
9 use Wikimedia\Rdbms\ILBFactory
;
10 use Wikimedia\Rdbms\LoadBalancer
;
11 use Wikimedia\ScopedCallback
;
14 * Storage layer class for WatchedItems.
15 * Database interaction & caching
16 * TODO caching should be factored out into a CachingWatchedItemStore class
21 class WatchedItemStore
implements WatchedItemStoreInterface
, StatsdAwareInterface
{
31 private $loadBalancer;
46 private $readOnlyMode;
56 private $latestUpdateCache;
59 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
60 * The index is needed so that on mass changes all relevant items can be un-cached.
61 * For example: Clearing a users watchlist of all items or updating notification timestamps
62 * for all users watching a single target.
64 private $cacheIndex = [];
69 private $deferredUpdatesAddCallableUpdateCallback;
74 private $updateRowsPerQuery;
84 private $revisionLookup;
87 * @var StatsdDataFactoryInterface
92 * @param ILBFactory $lbFactory
93 * @param JobQueueGroup $queueGroup
94 * @param BagOStuff $stash
95 * @param HashBagOStuff $cache
96 * @param ReadOnlyMode $readOnlyMode
97 * @param int $updateRowsPerQuery
98 * @param NamespaceInfo $nsInfo
99 * @param RevisionLookup $revisionLookup
101 public function __construct(
102 ILBFactory
$lbFactory,
103 JobQueueGroup
$queueGroup,
105 HashBagOStuff
$cache,
106 ReadOnlyMode
$readOnlyMode,
108 NamespaceInfo
$nsInfo,
109 RevisionLookup
$revisionLookup
111 $this->lbFactory
= $lbFactory;
112 $this->loadBalancer
= $lbFactory->getMainLB();
113 $this->queueGroup
= $queueGroup;
114 $this->stash
= $stash;
115 $this->cache
= $cache;
116 $this->readOnlyMode
= $readOnlyMode;
117 $this->stats
= new NullStatsdDataFactory();
118 $this->deferredUpdatesAddCallableUpdateCallback
=
119 [ DeferredUpdates
::class, 'addCallableUpdate' ];
120 $this->updateRowsPerQuery
= $updateRowsPerQuery;
121 $this->nsInfo
= $nsInfo;
122 $this->revisionLookup
= $revisionLookup;
124 $this->latestUpdateCache
= new HashBagOStuff( [ 'maxKeys' => 3 ] );
128 * @param StatsdDataFactoryInterface $stats
130 public function setStatsdDataFactory( StatsdDataFactoryInterface
$stats ) {
131 $this->stats
= $stats;
135 * Overrides the DeferredUpdates::addCallableUpdate callback
136 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
138 * @param callable $callback
140 * @see DeferredUpdates::addCallableUpdate for callback signiture
142 * @return ScopedCallback to reset the overridden value
143 * @throws MWException
145 public function overrideDeferredUpdatesAddCallableUpdateCallback( callable
$callback ) {
146 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
147 throw new MWException(
148 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
151 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback
;
152 $this->deferredUpdatesAddCallableUpdateCallback
= $callback;
153 return new ScopedCallback( function () use ( $previousValue ) {
154 $this->deferredUpdatesAddCallableUpdateCallback
= $previousValue;
158 private function getCacheKey( UserIdentity
$user, LinkTarget
$target ) {
159 return $this->cache
->makeKey(
160 (string)$target->getNamespace(),
162 (string)$user->getId()
166 private function cache( WatchedItem
$item ) {
167 $user = $item->getUserIdentity();
168 $target = $item->getLinkTarget();
169 $key = $this->getCacheKey( $user, $target );
170 $this->cache
->set( $key, $item );
171 $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
172 $this->stats
->increment( 'WatchedItemStore.cache' );
175 private function uncache( UserIdentity
$user, LinkTarget
$target ) {
176 $this->cache
->delete( $this->getCacheKey( $user, $target ) );
177 unset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
178 $this->stats
->increment( 'WatchedItemStore.uncache' );
181 private function uncacheLinkTarget( LinkTarget
$target ) {
182 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget' );
183 if ( !isset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] ) ) {
186 foreach ( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] as $key ) {
187 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
188 $this->cache
->delete( $key );
192 private function uncacheUser( UserIdentity
$user ) {
193 $this->stats
->increment( 'WatchedItemStore.uncacheUser' );
194 foreach ( $this->cacheIndex
as $ns => $dbKeyArray ) {
195 foreach ( $dbKeyArray as $dbKey => $userArray ) {
196 if ( isset( $userArray[$user->getId()] ) ) {
197 $this->stats
->increment( 'WatchedItemStore.uncacheUser.items' );
198 $this->cache
->delete( $userArray[$user->getId()] );
203 $pageSeenKey = $this->getPageSeenTimestampsKey( $user );
204 $this->latestUpdateCache
->delete( $pageSeenKey );
205 $this->stash
->delete( $pageSeenKey );
209 * @param UserIdentity $user
210 * @param LinkTarget $target
212 * @return WatchedItem|false
214 private function getCached( UserIdentity
$user, LinkTarget
$target ) {
215 return $this->cache
->get( $this->getCacheKey( $user, $target ) );
219 * Return an array of conditions to select or update the appropriate database
222 * @param UserIdentity $user
223 * @param LinkTarget $target
227 private function dbCond( UserIdentity
$user, LinkTarget
$target ) {
229 'wl_user' => $user->getId(),
230 'wl_namespace' => $target->getNamespace(),
231 'wl_title' => $target->getDBkey(),
236 * @param int $dbIndex DB_MASTER or DB_REPLICA
239 * @throws MWException
241 private function getConnectionRef( $dbIndex ) {
242 return $this->loadBalancer
->getConnectionRef( $dbIndex, [ 'watchlist' ] );
246 * Deletes ALL watched items for the given user when under
247 * $updateRowsPerQuery entries exist.
251 * @param UserIdentity $user
253 * @return bool true on success, false when too many items are watched
255 public function clearUserWatchedItems( UserIdentity
$user ) {
256 if ( $this->countWatchedItems( $user ) > $this->updateRowsPerQuery
) {
260 $dbw = $this->loadBalancer
->getConnectionRef( DB_MASTER
);
263 [ 'wl_user' => $user->getId() ],
266 $this->uncacheAllItemsForUser( $user );
271 private function uncacheAllItemsForUser( UserIdentity
$user ) {
272 $userId = $user->getId();
273 foreach ( $this->cacheIndex
as $ns => $dbKeyIndex ) {
274 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
275 if ( array_key_exists( $userId, $userIndex ) ) {
276 $this->cache
->delete( $userIndex[$userId] );
277 unset( $this->cacheIndex
[$ns][$dbKey][$userId] );
282 // Cleanup empty cache keys
283 foreach ( $this->cacheIndex
as $ns => $dbKeyIndex ) {
284 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
285 if ( empty( $this->cacheIndex
[$ns][$dbKey] ) ) {
286 unset( $this->cacheIndex
[$ns][$dbKey] );
289 if ( empty( $this->cacheIndex
[$ns] ) ) {
290 unset( $this->cacheIndex
[$ns] );
296 * Queues a job that will clear the users watchlist using the Job Queue.
300 * @param UserIdentity $user
302 public function clearUserWatchedItemsUsingJobQueue( UserIdentity
$user ) {
303 $job = ClearUserWatchlistJob
::newForUser( $user, $this->getMaxId() );
304 $this->queueGroup
->push( $job );
309 * @return int The maximum current wl_id
311 public function getMaxId() {
312 $dbr = $this->getConnectionRef( DB_REPLICA
);
313 return (int)$dbr->selectField(
323 * @param UserIdentity $user
326 public function countWatchedItems( UserIdentity
$user ) {
327 $dbr = $this->getConnectionRef( DB_REPLICA
);
328 $return = (int)$dbr->selectField(
332 'wl_user' => $user->getId()
342 * @param LinkTarget $target
345 public function countWatchers( LinkTarget
$target ) {
346 $dbr = $this->getConnectionRef( DB_REPLICA
);
347 $return = (int)$dbr->selectField(
351 'wl_namespace' => $target->getNamespace(),
352 'wl_title' => $target->getDBkey(),
362 * @param LinkTarget $target
363 * @param string|int $threshold
366 public function countVisitingWatchers( LinkTarget
$target, $threshold ) {
367 $dbr = $this->getConnectionRef( DB_REPLICA
);
368 $visitingWatchers = (int)$dbr->selectField(
372 'wl_namespace' => $target->getNamespace(),
373 'wl_title' => $target->getDBkey(),
374 'wl_notificationtimestamp >= ' .
375 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
376 ' OR wl_notificationtimestamp IS NULL'
381 return $visitingWatchers;
385 * @param UserIdentity $user
386 * @param TitleValue[] $titles
388 * @throws MWException
390 public function removeWatchBatchForUser( UserIdentity
$user, array $titles ) {
391 if ( $this->readOnlyMode
->isReadOnly() ) {
394 if ( !$user->isRegistered() ) {
401 $rows = $this->getTitleDbKeysGroupedByNamespace( $titles );
402 $this->uncacheTitlesForUser( $user, $titles );
404 $dbw = $this->getConnectionRef( DB_MASTER
);
405 $ticket = count( $titles ) > $this->updateRowsPerQuery ?
406 $this->lbFactory
->getEmptyTransactionTicket( __METHOD__
) : null;
409 // Batch delete items per namespace.
410 foreach ( $rows as $namespace => $namespaceTitles ) {
411 $rowBatches = array_chunk( $namespaceTitles, $this->updateRowsPerQuery
);
412 foreach ( $rowBatches as $toDelete ) {
413 $dbw->delete( 'watchlist', [
414 'wl_user' => $user->getId(),
415 'wl_namespace' => $namespace,
416 'wl_title' => $toDelete
418 $affectedRows +
= $dbw->affectedRows();
420 $this->lbFactory
->commitAndWaitForReplication( __METHOD__
, $ticket );
425 return (bool)$affectedRows;
430 * @param LinkTarget[] $targets
431 * @param array $options
434 public function countWatchersMultiple( array $targets, array $options = [] ) {
435 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
437 $dbr = $this->getConnectionRef( DB_REPLICA
);
439 if ( array_key_exists( 'minimumWatchers', $options ) ) {
440 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
443 $lb = new LinkBatch( $targets );
446 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
447 [ $lb->constructSet( 'wl', $dbr ) ],
453 foreach ( $targets as $linkTarget ) {
454 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
457 foreach ( $res as $row ) {
458 $watchCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
466 * @param array $targetsWithVisitThresholds
467 * @param int|null $minimumWatchers
470 public function countVisitingWatchersMultiple(
471 array $targetsWithVisitThresholds,
472 $minimumWatchers = null
474 if ( $targetsWithVisitThresholds === [] ) {
475 // No titles requested => no results returned
479 $dbr = $this->getConnectionRef( DB_REPLICA
);
481 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
483 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
484 if ( $minimumWatchers !== null ) {
485 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
489 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
496 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
497 /* @var LinkTarget $target */
498 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
501 foreach ( $res as $row ) {
502 $watcherCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
505 return $watcherCounts;
509 * Generates condition for the query used in a batch count visiting watchers.
511 * @param IDatabase $db
512 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
515 private function getVisitingWatchersCondition(
517 array $targetsWithVisitThresholds
519 $missingTargets = [];
520 $namespaceConds = [];
521 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
522 if ( $threshold === null ) {
523 $missingTargets[] = $target;
526 /* @var LinkTarget $target */
527 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
528 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
530 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
531 'wl_notificationtimestamp IS NULL'
537 foreach ( $namespaceConds as $namespace => $pageConds ) {
538 $conds[] = $db->makeList( [
539 'wl_namespace = ' . $namespace,
540 '(' . $db->makeList( $pageConds, LIST_OR
) . ')'
544 if ( $missingTargets ) {
545 $lb = new LinkBatch( $missingTargets );
546 $conds[] = $lb->constructSet( 'wl', $db );
549 return $db->makeList( $conds, LIST_OR
);
554 * @param UserIdentity $user
555 * @param LinkTarget $target
558 public function getWatchedItem( UserIdentity
$user, LinkTarget
$target ) {
559 if ( !$user->isRegistered() ) {
563 $cached = $this->getCached( $user, $target );
565 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.cached' );
568 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.load' );
569 return $this->loadWatchedItem( $user, $target );
574 * @param UserIdentity $user
575 * @param LinkTarget $target
576 * @return WatchedItem|bool
578 public function loadWatchedItem( UserIdentity
$user, LinkTarget
$target ) {
579 // Only registered user can have a watchlist
580 if ( !$user->isRegistered() ) {
584 $dbr = $this->getConnectionRef( DB_REPLICA
);
586 $row = $dbr->selectRow(
588 'wl_notificationtimestamp',
589 $this->dbCond( $user, $target ),
597 $item = new WatchedItem(
600 $this->getLatestNotificationTimestamp( $row->wl_notificationtimestamp
, $user, $target )
602 $this->cache( $item );
609 * @param UserIdentity $user
610 * @param array $options
611 * @return WatchedItem[]
613 public function getWatchedItemsForUser( UserIdentity
$user, array $options = [] ) {
614 $options +
= [ 'forWrite' => false ];
617 if ( array_key_exists( 'sort', $options ) ) {
619 ( in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ) ),
620 '$options[\'sort\']',
621 'must be SORT_ASC or SORT_DESC'
623 $dbOptions['ORDER BY'] = [
624 "wl_namespace {$options['sort']}",
625 "wl_title {$options['sort']}"
628 $db = $this->getConnectionRef( $options['forWrite'] ? DB_MASTER
: DB_REPLICA
);
632 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
633 [ 'wl_user' => $user->getId() ],
639 foreach ( $res as $row ) {
640 $target = new TitleValue( (int)$row->wl_namespace
, $row->wl_title
);
641 // @todo: Should we add these to the process cache?
642 $watchedItems[] = new WatchedItem(
644 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
645 $this->getLatestNotificationTimestamp(
646 $row->wl_notificationtimestamp
, $user, $target )
650 return $watchedItems;
655 * @param UserIdentity $user
656 * @param LinkTarget $target
659 public function isWatched( UserIdentity
$user, LinkTarget
$target ) {
660 return (bool)$this->getWatchedItem( $user, $target );
665 * @param UserIdentity $user
666 * @param LinkTarget[] $targets
669 public function getNotificationTimestampsBatch( UserIdentity
$user, array $targets ) {
671 foreach ( $targets as $target ) {
672 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
675 if ( !$user->isRegistered() ) {
680 foreach ( $targets as $target ) {
681 $cachedItem = $this->getCached( $user, $target );
683 $timestamps[$target->getNamespace()][$target->getDBkey()] =
684 $cachedItem->getNotificationTimestamp();
686 $targetsToLoad[] = $target;
690 if ( !$targetsToLoad ) {
694 $dbr = $this->getConnectionRef( DB_REPLICA
);
696 $lb = new LinkBatch( $targetsToLoad );
699 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
701 $lb->constructSet( 'wl', $dbr ),
702 'wl_user' => $user->getId(),
707 foreach ( $res as $row ) {
708 $target = new TitleValue( (int)$row->wl_namespace
, $row->wl_title
);
709 $timestamps[$row->wl_namespace
][$row->wl_title
] =
710 $this->getLatestNotificationTimestamp(
711 $row->wl_notificationtimestamp
, $user, $target );
719 * @param UserIdentity $user
720 * @param LinkTarget $target
721 * @throws MWException
723 public function addWatch( UserIdentity
$user, LinkTarget
$target ) {
724 $this->addWatchBatchForUser( $user, [ $target ] );
729 * @param UserIdentity $user
730 * @param LinkTarget[] $targets
732 * @throws MWException
734 public function addWatchBatchForUser( UserIdentity
$user, array $targets ) {
735 if ( $this->readOnlyMode
->isReadOnly() ) {
738 // Only registered user can have a watchlist
739 if ( !$user->isRegistered() ) {
749 foreach ( $targets as $target ) {
751 'wl_user' => $user->getId(),
752 'wl_namespace' => $target->getNamespace(),
753 'wl_title' => $target->getDBkey(),
754 'wl_notificationtimestamp' => null,
756 $items[] = new WatchedItem(
761 $this->uncache( $user, $target );
764 $dbw = $this->getConnectionRef( DB_MASTER
);
765 $ticket = count( $targets ) > $this->updateRowsPerQuery ?
766 $this->lbFactory
->getEmptyTransactionTicket( __METHOD__
) : null;
768 $rowBatches = array_chunk( $rows, $this->updateRowsPerQuery
);
769 foreach ( $rowBatches as $toInsert ) {
770 // Use INSERT IGNORE to avoid overwriting the notification timestamp
771 // if there's already an entry for this page
772 $dbw->insert( 'watchlist', $toInsert, __METHOD__
, 'IGNORE' );
773 $affectedRows +
= $dbw->affectedRows();
775 $this->lbFactory
->commitAndWaitForReplication( __METHOD__
, $ticket );
778 // Update process cache to ensure skin doesn't claim that the current
779 // page is unwatched in the response of action=watch itself (T28292).
780 // This would otherwise be re-queried from a replica by isWatched().
781 foreach ( $items as $item ) {
782 $this->cache( $item );
785 return (bool)$affectedRows;
790 * @param UserIdentity $user
791 * @param LinkTarget $target
793 * @throws MWException
795 public function removeWatch( UserIdentity
$user, LinkTarget
$target ) {
796 return $this->removeWatchBatchForUser( $user, [ $target ] );
800 * Set the "last viewed" timestamps for certain titles on a user's watchlist.
802 * If the $targets parameter is omitted or set to [], this method simply wraps
803 * resetAllNotificationTimestampsForUser(), and in that case you should instead call that method
804 * directly; support for omitting $targets is for backwards compatibility.
806 * If $targets is omitted or set to [], timestamps will be updated for every title on the user's
807 * watchlist, and this will be done through a DeferredUpdate. If $targets is a non-empty array,
808 * only the specified titles will be updated, and this will be done immediately (not deferred).
811 * @param UserIdentity $user
812 * @param string|int $timestamp Value to set the "last viewed" timestamp to (null to clear)
813 * @param LinkTarget[] $targets Titles to set the timestamp for; [] means the entire watchlist
816 public function setNotificationTimestampsForUser(
817 UserIdentity
$user, $timestamp, array $targets = []
819 // Only registered user can have a watchlist
820 if ( !$user->isRegistered() ||
$this->readOnlyMode
->isReadOnly() ) {
825 // Backwards compatibility
826 $this->resetAllNotificationTimestampsForUser( $user, $timestamp );
830 $rows = $this->getTitleDbKeysGroupedByNamespace( $targets );
832 $dbw = $this->getConnectionRef( DB_MASTER
);
833 if ( $timestamp !== null ) {
834 $timestamp = $dbw->timestamp( $timestamp );
836 $ticket = $this->lbFactory
->getEmptyTransactionTicket( __METHOD__
);
837 $affectedSinceWait = 0;
839 // Batch update items per namespace
840 foreach ( $rows as $namespace => $namespaceTitles ) {
841 $rowBatches = array_chunk( $namespaceTitles, $this->updateRowsPerQuery
);
842 foreach ( $rowBatches as $toUpdate ) {
845 [ 'wl_notificationtimestamp' => $timestamp ],
847 'wl_user' => $user->getId(),
848 'wl_namespace' => $namespace,
849 'wl_title' => $toUpdate
852 $affectedSinceWait +
= $dbw->affectedRows();
853 // Wait for replication every time we've touched updateRowsPerQuery rows
854 if ( $affectedSinceWait >= $this->updateRowsPerQuery
) {
855 $this->lbFactory
->commitAndWaitForReplication( __METHOD__
, $ticket );
856 $affectedSinceWait = 0;
861 $this->uncacheUser( $user );
866 public function getLatestNotificationTimestamp(
867 $timestamp, UserIdentity
$user, LinkTarget
$target
869 $timestamp = wfTimestampOrNull( TS_MW
, $timestamp );
870 if ( $timestamp === null ) {
871 return null; // no notification
874 $seenTimestamps = $this->getPageSeenTimestamps( $user );
877 $seenTimestamps->get( $this->getPageSeenKey( $target ) ) >= $timestamp
879 // If a reset job did not yet run, then the "seen" timestamp will be higher
887 * Schedule a DeferredUpdate that sets all of the "last viewed" timestamps for a given user
889 * @param UserIdentity $user
890 * @param string|int|null $timestamp Value to set all timestamps to, null to clear them
892 public function resetAllNotificationTimestampsForUser( UserIdentity
$user, $timestamp = null ) {
893 // Only registered user can have a watchlist
894 if ( !$user->isRegistered() ) {
898 // If the page is watched by the user (or may be watched), update the timestamp
899 $job = new ClearWatchlistNotificationsJob( [
900 'userId' => $user->getId(), 'timestamp' => $timestamp, 'casTime' => time()
903 // Try to run this post-send
904 // Calls DeferredUpdates::addCallableUpdate in normal operation
906 $this->deferredUpdatesAddCallableUpdateCallback
,
907 function () use ( $job ) {
915 * @param UserIdentity $editor
916 * @param LinkTarget $target
917 * @param string|int $timestamp
920 public function updateNotificationTimestamp(
921 UserIdentity
$editor, LinkTarget
$target, $timestamp
923 $dbw = $this->getConnectionRef( DB_MASTER
);
924 $uids = $dbw->selectFieldValues(
928 'wl_user != ' . intval( $editor->getId() ),
929 'wl_namespace' => $target->getNamespace(),
930 'wl_title' => $target->getDBkey(),
931 'wl_notificationtimestamp IS NULL',
936 $watchers = array_map( 'intval', $uids );
938 // Update wl_notificationtimestamp for all watching users except the editor
940 DeferredUpdates
::addCallableUpdate(
941 function () use ( $timestamp, $watchers, $target, $fname ) {
942 $dbw = $this->getConnectionRef( DB_MASTER
);
943 $ticket = $this->lbFactory
->getEmptyTransactionTicket( $fname );
945 $watchersChunks = array_chunk( $watchers, $this->updateRowsPerQuery
);
946 foreach ( $watchersChunks as $watchersChunk ) {
947 $dbw->update( 'watchlist',
949 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
950 ], [ /* WHERE - TODO Use wl_id T130067 */
951 'wl_user' => $watchersChunk,
952 'wl_namespace' => $target->getNamespace(),
953 'wl_title' => $target->getDBkey(),
956 if ( count( $watchersChunks ) > 1 ) {
957 $this->lbFactory
->commitAndWaitForReplication(
958 $fname, $ticket, [ 'domain' => $dbw->getDomainID() ]
962 $this->uncacheLinkTarget( $target );
964 DeferredUpdates
::POSTSEND
,
974 * @param UserIdentity $user
975 * @param LinkTarget $title
976 * @param string $force
980 public function resetNotificationTimestamp(
981 UserIdentity
$user, LinkTarget
$title, $force = '', $oldid = 0
985 // Only registered user can have a watchlist
986 if ( $this->readOnlyMode
->isReadOnly() ||
!$user->isRegistered() ) {
990 // Hook expects User and Title, not UserIdentity and LinkTarget
991 $userObj = User
::newFromId( $user->getId() );
992 $titleObj = Title
::castFromLinkTarget( $title );
993 if ( !Hooks
::run( 'BeforeResetNotificationTimestamp',
994 [ &$userObj, &$titleObj, $force, &$oldid ] )
998 if ( !$userObj->equals( $user ) ) {
1001 if ( !$titleObj->equals( $title ) ) {
1006 if ( $force != 'force' ) {
1007 $item = $this->loadWatchedItem( $user, $title );
1008 if ( !$item ||
$item->getNotificationTimestamp() === null ) {
1013 // Get the timestamp (TS_MW) of this revision to track the latest one seen
1017 $latestRev = $this->revisionLookup
->getRevisionByTitle( $title );
1019 $id = $latestRev->getId();
1021 $seenTime = $latestRev->getTimestamp();
1024 if ( $seenTime === null ) {
1025 $seenTime = $this->revisionLookup
->getTimestampFromId( $id );
1028 // Mark the item as read immediately in lightweight storage
1029 $this->stash
->merge(
1030 $this->getPageSeenTimestampsKey( $user ),
1031 function ( $cache, $key, $current ) use ( $title, $seenTime ) {
1032 $value = $current ?
: new MapCacheLRU( 300 );
1033 $subKey = $this->getPageSeenKey( $title );
1035 if ( $seenTime > $value->get( $subKey ) ) {
1036 // Revision is newer than the last one seen
1037 $value->set( $subKey, $seenTime );
1038 $this->latestUpdateCache
->set( $key, $value, IExpiringStore
::TTL_PROC_LONG
);
1039 } elseif ( $seenTime === false ) {
1040 // Revision does not exist
1041 $value->set( $subKey, wfTimestamp( TS_MW
) );
1042 $this->latestUpdateCache
->set( $key, $value, IExpiringStore
::TTL_PROC_LONG
);
1044 return false; // nothing to update
1049 IExpiringStore
::TTL_HOUR
1052 // If the page is watched by the user (or may be watched), update the timestamp
1053 $job = new ActivityUpdateJob(
1056 'type' => 'updateWatchlistNotification',
1057 'userid' => $user->getId(),
1058 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
1062 // Try to enqueue this post-send
1063 $this->queueGroup
->lazyPush( $job );
1065 $this->uncache( $user, $title );
1071 * @param UserIdentity $user
1072 * @return MapCacheLRU|null The map contains prefixed title keys and TS_MW values
1074 private function getPageSeenTimestamps( UserIdentity
$user ) {
1075 $key = $this->getPageSeenTimestampsKey( $user );
1077 return $this->latestUpdateCache
->getWithSetCallback(
1079 IExpiringStore
::TTL_PROC_LONG
,
1080 function () use ( $key ) {
1081 return $this->stash
->get( $key ) ?
: null;
1087 * @param UserIdentity $user
1090 private function getPageSeenTimestampsKey( UserIdentity
$user ) {
1091 return $this->stash
->makeGlobalKey(
1092 'watchlist-recent-updates',
1093 $this->lbFactory
->getLocalDomainID(),
1099 * @param LinkTarget $target
1102 private function getPageSeenKey( LinkTarget
$target ) {
1103 return "{$target->getNamespace()}:{$target->getDBkey()}";
1106 private function getNotificationTimestamp(
1107 UserIdentity
$user, LinkTarget
$title, $item, $force, $oldid
1110 // No oldid given, assuming latest revision; clear the timestamp.
1114 $oldRev = $this->revisionLookup
->getRevisionById( $oldid );
1115 if ( !$this->revisionLookup
->getNextRevision( $oldRev, $title ) ) {
1116 // Oldid given and is the latest revision for this title; clear the timestamp.
1120 if ( $item === null ) {
1121 $item = $this->loadWatchedItem( $user, $title );
1125 // This can only happen if $force is enabled.
1129 // Oldid given and isn't the latest; update the timestamp.
1130 // This will result in no further notification emails being sent!
1131 $notificationTimestamp = $this->revisionLookup
->getTimestampFromId( $oldid );
1133 // We need to go one second to the future because of various strict comparisons
1134 // throughout the codebase
1135 $ts = new MWTimestamp( $notificationTimestamp );
1136 $ts->timestamp
->add( new DateInterval( 'PT1S' ) );
1137 $notificationTimestamp = $ts->getTimestamp( TS_MW
);
1139 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
1140 if ( $force != 'force' ) {
1143 // This is a little silly…
1144 return $item->getNotificationTimestamp();
1148 return $notificationTimestamp;
1153 * @param UserIdentity $user
1154 * @param int|null $unreadLimit
1157 public function countUnreadNotifications( UserIdentity
$user, $unreadLimit = null ) {
1158 $dbr = $this->getConnectionRef( DB_REPLICA
);
1161 if ( $unreadLimit !== null ) {
1162 $unreadLimit = (int)$unreadLimit;
1163 $queryOptions['LIMIT'] = $unreadLimit;
1167 'wl_user' => $user->getId(),
1168 'wl_notificationtimestamp IS NOT NULL'
1171 $rowCount = $dbr->selectRowCount( 'watchlist', '1', $conds, __METHOD__
, $queryOptions );
1173 if ( $unreadLimit === null ) {
1177 if ( $rowCount >= $unreadLimit ) {
1186 * @param LinkTarget $oldTarget
1187 * @param LinkTarget $newTarget
1189 public function duplicateAllAssociatedEntries( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
1190 // Duplicate first the subject page, then the talk page
1191 $this->duplicateEntry(
1192 $this->nsInfo
->getSubjectPage( $oldTarget ),
1193 $this->nsInfo
->getSubjectPage( $newTarget )
1195 $this->duplicateEntry(
1196 $this->nsInfo
->getTalkPage( $oldTarget ),
1197 $this->nsInfo
->getTalkPage( $newTarget )
1203 * @param LinkTarget $oldTarget
1204 * @param LinkTarget $newTarget
1206 public function duplicateEntry( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
1207 $dbw = $this->getConnectionRef( DB_MASTER
);
1209 $result = $dbw->select(
1211 [ 'wl_user', 'wl_notificationtimestamp' ],
1213 'wl_namespace' => $oldTarget->getNamespace(),
1214 'wl_title' => $oldTarget->getDBkey(),
1220 $newNamespace = $newTarget->getNamespace();
1221 $newDBkey = $newTarget->getDBkey();
1223 # Construct array to replace into the watchlist
1225 foreach ( $result as $row ) {
1227 'wl_user' => $row->wl_user
,
1228 'wl_namespace' => $newNamespace,
1229 'wl_title' => $newDBkey,
1230 'wl_notificationtimestamp' => $row->wl_notificationtimestamp
,
1234 if ( !empty( $values ) ) {
1236 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
1237 # some other DBMSes, mostly due to poor simulation by us
1240 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
1248 * @param LinkTarget[] $titles
1251 private function getTitleDbKeysGroupedByNamespace( array $titles ) {
1253 foreach ( $titles as $title ) {
1254 // Group titles by namespace.
1255 $rows[ $title->getNamespace() ][] = $title->getDBkey();
1261 * @param UserIdentity $user
1262 * @param LinkTarget[] $titles
1264 private function uncacheTitlesForUser( UserIdentity
$user, array $titles ) {
1265 foreach ( $titles as $title ) {
1266 $this->uncache( $user, $title );