3 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
4 use MediaWiki\Linker\LinkTarget
;
5 use MediaWiki\User\UserIdentity
;
6 use Wikimedia\Assert\Assert
;
7 use Wikimedia\Rdbms\IDatabase
;
8 use Wikimedia\Rdbms\ILBFactory
;
9 use Wikimedia\Rdbms\LoadBalancer
;
10 use Wikimedia\ScopedCallback
;
13 * Storage layer class for WatchedItems.
14 * Database interaction & caching
15 * TODO caching should be factored out into a CachingWatchedItemStore class
20 class WatchedItemStore
implements WatchedItemStoreInterface
, StatsdAwareInterface
{
30 private $loadBalancer;
45 private $readOnlyMode;
55 private $latestUpdateCache;
58 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
59 * The index is needed so that on mass changes all relevant items can be un-cached.
60 * For example: Clearing a users watchlist of all items or updating notification timestamps
61 * for all users watching a single target.
63 private $cacheIndex = [];
68 private $deferredUpdatesAddCallableUpdateCallback;
73 private $revisionGetTimestampFromIdCallback;
78 private $updateRowsPerQuery;
81 * @var StatsdDataFactoryInterface
86 * @param ILBFactory $lbFactory
87 * @param JobQueueGroup $queueGroup
88 * @param BagOStuff $stash
89 * @param HashBagOStuff $cache
90 * @param ReadOnlyMode $readOnlyMode
91 * @param int $updateRowsPerQuery
93 public function __construct(
94 ILBFactory
$lbFactory,
95 JobQueueGroup
$queueGroup,
98 ReadOnlyMode
$readOnlyMode,
101 $this->lbFactory
= $lbFactory;
102 $this->loadBalancer
= $lbFactory->getMainLB();
103 $this->queueGroup
= $queueGroup;
104 $this->stash
= $stash;
105 $this->cache
= $cache;
106 $this->readOnlyMode
= $readOnlyMode;
107 $this->stats
= new NullStatsdDataFactory();
108 $this->deferredUpdatesAddCallableUpdateCallback
=
109 [ DeferredUpdates
::class, 'addCallableUpdate' ];
110 $this->revisionGetTimestampFromIdCallback
=
111 [ Revision
::class, 'getTimestampFromId' ];
112 $this->updateRowsPerQuery
= $updateRowsPerQuery;
114 $this->latestUpdateCache
= new HashBagOStuff( [ 'maxKeys' => 3 ] );
118 * @param StatsdDataFactoryInterface $stats
120 public function setStatsdDataFactory( StatsdDataFactoryInterface
$stats ) {
121 $this->stats
= $stats;
125 * Overrides the DeferredUpdates::addCallableUpdate callback
126 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
128 * @param callable $callback
130 * @see DeferredUpdates::addCallableUpdate for callback signiture
132 * @return ScopedCallback to reset the overridden value
133 * @throws MWException
135 public function overrideDeferredUpdatesAddCallableUpdateCallback( callable
$callback ) {
136 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
137 throw new MWException(
138 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
141 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback
;
142 $this->deferredUpdatesAddCallableUpdateCallback
= $callback;
143 return new ScopedCallback( function () use ( $previousValue ) {
144 $this->deferredUpdatesAddCallableUpdateCallback
= $previousValue;
149 * Overrides the Revision::getTimestampFromId callback
150 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
152 * @param callable $callback
153 * @see Revision::getTimestampFromId for callback signiture
155 * @return ScopedCallback to reset the overridden value
156 * @throws MWException
158 public function overrideRevisionGetTimestampFromIdCallback( callable
$callback ) {
159 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
160 throw new MWException(
161 'Cannot override Revision::getTimestampFromId callback in operation.'
164 $previousValue = $this->revisionGetTimestampFromIdCallback
;
165 $this->revisionGetTimestampFromIdCallback
= $callback;
166 return new ScopedCallback( function () use ( $previousValue ) {
167 $this->revisionGetTimestampFromIdCallback
= $previousValue;
171 private function getCacheKey( UserIdentity
$user, LinkTarget
$target ) {
172 return $this->cache
->makeKey(
173 (string)$target->getNamespace(),
175 (string)$user->getId()
179 private function cache( WatchedItem
$item ) {
180 $user = $item->getUserIdentity();
181 $target = $item->getLinkTarget();
182 $key = $this->getCacheKey( $user, $target );
183 $this->cache
->set( $key, $item );
184 $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
185 $this->stats
->increment( 'WatchedItemStore.cache' );
188 private function uncache( UserIdentity
$user, LinkTarget
$target ) {
189 $this->cache
->delete( $this->getCacheKey( $user, $target ) );
190 unset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
191 $this->stats
->increment( 'WatchedItemStore.uncache' );
194 private function uncacheLinkTarget( LinkTarget
$target ) {
195 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget' );
196 if ( !isset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] ) ) {
199 foreach ( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] as $key ) {
200 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
201 $this->cache
->delete( $key );
205 private function uncacheUser( UserIdentity
$user ) {
206 $this->stats
->increment( 'WatchedItemStore.uncacheUser' );
207 foreach ( $this->cacheIndex
as $ns => $dbKeyArray ) {
208 foreach ( $dbKeyArray as $dbKey => $userArray ) {
209 if ( isset( $userArray[$user->getId()] ) ) {
210 $this->stats
->increment( 'WatchedItemStore.uncacheUser.items' );
211 $this->cache
->delete( $userArray[$user->getId()] );
216 $pageSeenKey = $this->getPageSeenTimestampsKey( $user );
217 $this->latestUpdateCache
->delete( $pageSeenKey );
218 $this->stash
->delete( $pageSeenKey );
222 * @param UserIdentity $user
223 * @param LinkTarget $target
225 * @return WatchedItem|false
227 private function getCached( UserIdentity
$user, LinkTarget
$target ) {
228 return $this->cache
->get( $this->getCacheKey( $user, $target ) );
232 * Return an array of conditions to select or update the appropriate database
235 * @param UserIdentity $user
236 * @param LinkTarget $target
240 private function dbCond( UserIdentity
$user, LinkTarget
$target ) {
242 'wl_user' => $user->getId(),
243 'wl_namespace' => $target->getNamespace(),
244 'wl_title' => $target->getDBkey(),
249 * @param int $dbIndex DB_MASTER or DB_REPLICA
252 * @throws MWException
254 private function getConnectionRef( $dbIndex ) {
255 return $this->loadBalancer
->getConnectionRef( $dbIndex, [ 'watchlist' ] );
259 * Deletes ALL watched items for the given user when under
260 * $updateRowsPerQuery entries exist.
264 * @param UserIdentity $user
266 * @return bool true on success, false when too many items are watched
268 public function clearUserWatchedItems( UserIdentity
$user ) {
269 if ( $this->countWatchedItems( $user ) > $this->updateRowsPerQuery
) {
273 $dbw = $this->loadBalancer
->getConnectionRef( DB_MASTER
);
276 [ 'wl_user' => $user->getId() ],
279 $this->uncacheAllItemsForUser( $user );
284 private function uncacheAllItemsForUser( UserIdentity
$user ) {
285 $userId = $user->getId();
286 foreach ( $this->cacheIndex
as $ns => $dbKeyIndex ) {
287 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
288 if ( array_key_exists( $userId, $userIndex ) ) {
289 $this->cache
->delete( $userIndex[$userId] );
290 unset( $this->cacheIndex
[$ns][$dbKey][$userId] );
295 // Cleanup empty cache keys
296 foreach ( $this->cacheIndex
as $ns => $dbKeyIndex ) {
297 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
298 if ( empty( $this->cacheIndex
[$ns][$dbKey] ) ) {
299 unset( $this->cacheIndex
[$ns][$dbKey] );
302 if ( empty( $this->cacheIndex
[$ns] ) ) {
303 unset( $this->cacheIndex
[$ns] );
309 * Queues a job that will clear the users watchlist using the Job Queue.
313 * @param UserIdentity $user
315 public function clearUserWatchedItemsUsingJobQueue( UserIdentity
$user ) {
316 $job = ClearUserWatchlistJob
::newForUser( $user, $this->getMaxId() );
317 $this->queueGroup
->push( $job );
322 * @return int The maximum current wl_id
324 public function getMaxId() {
325 $dbr = $this->getConnectionRef( DB_REPLICA
);
326 return (int)$dbr->selectField(
336 * @param UserIdentity $user
339 public function countWatchedItems( UserIdentity
$user ) {
340 $dbr = $this->getConnectionRef( DB_REPLICA
);
341 $return = (int)$dbr->selectField(
345 'wl_user' => $user->getId()
355 * @param LinkTarget $target
358 public function countWatchers( LinkTarget
$target ) {
359 $dbr = $this->getConnectionRef( DB_REPLICA
);
360 $return = (int)$dbr->selectField(
364 'wl_namespace' => $target->getNamespace(),
365 'wl_title' => $target->getDBkey(),
375 * @param LinkTarget $target
376 * @param string|int $threshold
379 public function countVisitingWatchers( LinkTarget
$target, $threshold ) {
380 $dbr = $this->getConnectionRef( DB_REPLICA
);
381 $visitingWatchers = (int)$dbr->selectField(
385 'wl_namespace' => $target->getNamespace(),
386 'wl_title' => $target->getDBkey(),
387 'wl_notificationtimestamp >= ' .
388 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
389 ' OR wl_notificationtimestamp IS NULL'
394 return $visitingWatchers;
398 * @param UserIdentity $user
399 * @param TitleValue[] $titles
401 * @throws MWException
403 public function removeWatchBatchForUser( UserIdentity
$user, array $titles ) {
404 if ( $this->readOnlyMode
->isReadOnly() ) {
407 if ( !$user->isRegistered() ) {
414 $rows = $this->getTitleDbKeysGroupedByNamespace( $titles );
415 $this->uncacheTitlesForUser( $user, $titles );
417 $dbw = $this->getConnectionRef( DB_MASTER
);
418 $ticket = count( $titles ) > $this->updateRowsPerQuery ?
419 $this->lbFactory
->getEmptyTransactionTicket( __METHOD__
) : null;
422 // Batch delete items per namespace.
423 foreach ( $rows as $namespace => $namespaceTitles ) {
424 $rowBatches = array_chunk( $namespaceTitles, $this->updateRowsPerQuery
);
425 foreach ( $rowBatches as $toDelete ) {
426 $dbw->delete( 'watchlist', [
427 'wl_user' => $user->getId(),
428 'wl_namespace' => $namespace,
429 'wl_title' => $toDelete
431 $affectedRows +
= $dbw->affectedRows();
433 $this->lbFactory
->commitAndWaitForReplication( __METHOD__
, $ticket );
438 return (bool)$affectedRows;
443 * @param LinkTarget[] $targets
444 * @param array $options
447 public function countWatchersMultiple( array $targets, array $options = [] ) {
448 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
450 $dbr = $this->getConnectionRef( DB_REPLICA
);
452 if ( array_key_exists( 'minimumWatchers', $options ) ) {
453 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
456 $lb = new LinkBatch( $targets );
459 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
460 [ $lb->constructSet( 'wl', $dbr ) ],
466 foreach ( $targets as $linkTarget ) {
467 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
470 foreach ( $res as $row ) {
471 $watchCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
479 * @param array $targetsWithVisitThresholds
480 * @param int|null $minimumWatchers
483 public function countVisitingWatchersMultiple(
484 array $targetsWithVisitThresholds,
485 $minimumWatchers = null
487 if ( $targetsWithVisitThresholds === [] ) {
488 // No titles requested => no results returned
492 $dbr = $this->getConnectionRef( DB_REPLICA
);
494 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
496 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
497 if ( $minimumWatchers !== null ) {
498 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
502 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
509 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
510 /* @var LinkTarget $target */
511 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
514 foreach ( $res as $row ) {
515 $watcherCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
518 return $watcherCounts;
522 * Generates condition for the query used in a batch count visiting watchers.
524 * @param IDatabase $db
525 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
528 private function getVisitingWatchersCondition(
530 array $targetsWithVisitThresholds
532 $missingTargets = [];
533 $namespaceConds = [];
534 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
535 if ( $threshold === null ) {
536 $missingTargets[] = $target;
539 /* @var LinkTarget $target */
540 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
541 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
543 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
544 'wl_notificationtimestamp IS NULL'
550 foreach ( $namespaceConds as $namespace => $pageConds ) {
551 $conds[] = $db->makeList( [
552 'wl_namespace = ' . $namespace,
553 '(' . $db->makeList( $pageConds, LIST_OR
) . ')'
557 if ( $missingTargets ) {
558 $lb = new LinkBatch( $missingTargets );
559 $conds[] = $lb->constructSet( 'wl', $db );
562 return $db->makeList( $conds, LIST_OR
);
567 * @param UserIdentity $user
568 * @param LinkTarget $target
571 public function getWatchedItem( UserIdentity
$user, LinkTarget
$target ) {
572 if ( !$user->isRegistered() ) {
576 $cached = $this->getCached( $user, $target );
578 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.cached' );
581 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.load' );
582 return $this->loadWatchedItem( $user, $target );
587 * @param UserIdentity $user
588 * @param LinkTarget $target
589 * @return WatchedItem|bool
591 public function loadWatchedItem( UserIdentity
$user, LinkTarget
$target ) {
592 // Only registered user can have a watchlist
593 if ( !$user->isRegistered() ) {
597 $dbr = $this->getConnectionRef( DB_REPLICA
);
599 $row = $dbr->selectRow(
601 'wl_notificationtimestamp',
602 $this->dbCond( $user, $target ),
610 $item = new WatchedItem(
613 $this->getLatestNotificationTimestamp( $row->wl_notificationtimestamp
, $user, $target )
615 $this->cache( $item );
622 * @param UserIdentity $user
623 * @param array $options
624 * @return WatchedItem[]
626 public function getWatchedItemsForUser( UserIdentity
$user, array $options = [] ) {
627 $options +
= [ 'forWrite' => false ];
630 if ( array_key_exists( 'sort', $options ) ) {
632 ( in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ) ),
633 '$options[\'sort\']',
634 'must be SORT_ASC or SORT_DESC'
636 $dbOptions['ORDER BY'] = [
637 "wl_namespace {$options['sort']}",
638 "wl_title {$options['sort']}"
641 $db = $this->getConnectionRef( $options['forWrite'] ? DB_MASTER
: DB_REPLICA
);
645 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
646 [ 'wl_user' => $user->getId() ],
652 foreach ( $res as $row ) {
653 $target = new TitleValue( (int)$row->wl_namespace
, $row->wl_title
);
654 // @todo: Should we add these to the process cache?
655 $watchedItems[] = new WatchedItem(
657 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
658 $this->getLatestNotificationTimestamp(
659 $row->wl_notificationtimestamp
, $user, $target )
663 return $watchedItems;
668 * @param UserIdentity $user
669 * @param LinkTarget $target
672 public function isWatched( UserIdentity
$user, LinkTarget
$target ) {
673 return (bool)$this->getWatchedItem( $user, $target );
678 * @param UserIdentity $user
679 * @param LinkTarget[] $targets
682 public function getNotificationTimestampsBatch( UserIdentity
$user, array $targets ) {
684 foreach ( $targets as $target ) {
685 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
688 if ( !$user->isRegistered() ) {
693 foreach ( $targets as $target ) {
694 $cachedItem = $this->getCached( $user, $target );
696 $timestamps[$target->getNamespace()][$target->getDBkey()] =
697 $cachedItem->getNotificationTimestamp();
699 $targetsToLoad[] = $target;
703 if ( !$targetsToLoad ) {
707 $dbr = $this->getConnectionRef( DB_REPLICA
);
709 $lb = new LinkBatch( $targetsToLoad );
712 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
714 $lb->constructSet( 'wl', $dbr ),
715 'wl_user' => $user->getId(),
720 foreach ( $res as $row ) {
721 $target = new TitleValue( (int)$row->wl_namespace
, $row->wl_title
);
722 $timestamps[$row->wl_namespace
][$row->wl_title
] =
723 $this->getLatestNotificationTimestamp(
724 $row->wl_notificationtimestamp
, $user, $target );
732 * @param UserIdentity $user
733 * @param LinkTarget $target
734 * @throws MWException
736 public function addWatch( UserIdentity
$user, LinkTarget
$target ) {
737 $this->addWatchBatchForUser( $user, [ $target ] );
742 * @param UserIdentity $user
743 * @param LinkTarget[] $targets
745 * @throws MWException
747 public function addWatchBatchForUser( UserIdentity
$user, array $targets ) {
748 if ( $this->readOnlyMode
->isReadOnly() ) {
751 // Only registered user can have a watchlist
752 if ( !$user->isRegistered() ) {
762 foreach ( $targets as $target ) {
764 'wl_user' => $user->getId(),
765 'wl_namespace' => $target->getNamespace(),
766 'wl_title' => $target->getDBkey(),
767 'wl_notificationtimestamp' => null,
769 $items[] = new WatchedItem(
774 $this->uncache( $user, $target );
777 $dbw = $this->getConnectionRef( DB_MASTER
);
778 $ticket = count( $targets ) > $this->updateRowsPerQuery ?
779 $this->lbFactory
->getEmptyTransactionTicket( __METHOD__
) : null;
781 $rowBatches = array_chunk( $rows, $this->updateRowsPerQuery
);
782 foreach ( $rowBatches as $toInsert ) {
783 // Use INSERT IGNORE to avoid overwriting the notification timestamp
784 // if there's already an entry for this page
785 $dbw->insert( 'watchlist', $toInsert, __METHOD__
, 'IGNORE' );
786 $affectedRows +
= $dbw->affectedRows();
788 $this->lbFactory
->commitAndWaitForReplication( __METHOD__
, $ticket );
791 // Update process cache to ensure skin doesn't claim that the current
792 // page is unwatched in the response of action=watch itself (T28292).
793 // This would otherwise be re-queried from a replica by isWatched().
794 foreach ( $items as $item ) {
795 $this->cache( $item );
798 return (bool)$affectedRows;
803 * @param UserIdentity $user
804 * @param LinkTarget $target
806 * @throws MWException
808 public function removeWatch( UserIdentity
$user, LinkTarget
$target ) {
809 return $this->removeWatchBatchForUser( $user, [ $target ] );
813 * Set the "last viewed" timestamps for certain titles on a user's watchlist.
815 * If the $targets parameter is omitted or set to [], this method simply wraps
816 * resetAllNotificationTimestampsForUser(), and in that case you should instead call that method
817 * directly; support for omitting $targets is for backwards compatibility.
819 * If $targets is omitted or set to [], timestamps will be updated for every title on the user's
820 * watchlist, and this will be done through a DeferredUpdate. If $targets is a non-empty array,
821 * only the specified titles will be updated, and this will be done immediately (not deferred).
824 * @param UserIdentity $user
825 * @param string|int $timestamp Value to set the "last viewed" timestamp to (null to clear)
826 * @param LinkTarget[] $targets Titles to set the timestamp for; [] means the entire watchlist
829 public function setNotificationTimestampsForUser(
830 UserIdentity
$user, $timestamp, array $targets = []
832 // Only registered user can have a watchlist
833 if ( !$user->isRegistered() ||
$this->readOnlyMode
->isReadOnly() ) {
838 // Backwards compatibility
839 $this->resetAllNotificationTimestampsForUser( $user, $timestamp );
843 $rows = $this->getTitleDbKeysGroupedByNamespace( $targets );
845 $dbw = $this->getConnectionRef( DB_MASTER
);
846 if ( $timestamp !== null ) {
847 $timestamp = $dbw->timestamp( $timestamp );
849 $ticket = $this->lbFactory
->getEmptyTransactionTicket( __METHOD__
);
850 $affectedSinceWait = 0;
852 // Batch update items per namespace
853 foreach ( $rows as $namespace => $namespaceTitles ) {
854 $rowBatches = array_chunk( $namespaceTitles, $this->updateRowsPerQuery
);
855 foreach ( $rowBatches as $toUpdate ) {
858 [ 'wl_notificationtimestamp' => $timestamp ],
860 'wl_user' => $user->getId(),
861 'wl_namespace' => $namespace,
862 'wl_title' => $toUpdate
865 $affectedSinceWait +
= $dbw->affectedRows();
866 // Wait for replication every time we've touched updateRowsPerQuery rows
867 if ( $affectedSinceWait >= $this->updateRowsPerQuery
) {
868 $this->lbFactory
->commitAndWaitForReplication( __METHOD__
, $ticket );
869 $affectedSinceWait = 0;
874 $this->uncacheUser( $user );
879 public function getLatestNotificationTimestamp(
880 $timestamp, UserIdentity
$user, LinkTarget
$target
882 $timestamp = wfTimestampOrNull( TS_MW
, $timestamp );
883 if ( $timestamp === null ) {
884 return null; // no notification
887 $seenTimestamps = $this->getPageSeenTimestamps( $user );
890 $seenTimestamps->get( $this->getPageSeenKey( $target ) ) >= $timestamp
892 // If a reset job did not yet run, then the "seen" timestamp will be higher
900 * Schedule a DeferredUpdate that sets all of the "last viewed" timestamps for a given user
902 * @param UserIdentity $user
903 * @param string|int|null $timestamp Value to set all timestamps to, null to clear them
905 public function resetAllNotificationTimestampsForUser( UserIdentity
$user, $timestamp = null ) {
906 // Only registered user can have a watchlist
907 if ( !$user->isRegistered() ) {
911 // If the page is watched by the user (or may be watched), update the timestamp
912 $job = new ClearWatchlistNotificationsJob( [
913 'userId' => $user->getId(), 'timestamp' => $timestamp, 'casTime' => time()
916 // Try to run this post-send
917 // Calls DeferredUpdates::addCallableUpdate in normal operation
919 $this->deferredUpdatesAddCallableUpdateCallback
,
920 function () use ( $job ) {
928 * @param UserIdentity $editor
929 * @param LinkTarget $target
930 * @param string|int $timestamp
933 public function updateNotificationTimestamp(
934 UserIdentity
$editor, LinkTarget
$target, $timestamp
936 $dbw = $this->getConnectionRef( DB_MASTER
);
937 $uids = $dbw->selectFieldValues(
941 'wl_user != ' . intval( $editor->getId() ),
942 'wl_namespace' => $target->getNamespace(),
943 'wl_title' => $target->getDBkey(),
944 'wl_notificationtimestamp IS NULL',
949 $watchers = array_map( 'intval', $uids );
951 // Update wl_notificationtimestamp for all watching users except the editor
953 DeferredUpdates
::addCallableUpdate(
954 function () use ( $timestamp, $watchers, $target, $fname ) {
955 $dbw = $this->getConnectionRef( DB_MASTER
);
956 $ticket = $this->lbFactory
->getEmptyTransactionTicket( $fname );
958 $watchersChunks = array_chunk( $watchers, $this->updateRowsPerQuery
);
959 foreach ( $watchersChunks as $watchersChunk ) {
960 $dbw->update( 'watchlist',
962 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
963 ], [ /* WHERE - TODO Use wl_id T130067 */
964 'wl_user' => $watchersChunk,
965 'wl_namespace' => $target->getNamespace(),
966 'wl_title' => $target->getDBkey(),
969 if ( count( $watchersChunks ) > 1 ) {
970 $this->lbFactory
->commitAndWaitForReplication(
971 $fname, $ticket, [ 'domain' => $dbw->getDomainID() ]
975 $this->uncacheLinkTarget( $target );
977 DeferredUpdates
::POSTSEND
,
987 * @param UserIdentity $user
988 * @param Title $title
989 * @param string $force
993 public function resetNotificationTimestamp(
994 UserIdentity
$user, Title
$title, $force = '', $oldid = 0
998 // Only registered user can have a watchlist
999 if ( $this->readOnlyMode
->isReadOnly() ||
!$user->isRegistered() ) {
1003 // Hook expects User, not UserIdentity
1004 $userObj = User
::newFromId( $user->getId() );
1005 if ( !Hooks
::run( 'BeforeResetNotificationTimestamp',
1006 [ &$userObj, &$title, $force, &$oldid ] )
1010 if ( !$userObj->equals( $user ) ) {
1015 if ( $force != 'force' ) {
1016 $item = $this->loadWatchedItem( $user, $title );
1017 if ( !$item ||
$item->getNotificationTimestamp() === null ) {
1022 // Get the timestamp (TS_MW) of this revision to track the latest one seen
1023 $seenTime = call_user_func(
1024 $this->revisionGetTimestampFromIdCallback
,
1026 $oldid ?
: $title->getLatestRevID()
1029 // Mark the item as read immediately in lightweight storage
1030 $this->stash
->merge(
1031 $this->getPageSeenTimestampsKey( $user ),
1032 function ( $cache, $key, $current ) use ( $title, $seenTime ) {
1033 $value = $current ?
: new MapCacheLRU( 300 );
1034 $subKey = $this->getPageSeenKey( $title );
1036 if ( $seenTime > $value->get( $subKey ) ) {
1037 // Revision is newer than the last one seen
1038 $value->set( $subKey, $seenTime );
1039 $this->latestUpdateCache
->set( $key, $value, IExpiringStore
::TTL_PROC_LONG
);
1040 } elseif ( $seenTime === false ) {
1041 // Revision does not exist
1042 $value->set( $subKey, wfTimestamp( TS_MW
) );
1043 $this->latestUpdateCache
->set( $key, $value, IExpiringStore
::TTL_PROC_LONG
);
1045 return false; // nothing to update
1050 IExpiringStore
::TTL_HOUR
1053 // If the page is watched by the user (or may be watched), update the timestamp
1054 $job = new ActivityUpdateJob(
1057 'type' => 'updateWatchlistNotification',
1058 'userid' => $user->getId(),
1059 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
1063 // Try to enqueue this post-send
1064 $this->queueGroup
->lazyPush( $job );
1066 $this->uncache( $user, $title );
1072 * @param UserIdentity $user
1073 * @return MapCacheLRU|null The map contains prefixed title keys and TS_MW values
1075 private function getPageSeenTimestamps( UserIdentity
$user ) {
1076 $key = $this->getPageSeenTimestampsKey( $user );
1078 return $this->latestUpdateCache
->getWithSetCallback(
1080 IExpiringStore
::TTL_PROC_LONG
,
1081 function () use ( $key ) {
1082 return $this->stash
->get( $key ) ?
: null;
1088 * @param UserIdentity $user
1091 private function getPageSeenTimestampsKey( UserIdentity
$user ) {
1092 return $this->stash
->makeGlobalKey(
1093 'watchlist-recent-updates',
1094 $this->lbFactory
->getLocalDomainID(),
1100 * @param LinkTarget $target
1103 private function getPageSeenKey( LinkTarget
$target ) {
1104 return "{$target->getNamespace()}:{$target->getDBkey()}";
1107 private function getNotificationTimestamp(
1108 UserIdentity
$user, Title
$title, $item, $force, $oldid
1111 // No oldid given, assuming latest revision; clear the timestamp.
1115 if ( !$title->getNextRevisionID( $oldid ) ) {
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 // Calls Revision::getTimestampFromId in normal operation
1132 $notificationTimestamp = call_user_func(
1133 $this->revisionGetTimestampFromIdCallback
,
1138 // We need to go one second to the future because of various strict comparisons
1139 // throughout the codebase
1140 $ts = new MWTimestamp( $notificationTimestamp );
1141 $ts->timestamp
->add( new DateInterval( 'PT1S' ) );
1142 $notificationTimestamp = $ts->getTimestamp( TS_MW
);
1144 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
1145 if ( $force != 'force' ) {
1148 // This is a little silly…
1149 return $item->getNotificationTimestamp();
1153 return $notificationTimestamp;
1158 * @param UserIdentity $user
1159 * @param int|null $unreadLimit
1162 public function countUnreadNotifications( UserIdentity
$user, $unreadLimit = null ) {
1163 $dbr = $this->getConnectionRef( DB_REPLICA
);
1166 if ( $unreadLimit !== null ) {
1167 $unreadLimit = (int)$unreadLimit;
1168 $queryOptions['LIMIT'] = $unreadLimit;
1172 'wl_user' => $user->getId(),
1173 'wl_notificationtimestamp IS NOT NULL'
1176 $rowCount = $dbr->selectRowCount( 'watchlist', '1', $conds, __METHOD__
, $queryOptions );
1178 if ( $unreadLimit === null ) {
1182 if ( $rowCount >= $unreadLimit ) {
1191 * @param LinkTarget $oldTarget
1192 * @param LinkTarget $newTarget
1194 public function duplicateAllAssociatedEntries( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
1195 $oldTarget = Title
::newFromLinkTarget( $oldTarget );
1196 $newTarget = Title
::newFromLinkTarget( $newTarget );
1198 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
1199 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
1204 * @param LinkTarget $oldTarget
1205 * @param LinkTarget $newTarget
1207 public function duplicateEntry( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
1208 $dbw = $this->getConnectionRef( DB_MASTER
);
1210 $result = $dbw->select(
1212 [ 'wl_user', 'wl_notificationtimestamp' ],
1214 'wl_namespace' => $oldTarget->getNamespace(),
1215 'wl_title' => $oldTarget->getDBkey(),
1221 $newNamespace = $newTarget->getNamespace();
1222 $newDBkey = $newTarget->getDBkey();
1224 # Construct array to replace into the watchlist
1226 foreach ( $result as $row ) {
1228 'wl_user' => $row->wl_user
,
1229 'wl_namespace' => $newNamespace,
1230 'wl_title' => $newDBkey,
1231 'wl_notificationtimestamp' => $row->wl_notificationtimestamp
,
1235 if ( !empty( $values ) ) {
1237 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
1238 # some other DBMSes, mostly due to poor simulation by us
1241 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
1249 * @param LinkTarget[] $titles
1252 private function getTitleDbKeysGroupedByNamespace( array $titles ) {
1254 foreach ( $titles as $title ) {
1255 // Group titles by namespace.
1256 $rows[ $title->getNamespace() ][] = $title->getDBkey();
1262 * @param UserIdentity $user
1263 * @param Title[] $titles
1265 private function uncacheTitlesForUser( UserIdentity
$user, array $titles ) {
1266 foreach ( $titles as $title ) {
1267 $this->uncache( $user, $title );