Merge "WatchedItemStore add db connection helper methods"
[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 int $slaveOrMaster DB_MASTER or DB_SLAVE
211 *
212 * @return DatabaseBase
213 * @throws MWException
214 */
215 private function getConnection( $slaveOrMaster ) {
216 return $this->loadBalancer->getConnection( $slaveOrMaster, [ 'watchlist' ] );
217 }
218
219 /**
220 * @param DatabaseBase $connection
221 *
222 * @throws MWException
223 */
224 private function reuseConnection( $connection ) {
225 $this->loadBalancer->reuseConnection( $connection );
226 }
227
228 /**
229 * @param LinkTarget $target
230 *
231 * @return int
232 */
233 public function countWatchers( LinkTarget $target ) {
234 $dbr = $this->getConnection( DB_SLAVE );
235 $return = (int)$dbr->selectField(
236 'watchlist',
237 'COUNT(*)',
238 [
239 'wl_namespace' => $target->getNamespace(),
240 'wl_title' => $target->getDBkey(),
241 ],
242 __METHOD__
243 );
244 $this->reuseConnection( $dbr );
245
246 return $return;
247 }
248
249 /**
250 * Number of page watchers who also visited a "recent" edit
251 *
252 * @param LinkTarget $target
253 * @param mixed $threshold timestamp accepted by wfTimestamp
254 *
255 * @return int
256 * @throws DBUnexpectedError
257 * @throws MWException
258 */
259 public function countVisitingWatchers( LinkTarget $target, $threshold ) {
260 $dbr = $this->getConnection( DB_SLAVE );
261 $visitingWatchers = (int)$dbr->selectField(
262 'watchlist',
263 'COUNT(*)',
264 [
265 'wl_namespace' => $target->getNamespace(),
266 'wl_title' => $target->getDBkey(),
267 'wl_notificationtimestamp >= ' .
268 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
269 ' OR wl_notificationtimestamp IS NULL'
270 ],
271 __METHOD__
272 );
273 $this->reuseConnection( $dbr );
274
275 return $visitingWatchers;
276 }
277
278 /**
279 * @param LinkTarget[] $targets
280 * @param array $options Allowed keys:
281 * 'minimumWatchers' => int
282 *
283 * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers
284 * All targets will be present in the result. 0 either means no watchers or the number
285 * of watchers was below the minimumWatchers option if passed.
286 */
287 public function countWatchersMultiple( array $targets, array $options = [] ) {
288 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
289
290 $dbr = $this->getConnection( DB_SLAVE );
291
292 if ( array_key_exists( 'minimumWatchers', $options ) ) {
293 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
294 }
295
296 $lb = new LinkBatch( $targets );
297 $res = $dbr->select(
298 'watchlist',
299 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
300 [ $lb->constructSet( 'wl', $dbr ) ],
301 __METHOD__,
302 $dbOptions
303 );
304
305 $this->reuseConnection( $dbr );
306
307 $watchCounts = [];
308 foreach ( $targets as $linkTarget ) {
309 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
310 }
311
312 foreach ( $res as $row ) {
313 $watchCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
314 }
315
316 return $watchCounts;
317 }
318
319 /**
320 * Get an item (may be cached)
321 *
322 * @param User $user
323 * @param LinkTarget $target
324 *
325 * @return WatchedItem|false
326 */
327 public function getWatchedItem( User $user, LinkTarget $target ) {
328 if ( $user->isAnon() ) {
329 return false;
330 }
331
332 $cached = $this->getCached( $user, $target );
333 if ( $cached ) {
334 return $cached;
335 }
336 return $this->loadWatchedItem( $user, $target );
337 }
338
339 /**
340 * Loads an item from the db
341 *
342 * @param User $user
343 * @param LinkTarget $target
344 *
345 * @return WatchedItem|false
346 */
347 public function loadWatchedItem( User $user, LinkTarget $target ) {
348 // Only loggedin user can have a watchlist
349 if ( $user->isAnon() ) {
350 return false;
351 }
352
353 $dbr = $this->getConnection( DB_SLAVE );
354 $row = $dbr->selectRow(
355 'watchlist',
356 'wl_notificationtimestamp',
357 $this->dbCond( $user, $target ),
358 __METHOD__
359 );
360 $this->reuseConnection( $dbr );
361
362 if ( !$row ) {
363 return false;
364 }
365
366 $item = new WatchedItem(
367 $user,
368 $target,
369 $row->wl_notificationtimestamp
370 );
371 $this->cache( $item );
372
373 return $item;
374 }
375
376 /**
377 * Must be called separately for Subject & Talk namespaces
378 *
379 * @param User $user
380 * @param LinkTarget $target
381 *
382 * @return bool
383 */
384 public function isWatched( User $user, LinkTarget $target ) {
385 return (bool)$this->getWatchedItem( $user, $target );
386 }
387
388 /**
389 * Must be called separately for Subject & Talk namespaces
390 *
391 * @param User $user
392 * @param LinkTarget $target
393 */
394 public function addWatch( User $user, LinkTarget $target ) {
395 $this->addWatchBatch( [ [ $user, $target ] ] );
396 }
397
398 /**
399 * @param array[] $userTargetCombinations array of arrays containing [0] => User [1] => LinkTarget
400 *
401 * @return bool success
402 */
403 public function addWatchBatch( array $userTargetCombinations ) {
404 if ( $this->loadBalancer->getReadOnlyReason() !== false ) {
405 return false;
406 }
407
408 $rows = [];
409 foreach ( $userTargetCombinations as list( $user, $target ) ) {
410 /**
411 * @var User $user
412 * @var LinkTarget $target
413 */
414
415 // Only loggedin user can have a watchlist
416 if ( $user->isAnon() ) {
417 continue;
418 }
419 $rows[] = [
420 'wl_user' => $user->getId(),
421 'wl_namespace' => $target->getNamespace(),
422 'wl_title' => $target->getDBkey(),
423 'wl_notificationtimestamp' => null,
424 ];
425 $this->uncache( $user, $target );
426 }
427
428 if ( !$rows ) {
429 return false;
430 }
431
432 $dbw = $this->getConnection( DB_MASTER );
433 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
434 // Use INSERT IGNORE to avoid overwriting the notification timestamp
435 // if there's already an entry for this page
436 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
437 }
438 $this->reuseConnection( $dbw );
439
440 return true;
441 }
442
443 /**
444 * Removes the an entry for the User watching the LinkTarget
445 * Must be called separately for Subject & Talk namespaces
446 *
447 * @param User $user
448 * @param LinkTarget $target
449 *
450 * @return bool success
451 * @throws DBUnexpectedError
452 * @throws MWException
453 */
454 public function removeWatch( User $user, LinkTarget $target ) {
455 // Only logged in user can have a watchlist
456 if ( $this->loadBalancer->getReadOnlyReason() !== false || $user->isAnon() ) {
457 return false;
458 }
459
460 $this->uncache( $user, $target );
461
462 $dbw = $this->getConnection( DB_MASTER );
463 $dbw->delete( 'watchlist',
464 [
465 'wl_user' => $user->getId(),
466 'wl_namespace' => $target->getNamespace(),
467 'wl_title' => $target->getDBkey(),
468 ], __METHOD__
469 );
470 $success = (bool)$dbw->affectedRows();
471 $this->reuseConnection( $dbw );
472
473 return $success;
474 }
475
476 /**
477 * @param User $editor The editor that triggered the update. Their notification
478 * timestamp will not be updated(they have already seen it)
479 * @param LinkTarget $target The target to update timestamps for
480 * @param string $timestamp Set the update timestamp to this value
481 *
482 * @return int[] Array of user IDs the timestamp has been updated for
483 */
484 public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
485 $dbw = $this->getConnection( DB_MASTER );
486 $res = $dbw->select( [ 'watchlist' ],
487 [ 'wl_user' ],
488 [
489 'wl_user != ' . intval( $editor->getId() ),
490 'wl_namespace' => $target->getNamespace(),
491 'wl_title' => $target->getDBkey(),
492 'wl_notificationtimestamp IS NULL',
493 ], __METHOD__
494 );
495
496 $watchers = [];
497 foreach ( $res as $row ) {
498 $watchers[] = intval( $row->wl_user );
499 }
500
501 if ( $watchers ) {
502 // Update wl_notificationtimestamp for all watching users except the editor
503 $fname = __METHOD__;
504 $dbw->onTransactionIdle(
505 function () use ( $dbw, $timestamp, $watchers, $target, $fname ) {
506 $dbw->update( 'watchlist',
507 [ /* SET */
508 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
509 ], [ /* WHERE */
510 'wl_user' => $watchers,
511 'wl_namespace' => $target->getNamespace(),
512 'wl_title' => $target->getDBkey(),
513 ], $fname
514 );
515 $this->uncacheLinkTarget( $target );
516 }
517 );
518 }
519
520 $this->reuseConnection( $dbw );
521
522 return $watchers;
523 }
524
525 /**
526 * Reset the notification timestamp of this entry
527 *
528 * @param User $user
529 * @param Title $title
530 * @param string $force Whether to force the write query to be executed even if the
531 * page is not watched or the notification timestamp is already NULL.
532 * 'force' in order to force
533 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
534 *
535 * @return bool success
536 */
537 public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) {
538 // Only loggedin user can have a watchlist
539 if ( $this->loadBalancer->getReadOnlyReason() !== false || $user->isAnon() ) {
540 return false;
541 }
542
543 $item = null;
544 if ( $force != 'force' ) {
545 $item = $this->loadWatchedItem( $user, $title );
546 if ( !$item || $item->getNotificationTimestamp() === null ) {
547 return false;
548 }
549 }
550
551 // If the page is watched by the user (or may be watched), update the timestamp
552 $job = new ActivityUpdateJob(
553 $title,
554 [
555 'type' => 'updateWatchlistNotification',
556 'userid' => $user->getId(),
557 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
558 'curTime' => time()
559 ]
560 );
561
562 // Try to run this post-send
563 // Calls DeferredUpdates::addCallableUpdate in normal operation
564 call_user_func(
565 $this->deferredUpdatesAddCallableUpdateCallback,
566 function() use ( $job ) {
567 $job->run();
568 }
569 );
570
571 $this->uncache( $user, $title );
572
573 return true;
574 }
575
576 private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) {
577 if ( !$oldid ) {
578 // No oldid given, assuming latest revision; clear the timestamp.
579 return null;
580 }
581
582 if ( !$title->getNextRevisionID( $oldid ) ) {
583 // Oldid given and is the latest revision for this title; clear the timestamp.
584 return null;
585 }
586
587 if ( $item === null ) {
588 $item = $this->loadWatchedItem( $user, $title );
589 }
590
591 if ( !$item ) {
592 // This can only happen if $force is enabled.
593 return null;
594 }
595
596 // Oldid given and isn't the latest; update the timestamp.
597 // This will result in no further notification emails being sent!
598 // Calls Revision::getTimestampFromId in normal operation
599 $notificationTimestamp = call_user_func(
600 $this->revisionGetTimestampFromIdCallback,
601 $title,
602 $oldid
603 );
604
605 // We need to go one second to the future because of various strict comparisons
606 // throughout the codebase
607 $ts = new MWTimestamp( $notificationTimestamp );
608 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
609 $notificationTimestamp = $ts->getTimestamp( TS_MW );
610
611 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
612 if ( $force != 'force' ) {
613 return false;
614 } else {
615 // This is a little silly…
616 return $item->getNotificationTimestamp();
617 }
618 }
619
620 return $notificationTimestamp;
621 }
622
623 /**
624 * @param User $user
625 * @param int $unreadLimit
626 *
627 * @return int|bool The number of unread notifications
628 * true if greater than or equal to $unreadLimit
629 */
630 public function countUnreadNotifications( User $user, $unreadLimit = null ) {
631 $queryOptions = [];
632 if ( $unreadLimit !== null ) {
633 $unreadLimit = (int)$unreadLimit;
634 $queryOptions['LIMIT'] = $unreadLimit;
635 }
636
637 $dbr = $this->getConnection( DB_SLAVE );
638 $rowCount = $dbr->selectRowCount(
639 'watchlist',
640 '1',
641 [
642 'wl_user' => $user->getId(),
643 'wl_notificationtimestamp IS NOT NULL',
644 ],
645 __METHOD__,
646 $queryOptions
647 );
648 $this->reuseConnection( $dbr );
649
650 if ( !isset( $unreadLimit ) ) {
651 return $rowCount;
652 }
653
654 if ( $rowCount >= $unreadLimit ) {
655 return true;
656 }
657
658 return $rowCount;
659 }
660
661 /**
662 * Check if the given title already is watched by the user, and if so
663 * add a watch for the new title.
664 *
665 * To be used for page renames and such.
666 *
667 * @param LinkTarget $oldTarget
668 * @param LinkTarget $newTarget
669 */
670 public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) {
671 if ( !$oldTarget instanceof Title ) {
672 $oldTarget = Title::newFromLinkTarget( $oldTarget );
673 }
674 if ( !$newTarget instanceof Title ) {
675 $newTarget = Title::newFromLinkTarget( $newTarget );
676 }
677
678 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
679 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
680 }
681
682 /**
683 * Check if the given title already is watched by the user, and if so
684 * add a watch for the new title.
685 *
686 * To be used for page renames and such.
687 * This must be called separately for Subject and Talk pages
688 *
689 * @param LinkTarget $oldTarget
690 * @param LinkTarget $newTarget
691 */
692 public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
693 $dbw = $this->getConnection( DB_MASTER );
694
695 $result = $dbw->select(
696 'watchlist',
697 [ 'wl_user', 'wl_notificationtimestamp' ],
698 [
699 'wl_namespace' => $oldTarget->getNamespace(),
700 'wl_title' => $oldTarget->getDBkey(),
701 ],
702 __METHOD__,
703 [ 'FOR UPDATE' ]
704 );
705
706 $newNamespace = $newTarget->getNamespace();
707 $newDBkey = $newTarget->getDBkey();
708
709 # Construct array to replace into the watchlist
710 $values = [];
711 foreach ( $result as $row ) {
712 $values[] = [
713 'wl_user' => $row->wl_user,
714 'wl_namespace' => $newNamespace,
715 'wl_title' => $newDBkey,
716 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
717 ];
718 }
719
720 if ( !empty( $values ) ) {
721 # Perform replace
722 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
723 # some other DBMSes, mostly due to poor simulation by us
724 $dbw->replace(
725 'watchlist',
726 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
727 $values,
728 __METHOD__
729 );
730 }
731
732 $this->reuseConnection( $dbw );
733 }
734
735 }