Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / watcheditem / WatchedItemStore.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
5 use MediaWiki\Linker\LinkTarget;
6 use Wikimedia\Assert\Assert;
7 use Wikimedia\ScopedCallback;
8 use Wikimedia\Rdbms\ILBFactory;
9 use Wikimedia\Rdbms\LoadBalancer;
10
11 /**
12 * Storage layer class for WatchedItems.
13 * Database interaction & caching
14 * TODO caching should be factored out into a CachingWatchedItemStore class
15 *
16 * @author Addshore
17 * @since 1.27
18 */
19 class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterface {
20
21 /**
22 * @var ILBFactory
23 */
24 private $lbFactory;
25
26 /**
27 * @var LoadBalancer
28 */
29 private $loadBalancer;
30
31 /**
32 * @var JobQueueGroup
33 */
34 private $queueGroup;
35
36 /**
37 * @var BagOStuff
38 */
39 private $stash;
40
41 /**
42 * @var ReadOnlyMode
43 */
44 private $readOnlyMode;
45
46 /**
47 * @var HashBagOStuff
48 */
49 private $cache;
50
51 /**
52 * @var HashBagOStuff
53 */
54 private $latestUpdateCache;
55
56 /**
57 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
58 * The index is needed so that on mass changes all relevant items can be un-cached.
59 * For example: Clearing a users watchlist of all items or updating notification timestamps
60 * for all users watching a single target.
61 */
62 private $cacheIndex = [];
63
64 /**
65 * @var callable|null
66 */
67 private $deferredUpdatesAddCallableUpdateCallback;
68
69 /**
70 * @var callable|null
71 */
72 private $revisionGetTimestampFromIdCallback;
73
74 /**
75 * @var int
76 */
77 private $updateRowsPerQuery;
78
79 /**
80 * @var StatsdDataFactoryInterface
81 */
82 private $stats;
83
84 /**
85 * @param ILBFactory $lbFactory
86 * @param JobQueueGroup $queueGroup
87 * @param BagOStuff $stash
88 * @param HashBagOStuff $cache
89 * @param ReadOnlyMode $readOnlyMode
90 * @param int $updateRowsPerQuery
91 */
92 public function __construct(
93 ILBFactory $lbFactory,
94 JobQueueGroup $queueGroup,
95 BagOStuff $stash,
96 HashBagOStuff $cache,
97 ReadOnlyMode $readOnlyMode,
98 $updateRowsPerQuery
99 ) {
100 $this->lbFactory = $lbFactory;
101 $this->loadBalancer = $lbFactory->getMainLB();
102 $this->queueGroup = $queueGroup;
103 $this->stash = $stash;
104 $this->cache = $cache;
105 $this->readOnlyMode = $readOnlyMode;
106 $this->stats = new NullStatsdDataFactory();
107 $this->deferredUpdatesAddCallableUpdateCallback =
108 [ DeferredUpdates::class, 'addCallableUpdate' ];
109 $this->revisionGetTimestampFromIdCallback =
110 [ Revision::class, 'getTimestampFromId' ];
111 $this->updateRowsPerQuery = $updateRowsPerQuery;
112
113 $this->latestUpdateCache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
114 }
115
116 /**
117 * @param StatsdDataFactoryInterface $stats
118 */
119 public function setStatsdDataFactory( StatsdDataFactoryInterface $stats ) {
120 $this->stats = $stats;
121 }
122
123 /**
124 * Overrides the DeferredUpdates::addCallableUpdate callback
125 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
126 *
127 * @param callable $callback
128 *
129 * @see DeferredUpdates::addCallableUpdate for callback signiture
130 *
131 * @return ScopedCallback to reset the overridden value
132 * @throws MWException
133 */
134 public function overrideDeferredUpdatesAddCallableUpdateCallback( callable $callback ) {
135 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
136 throw new MWException(
137 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
138 );
139 }
140 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback;
141 $this->deferredUpdatesAddCallableUpdateCallback = $callback;
142 return new ScopedCallback( function () use ( $previousValue ) {
143 $this->deferredUpdatesAddCallableUpdateCallback = $previousValue;
144 } );
145 }
146
147 /**
148 * Overrides the Revision::getTimestampFromId callback
149 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
150 *
151 * @param callable $callback
152 * @see Revision::getTimestampFromId for callback signiture
153 *
154 * @return ScopedCallback to reset the overridden value
155 * @throws MWException
156 */
157 public function overrideRevisionGetTimestampFromIdCallback( callable $callback ) {
158 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
159 throw new MWException(
160 'Cannot override Revision::getTimestampFromId callback in operation.'
161 );
162 }
163 $previousValue = $this->revisionGetTimestampFromIdCallback;
164 $this->revisionGetTimestampFromIdCallback = $callback;
165 return new ScopedCallback( function () use ( $previousValue ) {
166 $this->revisionGetTimestampFromIdCallback = $previousValue;
167 } );
168 }
169
170 private function getCacheKey( User $user, LinkTarget $target ) {
171 return $this->cache->makeKey(
172 (string)$target->getNamespace(),
173 $target->getDBkey(),
174 (string)$user->getId()
175 );
176 }
177
178 private function cache( WatchedItem $item ) {
179 $user = $item->getUser();
180 $target = $item->getLinkTarget();
181 $key = $this->getCacheKey( $user, $target );
182 $this->cache->set( $key, $item );
183 $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
184 $this->stats->increment( 'WatchedItemStore.cache' );
185 }
186
187 private function uncache( User $user, LinkTarget $target ) {
188 $this->cache->delete( $this->getCacheKey( $user, $target ) );
189 unset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
190 $this->stats->increment( 'WatchedItemStore.uncache' );
191 }
192
193 private function uncacheLinkTarget( LinkTarget $target ) {
194 $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget' );
195 if ( !isset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] ) ) {
196 return;
197 }
198 foreach ( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] as $key ) {
199 $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
200 $this->cache->delete( $key );
201 }
202 }
203
204 private function uncacheUser( User $user ) {
205 $this->stats->increment( 'WatchedItemStore.uncacheUser' );
206 foreach ( $this->cacheIndex as $ns => $dbKeyArray ) {
207 foreach ( $dbKeyArray as $dbKey => $userArray ) {
208 if ( isset( $userArray[$user->getId()] ) ) {
209 $this->stats->increment( 'WatchedItemStore.uncacheUser.items' );
210 $this->cache->delete( $userArray[$user->getId()] );
211 }
212 }
213 }
214
215 $pageSeenKey = $this->getPageSeenTimestampsKey( $user );
216 $this->latestUpdateCache->delete( $pageSeenKey );
217 $this->stash->delete( $pageSeenKey );
218 }
219
220 /**
221 * @param User $user
222 * @param LinkTarget $target
223 *
224 * @return WatchedItem|false
225 */
226 private function getCached( User $user, LinkTarget $target ) {
227 return $this->cache->get( $this->getCacheKey( $user, $target ) );
228 }
229
230 /**
231 * Return an array of conditions to select or update the appropriate database
232 * row.
233 *
234 * @param User $user
235 * @param LinkTarget $target
236 *
237 * @return array
238 */
239 private function dbCond( User $user, LinkTarget $target ) {
240 return [
241 'wl_user' => $user->getId(),
242 'wl_namespace' => $target->getNamespace(),
243 'wl_title' => $target->getDBkey(),
244 ];
245 }
246
247 /**
248 * @param int $dbIndex DB_MASTER or DB_REPLICA
249 *
250 * @return IDatabase
251 * @throws MWException
252 */
253 private function getConnectionRef( $dbIndex ) {
254 return $this->loadBalancer->getConnectionRef( $dbIndex, [ 'watchlist' ] );
255 }
256
257 /**
258 * Deletes ALL watched items for the given user when under
259 * $updateRowsPerQuery entries exist.
260 *
261 * @since 1.30
262 *
263 * @param User $user
264 *
265 * @return bool true on success, false when too many items are watched
266 */
267 public function clearUserWatchedItems( User $user ) {
268 if ( $this->countWatchedItems( $user ) > $this->updateRowsPerQuery ) {
269 return false;
270 }
271
272 $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
273 $dbw->delete(
274 'watchlist',
275 [ 'wl_user' => $user->getId() ],
276 __METHOD__
277 );
278 $this->uncacheAllItemsForUser( $user );
279
280 return true;
281 }
282
283 private function uncacheAllItemsForUser( User $user ) {
284 $userId = $user->getId();
285 foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
286 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
287 if ( array_key_exists( $userId, $userIndex ) ) {
288 $this->cache->delete( $userIndex[$userId] );
289 unset( $this->cacheIndex[$ns][$dbKey][$userId] );
290 }
291 }
292 }
293
294 // Cleanup empty cache keys
295 foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
296 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
297 if ( empty( $this->cacheIndex[$ns][$dbKey] ) ) {
298 unset( $this->cacheIndex[$ns][$dbKey] );
299 }
300 }
301 if ( empty( $this->cacheIndex[$ns] ) ) {
302 unset( $this->cacheIndex[$ns] );
303 }
304 }
305 }
306
307 /**
308 * Queues a job that will clear the users watchlist using the Job Queue.
309 *
310 * @since 1.31
311 *
312 * @param User $user
313 */
314 public function clearUserWatchedItemsUsingJobQueue( User $user ) {
315 $job = ClearUserWatchlistJob::newForUser( $user, $this->getMaxId() );
316 $this->queueGroup->push( $job );
317 }
318
319 /**
320 * @since 1.31
321 * @return int The maximum current wl_id
322 */
323 public function getMaxId() {
324 $dbr = $this->getConnectionRef( DB_REPLICA );
325 return (int)$dbr->selectField(
326 'watchlist',
327 'MAX(wl_id)',
328 '',
329 __METHOD__
330 );
331 }
332
333 /**
334 * @since 1.31
335 * @param User $user
336 * @return int
337 */
338 public function countWatchedItems( User $user ) {
339 $dbr = $this->getConnectionRef( DB_REPLICA );
340 $return = (int)$dbr->selectField(
341 'watchlist',
342 'COUNT(*)',
343 [
344 'wl_user' => $user->getId()
345 ],
346 __METHOD__
347 );
348
349 return $return;
350 }
351
352 /**
353 * @since 1.27
354 * @param LinkTarget $target
355 * @return int
356 */
357 public function countWatchers( LinkTarget $target ) {
358 $dbr = $this->getConnectionRef( DB_REPLICA );
359 $return = (int)$dbr->selectField(
360 'watchlist',
361 'COUNT(*)',
362 [
363 'wl_namespace' => $target->getNamespace(),
364 'wl_title' => $target->getDBkey(),
365 ],
366 __METHOD__
367 );
368
369 return $return;
370 }
371
372 /**
373 * @since 1.27
374 * @param LinkTarget $target
375 * @param string|int $threshold
376 * @return int
377 */
378 public function countVisitingWatchers( LinkTarget $target, $threshold ) {
379 $dbr = $this->getConnectionRef( DB_REPLICA );
380 $visitingWatchers = (int)$dbr->selectField(
381 'watchlist',
382 'COUNT(*)',
383 [
384 'wl_namespace' => $target->getNamespace(),
385 'wl_title' => $target->getDBkey(),
386 'wl_notificationtimestamp >= ' .
387 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
388 ' OR wl_notificationtimestamp IS NULL'
389 ],
390 __METHOD__
391 );
392
393 return $visitingWatchers;
394 }
395
396 /**
397 * @param User $user
398 * @param TitleValue[] $titles
399 * @return bool
400 * @throws MWException
401 */
402 public function removeWatchBatchForUser( User $user, array $titles ) {
403 if ( $this->readOnlyMode->isReadOnly() ) {
404 return false;
405 }
406 if ( $user->isAnon() ) {
407 return false;
408 }
409 if ( !$titles ) {
410 return true;
411 }
412
413 $rows = $this->getTitleDbKeysGroupedByNamespace( $titles );
414 $this->uncacheTitlesForUser( $user, $titles );
415
416 $dbw = $this->getConnectionRef( DB_MASTER );
417 $ticket = count( $titles ) > $this->updateRowsPerQuery ?
418 $this->lbFactory->getEmptyTransactionTicket( __METHOD__ ) : null;
419 $affectedRows = 0;
420
421 // Batch delete items per namespace.
422 foreach ( $rows as $namespace => $namespaceTitles ) {
423 $rowBatches = array_chunk( $namespaceTitles, $this->updateRowsPerQuery );
424 foreach ( $rowBatches as $toDelete ) {
425 $dbw->delete( 'watchlist', [
426 'wl_user' => $user->getId(),
427 'wl_namespace' => $namespace,
428 'wl_title' => $toDelete
429 ], __METHOD__ );
430 $affectedRows += $dbw->affectedRows();
431 if ( $ticket ) {
432 $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
433 }
434 }
435 }
436
437 return (bool)$affectedRows;
438 }
439
440 /**
441 * @since 1.27
442 * @param LinkTarget[] $targets
443 * @param array $options
444 * @return array
445 */
446 public function countWatchersMultiple( array $targets, array $options = [] ) {
447 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
448
449 $dbr = $this->getConnectionRef( DB_REPLICA );
450
451 if ( array_key_exists( 'minimumWatchers', $options ) ) {
452 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
453 }
454
455 $lb = new LinkBatch( $targets );
456 $res = $dbr->select(
457 'watchlist',
458 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
459 [ $lb->constructSet( 'wl', $dbr ) ],
460 __METHOD__,
461 $dbOptions
462 );
463
464 $watchCounts = [];
465 foreach ( $targets as $linkTarget ) {
466 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
467 }
468
469 foreach ( $res as $row ) {
470 $watchCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
471 }
472
473 return $watchCounts;
474 }
475
476 /**
477 * @since 1.27
478 * @param array $targetsWithVisitThresholds
479 * @param int|null $minimumWatchers
480 * @return array
481 */
482 public function countVisitingWatchersMultiple(
483 array $targetsWithVisitThresholds,
484 $minimumWatchers = null
485 ) {
486 if ( $targetsWithVisitThresholds === [] ) {
487 // No titles requested => no results returned
488 return [];
489 }
490
491 $dbr = $this->getConnectionRef( DB_REPLICA );
492
493 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
494
495 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
496 if ( $minimumWatchers !== null ) {
497 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
498 }
499 $res = $dbr->select(
500 'watchlist',
501 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
502 $conds,
503 __METHOD__,
504 $dbOptions
505 );
506
507 $watcherCounts = [];
508 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
509 /* @var LinkTarget $target */
510 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
511 }
512
513 foreach ( $res as $row ) {
514 $watcherCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
515 }
516
517 return $watcherCounts;
518 }
519
520 /**
521 * Generates condition for the query used in a batch count visiting watchers.
522 *
523 * @param IDatabase $db
524 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
525 * @return string
526 */
527 private function getVisitingWatchersCondition(
528 IDatabase $db,
529 array $targetsWithVisitThresholds
530 ) {
531 $missingTargets = [];
532 $namespaceConds = [];
533 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
534 if ( $threshold === null ) {
535 $missingTargets[] = $target;
536 continue;
537 }
538 /* @var LinkTarget $target */
539 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
540 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
541 $db->makeList( [
542 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
543 'wl_notificationtimestamp IS NULL'
544 ], LIST_OR )
545 ], LIST_AND );
546 }
547
548 $conds = [];
549 foreach ( $namespaceConds as $namespace => $pageConds ) {
550 $conds[] = $db->makeList( [
551 'wl_namespace = ' . $namespace,
552 '(' . $db->makeList( $pageConds, LIST_OR ) . ')'
553 ], LIST_AND );
554 }
555
556 if ( $missingTargets ) {
557 $lb = new LinkBatch( $missingTargets );
558 $conds[] = $lb->constructSet( 'wl', $db );
559 }
560
561 return $db->makeList( $conds, LIST_OR );
562 }
563
564 /**
565 * @since 1.27
566 * @param User $user
567 * @param LinkTarget $target
568 * @return bool
569 */
570 public function getWatchedItem( User $user, LinkTarget $target ) {
571 if ( $user->isAnon() ) {
572 return false;
573 }
574
575 $cached = $this->getCached( $user, $target );
576 if ( $cached ) {
577 $this->stats->increment( 'WatchedItemStore.getWatchedItem.cached' );
578 return $cached;
579 }
580 $this->stats->increment( 'WatchedItemStore.getWatchedItem.load' );
581 return $this->loadWatchedItem( $user, $target );
582 }
583
584 /**
585 * @since 1.27
586 * @param User $user
587 * @param LinkTarget $target
588 * @return WatchedItem|bool
589 */
590 public function loadWatchedItem( User $user, LinkTarget $target ) {
591 // Only loggedin user can have a watchlist
592 if ( $user->isAnon() ) {
593 return false;
594 }
595
596 $dbr = $this->getConnectionRef( DB_REPLICA );
597
598 $row = $dbr->selectRow(
599 'watchlist',
600 'wl_notificationtimestamp',
601 $this->dbCond( $user, $target ),
602 __METHOD__
603 );
604
605 if ( !$row ) {
606 return false;
607 }
608
609 $item = new WatchedItem(
610 $user,
611 $target,
612 $this->getLatestNotificationTimestamp( $row->wl_notificationtimestamp, $user, $target )
613 );
614 $this->cache( $item );
615
616 return $item;
617 }
618
619 /**
620 * @since 1.27
621 * @param User $user
622 * @param array $options
623 * @return WatchedItem[]
624 */
625 public function getWatchedItemsForUser( User $user, array $options = [] ) {
626 $options += [ 'forWrite' => false ];
627
628 $dbOptions = [];
629 if ( array_key_exists( 'sort', $options ) ) {
630 Assert::parameter(
631 ( in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ) ),
632 '$options[\'sort\']',
633 'must be SORT_ASC or SORT_DESC'
634 );
635 $dbOptions['ORDER BY'] = [
636 "wl_namespace {$options['sort']}",
637 "wl_title {$options['sort']}"
638 ];
639 }
640 $db = $this->getConnectionRef( $options['forWrite'] ? DB_MASTER : DB_REPLICA );
641
642 $res = $db->select(
643 'watchlist',
644 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
645 [ 'wl_user' => $user->getId() ],
646 __METHOD__,
647 $dbOptions
648 );
649
650 $watchedItems = [];
651 foreach ( $res as $row ) {
652 $target = new TitleValue( (int)$row->wl_namespace, $row->wl_title );
653 // @todo: Should we add these to the process cache?
654 $watchedItems[] = new WatchedItem(
655 $user,
656 new TitleValue( (int)$row->wl_namespace, $row->wl_title ),
657 $this->getLatestNotificationTimestamp(
658 $row->wl_notificationtimestamp, $user, $target )
659 );
660 }
661
662 return $watchedItems;
663 }
664
665 /**
666 * @since 1.27
667 * @param User $user
668 * @param LinkTarget $target
669 * @return bool
670 */
671 public function isWatched( User $user, LinkTarget $target ) {
672 return (bool)$this->getWatchedItem( $user, $target );
673 }
674
675 /**
676 * @since 1.27
677 * @param User $user
678 * @param LinkTarget[] $targets
679 * @return array
680 */
681 public function getNotificationTimestampsBatch( User $user, array $targets ) {
682 $timestamps = [];
683 foreach ( $targets as $target ) {
684 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
685 }
686
687 if ( $user->isAnon() ) {
688 return $timestamps;
689 }
690
691 $targetsToLoad = [];
692 foreach ( $targets as $target ) {
693 $cachedItem = $this->getCached( $user, $target );
694 if ( $cachedItem ) {
695 $timestamps[$target->getNamespace()][$target->getDBkey()] =
696 $cachedItem->getNotificationTimestamp();
697 } else {
698 $targetsToLoad[] = $target;
699 }
700 }
701
702 if ( !$targetsToLoad ) {
703 return $timestamps;
704 }
705
706 $dbr = $this->getConnectionRef( DB_REPLICA );
707
708 $lb = new LinkBatch( $targetsToLoad );
709 $res = $dbr->select(
710 'watchlist',
711 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
712 [
713 $lb->constructSet( 'wl', $dbr ),
714 'wl_user' => $user->getId(),
715 ],
716 __METHOD__
717 );
718
719 foreach ( $res as $row ) {
720 $target = new TitleValue( (int)$row->wl_namespace, $row->wl_title );
721 $timestamps[$row->wl_namespace][$row->wl_title] =
722 $this->getLatestNotificationTimestamp(
723 $row->wl_notificationtimestamp, $user, $target );
724 }
725
726 return $timestamps;
727 }
728
729 /**
730 * @since 1.27
731 * @param User $user
732 * @param LinkTarget $target
733 * @throws MWException
734 */
735 public function addWatch( User $user, LinkTarget $target ) {
736 $this->addWatchBatchForUser( $user, [ $target ] );
737 }
738
739 /**
740 * @since 1.27
741 * @param User $user
742 * @param LinkTarget[] $targets
743 * @return bool
744 * @throws MWException
745 */
746 public function addWatchBatchForUser( User $user, array $targets ) {
747 if ( $this->readOnlyMode->isReadOnly() ) {
748 return false;
749 }
750 // Only logged-in user can have a watchlist
751 if ( $user->isAnon() ) {
752 return false;
753 }
754
755 if ( !$targets ) {
756 return true;
757 }
758
759 $rows = [];
760 $items = [];
761 foreach ( $targets as $target ) {
762 $rows[] = [
763 'wl_user' => $user->getId(),
764 'wl_namespace' => $target->getNamespace(),
765 'wl_title' => $target->getDBkey(),
766 'wl_notificationtimestamp' => null,
767 ];
768 $items[] = new WatchedItem(
769 $user,
770 $target,
771 null
772 );
773 $this->uncache( $user, $target );
774 }
775
776 $dbw = $this->getConnectionRef( DB_MASTER );
777 $ticket = count( $targets ) > $this->updateRowsPerQuery ?
778 $this->lbFactory->getEmptyTransactionTicket( __METHOD__ ) : null;
779 $affectedRows = 0;
780 $rowBatches = array_chunk( $rows, $this->updateRowsPerQuery );
781 foreach ( $rowBatches as $toInsert ) {
782 // Use INSERT IGNORE to avoid overwriting the notification timestamp
783 // if there's already an entry for this page
784 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
785 $affectedRows += $dbw->affectedRows();
786 if ( $ticket ) {
787 $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
788 }
789 }
790 // Update process cache to ensure skin doesn't claim that the current
791 // page is unwatched in the response of action=watch itself (T28292).
792 // This would otherwise be re-queried from a replica by isWatched().
793 foreach ( $items as $item ) {
794 $this->cache( $item );
795 }
796
797 return (bool)$affectedRows;
798 }
799
800 /**
801 * @since 1.27
802 * @param User $user
803 * @param LinkTarget $target
804 * @return bool
805 * @throws MWException
806 */
807 public function removeWatch( User $user, LinkTarget $target ) {
808 return $this->removeWatchBatchForUser( $user, [ $target ] );
809 }
810
811 /**
812 * Set the "last viewed" timestamps for certain titles on a user's watchlist.
813 *
814 * If the $targets parameter is omitted or set to [], this method simply wraps
815 * resetAllNotificationTimestampsForUser(), and in that case you should instead call that method
816 * directly; support for omitting $targets is for backwards compatibility.
817 *
818 * If $targets is omitted or set to [], timestamps will be updated for every title on the user's
819 * watchlist, and this will be done through a DeferredUpdate. If $targets is a non-empty array,
820 * only the specified titles will be updated, and this will be done immediately (not deferred).
821 *
822 * @since 1.27
823 * @param User $user
824 * @param string|int $timestamp Value to set the "last viewed" timestamp to (null to clear)
825 * @param LinkTarget[] $targets Titles to set the timestamp for; [] means the entire watchlist
826 * @return bool
827 */
828 public function setNotificationTimestampsForUser( User $user, $timestamp, array $targets = [] ) {
829 // Only loggedin user can have a watchlist
830 if ( $user->isAnon() || $this->readOnlyMode->isReadOnly() ) {
831 return false;
832 }
833
834 if ( !$targets ) {
835 // Backwards compatibility
836 $this->resetAllNotificationTimestampsForUser( $user, $timestamp );
837 return true;
838 }
839
840 $rows = $this->getTitleDbKeysGroupedByNamespace( $targets );
841
842 $dbw = $this->getConnectionRef( DB_MASTER );
843 if ( $timestamp !== null ) {
844 $timestamp = $dbw->timestamp( $timestamp );
845 }
846 $ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ );
847 $affectedSinceWait = 0;
848
849 // Batch update items per namespace
850 foreach ( $rows as $namespace => $namespaceTitles ) {
851 $rowBatches = array_chunk( $namespaceTitles, $this->updateRowsPerQuery );
852 foreach ( $rowBatches as $toUpdate ) {
853 $dbw->update(
854 'watchlist',
855 [ 'wl_notificationtimestamp' => $timestamp ],
856 [
857 'wl_user' => $user->getId(),
858 'wl_namespace' => $namespace,
859 'wl_title' => $toUpdate
860 ]
861 );
862 $affectedSinceWait += $dbw->affectedRows();
863 // Wait for replication every time we've touched updateRowsPerQuery rows
864 if ( $affectedSinceWait >= $this->updateRowsPerQuery ) {
865 $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
866 $affectedSinceWait = 0;
867 }
868 }
869 }
870
871 $this->uncacheUser( $user );
872
873 return true;
874 }
875
876 public function getLatestNotificationTimestamp( $timestamp, User $user, LinkTarget $target ) {
877 $timestamp = wfTimestampOrNull( TS_MW, $timestamp );
878 if ( $timestamp === null ) {
879 return null; // no notification
880 }
881
882 $seenTimestamps = $this->getPageSeenTimestamps( $user );
883 if (
884 $seenTimestamps &&
885 $seenTimestamps->get( $this->getPageSeenKey( $target ) ) >= $timestamp
886 ) {
887 // If a reset job did not yet run, then the "seen" timestamp will be higher
888 return null;
889 }
890
891 return $timestamp;
892 }
893
894 /**
895 * Schedule a DeferredUpdate that sets all of the "last viewed" timestamps for a given user
896 * to the same value.
897 * @param User $user
898 * @param string|int|null $timestamp Value to set all timestamps to, null to clear them
899 */
900 public function resetAllNotificationTimestampsForUser( User $user, $timestamp = null ) {
901 // Only loggedin user can have a watchlist
902 if ( $user->isAnon() ) {
903 return;
904 }
905
906 // If the page is watched by the user (or may be watched), update the timestamp
907 $job = new ClearWatchlistNotificationsJob(
908 $user->getUserPage(),
909 [ 'userId' => $user->getId(), 'timestamp' => $timestamp, 'casTime' => time() ]
910 );
911
912 // Try to run this post-send
913 // Calls DeferredUpdates::addCallableUpdate in normal operation
914 call_user_func(
915 $this->deferredUpdatesAddCallableUpdateCallback,
916 function () use ( $job ) {
917 $job->run();
918 }
919 );
920 }
921
922 /**
923 * @since 1.27
924 * @param User $editor
925 * @param LinkTarget $target
926 * @param string|int $timestamp
927 * @return int[]
928 */
929 public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
930 $dbw = $this->getConnectionRef( DB_MASTER );
931 $uids = $dbw->selectFieldValues(
932 'watchlist',
933 'wl_user',
934 [
935 'wl_user != ' . intval( $editor->getId() ),
936 'wl_namespace' => $target->getNamespace(),
937 'wl_title' => $target->getDBkey(),
938 'wl_notificationtimestamp IS NULL',
939 ],
940 __METHOD__
941 );
942
943 $watchers = array_map( 'intval', $uids );
944 if ( $watchers ) {
945 // Update wl_notificationtimestamp for all watching users except the editor
946 $fname = __METHOD__;
947 DeferredUpdates::addCallableUpdate(
948 function () use ( $timestamp, $watchers, $target, $fname ) {
949 $dbw = $this->getConnectionRef( DB_MASTER );
950 $ticket = $this->lbFactory->getEmptyTransactionTicket( $fname );
951
952 $watchersChunks = array_chunk( $watchers, $this->updateRowsPerQuery );
953 foreach ( $watchersChunks as $watchersChunk ) {
954 $dbw->update( 'watchlist',
955 [ /* SET */
956 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
957 ], [ /* WHERE - TODO Use wl_id T130067 */
958 'wl_user' => $watchersChunk,
959 'wl_namespace' => $target->getNamespace(),
960 'wl_title' => $target->getDBkey(),
961 ], $fname
962 );
963 if ( count( $watchersChunks ) > 1 ) {
964 $this->lbFactory->commitAndWaitForReplication(
965 $fname, $ticket, [ 'domain' => $dbw->getDomainID() ]
966 );
967 }
968 }
969 $this->uncacheLinkTarget( $target );
970 },
971 DeferredUpdates::POSTSEND,
972 $dbw
973 );
974 }
975
976 return $watchers;
977 }
978
979 /**
980 * @since 1.27
981 * @param User $user
982 * @param Title $title
983 * @param string $force
984 * @param int $oldid
985 * @return bool
986 */
987 public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) {
988 $time = time();
989
990 // Only loggedin user can have a watchlist
991 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
992 return false;
993 }
994
995 if ( !Hooks::run( 'BeforeResetNotificationTimestamp', [ &$user, &$title, $force, &$oldid ] ) ) {
996 return false;
997 }
998
999 $item = null;
1000 if ( $force != 'force' ) {
1001 $item = $this->loadWatchedItem( $user, $title );
1002 if ( !$item || $item->getNotificationTimestamp() === null ) {
1003 return false;
1004 }
1005 }
1006
1007 // Get the timestamp (TS_MW) of this revision to track the latest one seen
1008 $seenTime = call_user_func(
1009 $this->revisionGetTimestampFromIdCallback,
1010 $title,
1011 $oldid ?: $title->getLatestRevID()
1012 );
1013
1014 // Mark the item as read immediately in lightweight storage
1015 $this->stash->merge(
1016 $this->getPageSeenTimestampsKey( $user ),
1017 function ( $cache, $key, $current ) use ( $title, $seenTime ) {
1018 $value = $current ?: new MapCacheLRU( 300 );
1019 $subKey = $this->getPageSeenKey( $title );
1020
1021 if ( $seenTime > $value->get( $subKey ) ) {
1022 // Revision is newer than the last one seen
1023 $value->set( $subKey, $seenTime );
1024 $this->latestUpdateCache->set( $key, $value, IExpiringStore::TTL_PROC_LONG );
1025 } elseif ( $seenTime === false ) {
1026 // Revision does not exist
1027 $value->set( $subKey, wfTimestamp( TS_MW ) );
1028 $this->latestUpdateCache->set( $key, $value, IExpiringStore::TTL_PROC_LONG );
1029 } else {
1030 return false; // nothing to update
1031 }
1032
1033 return $value;
1034 },
1035 IExpiringStore::TTL_HOUR
1036 );
1037
1038 // If the page is watched by the user (or may be watched), update the timestamp
1039 $job = new ActivityUpdateJob(
1040 $title,
1041 [
1042 'type' => 'updateWatchlistNotification',
1043 'userid' => $user->getId(),
1044 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
1045 'curTime' => $time
1046 ]
1047 );
1048 // Try to enqueue this post-send
1049 $this->queueGroup->lazyPush( $job );
1050
1051 $this->uncache( $user, $title );
1052
1053 return true;
1054 }
1055
1056 /**
1057 * @param User $user
1058 * @return MapCacheLRU|null The map contains prefixed title keys and TS_MW values
1059 */
1060 private function getPageSeenTimestamps( User $user ) {
1061 $key = $this->getPageSeenTimestampsKey( $user );
1062
1063 return $this->latestUpdateCache->getWithSetCallback(
1064 $key,
1065 IExpiringStore::TTL_PROC_LONG,
1066 function () use ( $key ) {
1067 return $this->stash->get( $key ) ?: null;
1068 }
1069 );
1070 }
1071
1072 /**
1073 * @param User $user
1074 * @return string
1075 */
1076 private function getPageSeenTimestampsKey( User $user ) {
1077 return $this->stash->makeGlobalKey(
1078 'watchlist-recent-updates',
1079 $this->lbFactory->getLocalDomainID(),
1080 $user->getId()
1081 );
1082 }
1083
1084 /**
1085 * @param LinkTarget $target
1086 * @return string
1087 */
1088 private function getPageSeenKey( LinkTarget $target ) {
1089 return "{$target->getNamespace()}:{$target->getDBkey()}";
1090 }
1091
1092 private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) {
1093 if ( !$oldid ) {
1094 // No oldid given, assuming latest revision; clear the timestamp.
1095 return null;
1096 }
1097
1098 if ( !$title->getNextRevisionID( $oldid ) ) {
1099 // Oldid given and is the latest revision for this title; clear the timestamp.
1100 return null;
1101 }
1102
1103 if ( $item === null ) {
1104 $item = $this->loadWatchedItem( $user, $title );
1105 }
1106
1107 if ( !$item ) {
1108 // This can only happen if $force is enabled.
1109 return null;
1110 }
1111
1112 // Oldid given and isn't the latest; update the timestamp.
1113 // This will result in no further notification emails being sent!
1114 // Calls Revision::getTimestampFromId in normal operation
1115 $notificationTimestamp = call_user_func(
1116 $this->revisionGetTimestampFromIdCallback,
1117 $title,
1118 $oldid
1119 );
1120
1121 // We need to go one second to the future because of various strict comparisons
1122 // throughout the codebase
1123 $ts = new MWTimestamp( $notificationTimestamp );
1124 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
1125 $notificationTimestamp = $ts->getTimestamp( TS_MW );
1126
1127 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
1128 if ( $force != 'force' ) {
1129 return false;
1130 } else {
1131 // This is a little silly…
1132 return $item->getNotificationTimestamp();
1133 }
1134 }
1135
1136 return $notificationTimestamp;
1137 }
1138
1139 /**
1140 * @since 1.27
1141 * @param User $user
1142 * @param int|null $unreadLimit
1143 * @return int|bool
1144 */
1145 public function countUnreadNotifications( User $user, $unreadLimit = null ) {
1146 $dbr = $this->getConnectionRef( DB_REPLICA );
1147
1148 $queryOptions = [];
1149 if ( $unreadLimit !== null ) {
1150 $unreadLimit = (int)$unreadLimit;
1151 $queryOptions['LIMIT'] = $unreadLimit;
1152 }
1153
1154 $conds = [
1155 'wl_user' => $user->getId(),
1156 'wl_notificationtimestamp IS NOT NULL'
1157 ];
1158
1159 $rowCount = $dbr->selectRowCount( 'watchlist', '1', $conds, __METHOD__, $queryOptions );
1160
1161 if ( $unreadLimit === null ) {
1162 return $rowCount;
1163 }
1164
1165 if ( $rowCount >= $unreadLimit ) {
1166 return true;
1167 }
1168
1169 return $rowCount;
1170 }
1171
1172 /**
1173 * @since 1.27
1174 * @param LinkTarget $oldTarget
1175 * @param LinkTarget $newTarget
1176 */
1177 public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) {
1178 $oldTarget = Title::newFromLinkTarget( $oldTarget );
1179 $newTarget = Title::newFromLinkTarget( $newTarget );
1180
1181 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
1182 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
1183 }
1184
1185 /**
1186 * @since 1.27
1187 * @param LinkTarget $oldTarget
1188 * @param LinkTarget $newTarget
1189 */
1190 public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
1191 $dbw = $this->getConnectionRef( DB_MASTER );
1192
1193 $result = $dbw->select(
1194 'watchlist',
1195 [ 'wl_user', 'wl_notificationtimestamp' ],
1196 [
1197 'wl_namespace' => $oldTarget->getNamespace(),
1198 'wl_title' => $oldTarget->getDBkey(),
1199 ],
1200 __METHOD__,
1201 [ 'FOR UPDATE' ]
1202 );
1203
1204 $newNamespace = $newTarget->getNamespace();
1205 $newDBkey = $newTarget->getDBkey();
1206
1207 # Construct array to replace into the watchlist
1208 $values = [];
1209 foreach ( $result as $row ) {
1210 $values[] = [
1211 'wl_user' => $row->wl_user,
1212 'wl_namespace' => $newNamespace,
1213 'wl_title' => $newDBkey,
1214 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
1215 ];
1216 }
1217
1218 if ( !empty( $values ) ) {
1219 # Perform replace
1220 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
1221 # some other DBMSes, mostly due to poor simulation by us
1222 $dbw->replace(
1223 'watchlist',
1224 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
1225 $values,
1226 __METHOD__
1227 );
1228 }
1229 }
1230
1231 /**
1232 * @param LinkTarget[] $titles
1233 * @return array
1234 */
1235 private function getTitleDbKeysGroupedByNamespace( array $titles ) {
1236 $rows = [];
1237 foreach ( $titles as $title ) {
1238 // Group titles by namespace.
1239 $rows[ $title->getNamespace() ][] = $title->getDBkey();
1240 }
1241 return $rows;
1242 }
1243
1244 /**
1245 * @param User $user
1246 * @param Title[] $titles
1247 */
1248 private function uncacheTitlesForUser( User $user, array $titles ) {
1249 foreach ( $titles as $title ) {
1250 $this->uncache( $user, $title );
1251 }
1252 }
1253
1254 }