MediaWiki REL1_31
WatchedItemStore.php
Go to the documentation of this file.
1<?php
2
4use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
7use Wikimedia\Assert\Assert;
8use Wikimedia\ScopedCallback;
10
20
25
30
34 private $cache;
35
42 private $cacheIndex = [];
43
48
53
58
62 private $stats;
63
70 public function __construct(
75 ) {
76 $this->loadBalancer = $loadBalancer;
77 $this->cache = $cache;
78 $this->readOnlyMode = $readOnlyMode;
79 $this->stats = new NullStatsdDataFactory();
80 $this->deferredUpdatesAddCallableUpdateCallback =
81 [ DeferredUpdates::class, 'addCallableUpdate' ];
82 $this->revisionGetTimestampFromIdCallback =
83 [ Revision::class, 'getTimestampFromId' ];
84 $this->updateRowsPerQuery = $updateRowsPerQuery;
85 }
86
90 public function setStatsdDataFactory( StatsdDataFactoryInterface $stats ) {
91 $this->stats = $stats;
92 }
93
105 public function overrideDeferredUpdatesAddCallableUpdateCallback( callable $callback ) {
106 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
107 throw new MWException(
108 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
109 );
110 }
112 $this->deferredUpdatesAddCallableUpdateCallback = $callback;
113 return new ScopedCallback( function () use ( $previousValue ) {
114 $this->deferredUpdatesAddCallableUpdateCallback = $previousValue;
115 } );
116 }
117
128 public function overrideRevisionGetTimestampFromIdCallback( callable $callback ) {
129 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
130 throw new MWException(
131 'Cannot override Revision::getTimestampFromId callback in operation.'
132 );
133 }
135 $this->revisionGetTimestampFromIdCallback = $callback;
136 return new ScopedCallback( function () use ( $previousValue ) {
137 $this->revisionGetTimestampFromIdCallback = $previousValue;
138 } );
139 }
140
141 private function getCacheKey( User $user, LinkTarget $target ) {
142 return $this->cache->makeKey(
143 (string)$target->getNamespace(),
144 $target->getDBkey(),
145 (string)$user->getId()
146 );
147 }
148
149 private function cache( WatchedItem $item ) {
150 $user = $item->getUser();
151 $target = $item->getLinkTarget();
152 $key = $this->getCacheKey( $user, $target );
153 $this->cache->set( $key, $item );
154 $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
155 $this->stats->increment( 'WatchedItemStore.cache' );
156 }
157
158 private function uncache( User $user, LinkTarget $target ) {
159 $this->cache->delete( $this->getCacheKey( $user, $target ) );
160 unset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
161 $this->stats->increment( 'WatchedItemStore.uncache' );
162 }
163
164 private function uncacheLinkTarget( LinkTarget $target ) {
165 $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget' );
166 if ( !isset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] ) ) {
167 return;
168 }
169 foreach ( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] as $key ) {
170 $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
171 $this->cache->delete( $key );
172 }
173 }
174
175 private function uncacheUser( User $user ) {
176 $this->stats->increment( 'WatchedItemStore.uncacheUser' );
177 foreach ( $this->cacheIndex as $ns => $dbKeyArray ) {
178 foreach ( $dbKeyArray as $dbKey => $userArray ) {
179 if ( isset( $userArray[$user->getId()] ) ) {
180 $this->stats->increment( 'WatchedItemStore.uncacheUser.items' );
181 $this->cache->delete( $userArray[$user->getId()] );
182 }
183 }
184 }
185 }
186
193 private function getCached( User $user, LinkTarget $target ) {
194 return $this->cache->get( $this->getCacheKey( $user, $target ) );
195 }
196
206 private function dbCond( User $user, LinkTarget $target ) {
207 return [
208 'wl_user' => $user->getId(),
209 'wl_namespace' => $target->getNamespace(),
210 'wl_title' => $target->getDBkey(),
211 ];
212 }
213
220 private function getConnectionRef( $dbIndex ) {
221 return $this->loadBalancer->getConnectionRef( $dbIndex, [ 'watchlist' ] );
222 }
223
234 public function clearUserWatchedItems( User $user ) {
235 if ( $this->countWatchedItems( $user ) > $this->updateRowsPerQuery ) {
236 return false;
237 }
238
239 $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
240 $dbw->delete(
241 'watchlist',
242 [ 'wl_user' => $user->getId() ],
243 __METHOD__
244 );
245 $this->uncacheAllItemsForUser( $user );
246
247 return true;
248 }
249
250 private function uncacheAllItemsForUser( User $user ) {
251 $userId = $user->getId();
252 foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
253 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
254 if ( array_key_exists( $userId, $userIndex ) ) {
255 $this->cache->delete( $userIndex[$userId] );
256 unset( $this->cacheIndex[$ns][$dbKey][$userId] );
257 }
258 }
259 }
260
261 // Cleanup empty cache keys
262 foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
263 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
264 if ( empty( $this->cacheIndex[$ns][$dbKey] ) ) {
265 unset( $this->cacheIndex[$ns][$dbKey] );
266 }
267 }
268 if ( empty( $this->cacheIndex[$ns] ) ) {
269 unset( $this->cacheIndex[$ns] );
270 }
271 }
272 }
273
281 public function clearUserWatchedItemsUsingJobQueue( User $user ) {
283 // TODO inject me.
285 }
286
291 public function getMaxId() {
292 $dbr = $this->getConnectionRef( DB_REPLICA );
293 return (int)$dbr->selectField(
294 'watchlist',
295 'MAX(wl_id)',
296 '',
297 __METHOD__
298 );
299 }
300
306 public function countWatchedItems( User $user ) {
307 $dbr = $this->getConnectionRef( DB_REPLICA );
308 $return = (int)$dbr->selectField(
309 'watchlist',
310 'COUNT(*)',
311 [
312 'wl_user' => $user->getId()
313 ],
314 __METHOD__
315 );
316
317 return $return;
318 }
319
325 public function countWatchers( LinkTarget $target ) {
326 $dbr = $this->getConnectionRef( DB_REPLICA );
327 $return = (int)$dbr->selectField(
328 'watchlist',
329 'COUNT(*)',
330 [
331 'wl_namespace' => $target->getNamespace(),
332 'wl_title' => $target->getDBkey(),
333 ],
334 __METHOD__
335 );
336
337 return $return;
338 }
339
346 public function countVisitingWatchers( LinkTarget $target, $threshold ) {
347 $dbr = $this->getConnectionRef( DB_REPLICA );
348 $visitingWatchers = (int)$dbr->selectField(
349 'watchlist',
350 'COUNT(*)',
351 [
352 'wl_namespace' => $target->getNamespace(),
353 'wl_title' => $target->getDBkey(),
354 'wl_notificationtimestamp >= ' .
355 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
356 ' OR wl_notificationtimestamp IS NULL'
357 ],
358 __METHOD__
359 );
360
361 return $visitingWatchers;
362 }
363
370 public function countWatchersMultiple( array $targets, array $options = [] ) {
371 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
372
373 $dbr = $this->getConnectionRef( DB_REPLICA );
374
375 if ( array_key_exists( 'minimumWatchers', $options ) ) {
376 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
377 }
378
379 $lb = new LinkBatch( $targets );
380 $res = $dbr->select(
381 'watchlist',
382 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
383 [ $lb->constructSet( 'wl', $dbr ) ],
384 __METHOD__,
385 $dbOptions
386 );
387
388 $watchCounts = [];
389 foreach ( $targets as $linkTarget ) {
390 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
391 }
392
393 foreach ( $res as $row ) {
394 $watchCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
395 }
396
397 return $watchCounts;
398 }
399
407 array $targetsWithVisitThresholds,
408 $minimumWatchers = null
409 ) {
410 if ( $targetsWithVisitThresholds === [] ) {
411 // No titles requested => no results returned
412 return [];
413 }
414
415 $dbr = $this->getConnectionRef( DB_REPLICA );
416
417 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
418
419 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
420 if ( $minimumWatchers !== null ) {
421 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
422 }
423 $res = $dbr->select(
424 'watchlist',
425 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
426 $conds,
427 __METHOD__,
428 $dbOptions
429 );
430
431 $watcherCounts = [];
432 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
433 /* @var LinkTarget $target */
434 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
435 }
436
437 foreach ( $res as $row ) {
438 $watcherCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
439 }
440
441 return $watcherCounts;
442 }
443
452 IDatabase $db,
453 array $targetsWithVisitThresholds
454 ) {
455 $missingTargets = [];
456 $namespaceConds = [];
457 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
458 if ( $threshold === null ) {
459 $missingTargets[] = $target;
460 continue;
461 }
462 /* @var LinkTarget $target */
463 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
464 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
465 $db->makeList( [
466 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
467 'wl_notificationtimestamp IS NULL'
468 ], LIST_OR )
469 ], LIST_AND );
470 }
471
472 $conds = [];
473 foreach ( $namespaceConds as $namespace => $pageConds ) {
474 $conds[] = $db->makeList( [
475 'wl_namespace = ' . $namespace,
476 '(' . $db->makeList( $pageConds, LIST_OR ) . ')'
477 ], LIST_AND );
478 }
479
480 if ( $missingTargets ) {
481 $lb = new LinkBatch( $missingTargets );
482 $conds[] = $lb->constructSet( 'wl', $db );
483 }
484
485 return $db->makeList( $conds, LIST_OR );
486 }
487
494 public function getWatchedItem( User $user, LinkTarget $target ) {
495 if ( $user->isAnon() ) {
496 return false;
497 }
498
499 $cached = $this->getCached( $user, $target );
500 if ( $cached ) {
501 $this->stats->increment( 'WatchedItemStore.getWatchedItem.cached' );
502 return $cached;
503 }
504 $this->stats->increment( 'WatchedItemStore.getWatchedItem.load' );
505 return $this->loadWatchedItem( $user, $target );
506 }
507
514 public function loadWatchedItem( User $user, LinkTarget $target ) {
515 // Only loggedin user can have a watchlist
516 if ( $user->isAnon() ) {
517 return false;
518 }
519
520 $dbr = $this->getConnectionRef( DB_REPLICA );
521 $row = $dbr->selectRow(
522 'watchlist',
523 'wl_notificationtimestamp',
524 $this->dbCond( $user, $target ),
525 __METHOD__
526 );
527
528 if ( !$row ) {
529 return false;
530 }
531
532 $item = new WatchedItem(
533 $user,
534 $target,
535 wfTimestampOrNull( TS_MW, $row->wl_notificationtimestamp )
536 );
537 $this->cache( $item );
538
539 return $item;
540 }
541
548 public function getWatchedItemsForUser( User $user, array $options = [] ) {
549 $options += [ 'forWrite' => false ];
550
551 $dbOptions = [];
552 if ( array_key_exists( 'sort', $options ) ) {
553 Assert::parameter(
554 ( in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ) ),
555 '$options[\'sort\']',
556 'must be SORT_ASC or SORT_DESC'
557 );
558 $dbOptions['ORDER BY'] = [
559 "wl_namespace {$options['sort']}",
560 "wl_title {$options['sort']}"
561 ];
562 }
563 $db = $this->getConnectionRef( $options['forWrite'] ? DB_MASTER : DB_REPLICA );
564
565 $res = $db->select(
566 'watchlist',
567 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
568 [ 'wl_user' => $user->getId() ],
569 __METHOD__,
570 $dbOptions
571 );
572
573 $watchedItems = [];
574 foreach ( $res as $row ) {
575 // @todo: Should we add these to the process cache?
576 $watchedItems[] = new WatchedItem(
577 $user,
578 new TitleValue( (int)$row->wl_namespace, $row->wl_title ),
579 $row->wl_notificationtimestamp
580 );
581 }
582
583 return $watchedItems;
584 }
585
592 public function isWatched( User $user, LinkTarget $target ) {
593 return (bool)$this->getWatchedItem( $user, $target );
594 }
595
602 public function getNotificationTimestampsBatch( User $user, array $targets ) {
603 $timestamps = [];
604 foreach ( $targets as $target ) {
605 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
606 }
607
608 if ( $user->isAnon() ) {
609 return $timestamps;
610 }
611
612 $targetsToLoad = [];
613 foreach ( $targets as $target ) {
614 $cachedItem = $this->getCached( $user, $target );
615 if ( $cachedItem ) {
616 $timestamps[$target->getNamespace()][$target->getDBkey()] =
617 $cachedItem->getNotificationTimestamp();
618 } else {
619 $targetsToLoad[] = $target;
620 }
621 }
622
623 if ( !$targetsToLoad ) {
624 return $timestamps;
625 }
626
627 $dbr = $this->getConnectionRef( DB_REPLICA );
628
629 $lb = new LinkBatch( $targetsToLoad );
630 $res = $dbr->select(
631 'watchlist',
632 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
633 [
634 $lb->constructSet( 'wl', $dbr ),
635 'wl_user' => $user->getId(),
636 ],
637 __METHOD__
638 );
639
640 foreach ( $res as $row ) {
641 $timestamps[$row->wl_namespace][$row->wl_title] =
642 wfTimestampOrNull( TS_MW, $row->wl_notificationtimestamp );
643 }
644
645 return $timestamps;
646 }
647
653 public function addWatch( User $user, LinkTarget $target ) {
654 $this->addWatchBatchForUser( $user, [ $target ] );
655 }
656
663 public function addWatchBatchForUser( User $user, array $targets ) {
664 if ( $this->readOnlyMode->isReadOnly() ) {
665 return false;
666 }
667 // Only loggedin user can have a watchlist
668 if ( $user->isAnon() ) {
669 return false;
670 }
671
672 if ( !$targets ) {
673 return true;
674 }
675
676 $rows = [];
677 $items = [];
678 foreach ( $targets as $target ) {
679 $rows[] = [
680 'wl_user' => $user->getId(),
681 'wl_namespace' => $target->getNamespace(),
682 'wl_title' => $target->getDBkey(),
683 'wl_notificationtimestamp' => null,
684 ];
685 $items[] = new WatchedItem(
686 $user,
687 $target,
688 null
689 );
690 $this->uncache( $user, $target );
691 }
692
693 $dbw = $this->getConnectionRef( DB_MASTER );
694 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
695 // Use INSERT IGNORE to avoid overwriting the notification timestamp
696 // if there's already an entry for this page
697 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
698 }
699 // Update process cache to ensure skin doesn't claim that the current
700 // page is unwatched in the response of action=watch itself (T28292).
701 // This would otherwise be re-queried from a replica by isWatched().
702 foreach ( $items as $item ) {
703 $this->cache( $item );
704 }
705
706 return true;
707 }
708
715 public function removeWatch( User $user, LinkTarget $target ) {
716 // Only logged in user can have a watchlist
717 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
718 return false;
719 }
720
721 $this->uncache( $user, $target );
722
723 $dbw = $this->getConnectionRef( DB_MASTER );
724 $dbw->delete( 'watchlist',
725 [
726 'wl_user' => $user->getId(),
727 'wl_namespace' => $target->getNamespace(),
728 'wl_title' => $target->getDBkey(),
729 ], __METHOD__
730 );
731 $success = (bool)$dbw->affectedRows();
732
733 return $success;
734 }
735
743 public function setNotificationTimestampsForUser( User $user, $timestamp, array $targets = [] ) {
744 // Only loggedin user can have a watchlist
745 if ( $user->isAnon() ) {
746 return false;
747 }
748
749 $dbw = $this->getConnectionRef( DB_MASTER );
750
751 $conds = [ 'wl_user' => $user->getId() ];
752 if ( $targets ) {
753 $batch = new LinkBatch( $targets );
754 $conds[] = $batch->constructSet( 'wl', $dbw );
755 }
756
757 if ( $timestamp !== null ) {
758 $timestamp = $dbw->timestamp( $timestamp );
759 }
760
761 $success = $dbw->update(
762 'watchlist',
763 [ 'wl_notificationtimestamp' => $timestamp ],
764 $conds,
765 __METHOD__
766 );
767
768 $this->uncacheUser( $user );
769
770 return $success;
771 }
772
774 // Only loggedin user can have a watchlist
775 if ( $user->isAnon() ) {
776 return;
777 }
778
779 // If the page is watched by the user (or may be watched), update the timestamp
781 $user->getUserPage(),
782 [ 'userId' => $user->getId(), 'casTime' => time() ]
783 );
784
785 // Try to run this post-send
786 // Calls DeferredUpdates::addCallableUpdate in normal operation
787 call_user_func(
788 $this->deferredUpdatesAddCallableUpdateCallback,
789 function () use ( $job ) {
790 $job->run();
791 }
792 );
793 }
794
802 public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
803 $dbw = $this->getConnectionRef( DB_MASTER );
804 $uids = $dbw->selectFieldValues(
805 'watchlist',
806 'wl_user',
807 [
808 'wl_user != ' . intval( $editor->getId() ),
809 'wl_namespace' => $target->getNamespace(),
810 'wl_title' => $target->getDBkey(),
811 'wl_notificationtimestamp IS NULL',
812 ],
813 __METHOD__
814 );
815
816 $watchers = array_map( 'intval', $uids );
817 if ( $watchers ) {
818 // Update wl_notificationtimestamp for all watching users except the editor
819 $fname = __METHOD__;
820 DeferredUpdates::addCallableUpdate(
821 function () use ( $timestamp, $watchers, $target, $fname ) {
823
824 $dbw = $this->getConnectionRef( DB_MASTER );
825 $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
826 $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
827
828 $watchersChunks = array_chunk( $watchers, $wgUpdateRowsPerQuery );
829 foreach ( $watchersChunks as $watchersChunk ) {
830 $dbw->update( 'watchlist',
831 [ /* SET */
832 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
833 ], [ /* WHERE - TODO Use wl_id T130067 */
834 'wl_user' => $watchersChunk,
835 'wl_namespace' => $target->getNamespace(),
836 'wl_title' => $target->getDBkey(),
837 ], $fname
838 );
839 if ( count( $watchersChunks ) > 1 ) {
840 $factory->commitAndWaitForReplication(
841 __METHOD__, $ticket, [ 'domain' => $dbw->getDomainID() ]
842 );
843 }
844 }
845 $this->uncacheLinkTarget( $target );
846 },
847 DeferredUpdates::POSTSEND,
848 $dbw
849 );
850 }
851
852 return $watchers;
853 }
854
863 public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) {
864 // Only loggedin user can have a watchlist
865 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
866 return false;
867 }
868
869 $item = null;
870 if ( $force != 'force' ) {
871 $item = $this->loadWatchedItem( $user, $title );
872 if ( !$item || $item->getNotificationTimestamp() === null ) {
873 return false;
874 }
875 }
876
877 // If the page is watched by the user (or may be watched), update the timestamp
879 $title,
880 [
881 'type' => 'updateWatchlistNotification',
882 'userid' => $user->getId(),
883 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
884 'curTime' => time()
885 ]
886 );
887
888 // Try to run this post-send
889 // Calls DeferredUpdates::addCallableUpdate in normal operation
890 call_user_func(
891 $this->deferredUpdatesAddCallableUpdateCallback,
892 function () use ( $job ) {
893 $job->run();
894 }
895 );
896
897 $this->uncache( $user, $title );
898
899 return true;
900 }
901
902 private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) {
903 if ( !$oldid ) {
904 // No oldid given, assuming latest revision; clear the timestamp.
905 return null;
906 }
907
908 if ( !$title->getNextRevisionID( $oldid ) ) {
909 // Oldid given and is the latest revision for this title; clear the timestamp.
910 return null;
911 }
912
913 if ( $item === null ) {
914 $item = $this->loadWatchedItem( $user, $title );
915 }
916
917 if ( !$item ) {
918 // This can only happen if $force is enabled.
919 return null;
920 }
921
922 // Oldid given and isn't the latest; update the timestamp.
923 // This will result in no further notification emails being sent!
924 // Calls Revision::getTimestampFromId in normal operation
925 $notificationTimestamp = call_user_func(
926 $this->revisionGetTimestampFromIdCallback,
927 $title,
928 $oldid
929 );
930
931 // We need to go one second to the future because of various strict comparisons
932 // throughout the codebase
933 $ts = new MWTimestamp( $notificationTimestamp );
934 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
935 $notificationTimestamp = $ts->getTimestamp( TS_MW );
936
937 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
938 if ( $force != 'force' ) {
939 return false;
940 } else {
941 // This is a little silly…
942 return $item->getNotificationTimestamp();
943 }
944 }
945
946 return $notificationTimestamp;
947 }
948
955 public function countUnreadNotifications( User $user, $unreadLimit = null ) {
956 $queryOptions = [];
957 if ( $unreadLimit !== null ) {
958 $unreadLimit = (int)$unreadLimit;
959 $queryOptions['LIMIT'] = $unreadLimit;
960 }
961
962 $dbr = $this->getConnectionRef( DB_REPLICA );
963 $rowCount = $dbr->selectRowCount(
964 'watchlist',
965 '1',
966 [
967 'wl_user' => $user->getId(),
968 'wl_notificationtimestamp IS NOT NULL',
969 ],
970 __METHOD__,
971 $queryOptions
972 );
973
974 if ( !isset( $unreadLimit ) ) {
975 return $rowCount;
976 }
977
978 if ( $rowCount >= $unreadLimit ) {
979 return true;
980 }
981
982 return $rowCount;
983 }
984
990 public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) {
991 $oldTarget = Title::newFromLinkTarget( $oldTarget );
992 $newTarget = Title::newFromLinkTarget( $newTarget );
993
994 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
995 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
996 }
997
1003 public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
1004 $dbw = $this->getConnectionRef( DB_MASTER );
1005
1006 $result = $dbw->select(
1007 'watchlist',
1008 [ 'wl_user', 'wl_notificationtimestamp' ],
1009 [
1010 'wl_namespace' => $oldTarget->getNamespace(),
1011 'wl_title' => $oldTarget->getDBkey(),
1012 ],
1013 __METHOD__,
1014 [ 'FOR UPDATE' ]
1015 );
1016
1017 $newNamespace = $newTarget->getNamespace();
1018 $newDBkey = $newTarget->getDBkey();
1019
1020 # Construct array to replace into the watchlist
1021 $values = [];
1022 foreach ( $result as $row ) {
1023 $values[] = [
1024 'wl_user' => $row->wl_user,
1025 'wl_namespace' => $newNamespace,
1026 'wl_title' => $newDBkey,
1027 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
1028 ];
1029 }
1030
1031 if ( !empty( $values ) ) {
1032 # Perform replace
1033 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
1034 # some other DBMSes, mostly due to poor simulation by us
1035 $dbw->replace(
1036 'watchlist',
1037 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
1038 $values,
1039 __METHOD__
1040 );
1041 }
1042 }
1043
1044}
$wgUpdateRowsPerQuery
Number of rows to update per query.
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:112
Job for updating user activity like "last viewed" timestamps.
static newForUser(User $user, $maxWatchlistId)
Job for clearing all of the "last viewed" timestamps for a user's watchlist.
Simple store for keeping values in an associative array for the current process.
static singleton( $domain=false)
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
MediaWiki exception.
Library for creating and parsing MW-style timestamps.
MediaWikiServices is the service locator for the application scope of MediaWiki.
A service class for fetching the wiki's current read-only mode.
Represents a page (or page fragment) title within MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
Storage layer class for WatchedItems.
getCacheKey(User $user, LinkTarget $target)
countWatchedItems(User $user)
uncacheLinkTarget(LinkTarget $target)
callable null $deferredUpdatesAddCallableUpdateCallback
loadWatchedItem(User $user, LinkTarget $target)
duplicateEntry(LinkTarget $oldTarget, LinkTarget $newTarget)
duplicateAllAssociatedEntries(LinkTarget $oldTarget, LinkTarget $newTarget)
setNotificationTimestampsForUser(User $user, $timestamp, array $targets=[])
countVisitingWatchersMultiple(array $targetsWithVisitThresholds, $minimumWatchers=null)
countUnreadNotifications(User $user, $unreadLimit=null)
ReadOnlyMode $readOnlyMode
updateNotificationTimestamp(User $editor, LinkTarget $target, $timestamp)
getNotificationTimestamp(User $user, Title $title, $item, $force, $oldid)
uncache(User $user, LinkTarget $target)
countVisitingWatchers(LinkTarget $target, $threshold)
HashBagOStuff $cache
setStatsdDataFactory(StatsdDataFactoryInterface $stats)
addWatchBatchForUser(User $user, array $targets)
getWatchedItemsForUser(User $user, array $options=[])
dbCond(User $user, LinkTarget $target)
Return an array of conditions to select or update the appropriate database row.
cache(WatchedItem $item)
callable null $revisionGetTimestampFromIdCallback
uncacheAllItemsForUser(User $user)
getConnectionRef( $dbIndex)
countWatchers(LinkTarget $target)
overrideRevisionGetTimestampFromIdCallback(callable $callback)
Overrides the Revision::getTimestampFromId callback This is intended for use while testing and will f...
__construct(LoadBalancer $loadBalancer, HashBagOStuff $cache, ReadOnlyMode $readOnlyMode, $updateRowsPerQuery)
resetNotificationTimestamp(User $user, Title $title, $force='', $oldid=0)
addWatch(User $user, LinkTarget $target)
countWatchersMultiple(array $targets, array $options=[])
isWatched(User $user, LinkTarget $target)
clearUserWatchedItemsUsingJobQueue(User $user)
Queues a job that will clear the users watchlist using the Job Queue.
StatsdDataFactoryInterface $stats
getNotificationTimestampsBatch(User $user, array $targets)
getWatchedItem(User $user, LinkTarget $target)
resetAllNotificationTimestampsForUser(User $user)
Reset all watchlist notificaton timestamps for a user using the job queue.
getVisitingWatchersCondition(IDatabase $db, array $targetsWithVisitThresholds)
Generates condition for the query used in a batch count visiting watchers.
removeWatch(User $user, LinkTarget $target)
getCached(User $user, LinkTarget $target)
LoadBalancer $loadBalancer
overrideDeferredUpdatesAddCallableUpdateCallback(callable $callback)
Overrides the DeferredUpdates::addCallableUpdate callback This is intended for use while testing and ...
array[] $cacheIndex
Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key' The index is needed so that on ...
clearUserWatchedItems(User $user)
Deletes ALL watched items for the given user when under $updateRowsPerQuery entries exist.
Representation of a pair of user and title for watchlist entries.
Database connection, tracking, load balancing, and transaction manager for a cluster.
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition hooks.txt:2783
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
passed in as a query string parameter to the various URLs constructed here(i.e. $prevlink) $ldel you ll need to handle error etc yourself modifying $error and returning true will cause the contents of $error to be echoed at the top of the edit form as wikitext Return true without altering $error to allow the edit to proceed & $editor
Definition hooks.txt:1419
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
const LIST_OR
Definition Defines.php:56
const LIST_AND
Definition Defines.php:53
getNamespace()
Get the namespace index.
getDBkey()
Get the main part with underscores.
Describes a Statsd aware interface.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
addQuotes( $s)
Adds quotes and backslashes.
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by wfTimestamp() to the format used for inserting ...
makeList( $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
$batch
Definition linkcache.txt:23
you have access to all of the normal MediaWiki so you can get a DB use the cache
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29
if(count( $args)< 1) $job