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