3 use Wikimedia\Assert\Assert
;
6 * Storage layer class for WatchedItems.
7 * Database interaction.
13 class WatchedItemStore
{
15 const SORT_DESC
= 'DESC';
16 const SORT_ASC
= 'ASC';
21 private $loadBalancer;
29 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
30 * The index is needed so that on mass changes all relevant items can be un-cached.
31 * For example: Clearing a users watchlist of all items or updating notification timestamps
32 * for all users watching a single target.
34 private $cacheIndex = [];
39 private $deferredUpdatesAddCallableUpdateCallback;
44 private $revisionGetTimestampFromIdCallback;
49 private static $instance;
52 * @param LoadBalancer $loadBalancer
53 * @param HashBagOStuff $cache
55 public function __construct(
56 LoadBalancer
$loadBalancer,
59 $this->loadBalancer
= $loadBalancer;
60 $this->cache
= $cache;
61 $this->deferredUpdatesAddCallableUpdateCallback
= [ 'DeferredUpdates', 'addCallableUpdate' ];
62 $this->revisionGetTimestampFromIdCallback
= [ 'Revision', 'getTimestampFromId' ];
66 * Overrides the DeferredUpdates::addCallableUpdate callback
67 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
69 * @param callable $callback
71 * @see DeferredUpdates::addCallableUpdate for callback signiture
73 * @return ScopedCallback to reset the overridden value
76 public function overrideDeferredUpdatesAddCallableUpdateCallback( $callback ) {
77 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
78 throw new MWException(
79 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
82 Assert
::parameterType( 'callable', $callback, '$callback' );
84 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback
;
85 $this->deferredUpdatesAddCallableUpdateCallback
= $callback;
86 return new ScopedCallback( function() use ( $previousValue ) {
87 $this->deferredUpdatesAddCallableUpdateCallback
= $previousValue;
92 * Overrides the Revision::getTimestampFromId callback
93 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
95 * @param callable $callback
96 * @see Revision::getTimestampFromId for callback signiture
98 * @return ScopedCallback to reset the overridden value
101 public function overrideRevisionGetTimestampFromIdCallback( $callback ) {
102 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
103 throw new MWException(
104 'Cannot override Revision::getTimestampFromId callback in operation.'
107 Assert
::parameterType( 'callable', $callback, '$callback' );
109 $previousValue = $this->revisionGetTimestampFromIdCallback
;
110 $this->revisionGetTimestampFromIdCallback
= $callback;
111 return new ScopedCallback( function() use ( $previousValue ) {
112 $this->revisionGetTimestampFromIdCallback
= $previousValue;
117 * Overrides the default instance of this class
118 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
120 * If this method is used it MUST also be called with null after a test to ensure a new
121 * default instance is created next time getDefaultInstance is called.
123 * @param WatchedItemStore|null $store
125 * @return ScopedCallback to reset the overridden value
126 * @throws MWException
128 public static function overrideDefaultInstance( WatchedItemStore
$store = null ) {
129 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
130 throw new MWException(
131 'Cannot override ' . __CLASS__
. 'default instance in operation.'
135 $previousValue = self
::$instance;
136 self
::$instance = $store;
137 return new ScopedCallback( function() use ( $previousValue ) {
138 self
::$instance = $previousValue;
145 public static function getDefaultInstance() {
146 if ( !self
::$instance ) {
147 self
::$instance = new self(
149 new HashBagOStuff( [ 'maxKeys' => 100 ] )
152 return self
::$instance;
155 private function getCacheKey( User
$user, LinkTarget
$target ) {
156 return $this->cache
->makeKey(
157 (string)$target->getNamespace(),
159 (string)$user->getId()
163 private function cache( WatchedItem
$item ) {
164 $user = $item->getUser();
165 $target = $item->getLinkTarget();
166 $key = $this->getCacheKey( $user, $target );
167 $this->cache
->set( $key, $item );
168 $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
171 private function uncache( User
$user, LinkTarget
$target ) {
172 $this->cache
->delete( $this->getCacheKey( $user, $target ) );
173 unset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
176 private function uncacheLinkTarget( LinkTarget
$target ) {
177 if ( !isset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] ) ) {
180 foreach ( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] as $key ) {
181 $this->cache
->delete( $key );
187 * @param LinkTarget $target
189 * @return WatchedItem|null
191 private function getCached( User
$user, LinkTarget
$target ) {
192 return $this->cache
->get( $this->getCacheKey( $user, $target ) );
196 * Return an array of conditions to select or update the appropriate database
200 * @param LinkTarget $target
204 private function dbCond( User
$user, LinkTarget
$target ) {
206 'wl_user' => $user->getId(),
207 'wl_namespace' => $target->getNamespace(),
208 'wl_title' => $target->getDBkey(),
213 * @param int $slaveOrMaster DB_MASTER or DB_SLAVE
215 * @return DatabaseBase
216 * @throws MWException
218 private function getConnection( $slaveOrMaster ) {
219 return $this->loadBalancer
->getConnection( $slaveOrMaster, [ 'watchlist' ] );
223 * @param DatabaseBase $connection
225 * @throws MWException
227 private function reuseConnection( $connection ) {
228 $this->loadBalancer
->reuseConnection( $connection );
232 * Count the number of individual items that are watched by the user.
233 * If a subject and corresponding talk page are watched this will return 2.
239 public function countWatchedItems( User
$user ) {
240 $dbr = $this->getConnection( DB_SLAVE
);
241 $return = (int)$dbr->selectField(
245 'wl_user' => $user->getId()
249 $this->reuseConnection( $dbr );
255 * @param LinkTarget $target
259 public function countWatchers( LinkTarget
$target ) {
260 $dbr = $this->getConnection( DB_SLAVE
);
261 $return = (int)$dbr->selectField(
265 'wl_namespace' => $target->getNamespace(),
266 'wl_title' => $target->getDBkey(),
270 $this->reuseConnection( $dbr );
276 * Number of page watchers who also visited a "recent" edit
278 * @param LinkTarget $target
279 * @param mixed $threshold timestamp accepted by wfTimestamp
282 * @throws DBUnexpectedError
283 * @throws MWException
285 public function countVisitingWatchers( LinkTarget
$target, $threshold ) {
286 $dbr = $this->getConnection( DB_SLAVE
);
287 $visitingWatchers = (int)$dbr->selectField(
291 'wl_namespace' => $target->getNamespace(),
292 'wl_title' => $target->getDBkey(),
293 'wl_notificationtimestamp >= ' .
294 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
295 ' OR wl_notificationtimestamp IS NULL'
299 $this->reuseConnection( $dbr );
301 return $visitingWatchers;
305 * @param LinkTarget[] $targets
306 * @param array $options Allowed keys:
307 * 'minimumWatchers' => int
309 * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers
310 * All targets will be present in the result. 0 either means no watchers or the number
311 * of watchers was below the minimumWatchers option if passed.
313 public function countWatchersMultiple( array $targets, array $options = [] ) {
314 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
316 $dbr = $this->getConnection( DB_SLAVE
);
318 if ( array_key_exists( 'minimumWatchers', $options ) ) {
319 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
322 $lb = new LinkBatch( $targets );
325 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
326 [ $lb->constructSet( 'wl', $dbr ) ],
331 $this->reuseConnection( $dbr );
334 foreach ( $targets as $linkTarget ) {
335 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
338 foreach ( $res as $row ) {
339 $watchCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
346 * Number of watchers of each page who have visited recent edits to that page
348 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed $threshold),
350 * - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp)
351 * - null if $target doesn't exist
352 * @param int|null $minimumWatchers
353 * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers,
354 * where $watchers is an int:
355 * - if the page exists, number of users watching who have visited the page recently
356 * - if the page doesn't exist, number of users that have the page on their watchlist
357 * - 0 means there are no visiting watchers or their number is below the minimumWatchers
358 * option (if passed).
360 public function countVisitingWatchersMultiple(
361 array $targetsWithVisitThresholds,
362 $minimumWatchers = null
364 $dbr = $this->getConnection( DB_SLAVE
);
366 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
368 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
369 if ( $minimumWatchers !== null ) {
370 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
374 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
380 $this->reuseConnection( $dbr );
383 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
384 /* @var LinkTarget $target */
385 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
388 foreach ( $res as $row ) {
389 $watcherCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
392 return $watcherCounts;
396 * Generates condition for the query used in a batch count visiting watchers.
398 * @param IDatabase $db
399 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
402 private function getVisitingWatchersCondition(
404 array $targetsWithVisitThresholds
406 $missingTargets = [];
407 $namespaceConds = [];
408 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
409 if ( $threshold === null ) {
410 $missingTargets[] = $target;
413 /* @var LinkTarget $target */
414 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
415 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
417 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
418 'wl_notificationtimestamp IS NULL'
424 foreach ( $namespaceConds as $namespace => $pageConds ) {
425 $conds[] = $db->makeList( [
426 'wl_namespace = ' . $namespace,
427 '(' . $db->makeList( $pageConds, LIST_OR
) . ')'
431 if ( $missingTargets ) {
432 $lb = new LinkBatch( $missingTargets );
433 $conds[] = $lb->constructSet( 'wl', $db );
436 return $db->makeList( $conds, LIST_OR
);
440 * Get an item (may be cached)
443 * @param LinkTarget $target
445 * @return WatchedItem|false
447 public function getWatchedItem( User
$user, LinkTarget
$target ) {
448 if ( $user->isAnon() ) {
452 $cached = $this->getCached( $user, $target );
456 return $this->loadWatchedItem( $user, $target );
460 * Loads an item from the db
463 * @param LinkTarget $target
465 * @return WatchedItem|false
467 public function loadWatchedItem( User
$user, LinkTarget
$target ) {
468 // Only loggedin user can have a watchlist
469 if ( $user->isAnon() ) {
473 $dbr = $this->getConnection( DB_SLAVE
);
474 $row = $dbr->selectRow(
476 'wl_notificationtimestamp',
477 $this->dbCond( $user, $target ),
480 $this->reuseConnection( $dbr );
486 $item = new WatchedItem(
489 $row->wl_notificationtimestamp
491 $this->cache( $item );
498 * @param array $options Allowed keys:
499 * 'forWrite' => bool defaults to false
500 * 'sort' => string optional sorting by namespace ID and title
501 * one of the self::SORT_* constants
503 * @return WatchedItem[]
505 public function getWatchedItemsForUser( User
$user, array $options = [] ) {
506 $options +
= [ 'forWrite' => false ];
509 if ( array_key_exists( 'sort', $options ) ) {
511 ( in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ) ),
512 '$options[\'sort\']',
513 'must be SORT_ASC or SORT_DESC'
515 $dbOptions['ORDER BY'] = [
516 "wl_namespace {$options['sort']}",
517 "wl_title {$options['sort']}"
520 $db = $this->getConnection( $options['forWrite'] ? DB_MASTER
: DB_SLAVE
);
524 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
525 [ 'wl_user' => $user->getId() ],
529 $this->reuseConnection( $db );
532 foreach ( $res as $row ) {
533 // todo these could all be cached at some point?
534 $watchedItems[] = new WatchedItem(
536 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
537 $row->wl_notificationtimestamp
541 return $watchedItems;
545 * Must be called separately for Subject & Talk namespaces
548 * @param LinkTarget $target
552 public function isWatched( User
$user, LinkTarget
$target ) {
553 return (bool)$this->getWatchedItem( $user, $target );
558 * @param LinkTarget[] $targets
560 * @return array multi-dimensional like $return[$namespaceId][$titleString] = $timestamp,
561 * where $timestamp is:
562 * - string|null value of wl_notificationtimestamp,
563 * - false if $target is not watched by $user.
565 public function getNotificationTimestampsBatch( User
$user, array $targets ) {
567 foreach ( $targets as $target ) {
568 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
571 if ( $user->isAnon() ) {
576 foreach ( $targets as $target ) {
577 $cachedItem = $this->getCached( $user, $target );
579 $timestamps[$target->getNamespace()][$target->getDBkey()] =
580 $cachedItem->getNotificationTimestamp();
582 $targetsToLoad[] = $target;
586 if ( !$targetsToLoad ) {
590 $dbr = $this->getConnection( DB_SLAVE
);
592 $lb = new LinkBatch( $targetsToLoad );
595 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
597 $lb->constructSet( 'wl', $dbr ),
598 'wl_user' => $user->getId(),
602 $this->reuseConnection( $dbr );
604 foreach ( $res as $row ) {
605 $timestamps[(int)$row->wl_namespace
][$row->wl_title
] = $row->wl_notificationtimestamp
;
612 * Must be called separately for Subject & Talk namespaces
615 * @param LinkTarget $target
617 public function addWatch( User
$user, LinkTarget
$target ) {
618 $this->addWatchBatchForUser( $user, [ $target ] );
623 * @param LinkTarget[] $targets
625 * @return bool success
627 public function addWatchBatchForUser( User
$user, array $targets ) {
628 if ( $this->loadBalancer
->getReadOnlyReason() !== false ) {
631 // Only loggedin user can have a watchlist
632 if ( $user->isAnon() ) {
641 foreach ( $targets as $target ) {
643 'wl_user' => $user->getId(),
644 'wl_namespace' => $target->getNamespace(),
645 'wl_title' => $target->getDBkey(),
646 'wl_notificationtimestamp' => null,
648 $this->uncache( $user, $target );
651 $dbw = $this->getConnection( DB_MASTER
);
652 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
653 // Use INSERT IGNORE to avoid overwriting the notification timestamp
654 // if there's already an entry for this page
655 $dbw->insert( 'watchlist', $toInsert, __METHOD__
, 'IGNORE' );
657 $this->reuseConnection( $dbw );
663 * Removes the an entry for the User watching the LinkTarget
664 * Must be called separately for Subject & Talk namespaces
667 * @param LinkTarget $target
669 * @return bool success
670 * @throws DBUnexpectedError
671 * @throws MWException
673 public function removeWatch( User
$user, LinkTarget
$target ) {
674 // Only logged in user can have a watchlist
675 if ( $this->loadBalancer
->getReadOnlyReason() !== false ||
$user->isAnon() ) {
679 $this->uncache( $user, $target );
681 $dbw = $this->getConnection( DB_MASTER
);
682 $dbw->delete( 'watchlist',
684 'wl_user' => $user->getId(),
685 'wl_namespace' => $target->getNamespace(),
686 'wl_title' => $target->getDBkey(),
689 $success = (bool)$dbw->affectedRows();
690 $this->reuseConnection( $dbw );
696 * @param User $editor The editor that triggered the update. Their notification
697 * timestamp will not be updated(they have already seen it)
698 * @param LinkTarget $target The target to update timestamps for
699 * @param string $timestamp Set the update timestamp to this value
701 * @return int[] Array of user IDs the timestamp has been updated for
703 public function updateNotificationTimestamp( User
$editor, LinkTarget
$target, $timestamp ) {
704 $dbw = $this->getConnection( DB_MASTER
);
705 $res = $dbw->select( [ 'watchlist' ],
708 'wl_user != ' . intval( $editor->getId() ),
709 'wl_namespace' => $target->getNamespace(),
710 'wl_title' => $target->getDBkey(),
711 'wl_notificationtimestamp IS NULL',
716 foreach ( $res as $row ) {
717 $watchers[] = intval( $row->wl_user
);
721 // Update wl_notificationtimestamp for all watching users except the editor
723 $dbw->onTransactionIdle(
724 function () use ( $dbw, $timestamp, $watchers, $target, $fname ) {
725 $dbw->update( 'watchlist',
727 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
729 'wl_user' => $watchers,
730 'wl_namespace' => $target->getNamespace(),
731 'wl_title' => $target->getDBkey(),
734 $this->uncacheLinkTarget( $target );
739 $this->reuseConnection( $dbw );
745 * Reset the notification timestamp of this entry
748 * @param Title $title
749 * @param string $force Whether to force the write query to be executed even if the
750 * page is not watched or the notification timestamp is already NULL.
751 * 'force' in order to force
752 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
754 * @return bool success
756 public function resetNotificationTimestamp( User
$user, Title
$title, $force = '', $oldid = 0 ) {
757 // Only loggedin user can have a watchlist
758 if ( $this->loadBalancer
->getReadOnlyReason() !== false ||
$user->isAnon() ) {
763 if ( $force != 'force' ) {
764 $item = $this->loadWatchedItem( $user, $title );
765 if ( !$item ||
$item->getNotificationTimestamp() === null ) {
770 // If the page is watched by the user (or may be watched), update the timestamp
771 $job = new ActivityUpdateJob(
774 'type' => 'updateWatchlistNotification',
775 'userid' => $user->getId(),
776 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
781 // Try to run this post-send
782 // Calls DeferredUpdates::addCallableUpdate in normal operation
784 $this->deferredUpdatesAddCallableUpdateCallback
,
785 function() use ( $job ) {
790 $this->uncache( $user, $title );
795 private function getNotificationTimestamp( User
$user, Title
$title, $item, $force, $oldid ) {
797 // No oldid given, assuming latest revision; clear the timestamp.
801 if ( !$title->getNextRevisionID( $oldid ) ) {
802 // Oldid given and is the latest revision for this title; clear the timestamp.
806 if ( $item === null ) {
807 $item = $this->loadWatchedItem( $user, $title );
811 // This can only happen if $force is enabled.
815 // Oldid given and isn't the latest; update the timestamp.
816 // This will result in no further notification emails being sent!
817 // Calls Revision::getTimestampFromId in normal operation
818 $notificationTimestamp = call_user_func(
819 $this->revisionGetTimestampFromIdCallback
,
824 // We need to go one second to the future because of various strict comparisons
825 // throughout the codebase
826 $ts = new MWTimestamp( $notificationTimestamp );
827 $ts->timestamp
->add( new DateInterval( 'PT1S' ) );
828 $notificationTimestamp = $ts->getTimestamp( TS_MW
);
830 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
831 if ( $force != 'force' ) {
834 // This is a little silly…
835 return $item->getNotificationTimestamp();
839 return $notificationTimestamp;
844 * @param int $unreadLimit
846 * @return int|bool The number of unread notifications
847 * true if greater than or equal to $unreadLimit
849 public function countUnreadNotifications( User
$user, $unreadLimit = null ) {
851 if ( $unreadLimit !== null ) {
852 $unreadLimit = (int)$unreadLimit;
853 $queryOptions['LIMIT'] = $unreadLimit;
856 $dbr = $this->getConnection( DB_SLAVE
);
857 $rowCount = $dbr->selectRowCount(
861 'wl_user' => $user->getId(),
862 'wl_notificationtimestamp IS NOT NULL',
867 $this->reuseConnection( $dbr );
869 if ( !isset( $unreadLimit ) ) {
873 if ( $rowCount >= $unreadLimit ) {
881 * Check if the given title already is watched by the user, and if so
882 * add a watch for the new title.
884 * To be used for page renames and such.
886 * @param LinkTarget $oldTarget
887 * @param LinkTarget $newTarget
889 public function duplicateAllAssociatedEntries( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
890 if ( !$oldTarget instanceof Title
) {
891 $oldTarget = Title
::newFromLinkTarget( $oldTarget );
893 if ( !$newTarget instanceof Title
) {
894 $newTarget = Title
::newFromLinkTarget( $newTarget );
897 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
898 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
902 * Check if the given title already is watched by the user, and if so
903 * add a watch for the new title.
905 * To be used for page renames and such.
906 * This must be called separately for Subject and Talk pages
908 * @param LinkTarget $oldTarget
909 * @param LinkTarget $newTarget
911 public function duplicateEntry( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
912 $dbw = $this->getConnection( DB_MASTER
);
914 $result = $dbw->select(
916 [ 'wl_user', 'wl_notificationtimestamp' ],
918 'wl_namespace' => $oldTarget->getNamespace(),
919 'wl_title' => $oldTarget->getDBkey(),
925 $newNamespace = $newTarget->getNamespace();
926 $newDBkey = $newTarget->getDBkey();
928 # Construct array to replace into the watchlist
930 foreach ( $result as $row ) {
932 'wl_user' => $row->wl_user
,
933 'wl_namespace' => $newNamespace,
934 'wl_title' => $newDBkey,
935 'wl_notificationtimestamp' => $row->wl_notificationtimestamp
,
939 if ( !empty( $values ) ) {
941 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
942 # some other DBMSes, mostly due to poor simulation by us
945 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
951 $this->reuseConnection( $dbw );