Merge "Fix comment handling on image upload or deletion"
[lhc/web/wiklou.git] / includes / watcheditem / WatchedItemStore.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
5 use MediaWiki\Linker\LinkTarget;
6 use Wikimedia\Assert\Assert;
7 use Wikimedia\Rdbms\LBFactory;
8 use Wikimedia\ScopedCallback;
9 use Wikimedia\Rdbms\ILBFactory;
10 use Wikimedia\Rdbms\LoadBalancer;
11
12 /**
13 * Storage layer class for WatchedItems.
14 * Database interaction & caching
15 * TODO caching should be factored out into a CachingWatchedItemStore class
16 *
17 * @author Addshore
18 * @since 1.27
19 */
20 class WatchedItemStore implements WatchedItemStoreInterface, StatsdAwareInterface {
21
22 /**
23 * @var ILBFactory
24 */
25 private $lbFactory;
26
27 /**
28 * @var LoadBalancer
29 */
30 private $loadBalancer;
31
32 /**
33 * @var ReadOnlyMode
34 */
35 private $readOnlyMode;
36
37 /**
38 * @var HashBagOStuff
39 */
40 private $cache;
41
42 /**
43 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
44 * The index is needed so that on mass changes all relevant items can be un-cached.
45 * For example: Clearing a users watchlist of all items or updating notification timestamps
46 * for all users watching a single target.
47 */
48 private $cacheIndex = [];
49
50 /**
51 * @var callable|null
52 */
53 private $deferredUpdatesAddCallableUpdateCallback;
54
55 /**
56 * @var callable|null
57 */
58 private $revisionGetTimestampFromIdCallback;
59
60 /**
61 * @var int
62 */
63 private $updateRowsPerQuery;
64
65 /**
66 * @var StatsdDataFactoryInterface
67 */
68 private $stats;
69
70 /**
71 * @param ILBFactory $lbFactory
72 * @param HashBagOStuff $cache
73 * @param ReadOnlyMode $readOnlyMode
74 * @param int $updateRowsPerQuery
75 */
76 public function __construct(
77 ILBFactory $lbFactory,
78 HashBagOStuff $cache,
79 ReadOnlyMode $readOnlyMode,
80 $updateRowsPerQuery
81 ) {
82 $this->lbFactory = $lbFactory;
83 $this->loadBalancer = $lbFactory->getMainLB();
84 $this->cache = $cache;
85 $this->readOnlyMode = $readOnlyMode;
86 $this->stats = new NullStatsdDataFactory();
87 $this->deferredUpdatesAddCallableUpdateCallback =
88 [ DeferredUpdates::class, 'addCallableUpdate' ];
89 $this->revisionGetTimestampFromIdCallback =
90 [ Revision::class, 'getTimestampFromId' ];
91 $this->updateRowsPerQuery = $updateRowsPerQuery;
92 }
93
94 /**
95 * @param StatsdDataFactoryInterface $stats
96 */
97 public function setStatsdDataFactory( StatsdDataFactoryInterface $stats ) {
98 $this->stats = $stats;
99 }
100
101 /**
102 * Overrides the DeferredUpdates::addCallableUpdate callback
103 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
104 *
105 * @param callable $callback
106 *
107 * @see DeferredUpdates::addCallableUpdate for callback signiture
108 *
109 * @return ScopedCallback to reset the overridden value
110 * @throws MWException
111 */
112 public function overrideDeferredUpdatesAddCallableUpdateCallback( callable $callback ) {
113 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
114 throw new MWException(
115 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
116 );
117 }
118 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback;
119 $this->deferredUpdatesAddCallableUpdateCallback = $callback;
120 return new ScopedCallback( function () use ( $previousValue ) {
121 $this->deferredUpdatesAddCallableUpdateCallback = $previousValue;
122 } );
123 }
124
125 /**
126 * Overrides the Revision::getTimestampFromId callback
127 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
128 *
129 * @param callable $callback
130 * @see Revision::getTimestampFromId for callback signiture
131 *
132 * @return ScopedCallback to reset the overridden value
133 * @throws MWException
134 */
135 public function overrideRevisionGetTimestampFromIdCallback( callable $callback ) {
136 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
137 throw new MWException(
138 'Cannot override Revision::getTimestampFromId callback in operation.'
139 );
140 }
141 $previousValue = $this->revisionGetTimestampFromIdCallback;
142 $this->revisionGetTimestampFromIdCallback = $callback;
143 return new ScopedCallback( function () use ( $previousValue ) {
144 $this->revisionGetTimestampFromIdCallback = $previousValue;
145 } );
146 }
147
148 private function getCacheKey( User $user, LinkTarget $target ) {
149 return $this->cache->makeKey(
150 (string)$target->getNamespace(),
151 $target->getDBkey(),
152 (string)$user->getId()
153 );
154 }
155
156 private function cache( WatchedItem $item ) {
157 $user = $item->getUser();
158 $target = $item->getLinkTarget();
159 $key = $this->getCacheKey( $user, $target );
160 $this->cache->set( $key, $item );
161 $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
162 $this->stats->increment( 'WatchedItemStore.cache' );
163 }
164
165 private function uncache( User $user, LinkTarget $target ) {
166 $this->cache->delete( $this->getCacheKey( $user, $target ) );
167 unset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
168 $this->stats->increment( 'WatchedItemStore.uncache' );
169 }
170
171 private function uncacheLinkTarget( LinkTarget $target ) {
172 $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget' );
173 if ( !isset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] ) ) {
174 return;
175 }
176 foreach ( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] as $key ) {
177 $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
178 $this->cache->delete( $key );
179 }
180 }
181
182 private function uncacheUser( User $user ) {
183 $this->stats->increment( 'WatchedItemStore.uncacheUser' );
184 foreach ( $this->cacheIndex as $ns => $dbKeyArray ) {
185 foreach ( $dbKeyArray as $dbKey => $userArray ) {
186 if ( isset( $userArray[$user->getId()] ) ) {
187 $this->stats->increment( 'WatchedItemStore.uncacheUser.items' );
188 $this->cache->delete( $userArray[$user->getId()] );
189 }
190 }
191 }
192 }
193
194 /**
195 * @param User $user
196 * @param LinkTarget $target
197 *
198 * @return WatchedItem|false
199 */
200 private function getCached( User $user, LinkTarget $target ) {
201 return $this->cache->get( $this->getCacheKey( $user, $target ) );
202 }
203
204 /**
205 * Return an array of conditions to select or update the appropriate database
206 * row.
207 *
208 * @param User $user
209 * @param LinkTarget $target
210 *
211 * @return array
212 */
213 private function dbCond( User $user, LinkTarget $target ) {
214 return [
215 'wl_user' => $user->getId(),
216 'wl_namespace' => $target->getNamespace(),
217 'wl_title' => $target->getDBkey(),
218 ];
219 }
220
221 /**
222 * @param int $dbIndex DB_MASTER or DB_REPLICA
223 *
224 * @return IDatabase
225 * @throws MWException
226 */
227 private function getConnectionRef( $dbIndex ) {
228 return $this->loadBalancer->getConnectionRef( $dbIndex, [ 'watchlist' ] );
229 }
230
231 /**
232 * Deletes ALL watched items for the given user when under
233 * $updateRowsPerQuery entries exist.
234 *
235 * @since 1.30
236 *
237 * @param User $user
238 *
239 * @return bool true on success, false when too many items are watched
240 */
241 public function clearUserWatchedItems( User $user ) {
242 if ( $this->countWatchedItems( $user ) > $this->updateRowsPerQuery ) {
243 return false;
244 }
245
246 $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
247 $dbw->delete(
248 'watchlist',
249 [ 'wl_user' => $user->getId() ],
250 __METHOD__
251 );
252 $this->uncacheAllItemsForUser( $user );
253
254 return true;
255 }
256
257 private function uncacheAllItemsForUser( User $user ) {
258 $userId = $user->getId();
259 foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
260 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
261 if ( array_key_exists( $userId, $userIndex ) ) {
262 $this->cache->delete( $userIndex[$userId] );
263 unset( $this->cacheIndex[$ns][$dbKey][$userId] );
264 }
265 }
266 }
267
268 // Cleanup empty cache keys
269 foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
270 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
271 if ( empty( $this->cacheIndex[$ns][$dbKey] ) ) {
272 unset( $this->cacheIndex[$ns][$dbKey] );
273 }
274 }
275 if ( empty( $this->cacheIndex[$ns] ) ) {
276 unset( $this->cacheIndex[$ns] );
277 }
278 }
279 }
280
281 /**
282 * Queues a job that will clear the users watchlist using the Job Queue.
283 *
284 * @since 1.31
285 *
286 * @param User $user
287 */
288 public function clearUserWatchedItemsUsingJobQueue( User $user ) {
289 $job = ClearUserWatchlistJob::newForUser( $user, $this->getMaxId() );
290 // TODO inject me.
291 JobQueueGroup::singleton()->push( $job );
292 }
293
294 /**
295 * @since 1.31
296 * @return int The maximum current wl_id
297 */
298 public function getMaxId() {
299 $dbr = $this->getConnectionRef( DB_REPLICA );
300 return (int)$dbr->selectField(
301 'watchlist',
302 'MAX(wl_id)',
303 '',
304 __METHOD__
305 );
306 }
307
308 /**
309 * @since 1.31
310 * @param User $user
311 * @return int
312 */
313 public function countWatchedItems( User $user ) {
314 $dbr = $this->getConnectionRef( DB_REPLICA );
315 $return = (int)$dbr->selectField(
316 'watchlist',
317 'COUNT(*)',
318 [
319 'wl_user' => $user->getId()
320 ],
321 __METHOD__
322 );
323
324 return $return;
325 }
326
327 /**
328 * @since 1.27
329 * @param LinkTarget $target
330 * @return int
331 */
332 public function countWatchers( LinkTarget $target ) {
333 $dbr = $this->getConnectionRef( DB_REPLICA );
334 $return = (int)$dbr->selectField(
335 'watchlist',
336 'COUNT(*)',
337 [
338 'wl_namespace' => $target->getNamespace(),
339 'wl_title' => $target->getDBkey(),
340 ],
341 __METHOD__
342 );
343
344 return $return;
345 }
346
347 /**
348 * @since 1.27
349 * @param LinkTarget $target
350 * @param string|int $threshold
351 * @return int
352 */
353 public function countVisitingWatchers( LinkTarget $target, $threshold ) {
354 $dbr = $this->getConnectionRef( DB_REPLICA );
355 $visitingWatchers = (int)$dbr->selectField(
356 'watchlist',
357 'COUNT(*)',
358 [
359 'wl_namespace' => $target->getNamespace(),
360 'wl_title' => $target->getDBkey(),
361 'wl_notificationtimestamp >= ' .
362 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
363 ' OR wl_notificationtimestamp IS NULL'
364 ],
365 __METHOD__
366 );
367
368 return $visitingWatchers;
369 }
370
371 /**
372 * @param User $user
373 * @param TitleValue[] $titles
374 * @return bool
375 * @throws MWException
376 */
377 public function removeWatchBatchForUser( User $user, array $titles ) {
378 if ( $this->readOnlyMode->isReadOnly() ) {
379 return false;
380 }
381 if ( $user->isAnon() ) {
382 return false;
383 }
384 if ( !$titles ) {
385 return true;
386 }
387
388 $rows = $this->getTitleDbKeysGroupedByNamespace( $titles );
389 $this->uncacheTitlesForUser( $user, $titles );
390
391 $dbw = $this->getConnectionRef( DB_MASTER );
392 $ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ );
393 $affectedRows = 0;
394
395 // Batch delete items per namespace.
396 foreach ( $rows as $namespace => $namespaceTitles ) {
397 $rowBatches = array_chunk( $namespaceTitles, $this->updateRowsPerQuery );
398 foreach ( $rowBatches as $toDelete ) {
399 $dbw->delete( 'watchlist', [
400 'wl_user' => $user->getId(),
401 'wl_namespace' => $namespace,
402 'wl_title' => $toDelete
403 ], __METHOD__ );
404 $affectedRows += $dbw->affectedRows();
405 $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
406 }
407 }
408
409 return (bool)$affectedRows;
410 }
411
412 /**
413 * @since 1.27
414 * @param LinkTarget[] $targets
415 * @param array $options
416 * @return array
417 */
418 public function countWatchersMultiple( array $targets, array $options = [] ) {
419 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
420
421 $dbr = $this->getConnectionRef( DB_REPLICA );
422
423 if ( array_key_exists( 'minimumWatchers', $options ) ) {
424 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
425 }
426
427 $lb = new LinkBatch( $targets );
428 $res = $dbr->select(
429 'watchlist',
430 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
431 [ $lb->constructSet( 'wl', $dbr ) ],
432 __METHOD__,
433 $dbOptions
434 );
435
436 $watchCounts = [];
437 foreach ( $targets as $linkTarget ) {
438 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
439 }
440
441 foreach ( $res as $row ) {
442 $watchCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
443 }
444
445 return $watchCounts;
446 }
447
448 /**
449 * @since 1.27
450 * @param array $targetsWithVisitThresholds
451 * @param int|null $minimumWatchers
452 * @return array
453 */
454 public function countVisitingWatchersMultiple(
455 array $targetsWithVisitThresholds,
456 $minimumWatchers = null
457 ) {
458 if ( $targetsWithVisitThresholds === [] ) {
459 // No titles requested => no results returned
460 return [];
461 }
462
463 $dbr = $this->getConnectionRef( DB_REPLICA );
464
465 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
466
467 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
468 if ( $minimumWatchers !== null ) {
469 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
470 }
471 $res = $dbr->select(
472 'watchlist',
473 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
474 $conds,
475 __METHOD__,
476 $dbOptions
477 );
478
479 $watcherCounts = [];
480 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
481 /* @var LinkTarget $target */
482 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
483 }
484
485 foreach ( $res as $row ) {
486 $watcherCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
487 }
488
489 return $watcherCounts;
490 }
491
492 /**
493 * Generates condition for the query used in a batch count visiting watchers.
494 *
495 * @param IDatabase $db
496 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
497 * @return string
498 */
499 private function getVisitingWatchersCondition(
500 IDatabase $db,
501 array $targetsWithVisitThresholds
502 ) {
503 $missingTargets = [];
504 $namespaceConds = [];
505 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
506 if ( $threshold === null ) {
507 $missingTargets[] = $target;
508 continue;
509 }
510 /* @var LinkTarget $target */
511 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
512 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
513 $db->makeList( [
514 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
515 'wl_notificationtimestamp IS NULL'
516 ], LIST_OR )
517 ], LIST_AND );
518 }
519
520 $conds = [];
521 foreach ( $namespaceConds as $namespace => $pageConds ) {
522 $conds[] = $db->makeList( [
523 'wl_namespace = ' . $namespace,
524 '(' . $db->makeList( $pageConds, LIST_OR ) . ')'
525 ], LIST_AND );
526 }
527
528 if ( $missingTargets ) {
529 $lb = new LinkBatch( $missingTargets );
530 $conds[] = $lb->constructSet( 'wl', $db );
531 }
532
533 return $db->makeList( $conds, LIST_OR );
534 }
535
536 /**
537 * @since 1.27
538 * @param User $user
539 * @param LinkTarget $target
540 * @return bool
541 */
542 public function getWatchedItem( User $user, LinkTarget $target ) {
543 if ( $user->isAnon() ) {
544 return false;
545 }
546
547 $cached = $this->getCached( $user, $target );
548 if ( $cached ) {
549 $this->stats->increment( 'WatchedItemStore.getWatchedItem.cached' );
550 return $cached;
551 }
552 $this->stats->increment( 'WatchedItemStore.getWatchedItem.load' );
553 return $this->loadWatchedItem( $user, $target );
554 }
555
556 /**
557 * @since 1.27
558 * @param User $user
559 * @param LinkTarget $target
560 * @return WatchedItem|bool
561 */
562 public function loadWatchedItem( User $user, LinkTarget $target ) {
563 // Only loggedin user can have a watchlist
564 if ( $user->isAnon() ) {
565 return false;
566 }
567
568 $dbr = $this->getConnectionRef( DB_REPLICA );
569 $row = $dbr->selectRow(
570 'watchlist',
571 'wl_notificationtimestamp',
572 $this->dbCond( $user, $target ),
573 __METHOD__
574 );
575
576 if ( !$row ) {
577 return false;
578 }
579
580 $item = new WatchedItem(
581 $user,
582 $target,
583 wfTimestampOrNull( TS_MW, $row->wl_notificationtimestamp )
584 );
585 $this->cache( $item );
586
587 return $item;
588 }
589
590 /**
591 * @since 1.27
592 * @param User $user
593 * @param array $options
594 * @return WatchedItem[]
595 */
596 public function getWatchedItemsForUser( User $user, array $options = [] ) {
597 $options += [ 'forWrite' => false ];
598
599 $dbOptions = [];
600 if ( array_key_exists( 'sort', $options ) ) {
601 Assert::parameter(
602 ( in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ) ),
603 '$options[\'sort\']',
604 'must be SORT_ASC or SORT_DESC'
605 );
606 $dbOptions['ORDER BY'] = [
607 "wl_namespace {$options['sort']}",
608 "wl_title {$options['sort']}"
609 ];
610 }
611 $db = $this->getConnectionRef( $options['forWrite'] ? DB_MASTER : DB_REPLICA );
612
613 $res = $db->select(
614 'watchlist',
615 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
616 [ 'wl_user' => $user->getId() ],
617 __METHOD__,
618 $dbOptions
619 );
620
621 $watchedItems = [];
622 foreach ( $res as $row ) {
623 // @todo: Should we add these to the process cache?
624 $watchedItems[] = new WatchedItem(
625 $user,
626 new TitleValue( (int)$row->wl_namespace, $row->wl_title ),
627 $row->wl_notificationtimestamp
628 );
629 }
630
631 return $watchedItems;
632 }
633
634 /**
635 * @since 1.27
636 * @param User $user
637 * @param LinkTarget $target
638 * @return bool
639 */
640 public function isWatched( User $user, LinkTarget $target ) {
641 return (bool)$this->getWatchedItem( $user, $target );
642 }
643
644 /**
645 * @since 1.27
646 * @param User $user
647 * @param LinkTarget[] $targets
648 * @return array
649 */
650 public function getNotificationTimestampsBatch( User $user, array $targets ) {
651 $timestamps = [];
652 foreach ( $targets as $target ) {
653 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
654 }
655
656 if ( $user->isAnon() ) {
657 return $timestamps;
658 }
659
660 $targetsToLoad = [];
661 foreach ( $targets as $target ) {
662 $cachedItem = $this->getCached( $user, $target );
663 if ( $cachedItem ) {
664 $timestamps[$target->getNamespace()][$target->getDBkey()] =
665 $cachedItem->getNotificationTimestamp();
666 } else {
667 $targetsToLoad[] = $target;
668 }
669 }
670
671 if ( !$targetsToLoad ) {
672 return $timestamps;
673 }
674
675 $dbr = $this->getConnectionRef( DB_REPLICA );
676
677 $lb = new LinkBatch( $targetsToLoad );
678 $res = $dbr->select(
679 'watchlist',
680 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
681 [
682 $lb->constructSet( 'wl', $dbr ),
683 'wl_user' => $user->getId(),
684 ],
685 __METHOD__
686 );
687
688 foreach ( $res as $row ) {
689 $timestamps[$row->wl_namespace][$row->wl_title] =
690 wfTimestampOrNull( TS_MW, $row->wl_notificationtimestamp );
691 }
692
693 return $timestamps;
694 }
695
696 /**
697 * @since 1.27
698 * @param User $user
699 * @param LinkTarget $target
700 * @throws MWException
701 */
702 public function addWatch( User $user, LinkTarget $target ) {
703 $this->addWatchBatchForUser( $user, [ $target ] );
704 }
705
706 /**
707 * @since 1.27
708 * @param User $user
709 * @param LinkTarget[] $targets
710 * @return bool
711 * @throws MWException
712 */
713 public function addWatchBatchForUser( User $user, array $targets ) {
714 if ( $this->readOnlyMode->isReadOnly() ) {
715 return false;
716 }
717 // Only loggedin user can have a watchlist
718 if ( $user->isAnon() ) {
719 return false;
720 }
721
722 if ( !$targets ) {
723 return true;
724 }
725
726 $rows = [];
727 $items = [];
728 foreach ( $targets as $target ) {
729 $rows[] = [
730 'wl_user' => $user->getId(),
731 'wl_namespace' => $target->getNamespace(),
732 'wl_title' => $target->getDBkey(),
733 'wl_notificationtimestamp' => null,
734 ];
735 $items[] = new WatchedItem(
736 $user,
737 $target,
738 null
739 );
740 $this->uncache( $user, $target );
741 }
742
743 $dbw = $this->getConnectionRef( DB_MASTER );
744 $ticket = $this->lbFactory->getEmptyTransactionTicket( __METHOD__ );
745 $affectedRows = 0;
746 $rowBatches = array_chunk( $rows, $this->updateRowsPerQuery );
747 foreach ( $rowBatches as $toInsert ) {
748 // Use INSERT IGNORE to avoid overwriting the notification timestamp
749 // if there's already an entry for this page
750 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
751 $affectedRows += $dbw->affectedRows();
752 $this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
753 }
754 // Update process cache to ensure skin doesn't claim that the current
755 // page is unwatched in the response of action=watch itself (T28292).
756 // This would otherwise be re-queried from a replica by isWatched().
757 foreach ( $items as $item ) {
758 $this->cache( $item );
759 }
760
761 return (bool)$affectedRows;
762 }
763
764 /**
765 * @since 1.27
766 * @param User $user
767 * @param LinkTarget $target
768 * @return bool
769 * @throws MWException
770 */
771 public function removeWatch( User $user, LinkTarget $target ) {
772 return $this->removeWatchBatchForUser( $user, [ $target ] );
773 }
774
775 /**
776 * @since 1.27
777 * @param User $user
778 * @param string|int $timestamp
779 * @param LinkTarget[] $targets
780 * @return bool
781 */
782 public function setNotificationTimestampsForUser( User $user, $timestamp, array $targets = [] ) {
783 // Only loggedin user can have a watchlist
784 if ( $user->isAnon() ) {
785 return false;
786 }
787
788 $dbw = $this->getConnectionRef( DB_MASTER );
789
790 $conds = [ 'wl_user' => $user->getId() ];
791 if ( $targets ) {
792 $batch = new LinkBatch( $targets );
793 $conds[] = $batch->constructSet( 'wl', $dbw );
794 }
795
796 if ( $timestamp !== null ) {
797 $timestamp = $dbw->timestamp( $timestamp );
798 }
799
800 $success = $dbw->update(
801 'watchlist',
802 [ 'wl_notificationtimestamp' => $timestamp ],
803 $conds,
804 __METHOD__
805 );
806
807 $this->uncacheUser( $user );
808
809 return $success;
810 }
811
812 public function resetAllNotificationTimestampsForUser( User $user ) {
813 // Only loggedin user can have a watchlist
814 if ( $user->isAnon() ) {
815 return;
816 }
817
818 // If the page is watched by the user (or may be watched), update the timestamp
819 $job = new ClearWatchlistNotificationsJob(
820 $user->getUserPage(),
821 [ 'userId' => $user->getId(), 'casTime' => time() ]
822 );
823
824 // Try to run this post-send
825 // Calls DeferredUpdates::addCallableUpdate in normal operation
826 call_user_func(
827 $this->deferredUpdatesAddCallableUpdateCallback,
828 function () use ( $job ) {
829 $job->run();
830 }
831 );
832 }
833
834 /**
835 * @since 1.27
836 * @param User $editor
837 * @param LinkTarget $target
838 * @param string|int $timestamp
839 * @return int[]
840 */
841 public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
842 $dbw = $this->getConnectionRef( DB_MASTER );
843 $uids = $dbw->selectFieldValues(
844 'watchlist',
845 'wl_user',
846 [
847 'wl_user != ' . intval( $editor->getId() ),
848 'wl_namespace' => $target->getNamespace(),
849 'wl_title' => $target->getDBkey(),
850 'wl_notificationtimestamp IS NULL',
851 ],
852 __METHOD__
853 );
854
855 $watchers = array_map( 'intval', $uids );
856 if ( $watchers ) {
857 // Update wl_notificationtimestamp for all watching users except the editor
858 $fname = __METHOD__;
859 DeferredUpdates::addCallableUpdate(
860 function () use ( $timestamp, $watchers, $target, $fname ) {
861 $dbw = $this->getConnectionRef( DB_MASTER );
862 $ticket = $this->lbFactory->getEmptyTransactionTicket( $fname );
863
864 $watchersChunks = array_chunk( $watchers, $this->updateRowsPerQuery );
865 foreach ( $watchersChunks as $watchersChunk ) {
866 $dbw->update( 'watchlist',
867 [ /* SET */
868 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
869 ], [ /* WHERE - TODO Use wl_id T130067 */
870 'wl_user' => $watchersChunk,
871 'wl_namespace' => $target->getNamespace(),
872 'wl_title' => $target->getDBkey(),
873 ], $fname
874 );
875 if ( count( $watchersChunks ) > 1 ) {
876 $this->lbFactory->commitAndWaitForReplication(
877 $fname, $ticket, [ 'domain' => $dbw->getDomainID() ]
878 );
879 }
880 }
881 $this->uncacheLinkTarget( $target );
882 },
883 DeferredUpdates::POSTSEND,
884 $dbw
885 );
886 }
887
888 return $watchers;
889 }
890
891 /**
892 * @since 1.27
893 * @param User $user
894 * @param Title $title
895 * @param string $force
896 * @param int $oldid
897 * @return bool
898 */
899 public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) {
900 // Only loggedin user can have a watchlist
901 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
902 return false;
903 }
904
905 $item = null;
906 if ( $force != 'force' ) {
907 $item = $this->loadWatchedItem( $user, $title );
908 if ( !$item || $item->getNotificationTimestamp() === null ) {
909 return false;
910 }
911 }
912
913 // If the page is watched by the user (or may be watched), update the timestamp
914 $job = new ActivityUpdateJob(
915 $title,
916 [
917 'type' => 'updateWatchlistNotification',
918 'userid' => $user->getId(),
919 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
920 'curTime' => time()
921 ]
922 );
923
924 // Try to run this post-send
925 // Calls DeferredUpdates::addCallableUpdate in normal operation
926 call_user_func(
927 $this->deferredUpdatesAddCallableUpdateCallback,
928 function () use ( $job ) {
929 $job->run();
930 }
931 );
932
933 $this->uncache( $user, $title );
934
935 return true;
936 }
937
938 private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) {
939 if ( !$oldid ) {
940 // No oldid given, assuming latest revision; clear the timestamp.
941 return null;
942 }
943
944 if ( !$title->getNextRevisionID( $oldid ) ) {
945 // Oldid given and is the latest revision for this title; clear the timestamp.
946 return null;
947 }
948
949 if ( $item === null ) {
950 $item = $this->loadWatchedItem( $user, $title );
951 }
952
953 if ( !$item ) {
954 // This can only happen if $force is enabled.
955 return null;
956 }
957
958 // Oldid given and isn't the latest; update the timestamp.
959 // This will result in no further notification emails being sent!
960 // Calls Revision::getTimestampFromId in normal operation
961 $notificationTimestamp = call_user_func(
962 $this->revisionGetTimestampFromIdCallback,
963 $title,
964 $oldid
965 );
966
967 // We need to go one second to the future because of various strict comparisons
968 // throughout the codebase
969 $ts = new MWTimestamp( $notificationTimestamp );
970 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
971 $notificationTimestamp = $ts->getTimestamp( TS_MW );
972
973 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
974 if ( $force != 'force' ) {
975 return false;
976 } else {
977 // This is a little silly…
978 return $item->getNotificationTimestamp();
979 }
980 }
981
982 return $notificationTimestamp;
983 }
984
985 /**
986 * @since 1.27
987 * @param User $user
988 * @param int|null $unreadLimit
989 * @return int|bool
990 */
991 public function countUnreadNotifications( User $user, $unreadLimit = null ) {
992 $queryOptions = [];
993 if ( $unreadLimit !== null ) {
994 $unreadLimit = (int)$unreadLimit;
995 $queryOptions['LIMIT'] = $unreadLimit;
996 }
997
998 $dbr = $this->getConnectionRef( DB_REPLICA );
999 $rowCount = $dbr->selectRowCount(
1000 'watchlist',
1001 '1',
1002 [
1003 'wl_user' => $user->getId(),
1004 'wl_notificationtimestamp IS NOT NULL',
1005 ],
1006 __METHOD__,
1007 $queryOptions
1008 );
1009
1010 if ( !isset( $unreadLimit ) ) {
1011 return $rowCount;
1012 }
1013
1014 if ( $rowCount >= $unreadLimit ) {
1015 return true;
1016 }
1017
1018 return $rowCount;
1019 }
1020
1021 /**
1022 * @since 1.27
1023 * @param LinkTarget $oldTarget
1024 * @param LinkTarget $newTarget
1025 */
1026 public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) {
1027 $oldTarget = Title::newFromLinkTarget( $oldTarget );
1028 $newTarget = Title::newFromLinkTarget( $newTarget );
1029
1030 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
1031 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
1032 }
1033
1034 /**
1035 * @since 1.27
1036 * @param LinkTarget $oldTarget
1037 * @param LinkTarget $newTarget
1038 */
1039 public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
1040 $dbw = $this->getConnectionRef( DB_MASTER );
1041
1042 $result = $dbw->select(
1043 'watchlist',
1044 [ 'wl_user', 'wl_notificationtimestamp' ],
1045 [
1046 'wl_namespace' => $oldTarget->getNamespace(),
1047 'wl_title' => $oldTarget->getDBkey(),
1048 ],
1049 __METHOD__,
1050 [ 'FOR UPDATE' ]
1051 );
1052
1053 $newNamespace = $newTarget->getNamespace();
1054 $newDBkey = $newTarget->getDBkey();
1055
1056 # Construct array to replace into the watchlist
1057 $values = [];
1058 foreach ( $result as $row ) {
1059 $values[] = [
1060 'wl_user' => $row->wl_user,
1061 'wl_namespace' => $newNamespace,
1062 'wl_title' => $newDBkey,
1063 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
1064 ];
1065 }
1066
1067 if ( !empty( $values ) ) {
1068 # Perform replace
1069 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
1070 # some other DBMSes, mostly due to poor simulation by us
1071 $dbw->replace(
1072 'watchlist',
1073 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
1074 $values,
1075 __METHOD__
1076 );
1077 }
1078 }
1079
1080 /**
1081 * @param TitleValue[] $titles
1082 * @return array
1083 */
1084 private function getTitleDbKeysGroupedByNamespace( array $titles ) {
1085 $rows = [];
1086 foreach ( $titles as $title ) {
1087 // Group titles by namespace.
1088 $rows[ $title->getNamespace() ][] = $title->getDBkey();
1089 }
1090 return $rows;
1091 }
1092
1093 /**
1094 * @param User $user
1095 * @param Title[] $titles
1096 */
1097 private function uncacheTitlesForUser( User $user, array $titles ) {
1098 foreach ( $titles as $title ) {
1099 $this->uncache( $user, $title );
1100 }
1101 }
1102
1103 }