3 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
4 use MediaWiki\MediaWikiServices
;
5 use MediaWiki\Linker\LinkTarget
;
6 use Wikimedia\Assert\Assert
;
9 * Storage layer class for WatchedItems.
10 * Database interaction.
16 class WatchedItemStore
implements StatsdAwareInterface
{
18 const SORT_DESC
= 'DESC';
19 const SORT_ASC
= 'ASC';
24 private $loadBalancer;
32 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
33 * The index is needed so that on mass changes all relevant items can be un-cached.
34 * For example: Clearing a users watchlist of all items or updating notification timestamps
35 * for all users watching a single target.
37 private $cacheIndex = [];
42 private $deferredUpdatesAddCallableUpdateCallback;
47 private $revisionGetTimestampFromIdCallback;
50 * @var StatsdDataFactoryInterface
55 * @param LoadBalancer $loadBalancer
56 * @param HashBagOStuff $cache
58 public function __construct(
59 LoadBalancer
$loadBalancer,
62 $this->loadBalancer
= $loadBalancer;
63 $this->cache
= $cache;
64 $this->stats
= new NullStatsdDataFactory();
65 $this->deferredUpdatesAddCallableUpdateCallback
= [ 'DeferredUpdates', 'addCallableUpdate' ];
66 $this->revisionGetTimestampFromIdCallback
= [ 'Revision', 'getTimestampFromId' ];
69 public function setStatsdDataFactory( StatsdDataFactoryInterface
$stats ) {
70 $this->stats
= $stats;
74 * Overrides the DeferredUpdates::addCallableUpdate callback
75 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
77 * @param callable $callback
79 * @see DeferredUpdates::addCallableUpdate for callback signiture
81 * @return ScopedCallback to reset the overridden value
84 public function overrideDeferredUpdatesAddCallableUpdateCallback( $callback ) {
85 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
86 throw new MWException(
87 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
90 Assert
::parameterType( 'callable', $callback, '$callback' );
92 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback
;
93 $this->deferredUpdatesAddCallableUpdateCallback
= $callback;
94 return new ScopedCallback( function() use ( $previousValue ) {
95 $this->deferredUpdatesAddCallableUpdateCallback
= $previousValue;
100 * Overrides the Revision::getTimestampFromId callback
101 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
103 * @param callable $callback
104 * @see Revision::getTimestampFromId for callback signiture
106 * @return ScopedCallback to reset the overridden value
107 * @throws MWException
109 public function overrideRevisionGetTimestampFromIdCallback( $callback ) {
110 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
111 throw new MWException(
112 'Cannot override Revision::getTimestampFromId callback in operation.'
115 Assert
::parameterType( 'callable', $callback, '$callback' );
117 $previousValue = $this->revisionGetTimestampFromIdCallback
;
118 $this->revisionGetTimestampFromIdCallback
= $callback;
119 return new ScopedCallback( function() use ( $previousValue ) {
120 $this->revisionGetTimestampFromIdCallback
= $previousValue;
125 * @deprecated use MediaWikiServices::getInstance()->getWatchedItemStore()
128 public static function getDefaultInstance() {
129 return MediaWikiServices
::getInstance()->getWatchedItemStore();
132 private function getCacheKey( User
$user, LinkTarget
$target ) {
133 return $this->cache
->makeKey(
134 (string)$target->getNamespace(),
136 (string)$user->getId()
140 private function cache( WatchedItem
$item ) {
141 $user = $item->getUser();
142 $target = $item->getLinkTarget();
143 $key = $this->getCacheKey( $user, $target );
144 $this->cache
->set( $key, $item );
145 $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
146 $this->stats
->increment( 'WatchedItemStore.cache' );
149 private function uncache( User
$user, LinkTarget
$target ) {
150 $this->cache
->delete( $this->getCacheKey( $user, $target ) );
151 unset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
152 $this->stats
->increment( 'WatchedItemStore.uncache' );
155 private function uncacheLinkTarget( LinkTarget
$target ) {
156 if ( !isset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] ) ) {
159 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget' );
160 foreach ( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] as $key ) {
161 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
162 $this->cache
->delete( $key );
168 * @param LinkTarget $target
170 * @return WatchedItem|null
172 private function getCached( User
$user, LinkTarget
$target ) {
173 return $this->cache
->get( $this->getCacheKey( $user, $target ) );
177 * Return an array of conditions to select or update the appropriate database
181 * @param LinkTarget $target
185 private function dbCond( User
$user, LinkTarget
$target ) {
187 'wl_user' => $user->getId(),
188 'wl_namespace' => $target->getNamespace(),
189 'wl_title' => $target->getDBkey(),
194 * @param int $slaveOrMaster DB_MASTER or DB_SLAVE
196 * @return DatabaseBase
197 * @throws MWException
199 private function getConnection( $slaveOrMaster ) {
200 return $this->loadBalancer
->getConnection( $slaveOrMaster, [ 'watchlist' ] );
204 * @param DatabaseBase $connection
206 * @throws MWException
208 private function reuseConnection( $connection ) {
209 $this->loadBalancer
->reuseConnection( $connection );
213 * Count the number of individual items that are watched by the user.
214 * If a subject and corresponding talk page are watched this will return 2.
220 public function countWatchedItems( User
$user ) {
221 $dbr = $this->getConnection( DB_SLAVE
);
222 $return = (int)$dbr->selectField(
226 'wl_user' => $user->getId()
230 $this->reuseConnection( $dbr );
236 * @param LinkTarget $target
240 public function countWatchers( LinkTarget
$target ) {
241 $dbr = $this->getConnection( DB_SLAVE
);
242 $return = (int)$dbr->selectField(
246 'wl_namespace' => $target->getNamespace(),
247 'wl_title' => $target->getDBkey(),
251 $this->reuseConnection( $dbr );
257 * Number of page watchers who also visited a "recent" edit
259 * @param LinkTarget $target
260 * @param mixed $threshold timestamp accepted by wfTimestamp
263 * @throws DBUnexpectedError
264 * @throws MWException
266 public function countVisitingWatchers( LinkTarget
$target, $threshold ) {
267 $dbr = $this->getConnection( DB_SLAVE
);
268 $visitingWatchers = (int)$dbr->selectField(
272 'wl_namespace' => $target->getNamespace(),
273 'wl_title' => $target->getDBkey(),
274 'wl_notificationtimestamp >= ' .
275 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
276 ' OR wl_notificationtimestamp IS NULL'
280 $this->reuseConnection( $dbr );
282 return $visitingWatchers;
286 * @param LinkTarget[] $targets
287 * @param array $options Allowed keys:
288 * 'minimumWatchers' => int
290 * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers
291 * All targets will be present in the result. 0 either means no watchers or the number
292 * of watchers was below the minimumWatchers option if passed.
294 public function countWatchersMultiple( array $targets, array $options = [] ) {
295 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
297 $dbr = $this->getConnection( DB_SLAVE
);
299 if ( array_key_exists( 'minimumWatchers', $options ) ) {
300 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
303 $lb = new LinkBatch( $targets );
306 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
307 [ $lb->constructSet( 'wl', $dbr ) ],
312 $this->reuseConnection( $dbr );
315 foreach ( $targets as $linkTarget ) {
316 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
319 foreach ( $res as $row ) {
320 $watchCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
327 * Number of watchers of each page who have visited recent edits to that page
329 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed $threshold),
331 * - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp)
332 * - null if $target doesn't exist
333 * @param int|null $minimumWatchers
334 * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers,
335 * where $watchers is an int:
336 * - if the page exists, number of users watching who have visited the page recently
337 * - if the page doesn't exist, number of users that have the page on their watchlist
338 * - 0 means there are no visiting watchers or their number is below the minimumWatchers
339 * option (if passed).
341 public function countVisitingWatchersMultiple(
342 array $targetsWithVisitThresholds,
343 $minimumWatchers = null
345 $dbr = $this->getConnection( DB_SLAVE
);
347 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
349 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
350 if ( $minimumWatchers !== null ) {
351 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
355 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
361 $this->reuseConnection( $dbr );
364 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
365 /* @var LinkTarget $target */
366 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
369 foreach ( $res as $row ) {
370 $watcherCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
373 return $watcherCounts;
377 * Generates condition for the query used in a batch count visiting watchers.
379 * @param IDatabase $db
380 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
383 private function getVisitingWatchersCondition(
385 array $targetsWithVisitThresholds
387 $missingTargets = [];
388 $namespaceConds = [];
389 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
390 if ( $threshold === null ) {
391 $missingTargets[] = $target;
394 /* @var LinkTarget $target */
395 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
396 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
398 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
399 'wl_notificationtimestamp IS NULL'
405 foreach ( $namespaceConds as $namespace => $pageConds ) {
406 $conds[] = $db->makeList( [
407 'wl_namespace = ' . $namespace,
408 '(' . $db->makeList( $pageConds, LIST_OR
) . ')'
412 if ( $missingTargets ) {
413 $lb = new LinkBatch( $missingTargets );
414 $conds[] = $lb->constructSet( 'wl', $db );
417 return $db->makeList( $conds, LIST_OR
);
421 * Get an item (may be cached)
424 * @param LinkTarget $target
426 * @return WatchedItem|false
428 public function getWatchedItem( User
$user, LinkTarget
$target ) {
429 if ( $user->isAnon() ) {
433 $cached = $this->getCached( $user, $target );
435 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.cached' );
438 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.load' );
439 return $this->loadWatchedItem( $user, $target );
443 * Loads an item from the db
446 * @param LinkTarget $target
448 * @return WatchedItem|false
450 public function loadWatchedItem( User
$user, LinkTarget
$target ) {
451 // Only loggedin user can have a watchlist
452 if ( $user->isAnon() ) {
456 $dbr = $this->getConnection( DB_SLAVE
);
457 $row = $dbr->selectRow(
459 'wl_notificationtimestamp',
460 $this->dbCond( $user, $target ),
463 $this->reuseConnection( $dbr );
469 $item = new WatchedItem(
472 $row->wl_notificationtimestamp
474 $this->cache( $item );
481 * @param array $options Allowed keys:
482 * 'forWrite' => bool defaults to false
483 * 'sort' => string optional sorting by namespace ID and title
484 * one of the self::SORT_* constants
486 * @return WatchedItem[]
488 public function getWatchedItemsForUser( User
$user, array $options = [] ) {
489 $options +
= [ 'forWrite' => false ];
492 if ( array_key_exists( 'sort', $options ) ) {
494 ( in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ) ),
495 '$options[\'sort\']',
496 'must be SORT_ASC or SORT_DESC'
498 $dbOptions['ORDER BY'] = [
499 "wl_namespace {$options['sort']}",
500 "wl_title {$options['sort']}"
503 $db = $this->getConnection( $options['forWrite'] ? DB_MASTER
: DB_SLAVE
);
507 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
508 [ 'wl_user' => $user->getId() ],
512 $this->reuseConnection( $db );
515 foreach ( $res as $row ) {
516 // todo these could all be cached at some point?
517 $watchedItems[] = new WatchedItem(
519 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
520 $row->wl_notificationtimestamp
524 return $watchedItems;
528 * Must be called separately for Subject & Talk namespaces
531 * @param LinkTarget $target
535 public function isWatched( User
$user, LinkTarget
$target ) {
536 return (bool)$this->getWatchedItem( $user, $target );
541 * @param LinkTarget[] $targets
543 * @return array multi-dimensional like $return[$namespaceId][$titleString] = $timestamp,
544 * where $timestamp is:
545 * - string|null value of wl_notificationtimestamp,
546 * - false if $target is not watched by $user.
548 public function getNotificationTimestampsBatch( User
$user, array $targets ) {
550 foreach ( $targets as $target ) {
551 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
554 if ( $user->isAnon() ) {
559 foreach ( $targets as $target ) {
560 $cachedItem = $this->getCached( $user, $target );
562 $timestamps[$target->getNamespace()][$target->getDBkey()] =
563 $cachedItem->getNotificationTimestamp();
565 $targetsToLoad[] = $target;
569 if ( !$targetsToLoad ) {
573 $dbr = $this->getConnection( DB_SLAVE
);
575 $lb = new LinkBatch( $targetsToLoad );
578 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
580 $lb->constructSet( 'wl', $dbr ),
581 'wl_user' => $user->getId(),
585 $this->reuseConnection( $dbr );
587 foreach ( $res as $row ) {
588 $timestamps[$row->wl_namespace
][$row->wl_title
] = $row->wl_notificationtimestamp
;
595 * Must be called separately for Subject & Talk namespaces
598 * @param LinkTarget $target
600 public function addWatch( User
$user, LinkTarget
$target ) {
601 $this->addWatchBatchForUser( $user, [ $target ] );
606 * @param LinkTarget[] $targets
608 * @return bool success
610 public function addWatchBatchForUser( User
$user, array $targets ) {
611 if ( $this->loadBalancer
->getReadOnlyReason() !== false ) {
614 // Only loggedin user can have a watchlist
615 if ( $user->isAnon() ) {
624 foreach ( $targets as $target ) {
626 'wl_user' => $user->getId(),
627 'wl_namespace' => $target->getNamespace(),
628 'wl_title' => $target->getDBkey(),
629 'wl_notificationtimestamp' => null,
631 $this->uncache( $user, $target );
634 $dbw = $this->getConnection( DB_MASTER
);
635 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
636 // Use INSERT IGNORE to avoid overwriting the notification timestamp
637 // if there's already an entry for this page
638 $dbw->insert( 'watchlist', $toInsert, __METHOD__
, 'IGNORE' );
640 $this->reuseConnection( $dbw );
646 * Removes the an entry for the User watching the LinkTarget
647 * Must be called separately for Subject & Talk namespaces
650 * @param LinkTarget $target
652 * @return bool success
653 * @throws DBUnexpectedError
654 * @throws MWException
656 public function removeWatch( User
$user, LinkTarget
$target ) {
657 // Only logged in user can have a watchlist
658 if ( $this->loadBalancer
->getReadOnlyReason() !== false ||
$user->isAnon() ) {
662 $this->uncache( $user, $target );
664 $dbw = $this->getConnection( DB_MASTER
);
665 $dbw->delete( 'watchlist',
667 'wl_user' => $user->getId(),
668 'wl_namespace' => $target->getNamespace(),
669 'wl_title' => $target->getDBkey(),
672 $success = (bool)$dbw->affectedRows();
673 $this->reuseConnection( $dbw );
679 * @param User $editor The editor that triggered the update. Their notification
680 * timestamp will not be updated(they have already seen it)
681 * @param LinkTarget $target The target to update timestamps for
682 * @param string $timestamp Set the update timestamp to this value
684 * @return int[] Array of user IDs the timestamp has been updated for
686 public function updateNotificationTimestamp( User
$editor, LinkTarget
$target, $timestamp ) {
687 $dbw = $this->getConnection( DB_MASTER
);
688 $res = $dbw->select( [ 'watchlist' ],
691 'wl_user != ' . intval( $editor->getId() ),
692 'wl_namespace' => $target->getNamespace(),
693 'wl_title' => $target->getDBkey(),
694 'wl_notificationtimestamp IS NULL',
699 foreach ( $res as $row ) {
700 $watchers[] = intval( $row->wl_user
);
704 // Update wl_notificationtimestamp for all watching users except the editor
706 $dbw->onTransactionIdle(
707 function () use ( $dbw, $timestamp, $watchers, $target, $fname ) {
708 $dbw->update( 'watchlist',
710 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
712 'wl_user' => $watchers,
713 'wl_namespace' => $target->getNamespace(),
714 'wl_title' => $target->getDBkey(),
717 $this->uncacheLinkTarget( $target );
722 $this->reuseConnection( $dbw );
728 * Reset the notification timestamp of this entry
731 * @param Title $title
732 * @param string $force Whether to force the write query to be executed even if the
733 * page is not watched or the notification timestamp is already NULL.
734 * 'force' in order to force
735 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
737 * @return bool success
739 public function resetNotificationTimestamp( User
$user, Title
$title, $force = '', $oldid = 0 ) {
740 // Only loggedin user can have a watchlist
741 if ( $this->loadBalancer
->getReadOnlyReason() !== false ||
$user->isAnon() ) {
746 if ( $force != 'force' ) {
747 $item = $this->loadWatchedItem( $user, $title );
748 if ( !$item ||
$item->getNotificationTimestamp() === null ) {
753 // If the page is watched by the user (or may be watched), update the timestamp
754 $job = new ActivityUpdateJob(
757 'type' => 'updateWatchlistNotification',
758 'userid' => $user->getId(),
759 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
764 // Try to run this post-send
765 // Calls DeferredUpdates::addCallableUpdate in normal operation
767 $this->deferredUpdatesAddCallableUpdateCallback
,
768 function() use ( $job ) {
773 $this->uncache( $user, $title );
778 private function getNotificationTimestamp( User
$user, Title
$title, $item, $force, $oldid ) {
780 // No oldid given, assuming latest revision; clear the timestamp.
784 if ( !$title->getNextRevisionID( $oldid ) ) {
785 // Oldid given and is the latest revision for this title; clear the timestamp.
789 if ( $item === null ) {
790 $item = $this->loadWatchedItem( $user, $title );
794 // This can only happen if $force is enabled.
798 // Oldid given and isn't the latest; update the timestamp.
799 // This will result in no further notification emails being sent!
800 // Calls Revision::getTimestampFromId in normal operation
801 $notificationTimestamp = call_user_func(
802 $this->revisionGetTimestampFromIdCallback
,
807 // We need to go one second to the future because of various strict comparisons
808 // throughout the codebase
809 $ts = new MWTimestamp( $notificationTimestamp );
810 $ts->timestamp
->add( new DateInterval( 'PT1S' ) );
811 $notificationTimestamp = $ts->getTimestamp( TS_MW
);
813 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
814 if ( $force != 'force' ) {
817 // This is a little silly…
818 return $item->getNotificationTimestamp();
822 return $notificationTimestamp;
827 * @param int $unreadLimit
829 * @return int|bool The number of unread notifications
830 * true if greater than or equal to $unreadLimit
832 public function countUnreadNotifications( User
$user, $unreadLimit = null ) {
834 if ( $unreadLimit !== null ) {
835 $unreadLimit = (int)$unreadLimit;
836 $queryOptions['LIMIT'] = $unreadLimit;
839 $dbr = $this->getConnection( DB_SLAVE
);
840 $rowCount = $dbr->selectRowCount(
844 'wl_user' => $user->getId(),
845 'wl_notificationtimestamp IS NOT NULL',
850 $this->reuseConnection( $dbr );
852 if ( !isset( $unreadLimit ) ) {
856 if ( $rowCount >= $unreadLimit ) {
864 * Check if the given title already is watched by the user, and if so
865 * add a watch for the new title.
867 * To be used for page renames and such.
869 * @param LinkTarget $oldTarget
870 * @param LinkTarget $newTarget
872 public function duplicateAllAssociatedEntries( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
873 $oldTarget = Title
::newFromLinkTarget( $oldTarget );
874 $newTarget = Title
::newFromLinkTarget( $newTarget );
876 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
877 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
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.
885 * This must be called separately for Subject and Talk pages
887 * @param LinkTarget $oldTarget
888 * @param LinkTarget $newTarget
890 public function duplicateEntry( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
891 $dbw = $this->getConnection( DB_MASTER
);
893 $result = $dbw->select(
895 [ 'wl_user', 'wl_notificationtimestamp' ],
897 'wl_namespace' => $oldTarget->getNamespace(),
898 'wl_title' => $oldTarget->getDBkey(),
904 $newNamespace = $newTarget->getNamespace();
905 $newDBkey = $newTarget->getDBkey();
907 # Construct array to replace into the watchlist
909 foreach ( $result as $row ) {
911 'wl_user' => $row->wl_user
,
912 'wl_namespace' => $newNamespace,
913 'wl_title' => $newDBkey,
914 'wl_notificationtimestamp' => $row->wl_notificationtimestamp
,
918 if ( !empty( $values ) ) {
920 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
921 # some other DBMSes, mostly due to poor simulation by us
924 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
930 $this->reuseConnection( $dbw );