3 use Wikimedia\Rdbms\IDatabase
;
4 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
5 use MediaWiki\Linker\LinkTarget
;
6 use MediaWiki\MediaWikiServices
;
7 use Wikimedia\Assert\Assert
;
8 use Wikimedia\ScopedCallback
;
9 use Wikimedia\Rdbms\LoadBalancer
;
12 * Storage layer class for WatchedItems.
13 * Database interaction & caching
14 * TODO caching should be factored out into a CachingWatchedItemStore class
19 class WatchedItemStore
implements WatchedItemStoreInterface
, StatsdAwareInterface
{
24 private $loadBalancer;
29 private $readOnlyMode;
37 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
38 * The index is needed so that on mass changes all relevant items can be un-cached.
39 * For example: Clearing a users watchlist of all items or updating notification timestamps
40 * for all users watching a single target.
42 private $cacheIndex = [];
47 private $deferredUpdatesAddCallableUpdateCallback;
52 private $revisionGetTimestampFromIdCallback;
57 private $updateRowsPerQuery;
60 * @var StatsdDataFactoryInterface
65 * @param LoadBalancer $loadBalancer
66 * @param HashBagOStuff $cache
67 * @param ReadOnlyMode $readOnlyMode
68 * @param int $updateRowsPerQuery
70 public function __construct(
71 LoadBalancer
$loadBalancer,
73 ReadOnlyMode
$readOnlyMode,
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;
88 * @param StatsdDataFactoryInterface $stats
90 public function setStatsdDataFactory( StatsdDataFactoryInterface
$stats ) {
91 $this->stats
= $stats;
95 * Overrides the DeferredUpdates::addCallableUpdate callback
96 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
98 * @param callable $callback
100 * @see DeferredUpdates::addCallableUpdate for callback signiture
102 * @return ScopedCallback to reset the overridden value
103 * @throws MWException
105 public function overrideDeferredUpdatesAddCallableUpdateCallback( callable
$callback ) {
106 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
107 throw new MWException(
108 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
111 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback
;
112 $this->deferredUpdatesAddCallableUpdateCallback
= $callback;
113 return new ScopedCallback( function () use ( $previousValue ) {
114 $this->deferredUpdatesAddCallableUpdateCallback
= $previousValue;
119 * Overrides the Revision::getTimestampFromId callback
120 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
122 * @param callable $callback
123 * @see Revision::getTimestampFromId for callback signiture
125 * @return ScopedCallback to reset the overridden value
126 * @throws MWException
128 public function overrideRevisionGetTimestampFromIdCallback( callable
$callback ) {
129 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
130 throw new MWException(
131 'Cannot override Revision::getTimestampFromId callback in operation.'
134 $previousValue = $this->revisionGetTimestampFromIdCallback
;
135 $this->revisionGetTimestampFromIdCallback
= $callback;
136 return new ScopedCallback( function () use ( $previousValue ) {
137 $this->revisionGetTimestampFromIdCallback
= $previousValue;
141 private function getCacheKey( User
$user, LinkTarget
$target ) {
142 return $this->cache
->makeKey(
143 (string)$target->getNamespace(),
145 (string)$user->getId()
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' );
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' );
164 private function uncacheLinkTarget( LinkTarget
$target ) {
165 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget' );
166 if ( !isset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] ) ) {
169 foreach ( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] as $key ) {
170 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
171 $this->cache
->delete( $key );
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()] );
189 * @param LinkTarget $target
191 * @return WatchedItem|false
193 private function getCached( User
$user, LinkTarget
$target ) {
194 return $this->cache
->get( $this->getCacheKey( $user, $target ) );
198 * Return an array of conditions to select or update the appropriate database
202 * @param LinkTarget $target
206 private function dbCond( User
$user, LinkTarget
$target ) {
208 'wl_user' => $user->getId(),
209 'wl_namespace' => $target->getNamespace(),
210 'wl_title' => $target->getDBkey(),
215 * @param int $dbIndex DB_MASTER or DB_REPLICA
218 * @throws MWException
220 private function getConnectionRef( $dbIndex ) {
221 return $this->loadBalancer
->getConnectionRef( $dbIndex, [ 'watchlist' ] );
225 * Deletes ALL watched items for the given user when under
226 * $updateRowsPerQuery entries exist.
232 * @return bool true on success, false when too many items are watched
234 public function clearUserWatchedItems( User
$user ) {
235 if ( $this->countWatchedItems( $user ) > $this->updateRowsPerQuery
) {
239 $dbw = $this->loadBalancer
->getConnectionRef( DB_MASTER
);
242 [ 'wl_user' => $user->getId() ],
245 $this->uncacheAllItemsForUser( $user );
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] );
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] );
268 if ( empty( $this->cacheIndex
[$ns] ) ) {
269 unset( $this->cacheIndex
[$ns] );
275 * Queues a job that will clear the users watchlist using the Job Queue.
281 public function clearUserWatchedItemsUsingJobQueue( User
$user ) {
282 $job = ClearUserWatchlistJob
::newForUser( $user, $this->getMaxId() );
284 JobQueueGroup
::singleton()->push( $job );
289 * @return int The maximum current wl_id
291 public function getMaxId() {
292 $dbr = $this->getConnectionRef( DB_REPLICA
);
293 return (int)$dbr->selectField(
306 public function countWatchedItems( User
$user ) {
307 $dbr = $this->getConnectionRef( DB_REPLICA
);
308 $return = (int)$dbr->selectField(
312 'wl_user' => $user->getId()
322 * @param LinkTarget $target
325 public function countWatchers( LinkTarget
$target ) {
326 $dbr = $this->getConnectionRef( DB_REPLICA
);
327 $return = (int)$dbr->selectField(
331 'wl_namespace' => $target->getNamespace(),
332 'wl_title' => $target->getDBkey(),
342 * @param LinkTarget $target
343 * @param string|int $threshold
346 public function countVisitingWatchers( LinkTarget
$target, $threshold ) {
347 $dbr = $this->getConnectionRef( DB_REPLICA
);
348 $visitingWatchers = (int)$dbr->selectField(
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'
361 return $visitingWatchers;
366 * @param LinkTarget[] $targets
367 * @param array $options
370 public function countWatchersMultiple( array $targets, array $options = [] ) {
371 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
373 $dbr = $this->getConnectionRef( DB_REPLICA
);
375 if ( array_key_exists( 'minimumWatchers', $options ) ) {
376 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
379 $lb = new LinkBatch( $targets );
382 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
383 [ $lb->constructSet( 'wl', $dbr ) ],
389 foreach ( $targets as $linkTarget ) {
390 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
393 foreach ( $res as $row ) {
394 $watchCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
402 * @param array $targetsWithVisitThresholds
403 * @param int|null $minimumWatchers
406 public function countVisitingWatchersMultiple(
407 array $targetsWithVisitThresholds,
408 $minimumWatchers = null
410 $dbr = $this->getConnectionRef( DB_REPLICA
);
412 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
414 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
415 if ( $minimumWatchers !== null ) {
416 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
420 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
427 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
428 /* @var LinkTarget $target */
429 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
432 foreach ( $res as $row ) {
433 $watcherCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
436 return $watcherCounts;
440 * Generates condition for the query used in a batch count visiting watchers.
442 * @param IDatabase $db
443 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
446 private function getVisitingWatchersCondition(
448 array $targetsWithVisitThresholds
450 $missingTargets = [];
451 $namespaceConds = [];
452 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
453 if ( $threshold === null ) {
454 $missingTargets[] = $target;
457 /* @var LinkTarget $target */
458 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
459 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
461 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
462 'wl_notificationtimestamp IS NULL'
468 foreach ( $namespaceConds as $namespace => $pageConds ) {
469 $conds[] = $db->makeList( [
470 'wl_namespace = ' . $namespace,
471 '(' . $db->makeList( $pageConds, LIST_OR
) . ')'
475 if ( $missingTargets ) {
476 $lb = new LinkBatch( $missingTargets );
477 $conds[] = $lb->constructSet( 'wl', $db );
480 return $db->makeList( $conds, LIST_OR
);
486 * @param LinkTarget $target
489 public function getWatchedItem( User
$user, LinkTarget
$target ) {
490 if ( $user->isAnon() ) {
494 $cached = $this->getCached( $user, $target );
496 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.cached' );
499 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.load' );
500 return $this->loadWatchedItem( $user, $target );
506 * @param LinkTarget $target
507 * @return WatchedItem|bool
509 public function loadWatchedItem( User
$user, LinkTarget
$target ) {
510 // Only loggedin user can have a watchlist
511 if ( $user->isAnon() ) {
515 $dbr = $this->getConnectionRef( DB_REPLICA
);
516 $row = $dbr->selectRow(
518 'wl_notificationtimestamp',
519 $this->dbCond( $user, $target ),
527 $item = new WatchedItem(
530 wfTimestampOrNull( TS_MW
, $row->wl_notificationtimestamp
)
532 $this->cache( $item );
540 * @param array $options
541 * @return WatchedItem[]
543 public function getWatchedItemsForUser( User
$user, array $options = [] ) {
544 $options +
= [ 'forWrite' => false ];
547 if ( array_key_exists( 'sort', $options ) ) {
549 ( in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ) ),
550 '$options[\'sort\']',
551 'must be SORT_ASC or SORT_DESC'
553 $dbOptions['ORDER BY'] = [
554 "wl_namespace {$options['sort']}",
555 "wl_title {$options['sort']}"
558 $db = $this->getConnectionRef( $options['forWrite'] ? DB_MASTER
: DB_REPLICA
);
562 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
563 [ 'wl_user' => $user->getId() ],
569 foreach ( $res as $row ) {
570 // @todo: Should we add these to the process cache?
571 $watchedItems[] = new WatchedItem(
573 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
574 $row->wl_notificationtimestamp
578 return $watchedItems;
584 * @param LinkTarget $target
587 public function isWatched( User
$user, LinkTarget
$target ) {
588 return (bool)$this->getWatchedItem( $user, $target );
594 * @param LinkTarget[] $targets
597 public function getNotificationTimestampsBatch( User
$user, array $targets ) {
599 foreach ( $targets as $target ) {
600 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
603 if ( $user->isAnon() ) {
608 foreach ( $targets as $target ) {
609 $cachedItem = $this->getCached( $user, $target );
611 $timestamps[$target->getNamespace()][$target->getDBkey()] =
612 $cachedItem->getNotificationTimestamp();
614 $targetsToLoad[] = $target;
618 if ( !$targetsToLoad ) {
622 $dbr = $this->getConnectionRef( DB_REPLICA
);
624 $lb = new LinkBatch( $targetsToLoad );
627 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
629 $lb->constructSet( 'wl', $dbr ),
630 'wl_user' => $user->getId(),
635 foreach ( $res as $row ) {
636 $timestamps[$row->wl_namespace
][$row->wl_title
] =
637 wfTimestampOrNull( TS_MW
, $row->wl_notificationtimestamp
);
646 * @param LinkTarget $target
648 public function addWatch( User
$user, LinkTarget
$target ) {
649 $this->addWatchBatchForUser( $user, [ $target ] );
655 * @param LinkTarget[] $targets
658 public function addWatchBatchForUser( User
$user, array $targets ) {
659 if ( $this->readOnlyMode
->isReadOnly() ) {
662 // Only loggedin user can have a watchlist
663 if ( $user->isAnon() ) {
673 foreach ( $targets as $target ) {
675 'wl_user' => $user->getId(),
676 'wl_namespace' => $target->getNamespace(),
677 'wl_title' => $target->getDBkey(),
678 'wl_notificationtimestamp' => null,
680 $items[] = new WatchedItem(
685 $this->uncache( $user, $target );
688 $dbw = $this->getConnectionRef( DB_MASTER
);
689 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
690 // Use INSERT IGNORE to avoid overwriting the notification timestamp
691 // if there's already an entry for this page
692 $dbw->insert( 'watchlist', $toInsert, __METHOD__
, 'IGNORE' );
694 // Update process cache to ensure skin doesn't claim that the current
695 // page is unwatched in the response of action=watch itself (T28292).
696 // This would otherwise be re-queried from a replica by isWatched().
697 foreach ( $items as $item ) {
698 $this->cache( $item );
707 * @param LinkTarget $target
710 public function removeWatch( User
$user, LinkTarget
$target ) {
711 // Only logged in user can have a watchlist
712 if ( $this->readOnlyMode
->isReadOnly() ||
$user->isAnon() ) {
716 $this->uncache( $user, $target );
718 $dbw = $this->getConnectionRef( DB_MASTER
);
719 $dbw->delete( 'watchlist',
721 'wl_user' => $user->getId(),
722 'wl_namespace' => $target->getNamespace(),
723 'wl_title' => $target->getDBkey(),
726 $success = (bool)$dbw->affectedRows();
734 * @param string|int $timestamp
735 * @param LinkTarget[] $targets
738 public function setNotificationTimestampsForUser( User
$user, $timestamp, array $targets = [] ) {
739 // Only loggedin user can have a watchlist
740 if ( $user->isAnon() ) {
744 $dbw = $this->getConnectionRef( DB_MASTER
);
746 $conds = [ 'wl_user' => $user->getId() ];
748 $batch = new LinkBatch( $targets );
749 $conds[] = $batch->constructSet( 'wl', $dbw );
752 if ( $timestamp !== null ) {
753 $timestamp = $dbw->timestamp( $timestamp );
756 $success = $dbw->update(
758 [ 'wl_notificationtimestamp' => $timestamp ],
763 $this->uncacheUser( $user );
768 public function resetAllNotificationTimestampsForUser( User
$user ) {
769 // Only loggedin user can have a watchlist
770 if ( $user->isAnon() ) {
774 // If the page is watched by the user (or may be watched), update the timestamp
775 $job = new ClearWatchlistNotificationsJob(
776 $user->getUserPage(),
777 [ 'userId' => $user->getId(), 'casTime' => time() ]
780 // Try to run this post-send
781 // Calls DeferredUpdates::addCallableUpdate in normal operation
783 $this->deferredUpdatesAddCallableUpdateCallback
,
784 function () use ( $job ) {
792 * @param User $editor
793 * @param LinkTarget $target
794 * @param string|int $timestamp
797 public function updateNotificationTimestamp( User
$editor, LinkTarget
$target, $timestamp ) {
798 $dbw = $this->getConnectionRef( DB_MASTER
);
799 $uids = $dbw->selectFieldValues(
803 'wl_user != ' . intval( $editor->getId() ),
804 'wl_namespace' => $target->getNamespace(),
805 'wl_title' => $target->getDBkey(),
806 'wl_notificationtimestamp IS NULL',
811 $watchers = array_map( 'intval', $uids );
813 // Update wl_notificationtimestamp for all watching users except the editor
815 DeferredUpdates
::addCallableUpdate(
816 function () use ( $timestamp, $watchers, $target, $fname ) {
817 global $wgUpdateRowsPerQuery;
819 $dbw = $this->getConnectionRef( DB_MASTER
);
820 $factory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
821 $ticket = $factory->getEmptyTransactionTicket( __METHOD__
);
823 $watchersChunks = array_chunk( $watchers, $wgUpdateRowsPerQuery );
824 foreach ( $watchersChunks as $watchersChunk ) {
825 $dbw->update( 'watchlist',
827 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
828 ], [ /* WHERE - TODO Use wl_id T130067 */
829 'wl_user' => $watchersChunk,
830 'wl_namespace' => $target->getNamespace(),
831 'wl_title' => $target->getDBkey(),
834 if ( count( $watchersChunks ) > 1 ) {
835 $factory->commitAndWaitForReplication(
836 __METHOD__
, $ticket, [ 'domain' => $dbw->getDomainID() ]
840 $this->uncacheLinkTarget( $target );
842 DeferredUpdates
::POSTSEND
,
853 * @param Title $title
854 * @param string $force
858 public function resetNotificationTimestamp( User
$user, Title
$title, $force = '', $oldid = 0 ) {
859 // Only loggedin user can have a watchlist
860 if ( $this->readOnlyMode
->isReadOnly() ||
$user->isAnon() ) {
865 if ( $force != 'force' ) {
866 $item = $this->loadWatchedItem( $user, $title );
867 if ( !$item ||
$item->getNotificationTimestamp() === null ) {
872 // If the page is watched by the user (or may be watched), update the timestamp
873 $job = new ActivityUpdateJob(
876 'type' => 'updateWatchlistNotification',
877 'userid' => $user->getId(),
878 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
883 // Try to run this post-send
884 // Calls DeferredUpdates::addCallableUpdate in normal operation
886 $this->deferredUpdatesAddCallableUpdateCallback
,
887 function () use ( $job ) {
892 $this->uncache( $user, $title );
897 private function getNotificationTimestamp( User
$user, Title
$title, $item, $force, $oldid ) {
899 // No oldid given, assuming latest revision; clear the timestamp.
903 if ( !$title->getNextRevisionID( $oldid ) ) {
904 // Oldid given and is the latest revision for this title; clear the timestamp.
908 if ( $item === null ) {
909 $item = $this->loadWatchedItem( $user, $title );
913 // This can only happen if $force is enabled.
917 // Oldid given and isn't the latest; update the timestamp.
918 // This will result in no further notification emails being sent!
919 // Calls Revision::getTimestampFromId in normal operation
920 $notificationTimestamp = call_user_func(
921 $this->revisionGetTimestampFromIdCallback
,
926 // We need to go one second to the future because of various strict comparisons
927 // throughout the codebase
928 $ts = new MWTimestamp( $notificationTimestamp );
929 $ts->timestamp
->add( new DateInterval( 'PT1S' ) );
930 $notificationTimestamp = $ts->getTimestamp( TS_MW
);
932 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
933 if ( $force != 'force' ) {
936 // This is a little silly…
937 return $item->getNotificationTimestamp();
941 return $notificationTimestamp;
947 * @param int|null $unreadLimit
950 public function countUnreadNotifications( User
$user, $unreadLimit = null ) {
952 if ( $unreadLimit !== null ) {
953 $unreadLimit = (int)$unreadLimit;
954 $queryOptions['LIMIT'] = $unreadLimit;
957 $dbr = $this->getConnectionRef( DB_REPLICA
);
958 $rowCount = $dbr->selectRowCount(
962 'wl_user' => $user->getId(),
963 'wl_notificationtimestamp IS NOT NULL',
969 if ( !isset( $unreadLimit ) ) {
973 if ( $rowCount >= $unreadLimit ) {
982 * @param LinkTarget $oldTarget
983 * @param LinkTarget $newTarget
985 public function duplicateAllAssociatedEntries( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
986 $oldTarget = Title
::newFromLinkTarget( $oldTarget );
987 $newTarget = Title
::newFromLinkTarget( $newTarget );
989 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
990 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
995 * @param LinkTarget $oldTarget
996 * @param LinkTarget $newTarget
998 public function duplicateEntry( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
999 $dbw = $this->getConnectionRef( DB_MASTER
);
1001 $result = $dbw->select(
1003 [ 'wl_user', 'wl_notificationtimestamp' ],
1005 'wl_namespace' => $oldTarget->getNamespace(),
1006 'wl_title' => $oldTarget->getDBkey(),
1012 $newNamespace = $newTarget->getNamespace();
1013 $newDBkey = $newTarget->getDBkey();
1015 # Construct array to replace into the watchlist
1017 foreach ( $result as $row ) {
1019 'wl_user' => $row->wl_user
,
1020 'wl_namespace' => $newNamespace,
1021 'wl_title' => $newDBkey,
1022 'wl_notificationtimestamp' => $row->wl_notificationtimestamp
,
1026 if ( !empty( $values ) ) {
1028 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
1029 # some other DBMSes, mostly due to poor simulation by us
1032 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],