3 use Wikimedia\Assert\Assert
;
6 * Storage layer class for WatchedItems.
7 * Database interaction.
13 class WatchedItemStore
{
18 private $loadBalancer;
26 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
27 * The index is needed so that on mass changes all relevant items can be un-cached.
28 * For example: Clearing a users watchlist of all items or updating notification timestamps
29 * for all users watching a single target.
31 private $cacheIndex = [];
36 private $deferredUpdatesAddCallableUpdateCallback;
41 private $revisionGetTimestampFromIdCallback;
46 private static $instance;
49 * @param LoadBalancer $loadBalancer
50 * @param HashBagOStuff $cache
52 public function __construct(
53 LoadBalancer
$loadBalancer,
56 $this->loadBalancer
= $loadBalancer;
57 $this->cache
= $cache;
58 $this->deferredUpdatesAddCallableUpdateCallback
= [ 'DeferredUpdates', 'addCallableUpdate' ];
59 $this->revisionGetTimestampFromIdCallback
= [ 'Revision', 'getTimestampFromId' ];
63 * Overrides the DeferredUpdates::addCallableUpdate callback
64 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
66 * @param callable $callback
68 * @see DeferredUpdates::addCallableUpdate for callback signiture
70 * @return ScopedCallback to reset the overridden value
73 public function overrideDeferredUpdatesAddCallableUpdateCallback( $callback ) {
74 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
75 throw new MWException(
76 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
79 Assert
::parameterType( 'callable', $callback, '$callback' );
81 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback
;
82 $this->deferredUpdatesAddCallableUpdateCallback
= $callback;
83 return new ScopedCallback( function() use ( $previousValue ) {
84 $this->deferredUpdatesAddCallableUpdateCallback
= $previousValue;
89 * Overrides the Revision::getTimestampFromId callback
90 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
92 * @param callable $callback
93 * @see Revision::getTimestampFromId for callback signiture
95 * @return ScopedCallback to reset the overridden value
98 public function overrideRevisionGetTimestampFromIdCallback( $callback ) {
99 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
100 throw new MWException(
101 'Cannot override Revision::getTimestampFromId callback in operation.'
104 Assert
::parameterType( 'callable', $callback, '$callback' );
106 $previousValue = $this->revisionGetTimestampFromIdCallback
;
107 $this->revisionGetTimestampFromIdCallback
= $callback;
108 return new ScopedCallback( function() use ( $previousValue ) {
109 $this->revisionGetTimestampFromIdCallback
= $previousValue;
114 * Overrides the default instance of this class
115 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
117 * If this method is used it MUST also be called with null after a test to ensure a new
118 * default instance is created next time getDefaultInstance is called.
120 * @param WatchedItemStore|null $store
122 * @return ScopedCallback to reset the overridden value
123 * @throws MWException
125 public static function overrideDefaultInstance( WatchedItemStore
$store = null ) {
126 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
127 throw new MWException(
128 'Cannot override ' . __CLASS__
. 'default instance in operation.'
132 $previousValue = self
::$instance;
133 self
::$instance = $store;
134 return new ScopedCallback( function() use ( $previousValue ) {
135 self
::$instance = $previousValue;
142 public static function getDefaultInstance() {
143 if ( !self
::$instance ) {
144 self
::$instance = new self(
146 new HashBagOStuff( [ 'maxKeys' => 100 ] )
149 return self
::$instance;
152 private function getCacheKey( User
$user, LinkTarget
$target ) {
153 return $this->cache
->makeKey(
154 (string)$target->getNamespace(),
156 (string)$user->getId()
160 private function cache( WatchedItem
$item ) {
161 $user = $item->getUser();
162 $target = $item->getLinkTarget();
163 $key = $this->getCacheKey( $user, $target );
164 $this->cache
->set( $key, $item );
165 $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
168 private function uncache( User
$user, LinkTarget
$target ) {
169 $this->cache
->delete( $this->getCacheKey( $user, $target ) );
170 unset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
173 private function uncacheLinkTarget( LinkTarget
$target ) {
174 if ( !isset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] ) ) {
177 foreach ( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] as $key ) {
178 $this->cache
->delete( $key );
184 * @param LinkTarget $target
186 * @return WatchedItem|null
188 private function getCached( User
$user, LinkTarget
$target ) {
189 return $this->cache
->get( $this->getCacheKey( $user, $target ) );
193 * Return an array of conditions to select or update the appropriate database
197 * @param LinkTarget $target
201 private function dbCond( User
$user, LinkTarget
$target ) {
203 'wl_user' => $user->getId(),
204 'wl_namespace' => $target->getNamespace(),
205 'wl_title' => $target->getDBkey(),
210 * @param int $slaveOrMaster DB_MASTER or DB_SLAVE
212 * @return DatabaseBase
213 * @throws MWException
215 private function getConnection( $slaveOrMaster ) {
216 return $this->loadBalancer
->getConnection( $slaveOrMaster, [ 'watchlist' ] );
220 * @param DatabaseBase $connection
222 * @throws MWException
224 private function reuseConnection( $connection ) {
225 $this->loadBalancer
->reuseConnection( $connection );
229 * Count the number of individual items that are watched by the user.
230 * If a subject and corresponding talk page are watched this will return 2.
236 public function countWatchedItems( User
$user ) {
237 $dbr = $this->getConnection( DB_SLAVE
);
238 $return = (int)$dbr->selectField(
242 'wl_user' => $user->getId()
246 $this->reuseConnection( $dbr );
252 * @param LinkTarget $target
256 public function countWatchers( LinkTarget
$target ) {
257 $dbr = $this->getConnection( DB_SLAVE
);
258 $return = (int)$dbr->selectField(
262 'wl_namespace' => $target->getNamespace(),
263 'wl_title' => $target->getDBkey(),
267 $this->reuseConnection( $dbr );
273 * Number of page watchers who also visited a "recent" edit
275 * @param LinkTarget $target
276 * @param mixed $threshold timestamp accepted by wfTimestamp
279 * @throws DBUnexpectedError
280 * @throws MWException
282 public function countVisitingWatchers( LinkTarget
$target, $threshold ) {
283 $dbr = $this->getConnection( DB_SLAVE
);
284 $visitingWatchers = (int)$dbr->selectField(
288 'wl_namespace' => $target->getNamespace(),
289 'wl_title' => $target->getDBkey(),
290 'wl_notificationtimestamp >= ' .
291 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
292 ' OR wl_notificationtimestamp IS NULL'
296 $this->reuseConnection( $dbr );
298 return $visitingWatchers;
302 * @param LinkTarget[] $targets
303 * @param array $options Allowed keys:
304 * 'minimumWatchers' => int
306 * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers
307 * All targets will be present in the result. 0 either means no watchers or the number
308 * of watchers was below the minimumWatchers option if passed.
310 public function countWatchersMultiple( array $targets, array $options = [] ) {
311 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
313 $dbr = $this->getConnection( DB_SLAVE
);
315 if ( array_key_exists( 'minimumWatchers', $options ) ) {
316 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
319 $lb = new LinkBatch( $targets );
322 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
323 [ $lb->constructSet( 'wl', $dbr ) ],
328 $this->reuseConnection( $dbr );
331 foreach ( $targets as $linkTarget ) {
332 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
335 foreach ( $res as $row ) {
336 $watchCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
343 * Number of watchers of each page who have visited recent edits to that page
345 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed $threshold),
347 * - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp)
348 * - null if $target doesn't exist
349 * @param int|null $minimumWatchers
350 * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers,
351 * where $watchers is an int:
352 * - if the page exists, number of users watching who have visited the page recently
353 * - if the page doesn't exist, number of users that have the page on their watchlist
354 * - 0 means there are no visiting watchers or their number is below the minimumWatchers
355 * option (if passed).
357 public function countVisitingWatchersMultiple(
358 array $targetsWithVisitThresholds,
359 $minimumWatchers = null
361 $dbr = $this->getConnection( DB_SLAVE
);
363 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
365 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
366 if ( $minimumWatchers !== null ) {
367 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
371 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
377 $this->reuseConnection( $dbr );
380 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
381 /* @var LinkTarget $target */
382 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
385 foreach ( $res as $row ) {
386 $watcherCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
389 return $watcherCounts;
393 * Generates condition for the query used in a batch count visiting watchers.
395 * @param IDatabase $db
396 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
399 private function getVisitingWatchersCondition(
401 array $targetsWithVisitThresholds
403 $missingTargets = [];
404 $namespaceConds = [];
405 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
406 if ( $threshold === null ) {
407 $missingTargets[] = $target;
410 /* @var LinkTarget $target */
411 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
412 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
414 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
415 'wl_notificationtimestamp IS NULL'
421 foreach ( $namespaceConds as $namespace => $pageConds ) {
422 $conds[] = $db->makeList( [
423 'wl_namespace = ' . $namespace,
424 '(' . $db->makeList( $pageConds, LIST_OR
) . ')'
428 if ( $missingTargets ) {
429 $lb = new LinkBatch( $missingTargets );
430 $conds[] = $lb->constructSet( 'wl', $db );
433 return $db->makeList( $conds, LIST_OR
);
437 * Get an item (may be cached)
440 * @param LinkTarget $target
442 * @return WatchedItem|false
444 public function getWatchedItem( User
$user, LinkTarget
$target ) {
445 if ( $user->isAnon() ) {
449 $cached = $this->getCached( $user, $target );
453 return $this->loadWatchedItem( $user, $target );
457 * Loads an item from the db
460 * @param LinkTarget $target
462 * @return WatchedItem|false
464 public function loadWatchedItem( User
$user, LinkTarget
$target ) {
465 // Only loggedin user can have a watchlist
466 if ( $user->isAnon() ) {
470 $dbr = $this->getConnection( DB_SLAVE
);
471 $row = $dbr->selectRow(
473 'wl_notificationtimestamp',
474 $this->dbCond( $user, $target ),
477 $this->reuseConnection( $dbr );
483 $item = new WatchedItem(
486 $row->wl_notificationtimestamp
488 $this->cache( $item );
494 * Must be called separately for Subject & Talk namespaces
497 * @param LinkTarget $target
501 public function isWatched( User
$user, LinkTarget
$target ) {
502 return (bool)$this->getWatchedItem( $user, $target );
506 * Must be called separately for Subject & Talk namespaces
509 * @param LinkTarget $target
511 public function addWatch( User
$user, LinkTarget
$target ) {
512 $this->addWatchBatch( [ [ $user, $target ] ] );
516 * @param array[] $userTargetCombinations array of arrays containing [0] => User [1] => LinkTarget
518 * @return bool success
520 public function addWatchBatch( array $userTargetCombinations ) {
521 if ( $this->loadBalancer
->getReadOnlyReason() !== false ) {
526 foreach ( $userTargetCombinations as list( $user, $target ) ) {
529 * @var LinkTarget $target
532 // Only loggedin user can have a watchlist
533 if ( $user->isAnon() ) {
537 'wl_user' => $user->getId(),
538 'wl_namespace' => $target->getNamespace(),
539 'wl_title' => $target->getDBkey(),
540 'wl_notificationtimestamp' => null,
542 $this->uncache( $user, $target );
549 $dbw = $this->getConnection( DB_MASTER
);
550 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
551 // Use INSERT IGNORE to avoid overwriting the notification timestamp
552 // if there's already an entry for this page
553 $dbw->insert( 'watchlist', $toInsert, __METHOD__
, 'IGNORE' );
555 $this->reuseConnection( $dbw );
561 * Removes the an entry for the User watching the LinkTarget
562 * Must be called separately for Subject & Talk namespaces
565 * @param LinkTarget $target
567 * @return bool success
568 * @throws DBUnexpectedError
569 * @throws MWException
571 public function removeWatch( User
$user, LinkTarget
$target ) {
572 // Only logged in user can have a watchlist
573 if ( $this->loadBalancer
->getReadOnlyReason() !== false ||
$user->isAnon() ) {
577 $this->uncache( $user, $target );
579 $dbw = $this->getConnection( DB_MASTER
);
580 $dbw->delete( 'watchlist',
582 'wl_user' => $user->getId(),
583 'wl_namespace' => $target->getNamespace(),
584 'wl_title' => $target->getDBkey(),
587 $success = (bool)$dbw->affectedRows();
588 $this->reuseConnection( $dbw );
594 * @param User $editor The editor that triggered the update. Their notification
595 * timestamp will not be updated(they have already seen it)
596 * @param LinkTarget $target The target to update timestamps for
597 * @param string $timestamp Set the update timestamp to this value
599 * @return int[] Array of user IDs the timestamp has been updated for
601 public function updateNotificationTimestamp( User
$editor, LinkTarget
$target, $timestamp ) {
602 $dbw = $this->getConnection( DB_MASTER
);
603 $res = $dbw->select( [ 'watchlist' ],
606 'wl_user != ' . intval( $editor->getId() ),
607 'wl_namespace' => $target->getNamespace(),
608 'wl_title' => $target->getDBkey(),
609 'wl_notificationtimestamp IS NULL',
614 foreach ( $res as $row ) {
615 $watchers[] = intval( $row->wl_user
);
619 // Update wl_notificationtimestamp for all watching users except the editor
621 $dbw->onTransactionIdle(
622 function () use ( $dbw, $timestamp, $watchers, $target, $fname ) {
623 $dbw->update( 'watchlist',
625 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
627 'wl_user' => $watchers,
628 'wl_namespace' => $target->getNamespace(),
629 'wl_title' => $target->getDBkey(),
632 $this->uncacheLinkTarget( $target );
637 $this->reuseConnection( $dbw );
643 * Reset the notification timestamp of this entry
646 * @param Title $title
647 * @param string $force Whether to force the write query to be executed even if the
648 * page is not watched or the notification timestamp is already NULL.
649 * 'force' in order to force
650 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
652 * @return bool success
654 public function resetNotificationTimestamp( User
$user, Title
$title, $force = '', $oldid = 0 ) {
655 // Only loggedin user can have a watchlist
656 if ( $this->loadBalancer
->getReadOnlyReason() !== false ||
$user->isAnon() ) {
661 if ( $force != 'force' ) {
662 $item = $this->loadWatchedItem( $user, $title );
663 if ( !$item ||
$item->getNotificationTimestamp() === null ) {
668 // If the page is watched by the user (or may be watched), update the timestamp
669 $job = new ActivityUpdateJob(
672 'type' => 'updateWatchlistNotification',
673 'userid' => $user->getId(),
674 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
679 // Try to run this post-send
680 // Calls DeferredUpdates::addCallableUpdate in normal operation
682 $this->deferredUpdatesAddCallableUpdateCallback
,
683 function() use ( $job ) {
688 $this->uncache( $user, $title );
693 private function getNotificationTimestamp( User
$user, Title
$title, $item, $force, $oldid ) {
695 // No oldid given, assuming latest revision; clear the timestamp.
699 if ( !$title->getNextRevisionID( $oldid ) ) {
700 // Oldid given and is the latest revision for this title; clear the timestamp.
704 if ( $item === null ) {
705 $item = $this->loadWatchedItem( $user, $title );
709 // This can only happen if $force is enabled.
713 // Oldid given and isn't the latest; update the timestamp.
714 // This will result in no further notification emails being sent!
715 // Calls Revision::getTimestampFromId in normal operation
716 $notificationTimestamp = call_user_func(
717 $this->revisionGetTimestampFromIdCallback
,
722 // We need to go one second to the future because of various strict comparisons
723 // throughout the codebase
724 $ts = new MWTimestamp( $notificationTimestamp );
725 $ts->timestamp
->add( new DateInterval( 'PT1S' ) );
726 $notificationTimestamp = $ts->getTimestamp( TS_MW
);
728 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
729 if ( $force != 'force' ) {
732 // This is a little silly…
733 return $item->getNotificationTimestamp();
737 return $notificationTimestamp;
742 * @param int $unreadLimit
744 * @return int|bool The number of unread notifications
745 * true if greater than or equal to $unreadLimit
747 public function countUnreadNotifications( User
$user, $unreadLimit = null ) {
749 if ( $unreadLimit !== null ) {
750 $unreadLimit = (int)$unreadLimit;
751 $queryOptions['LIMIT'] = $unreadLimit;
754 $dbr = $this->getConnection( DB_SLAVE
);
755 $rowCount = $dbr->selectRowCount(
759 'wl_user' => $user->getId(),
760 'wl_notificationtimestamp IS NOT NULL',
765 $this->reuseConnection( $dbr );
767 if ( !isset( $unreadLimit ) ) {
771 if ( $rowCount >= $unreadLimit ) {
779 * Check if the given title already is watched by the user, and if so
780 * add a watch for the new title.
782 * To be used for page renames and such.
784 * @param LinkTarget $oldTarget
785 * @param LinkTarget $newTarget
787 public function duplicateAllAssociatedEntries( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
788 if ( !$oldTarget instanceof Title
) {
789 $oldTarget = Title
::newFromLinkTarget( $oldTarget );
791 if ( !$newTarget instanceof Title
) {
792 $newTarget = Title
::newFromLinkTarget( $newTarget );
795 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
796 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
800 * Check if the given title already is watched by the user, and if so
801 * add a watch for the new title.
803 * To be used for page renames and such.
804 * This must be called separately for Subject and Talk pages
806 * @param LinkTarget $oldTarget
807 * @param LinkTarget $newTarget
809 public function duplicateEntry( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
810 $dbw = $this->getConnection( DB_MASTER
);
812 $result = $dbw->select(
814 [ 'wl_user', 'wl_notificationtimestamp' ],
816 'wl_namespace' => $oldTarget->getNamespace(),
817 'wl_title' => $oldTarget->getDBkey(),
823 $newNamespace = $newTarget->getNamespace();
824 $newDBkey = $newTarget->getDBkey();
826 # Construct array to replace into the watchlist
828 foreach ( $result as $row ) {
830 'wl_user' => $row->wl_user
,
831 'wl_namespace' => $newNamespace,
832 'wl_title' => $newDBkey,
833 'wl_notificationtimestamp' => $row->wl_notificationtimestamp
,
837 if ( !empty( $values ) ) {
839 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
840 # some other DBMSes, mostly due to poor simulation by us
843 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
849 $this->reuseConnection( $dbw );