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