Merge "Improve cache assertions in WatchedItemStoreUnitTest"
[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 public function __construct( LoadBalancer $loadBalancer, HashBagOStuff $cache ) {
49 $this->loadBalancer = $loadBalancer;
50 $this->cache = $cache;
51 $this->deferredUpdatesAddCallableUpdateCallback = [ 'DeferredUpdates', 'addCallableUpdate' ];
52 $this->revisionGetTimestampFromIdCallback = [ 'Revision', 'getTimestampFromId' ];
53 }
54
55 /**
56 * Overrides the DeferredUpdates::addCallableUpdate callback
57 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
58 *
59 * @param callable $callback
60 * @see DeferredUpdates::addCallableUpdate for callback signiture
61 *
62 * @throws MWException
63 */
64 public function overrideDeferredUpdatesAddCallableUpdateCallback( $callback ) {
65 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
66 throw new MWException(
67 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
68 );
69 }
70 Assert::parameterType( 'callable', $callback, '$callback' );
71 $this->deferredUpdatesAddCallableUpdateCallback = $callback;
72 }
73
74 /**
75 * Overrides the Revision::getTimestampFromId callback
76 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
77 *
78 * @param callable $callback
79 * @see Revision::getTimestampFromId for callback signiture
80 *
81 * @throws MWException
82 */
83 public function overrideRevisionGetTimestampFromIdCallback( $callback ) {
84 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
85 throw new MWException(
86 'Cannot override Revision::getTimestampFromId callback in operation.'
87 );
88 }
89 Assert::parameterType( 'callable', $callback, '$callback' );
90 $this->revisionGetTimestampFromIdCallback = $callback;
91 }
92
93 /**
94 * Overrides the default instance of this class
95 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
96 *
97 * @param WatchedItemStore $store
98 *
99 * @throws MWException
100 */
101 public static function overrideDefaultInstance( WatchedItemStore $store ) {
102 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
103 throw new MWException(
104 'Cannot override ' . __CLASS__ . 'default instance in operation.'
105 );
106 }
107 self::$instance = $store;
108 }
109
110 /**
111 * @return self
112 */
113 public static function getDefaultInstance() {
114 if ( !self::$instance ) {
115 self::$instance = new self(
116 wfGetLB(),
117 new HashBagOStuff( [ 'maxKeys' => 100 ] )
118 );
119 }
120 return self::$instance;
121 }
122
123 private function getCacheKey( User $user, LinkTarget $target ) {
124 return $this->cache->makeKey(
125 (string)$target->getNamespace(),
126 $target->getDBkey(),
127 (string)$user->getId()
128 );
129 }
130
131 private function cache( WatchedItem $item ) {
132 $user = $item->getUser();
133 $target = $item->getLinkTarget();
134 $key = $this->getCacheKey( $user, $target );
135 $this->cache->set( $key, $item );
136 $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
137 }
138
139 private function uncache( User $user, LinkTarget $target ) {
140 $this->cache->delete( $this->getCacheKey( $user, $target ) );
141 unset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
142 }
143
144 private function uncacheLinkTarget( LinkTarget $target ) {
145 if ( !isset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] ) ) {
146 return;
147 }
148 foreach ( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] as $key ) {
149 $this->cache->delete( $key );
150 }
151 }
152
153 /**
154 * @param User $user
155 * @param LinkTarget $target
156 *
157 * @return WatchedItem|null
158 */
159 private function getCached( User $user, LinkTarget $target ) {
160 return $this->cache->get( $this->getCacheKey( $user, $target ) );
161 }
162
163 /**
164 * Return an array of conditions to select or update the appropriate database
165 * row.
166 *
167 * @param User $user
168 * @param LinkTarget $target
169 *
170 * @return array
171 */
172 private function dbCond( User $user, LinkTarget $target ) {
173 return [
174 'wl_user' => $user->getId(),
175 'wl_namespace' => $target->getNamespace(),
176 'wl_title' => $target->getDBkey(),
177 ];
178 }
179
180 /**
181 * Get an item (may be cached)
182 *
183 * @param User $user
184 * @param LinkTarget $target
185 *
186 * @return WatchedItem|false
187 */
188 public function getWatchedItem( User $user, LinkTarget $target ) {
189 if ( $user->isAnon() ) {
190 return false;
191 }
192
193 $cached = $this->getCached( $user, $target );
194 if ( $cached ) {
195 return $cached;
196 }
197 return $this->loadWatchedItem( $user, $target );
198 }
199
200 /**
201 * Loads an item from the db
202 *
203 * @param User $user
204 * @param LinkTarget $target
205 *
206 * @return WatchedItem|false
207 */
208 public function loadWatchedItem( User $user, LinkTarget $target ) {
209 // Only loggedin user can have a watchlist
210 if ( $user->isAnon() ) {
211 return false;
212 }
213
214 $dbr = $this->loadBalancer->getConnection( DB_SLAVE, [ 'watchlist' ] );
215 $row = $dbr->selectRow(
216 'watchlist',
217 'wl_notificationtimestamp',
218 $this->dbCond( $user, $target ),
219 __METHOD__
220 );
221 $this->loadBalancer->reuseConnection( $dbr );
222
223 if ( !$row ) {
224 return false;
225 }
226
227 $item = new WatchedItem(
228 $user,
229 $target,
230 $row->wl_notificationtimestamp
231 );
232 $this->cache( $item );
233
234 return $item;
235 }
236
237 /**
238 * Must be called separately for Subject & Talk namespaces
239 *
240 * @param User $user
241 * @param LinkTarget $target
242 *
243 * @return bool
244 */
245 public function isWatched( User $user, LinkTarget $target ) {
246 return (bool)$this->getWatchedItem( $user, $target );
247 }
248
249 /**
250 * Must be called separately for Subject & Talk namespaces
251 *
252 * @param User $user
253 * @param LinkTarget $target
254 */
255 public function addWatch( User $user, LinkTarget $target ) {
256 $this->addWatchBatch( [ [ $user, $target ] ] );
257 }
258
259 /**
260 * @param array[] $userTargetCombinations array of arrays containing [0] => User [1] => LinkTarget
261 *
262 * @return bool success
263 */
264 public function addWatchBatch( array $userTargetCombinations ) {
265 if ( $this->loadBalancer->getReadOnlyReason() !== false ) {
266 return false;
267 }
268
269 $rows = [];
270 foreach ( $userTargetCombinations as list( $user, $target ) ) {
271 /**
272 * @var User $user
273 * @var LinkTarget $target
274 */
275
276 // Only loggedin user can have a watchlist
277 if ( $user->isAnon() ) {
278 continue;
279 }
280 $rows[] = [
281 'wl_user' => $user->getId(),
282 'wl_namespace' => $target->getNamespace(),
283 'wl_title' => $target->getDBkey(),
284 'wl_notificationtimestamp' => null,
285 ];
286 $this->uncache( $user, $target );
287 }
288
289 if ( !$rows ) {
290 return false;
291 }
292
293 $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
294 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
295 // Use INSERT IGNORE to avoid overwriting the notification timestamp
296 // if there's already an entry for this page
297 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
298 }
299 $this->loadBalancer->reuseConnection( $dbw );
300
301 return true;
302 }
303
304 /**
305 * Removes the an entry for the User watching the LinkTarget
306 * Must be called separately for Subject & Talk namespaces
307 *
308 * @param User $user
309 * @param LinkTarget $target
310 *
311 * @return bool success
312 * @throws DBUnexpectedError
313 * @throws MWException
314 */
315 public function removeWatch( User $user, LinkTarget $target ) {
316 // Only logged in user can have a watchlist
317 if ( $this->loadBalancer->getReadOnlyReason() !== false || $user->isAnon() ) {
318 return false;
319 }
320
321 $this->uncache( $user, $target );
322
323 $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
324 $dbw->delete( 'watchlist',
325 [
326 'wl_user' => $user->getId(),
327 'wl_namespace' => $target->getNamespace(),
328 'wl_title' => $target->getDBkey(),
329 ], __METHOD__
330 );
331 $success = (bool)$dbw->affectedRows();
332 $this->loadBalancer->reuseConnection( $dbw );
333
334 return $success;
335 }
336
337 /**
338 * @param User $editor The editor that triggered the update. Their notification
339 * timestamp will not be updated(they have already seen it)
340 * @param LinkTarget $target The target to update timestamps for
341 * @param string $timestamp Set the update timestamp to this value
342 *
343 * @return int[] Array of user IDs the timestamp has been updated for
344 */
345 public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
346 $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
347 $res = $dbw->select( [ 'watchlist' ],
348 [ 'wl_user' ],
349 [
350 'wl_user != ' . intval( $editor->getId() ),
351 'wl_namespace' => $target->getNamespace(),
352 'wl_title' => $target->getDBkey(),
353 'wl_notificationtimestamp IS NULL',
354 ], __METHOD__
355 );
356
357 $watchers = [];
358 foreach ( $res as $row ) {
359 $watchers[] = intval( $row->wl_user );
360 }
361
362 if ( $watchers ) {
363 // Update wl_notificationtimestamp for all watching users except the editor
364 $fname = __METHOD__;
365 $dbw->onTransactionIdle(
366 function () use ( $dbw, $timestamp, $watchers, $target, $fname ) {
367 $dbw->update( 'watchlist',
368 [ /* SET */
369 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
370 ], [ /* WHERE */
371 'wl_user' => $watchers,
372 'wl_namespace' => $target->getNamespace(),
373 'wl_title' => $target->getDBkey(),
374 ], $fname
375 );
376 $this->uncacheLinkTarget( $target );
377 }
378 );
379 }
380
381 $this->loadBalancer->reuseConnection( $dbw );
382
383 return $watchers;
384 }
385
386 /**
387 * Reset the notification timestamp of this entry
388 *
389 * @param User $user
390 * @param Title $title
391 * @param string $force Whether to force the write query to be executed even if the
392 * page is not watched or the notification timestamp is already NULL.
393 * 'force' in order to force
394 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
395 *
396 * @return bool success
397 */
398 public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) {
399 // Only loggedin user can have a watchlist
400 if ( $this->loadBalancer->getReadOnlyReason() !== false || $user->isAnon() ) {
401 return false;
402 }
403
404 $item = null;
405 if ( $force != 'force' ) {
406 $item = $this->loadWatchedItem( $user, $title );
407 if ( !$item || $item->getNotificationTimestamp() === null ) {
408 return false;
409 }
410 }
411
412 // If the page is watched by the user (or may be watched), update the timestamp
413 $job = new ActivityUpdateJob(
414 $title,
415 [
416 'type' => 'updateWatchlistNotification',
417 'userid' => $user->getId(),
418 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
419 'curTime' => time()
420 ]
421 );
422
423 // Try to run this post-send
424 // Calls DeferredUpdates::addCallableUpdate in normal operation
425 call_user_func(
426 $this->deferredUpdatesAddCallableUpdateCallback,
427 function() use ( $job ) {
428 $job->run();
429 }
430 );
431
432 $this->uncache( $user, $title );
433
434 return true;
435 }
436
437 private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) {
438 if ( !$oldid ) {
439 // No oldid given, assuming latest revision; clear the timestamp.
440 return null;
441 }
442
443 if ( !$title->getNextRevisionID( $oldid ) ) {
444 // Oldid given and is the latest revision for this title; clear the timestamp.
445 return null;
446 }
447
448 if ( $item === null ) {
449 $item = $this->loadWatchedItem( $user, $title );
450 }
451
452 if ( !$item ) {
453 // This can only happen if $force is enabled.
454 return null;
455 }
456
457 // Oldid given and isn't the latest; update the timestamp.
458 // This will result in no further notification emails being sent!
459 // Calls Revision::getTimestampFromId in normal operation
460 $notificationTimestamp = call_user_func(
461 $this->revisionGetTimestampFromIdCallback,
462 $title,
463 $oldid
464 );
465
466 // We need to go one second to the future because of various strict comparisons
467 // throughout the codebase
468 $ts = new MWTimestamp( $notificationTimestamp );
469 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
470 $notificationTimestamp = $ts->getTimestamp( TS_MW );
471
472 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
473 if ( $force != 'force' ) {
474 return false;
475 } else {
476 // This is a little silly…
477 return $item->getNotificationTimestamp();
478 }
479 }
480
481 return $notificationTimestamp;
482 }
483
484 /**
485 * Check if the given title already is watched by the user, and if so
486 * add a watch for the new title.
487 *
488 * To be used for page renames and such.
489 *
490 * @param LinkTarget $oldTarget
491 * @param LinkTarget $newTarget
492 */
493 public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) {
494 if ( !$oldTarget instanceof Title ) {
495 $oldTarget = Title::newFromLinkTarget( $oldTarget );
496 }
497 if ( !$newTarget instanceof Title ) {
498 $newTarget = Title::newFromLinkTarget( $newTarget );
499 }
500
501 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
502 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
503 }
504
505 /**
506 * Check if the given title already is watched by the user, and if so
507 * add a watch for the new title.
508 *
509 * To be used for page renames and such.
510 * This must be called separately for Subject and Talk pages
511 *
512 * @param LinkTarget $oldTarget
513 * @param LinkTarget $newTarget
514 */
515 public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
516 $dbw = $this->loadBalancer->getConnection( DB_MASTER, [ 'watchlist' ] );
517
518 $result = $dbw->select(
519 'watchlist',
520 [ 'wl_user', 'wl_notificationtimestamp' ],
521 [
522 'wl_namespace' => $oldTarget->getNamespace(),
523 'wl_title' => $oldTarget->getDBkey(),
524 ],
525 __METHOD__,
526 [ 'FOR UPDATE' ]
527 );
528
529 $newNamespace = $newTarget->getNamespace();
530 $newDBkey = $newTarget->getDBkey();
531
532 # Construct array to replace into the watchlist
533 $values = [];
534 foreach ( $result as $row ) {
535 $values[] = [
536 'wl_user' => $row->wl_user,
537 'wl_namespace' => $newNamespace,
538 'wl_title' => $newDBkey,
539 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
540 ];
541 }
542
543 if ( !empty( $values ) ) {
544 # Perform replace
545 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
546 # some other DBMSes, mostly due to poor simulation by us
547 $dbw->replace(
548 'watchlist',
549 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
550 $values,
551 __METHOD__
552 );
553 }
554
555 $this->loadBalancer->reuseConnection( $dbw );
556 }
557
558 }