Merge "Reset WatchedItemStore default instance after tests"
[lhc/web/wiklou.git] / includes / WatchedItemStore.php
1 <?php
2
3 use Wikimedia\Assert\Assert;
4
5 /**
6 * Storage layer class for WatchedItems.
7 * Database interaction.
8 *
9 * @author Addshore
10 *
11 * @since 1.27
12 */
13 class WatchedItemStore {
14
15 /**
16 * @var LoadBalancer
17 */
18 private $loadBalancer;
19
20 /**
21 * @var HashBagOStuff
22 */
23 private $cache;
24
25 /**
26 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
27 * The index is needed so that on mass changes all relevant items can be un-cached.
28 * For example: Clearing a users watchlist of all items or updating notification timestamps
29 * for all users watching a single target.
30 */
31 private $cacheIndex = [];
32
33 /**
34 * @var callable|null
35 */
36 private $deferredUpdatesAddCallableUpdateCallback;
37
38 /**
39 * @var callable|null
40 */
41 private $revisionGetTimestampFromIdCallback;
42
43 /**
44 * @var self|null
45 */
46 private static $instance;
47
48 /**
49 * @param LoadBalancer $loadBalancer
50 * @param HashBagOStuff $cache
51 */
52 public function __construct(
53 LoadBalancer $loadBalancer,
54 HashBagOStuff $cache
55 ) {
56 $this->loadBalancer = $loadBalancer;
57 $this->cache = $cache;
58 $this->deferredUpdatesAddCallableUpdateCallback = [ 'DeferredUpdates', 'addCallableUpdate' ];
59 $this->revisionGetTimestampFromIdCallback = [ 'Revision', 'getTimestampFromId' ];
60 }
61
62 /**
63 * Overrides the DeferredUpdates::addCallableUpdate callback
64 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
65 *
66 * @param callable $callback
67 *
68 * @see DeferredUpdates::addCallableUpdate for callback signiture
69 *
70 * @return ScopedCallback to reset the overridden value
71 * @throws MWException
72 */
73 public function overrideDeferredUpdatesAddCallableUpdateCallback( $callback ) {
74 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
75 throw new MWException(
76 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
77 );
78 }
79 Assert::parameterType( 'callable', $callback, '$callback' );
80
81 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback;
82 $this->deferredUpdatesAddCallableUpdateCallback = $callback;
83 return new ScopedCallback( function() use ( $previousValue ) {
84 $this->deferredUpdatesAddCallableUpdateCallback = $previousValue;
85 } );
86 }
87
88 /**
89 * Overrides the Revision::getTimestampFromId callback
90 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
91 *
92 * @param callable $callback
93 * @see Revision::getTimestampFromId for callback signiture
94 *
95 * @return ScopedCallback to reset the overridden value
96 * @throws MWException
97 */
98 public function overrideRevisionGetTimestampFromIdCallback( $callback ) {
99 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
100 throw new MWException(
101 'Cannot override Revision::getTimestampFromId callback in operation.'
102 );
103 }
104 Assert::parameterType( 'callable', $callback, '$callback' );
105
106 $previousValue = $this->revisionGetTimestampFromIdCallback;
107 $this->revisionGetTimestampFromIdCallback = $callback;
108 return new ScopedCallback( function() use ( $previousValue ) {
109 $this->revisionGetTimestampFromIdCallback = $previousValue;
110 } );
111 }
112
113 /**
114 * Overrides the default instance of this class
115 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
116 *
117 * If this method is used it MUST also be called with null after a test to ensure a new
118 * default instance is created next time getDefaultInstance is called.
119 *
120 * @param WatchedItemStore|null $store
121 *
122 * @return ScopedCallback to reset the overridden value
123 * @throws MWException
124 */
125 public static function overrideDefaultInstance( WatchedItemStore $store = null ) {
126 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
127 throw new MWException(
128 'Cannot override ' . __CLASS__ . 'default instance in operation.'
129 );
130 }
131
132 $previousValue = self::$instance;
133 self::$instance = $store;
134 return new ScopedCallback( function() use ( $previousValue ) {
135 self::$instance = $previousValue;
136 } );
137 }
138
139 /**
140 * @return self
141 */
142 public static function getDefaultInstance() {
143 if ( !self::$instance ) {
144 self::$instance = new self(
145 wfGetLB(),
146 new HashBagOStuff( [ 'maxKeys' => 100 ] )
147 );
148 }
149 return self::$instance;
150 }
151
152 private function getCacheKey( User $user, LinkTarget $target ) {
153 return $this->cache->makeKey(
154 (string)$target->getNamespace(),
155 $target->getDBkey(),
156 (string)$user->getId()
157 );
158 }
159
160 private function cache( WatchedItem $item ) {
161 $user = $item->getUser();
162 $target = $item->getLinkTarget();
163 $key = $this->getCacheKey( $user, $target );
164 $this->cache->set( $key, $item );
165 $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
166 }
167
168 private function uncache( User $user, LinkTarget $target ) {
169 $this->cache->delete( $this->getCacheKey( $user, $target ) );
170 unset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
171 }
172
173 private function uncacheLinkTarget( LinkTarget $target ) {
174 if ( !isset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] ) ) {
175 return;
176 }
177 foreach ( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] as $key ) {
178 $this->cache->delete( $key );
179 }
180 }
181
182 /**
183 * @param User $user
184 * @param LinkTarget $target
185 *
186 * @return WatchedItem|null
187 */
188 private function getCached( User $user, LinkTarget $target ) {
189 return $this->cache->get( $this->getCacheKey( $user, $target ) );
190 }
191
192 /**
193 * Return an array of conditions to select or update the appropriate database
194 * row.
195 *
196 * @param User $user
197 * @param LinkTarget $target
198 *
199 * @return array
200 */
201 private function dbCond( User $user, LinkTarget $target ) {
202 return [
203 'wl_user' => $user->getId(),
204 'wl_namespace' => $target->getNamespace(),
205 'wl_title' => $target->getDBkey(),
206 ];
207 }
208
209 /**
210 * @param LinkTarget $target
211 *
212 * @return int
213 */
214 public function countWatchers( LinkTarget $target ) {
215 $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
216 $return = (int)$dbr->selectField(
217 'watchlist',
218 'COUNT(*)',
219 [
220 'wl_namespace' => $target->getNamespace(),
221 'wl_title' => $target->getDBkey(),
222 ],
223 __METHOD__
224 );
225 $this->loadBalancer->reuseConnection( $dbr );
226
227 return $return;
228 }
229
230 /**
231 * Number of page watchers who also visited a "recent" edit
232 *
233 * @param LinkTarget $target
234 * @param mixed $threshold timestamp accepted by wfTimestamp
235 *
236 * @return int
237 * @throws DBUnexpectedError
238 * @throws MWException
239 */
240 public function countVisitingWatchers( LinkTarget $target, $threshold ) {
241 $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
242 $visitingWatchers = (int)$dbr->selectField(
243 'watchlist',
244 'COUNT(*)',
245 [
246 'wl_namespace' => $target->getNamespace(),
247 'wl_title' => $target->getDBkey(),
248 'wl_notificationtimestamp >= ' .
249 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
250 ' OR wl_notificationtimestamp IS NULL'
251 ],
252 __METHOD__
253 );
254 $this->loadBalancer->reuseConnection( $dbr );
255
256 return $visitingWatchers;
257 }
258
259 /**
260 * @param LinkTarget[] $targets
261 * @param array $options Allowed keys:
262 * 'minimumWatchers' => int
263 *
264 * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers
265 * All targets will be present in the result. 0 either means no watchers or the number
266 * of watchers was below the minimumWatchers option if passed.
267 */
268 public function countWatchersMultiple( array $targets, array $options = [] ) {
269 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
270
271 $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
272
273 if ( array_key_exists( 'minimumWatchers', $options ) ) {
274 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
275 }
276
277 $lb = new LinkBatch( $targets );
278 $res = $dbr->select(
279 'watchlist',
280 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
281 [ $lb->constructSet( 'wl', $dbr ) ],
282 __METHOD__,
283 $dbOptions
284 );
285
286 $this->loadBalancer->reuseConnection( $dbr );
287
288 $watchCounts = [];
289 foreach ( $targets as $linkTarget ) {
290 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
291 }
292
293 foreach ( $res as $row ) {
294 $watchCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
295 }
296
297 return $watchCounts;
298 }
299
300 /**
301 * Get an item (may be cached)
302 *
303 * @param User $user
304 * @param LinkTarget $target
305 *
306 * @return WatchedItem|false
307 */
308 public function getWatchedItem( User $user, LinkTarget $target ) {
309 if ( $user->isAnon() ) {
310 return false;
311 }
312
313 $cached = $this->getCached( $user, $target );
314 if ( $cached ) {
315 return $cached;
316 }
317 return $this->loadWatchedItem( $user, $target );
318 }
319
320 /**
321 * Loads an item from the db
322 *
323 * @param User $user
324 * @param LinkTarget $target
325 *
326 * @return WatchedItem|false
327 */
328 public function loadWatchedItem( User $user, LinkTarget $target ) {
329 // Only loggedin user can have a watchlist
330 if ( $user->isAnon() ) {
331 return false;
332 }
333
334 $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
335 $row = $dbr->selectRow(
336 'watchlist',
337 'wl_notificationtimestamp',
338 $this->dbCond( $user, $target ),
339 __METHOD__
340 );
341 $this->loadBalancer->reuseConnection( $dbr );
342
343 if ( !$row ) {
344 return false;
345 }
346
347 $item = new WatchedItem(
348 $user,
349 $target,
350 $row->wl_notificationtimestamp
351 );
352 $this->cache( $item );
353
354 return $item;
355 }
356
357 /**
358 * Must be called separately for Subject & Talk namespaces
359 *
360 * @param User $user
361 * @param LinkTarget $target
362 *
363 * @return bool
364 */
365 public function isWatched( User $user, LinkTarget $target ) {
366 return (bool)$this->getWatchedItem( $user, $target );
367 }
368
369 /**
370 * Must be called separately for Subject & Talk namespaces
371 *
372 * @param User $user
373 * @param LinkTarget $target
374 */
375 public function addWatch( User $user, LinkTarget $target ) {
376 $this->addWatchBatch( [ [ $user, $target ] ] );
377 }
378
379 /**
380 * @param array[] $userTargetCombinations array of arrays containing [0] => User [1] => LinkTarget
381 *
382 * @return bool success
383 */
384 public function addWatchBatch( array $userTargetCombinations ) {
385 if ( $this->loadBalancer->getReadOnlyReason() !== false ) {
386 return false;
387 }
388
389 $rows = [];
390 foreach ( $userTargetCombinations as list( $user, $target ) ) {
391 /**
392 * @var User $user
393 * @var LinkTarget $target
394 */
395
396 // Only loggedin user can have a watchlist
397 if ( $user->isAnon() ) {
398 continue;
399 }
400 $rows[] = [
401 'wl_user' => $user->getId(),
402 'wl_namespace' => $target->getNamespace(),
403 'wl_title' => $target->getDBkey(),
404 'wl_notificationtimestamp' => null,
405 ];
406 $this->uncache( $user, $target );
407 }
408
409 if ( !$rows ) {
410 return false;
411 }
412
413 $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
414 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
415 // Use INSERT IGNORE to avoid overwriting the notification timestamp
416 // if there's already an entry for this page
417 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
418 }
419 $this->loadBalancer->reuseConnection( $dbw );
420
421 return true;
422 }
423
424 /**
425 * Removes the an entry for the User watching the LinkTarget
426 * Must be called separately for Subject & Talk namespaces
427 *
428 * @param User $user
429 * @param LinkTarget $target
430 *
431 * @return bool success
432 * @throws DBUnexpectedError
433 * @throws MWException
434 */
435 public function removeWatch( User $user, LinkTarget $target ) {
436 // Only logged in user can have a watchlist
437 if ( $this->loadBalancer->getReadOnlyReason() !== false || $user->isAnon() ) {
438 return false;
439 }
440
441 $this->uncache( $user, $target );
442
443 $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
444 $dbw->delete( 'watchlist',
445 [
446 'wl_user' => $user->getId(),
447 'wl_namespace' => $target->getNamespace(),
448 'wl_title' => $target->getDBkey(),
449 ], __METHOD__
450 );
451 $success = (bool)$dbw->affectedRows();
452 $this->loadBalancer->reuseConnection( $dbw );
453
454 return $success;
455 }
456
457 /**
458 * @param User $editor The editor that triggered the update. Their notification
459 * timestamp will not be updated(they have already seen it)
460 * @param LinkTarget $target The target to update timestamps for
461 * @param string $timestamp Set the update timestamp to this value
462 *
463 * @return int[] Array of user IDs the timestamp has been updated for
464 */
465 public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
466 $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
467 $res = $dbw->select( [ 'watchlist' ],
468 [ 'wl_user' ],
469 [
470 'wl_user != ' . intval( $editor->getId() ),
471 'wl_namespace' => $target->getNamespace(),
472 'wl_title' => $target->getDBkey(),
473 'wl_notificationtimestamp IS NULL',
474 ], __METHOD__
475 );
476
477 $watchers = [];
478 foreach ( $res as $row ) {
479 $watchers[] = intval( $row->wl_user );
480 }
481
482 if ( $watchers ) {
483 // Update wl_notificationtimestamp for all watching users except the editor
484 $fname = __METHOD__;
485 $dbw->onTransactionIdle(
486 function () use ( $dbw, $timestamp, $watchers, $target, $fname ) {
487 $dbw->update( 'watchlist',
488 [ /* SET */
489 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
490 ], [ /* WHERE */
491 'wl_user' => $watchers,
492 'wl_namespace' => $target->getNamespace(),
493 'wl_title' => $target->getDBkey(),
494 ], $fname
495 );
496 $this->uncacheLinkTarget( $target );
497 }
498 );
499 }
500
501 $this->loadBalancer->reuseConnection( $dbw );
502
503 return $watchers;
504 }
505
506 /**
507 * Reset the notification timestamp of this entry
508 *
509 * @param User $user
510 * @param Title $title
511 * @param string $force Whether to force the write query to be executed even if the
512 * page is not watched or the notification timestamp is already NULL.
513 * 'force' in order to force
514 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
515 *
516 * @return bool success
517 */
518 public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) {
519 // Only loggedin user can have a watchlist
520 if ( $this->loadBalancer->getReadOnlyReason() !== false || $user->isAnon() ) {
521 return false;
522 }
523
524 $item = null;
525 if ( $force != 'force' ) {
526 $item = $this->loadWatchedItem( $user, $title );
527 if ( !$item || $item->getNotificationTimestamp() === null ) {
528 return false;
529 }
530 }
531
532 // If the page is watched by the user (or may be watched), update the timestamp
533 $job = new ActivityUpdateJob(
534 $title,
535 [
536 'type' => 'updateWatchlistNotification',
537 'userid' => $user->getId(),
538 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
539 'curTime' => time()
540 ]
541 );
542
543 // Try to run this post-send
544 // Calls DeferredUpdates::addCallableUpdate in normal operation
545 call_user_func(
546 $this->deferredUpdatesAddCallableUpdateCallback,
547 function() use ( $job ) {
548 $job->run();
549 }
550 );
551
552 $this->uncache( $user, $title );
553
554 return true;
555 }
556
557 private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) {
558 if ( !$oldid ) {
559 // No oldid given, assuming latest revision; clear the timestamp.
560 return null;
561 }
562
563 if ( !$title->getNextRevisionID( $oldid ) ) {
564 // Oldid given and is the latest revision for this title; clear the timestamp.
565 return null;
566 }
567
568 if ( $item === null ) {
569 $item = $this->loadWatchedItem( $user, $title );
570 }
571
572 if ( !$item ) {
573 // This can only happen if $force is enabled.
574 return null;
575 }
576
577 // Oldid given and isn't the latest; update the timestamp.
578 // This will result in no further notification emails being sent!
579 // Calls Revision::getTimestampFromId in normal operation
580 $notificationTimestamp = call_user_func(
581 $this->revisionGetTimestampFromIdCallback,
582 $title,
583 $oldid
584 );
585
586 // We need to go one second to the future because of various strict comparisons
587 // throughout the codebase
588 $ts = new MWTimestamp( $notificationTimestamp );
589 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
590 $notificationTimestamp = $ts->getTimestamp( TS_MW );
591
592 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
593 if ( $force != 'force' ) {
594 return false;
595 } else {
596 // This is a little silly…
597 return $item->getNotificationTimestamp();
598 }
599 }
600
601 return $notificationTimestamp;
602 }
603
604 /**
605 * @param User $user
606 * @param int $unreadLimit
607 *
608 * @return int|bool The number of unread notifications
609 * true if greater than or equal to $unreadLimit
610 */
611 public function countUnreadNotifications( User $user, $unreadLimit = null ) {
612 $queryOptions = [];
613 if ( $unreadLimit !== null ) {
614 $unreadLimit = (int)$unreadLimit;
615 $queryOptions['LIMIT'] = $unreadLimit;
616 }
617
618 $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
619 $rowCount = $dbr->selectRowCount(
620 'watchlist',
621 '1',
622 [
623 'wl_user' => $user->getId(),
624 'wl_notificationtimestamp IS NOT NULL',
625 ],
626 __METHOD__,
627 $queryOptions
628 );
629 $this->loadBalancer->reuseConnection( $dbr );
630
631 if ( !isset( $unreadLimit ) ) {
632 return $rowCount;
633 }
634
635 if ( $rowCount >= $unreadLimit ) {
636 return true;
637 }
638
639 return $rowCount;
640 }
641
642 /**
643 * Check if the given title already is watched by the user, and if so
644 * add a watch for the new title.
645 *
646 * To be used for page renames and such.
647 *
648 * @param LinkTarget $oldTarget
649 * @param LinkTarget $newTarget
650 */
651 public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) {
652 if ( !$oldTarget instanceof Title ) {
653 $oldTarget = Title::newFromLinkTarget( $oldTarget );
654 }
655 if ( !$newTarget instanceof Title ) {
656 $newTarget = Title::newFromLinkTarget( $newTarget );
657 }
658
659 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
660 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
661 }
662
663 /**
664 * Check if the given title already is watched by the user, and if so
665 * add a watch for the new title.
666 *
667 * To be used for page renames and such.
668 * This must be called separately for Subject and Talk pages
669 *
670 * @param LinkTarget $oldTarget
671 * @param LinkTarget $newTarget
672 */
673 public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
674 $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
675
676 $result = $dbw->select(
677 'watchlist',
678 [ 'wl_user', 'wl_notificationtimestamp' ],
679 [
680 'wl_namespace' => $oldTarget->getNamespace(),
681 'wl_title' => $oldTarget->getDBkey(),
682 ],
683 __METHOD__,
684 [ 'FOR UPDATE' ]
685 );
686
687 $newNamespace = $newTarget->getNamespace();
688 $newDBkey = $newTarget->getDBkey();
689
690 # Construct array to replace into the watchlist
691 $values = [];
692 foreach ( $result as $row ) {
693 $values[] = [
694 'wl_user' => $row->wl_user,
695 'wl_namespace' => $newNamespace,
696 'wl_title' => $newDBkey,
697 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
698 ];
699 }
700
701 if ( !empty( $values ) ) {
702 # Perform replace
703 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
704 # some other DBMSes, mostly due to poor simulation by us
705 $dbw->replace(
706 'watchlist',
707 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
708 $values,
709 __METHOD__
710 );
711 }
712
713 $this->loadBalancer->reuseConnection( $dbw );
714 }
715
716 }