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