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