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