3 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
4 use MediaWiki\Linker\LinkTarget
;
5 use Wikimedia\Assert\Assert
;
8 * Storage layer class for WatchedItems.
9 * Database interaction.
15 class WatchedItemStore
implements StatsdAwareInterface
{
17 const SORT_DESC
= 'DESC';
18 const SORT_ASC
= 'ASC';
23 private $loadBalancer;
31 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
32 * The index is needed so that on mass changes all relevant items can be un-cached.
33 * For example: Clearing a users watchlist of all items or updating notification timestamps
34 * for all users watching a single target.
36 private $cacheIndex = [];
41 private $deferredUpdatesAddCallableUpdateCallback;
46 private $revisionGetTimestampFromIdCallback;
49 * @var StatsdDataFactoryInterface
54 * @param LoadBalancer $loadBalancer
55 * @param HashBagOStuff $cache
57 public function __construct(
58 LoadBalancer
$loadBalancer,
61 $this->loadBalancer
= $loadBalancer;
62 $this->cache
= $cache;
63 $this->stats
= new NullStatsdDataFactory();
64 $this->deferredUpdatesAddCallableUpdateCallback
= [ 'DeferredUpdates', 'addCallableUpdate' ];
65 $this->revisionGetTimestampFromIdCallback
= [ 'Revision', 'getTimestampFromId' ];
68 public function setStatsdDataFactory( StatsdDataFactoryInterface
$stats ) {
69 $this->stats
= $stats;
73 * Overrides the DeferredUpdates::addCallableUpdate callback
74 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
76 * @param callable $callback
78 * @see DeferredUpdates::addCallableUpdate for callback signiture
80 * @return ScopedCallback to reset the overridden value
83 public function overrideDeferredUpdatesAddCallableUpdateCallback( callable
$callback ) {
84 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
85 throw new MWException(
86 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
89 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback
;
90 $this->deferredUpdatesAddCallableUpdateCallback
= $callback;
91 return new ScopedCallback( function() use ( $previousValue ) {
92 $this->deferredUpdatesAddCallableUpdateCallback
= $previousValue;
97 * Overrides the Revision::getTimestampFromId callback
98 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
100 * @param callable $callback
101 * @see Revision::getTimestampFromId for callback signiture
103 * @return ScopedCallback to reset the overridden value
104 * @throws MWException
106 public function overrideRevisionGetTimestampFromIdCallback( callable
$callback ) {
107 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
108 throw new MWException(
109 'Cannot override Revision::getTimestampFromId callback in operation.'
112 $previousValue = $this->revisionGetTimestampFromIdCallback
;
113 $this->revisionGetTimestampFromIdCallback
= $callback;
114 return new ScopedCallback( function() use ( $previousValue ) {
115 $this->revisionGetTimestampFromIdCallback
= $previousValue;
119 private function getCacheKey( User
$user, LinkTarget
$target ) {
120 return $this->cache
->makeKey(
121 (string)$target->getNamespace(),
123 (string)$user->getId()
127 private function cache( WatchedItem
$item ) {
128 $user = $item->getUser();
129 $target = $item->getLinkTarget();
130 $key = $this->getCacheKey( $user, $target );
131 $this->cache
->set( $key, $item );
132 $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
133 $this->stats
->increment( 'WatchedItemStore.cache' );
136 private function uncache( User
$user, LinkTarget
$target ) {
137 $this->cache
->delete( $this->getCacheKey( $user, $target ) );
138 unset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
139 $this->stats
->increment( 'WatchedItemStore.uncache' );
142 private function uncacheLinkTarget( LinkTarget
$target ) {
143 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget' );
144 if ( !isset( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] ) ) {
147 foreach ( $this->cacheIndex
[$target->getNamespace()][$target->getDBkey()] as $key ) {
148 $this->stats
->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
149 $this->cache
->delete( $key );
153 private function uncacheUser( User
$user ) {
154 $this->stats
->increment( 'WatchedItemStore.uncacheUser' );
155 foreach ( $this->cacheIndex
as $ns => $dbKeyArray ) {
156 foreach ( $dbKeyArray as $dbKey => $userArray ) {
157 if ( isset( $userArray[$user->getId()] ) ) {
158 $this->stats
->increment( 'WatchedItemStore.uncacheUser.items' );
159 $this->cache
->delete( $userArray[$user->getId()] );
167 * @param LinkTarget $target
169 * @return WatchedItem|null
171 private function getCached( User
$user, LinkTarget
$target ) {
172 return $this->cache
->get( $this->getCacheKey( $user, $target ) );
176 * Return an array of conditions to select or update the appropriate database
180 * @param LinkTarget $target
184 private function dbCond( User
$user, LinkTarget
$target ) {
186 'wl_user' => $user->getId(),
187 'wl_namespace' => $target->getNamespace(),
188 'wl_title' => $target->getDBkey(),
193 * @param int $slaveOrMaster DB_MASTER or DB_SLAVE
195 * @return DatabaseBase
196 * @throws MWException
198 private function getConnection( $slaveOrMaster ) {
199 return $this->loadBalancer
->getConnection( $slaveOrMaster, [ 'watchlist' ] );
203 * @param DatabaseBase $connection
205 * @throws MWException
207 private function reuseConnection( $connection ) {
208 $this->loadBalancer
->reuseConnection( $connection );
212 * Count the number of individual items that are watched by the user.
213 * If a subject and corresponding talk page are watched this will return 2.
219 public function countWatchedItems( User
$user ) {
220 $dbr = $this->getConnection( DB_SLAVE
);
221 $return = (int)$dbr->selectField(
225 'wl_user' => $user->getId()
229 $this->reuseConnection( $dbr );
235 * @param LinkTarget $target
239 public function countWatchers( LinkTarget
$target ) {
240 $dbr = $this->getConnection( DB_SLAVE
);
241 $return = (int)$dbr->selectField(
245 'wl_namespace' => $target->getNamespace(),
246 'wl_title' => $target->getDBkey(),
250 $this->reuseConnection( $dbr );
256 * Number of page watchers who also visited a "recent" edit
258 * @param LinkTarget $target
259 * @param mixed $threshold timestamp accepted by wfTimestamp
262 * @throws DBUnexpectedError
263 * @throws MWException
265 public function countVisitingWatchers( LinkTarget
$target, $threshold ) {
266 $dbr = $this->getConnection( DB_SLAVE
);
267 $visitingWatchers = (int)$dbr->selectField(
271 'wl_namespace' => $target->getNamespace(),
272 'wl_title' => $target->getDBkey(),
273 'wl_notificationtimestamp >= ' .
274 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
275 ' OR wl_notificationtimestamp IS NULL'
279 $this->reuseConnection( $dbr );
281 return $visitingWatchers;
285 * @param LinkTarget[] $targets
286 * @param array $options Allowed keys:
287 * 'minimumWatchers' => int
289 * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers
290 * All targets will be present in the result. 0 either means no watchers or the number
291 * of watchers was below the minimumWatchers option if passed.
293 public function countWatchersMultiple( array $targets, array $options = [] ) {
294 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
296 $dbr = $this->getConnection( DB_SLAVE
);
298 if ( array_key_exists( 'minimumWatchers', $options ) ) {
299 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
302 $lb = new LinkBatch( $targets );
305 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
306 [ $lb->constructSet( 'wl', $dbr ) ],
311 $this->reuseConnection( $dbr );
314 foreach ( $targets as $linkTarget ) {
315 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
318 foreach ( $res as $row ) {
319 $watchCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
326 * Number of watchers of each page who have visited recent edits to that page
328 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed $threshold),
330 * - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp)
331 * - null if $target doesn't exist
332 * @param int|null $minimumWatchers
333 * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers,
334 * where $watchers is an int:
335 * - if the page exists, number of users watching who have visited the page recently
336 * - if the page doesn't exist, number of users that have the page on their watchlist
337 * - 0 means there are no visiting watchers or their number is below the minimumWatchers
338 * option (if passed).
340 public function countVisitingWatchersMultiple(
341 array $targetsWithVisitThresholds,
342 $minimumWatchers = null
344 $dbr = $this->getConnection( DB_SLAVE
);
346 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
348 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
349 if ( $minimumWatchers !== null ) {
350 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
354 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
360 $this->reuseConnection( $dbr );
363 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
364 /* @var LinkTarget $target */
365 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
368 foreach ( $res as $row ) {
369 $watcherCounts[$row->wl_namespace
][$row->wl_title
] = (int)$row->watchers
;
372 return $watcherCounts;
376 * Generates condition for the query used in a batch count visiting watchers.
378 * @param IDatabase $db
379 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
382 private function getVisitingWatchersCondition(
384 array $targetsWithVisitThresholds
386 $missingTargets = [];
387 $namespaceConds = [];
388 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
389 if ( $threshold === null ) {
390 $missingTargets[] = $target;
393 /* @var LinkTarget $target */
394 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
395 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
397 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
398 'wl_notificationtimestamp IS NULL'
404 foreach ( $namespaceConds as $namespace => $pageConds ) {
405 $conds[] = $db->makeList( [
406 'wl_namespace = ' . $namespace,
407 '(' . $db->makeList( $pageConds, LIST_OR
) . ')'
411 if ( $missingTargets ) {
412 $lb = new LinkBatch( $missingTargets );
413 $conds[] = $lb->constructSet( 'wl', $db );
416 return $db->makeList( $conds, LIST_OR
);
420 * Get an item (may be cached)
423 * @param LinkTarget $target
425 * @return WatchedItem|false
427 public function getWatchedItem( User
$user, LinkTarget
$target ) {
428 if ( $user->isAnon() ) {
432 $cached = $this->getCached( $user, $target );
434 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.cached' );
437 $this->stats
->increment( 'WatchedItemStore.getWatchedItem.load' );
438 return $this->loadWatchedItem( $user, $target );
442 * Loads an item from the db
445 * @param LinkTarget $target
447 * @return WatchedItem|false
449 public function loadWatchedItem( User
$user, LinkTarget
$target ) {
450 // Only loggedin user can have a watchlist
451 if ( $user->isAnon() ) {
455 $dbr = $this->getConnection( DB_SLAVE
);
456 $row = $dbr->selectRow(
458 'wl_notificationtimestamp',
459 $this->dbCond( $user, $target ),
462 $this->reuseConnection( $dbr );
468 $item = new WatchedItem(
471 $row->wl_notificationtimestamp
473 $this->cache( $item );
480 * @param array $options Allowed keys:
481 * 'forWrite' => bool defaults to false
482 * 'sort' => string optional sorting by namespace ID and title
483 * one of the self::SORT_* constants
485 * @return WatchedItem[]
487 public function getWatchedItemsForUser( User
$user, array $options = [] ) {
488 $options +
= [ 'forWrite' => false ];
491 if ( array_key_exists( 'sort', $options ) ) {
493 ( in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ) ),
494 '$options[\'sort\']',
495 'must be SORT_ASC or SORT_DESC'
497 $dbOptions['ORDER BY'] = [
498 "wl_namespace {$options['sort']}",
499 "wl_title {$options['sort']}"
502 $db = $this->getConnection( $options['forWrite'] ? DB_MASTER
: DB_SLAVE
);
506 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
507 [ 'wl_user' => $user->getId() ],
511 $this->reuseConnection( $db );
514 foreach ( $res as $row ) {
515 // todo these could all be cached at some point?
516 $watchedItems[] = new WatchedItem(
518 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
519 $row->wl_notificationtimestamp
523 return $watchedItems;
527 * Must be called separately for Subject & Talk namespaces
530 * @param LinkTarget $target
534 public function isWatched( User
$user, LinkTarget
$target ) {
535 return (bool)$this->getWatchedItem( $user, $target );
540 * @param LinkTarget[] $targets
542 * @return array multi-dimensional like $return[$namespaceId][$titleString] = $timestamp,
543 * where $timestamp is:
544 * - string|null value of wl_notificationtimestamp,
545 * - false if $target is not watched by $user.
547 public function getNotificationTimestampsBatch( User
$user, array $targets ) {
549 foreach ( $targets as $target ) {
550 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
553 if ( $user->isAnon() ) {
558 foreach ( $targets as $target ) {
559 $cachedItem = $this->getCached( $user, $target );
561 $timestamps[$target->getNamespace()][$target->getDBkey()] =
562 $cachedItem->getNotificationTimestamp();
564 $targetsToLoad[] = $target;
568 if ( !$targetsToLoad ) {
572 $dbr = $this->getConnection( DB_SLAVE
);
574 $lb = new LinkBatch( $targetsToLoad );
577 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
579 $lb->constructSet( 'wl', $dbr ),
580 'wl_user' => $user->getId(),
584 $this->reuseConnection( $dbr );
586 foreach ( $res as $row ) {
587 $timestamps[$row->wl_namespace
][$row->wl_title
] = $row->wl_notificationtimestamp
;
594 * Must be called separately for Subject & Talk namespaces
597 * @param LinkTarget $target
599 public function addWatch( User
$user, LinkTarget
$target ) {
600 $this->addWatchBatchForUser( $user, [ $target ] );
605 * @param LinkTarget[] $targets
607 * @return bool success
609 public function addWatchBatchForUser( User
$user, array $targets ) {
610 if ( $this->loadBalancer
->getReadOnlyReason() !== false ) {
613 // Only loggedin user can have a watchlist
614 if ( $user->isAnon() ) {
623 foreach ( $targets as $target ) {
625 'wl_user' => $user->getId(),
626 'wl_namespace' => $target->getNamespace(),
627 'wl_title' => $target->getDBkey(),
628 'wl_notificationtimestamp' => null,
630 $this->uncache( $user, $target );
633 $dbw = $this->getConnection( DB_MASTER
);
634 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
635 // Use INSERT IGNORE to avoid overwriting the notification timestamp
636 // if there's already an entry for this page
637 $dbw->insert( 'watchlist', $toInsert, __METHOD__
, 'IGNORE' );
639 $this->reuseConnection( $dbw );
645 * Removes the an entry for the User watching the LinkTarget
646 * Must be called separately for Subject & Talk namespaces
649 * @param LinkTarget $target
651 * @return bool success
652 * @throws DBUnexpectedError
653 * @throws MWException
655 public function removeWatch( User
$user, LinkTarget
$target ) {
656 // Only logged in user can have a watchlist
657 if ( $this->loadBalancer
->getReadOnlyReason() !== false ||
$user->isAnon() ) {
661 $this->uncache( $user, $target );
663 $dbw = $this->getConnection( DB_MASTER
);
664 $dbw->delete( 'watchlist',
666 'wl_user' => $user->getId(),
667 'wl_namespace' => $target->getNamespace(),
668 'wl_title' => $target->getDBkey(),
671 $success = (bool)$dbw->affectedRows();
672 $this->reuseConnection( $dbw );
678 * @param User $user The user to set the timestamp for
679 * @param string $timestamp Set the update timestamp to this value
680 * @param LinkTarget[] $targets List of targets to update. Default to all targets
682 * @return bool success
684 public function setNotificationTimestampsForUser( User
$user, $timestamp, array $targets = [] ) {
685 // Only loggedin user can have a watchlist
686 if ( $user->isAnon() ) {
690 $dbw = $this->getConnection( DB_MASTER
);
692 $conds = [ 'wl_user' => $user->getId() ];
694 $batch = new LinkBatch( $targets );
695 $conds[] = $batch->constructSet( 'wl', $dbw );
698 $success = $dbw->update(
700 [ 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp ) ],
705 $this->reuseConnection( $dbw );
707 $this->uncacheUser( $user );
713 * @param User $editor The editor that triggered the update. Their notification
714 * timestamp will not be updated(they have already seen it)
715 * @param LinkTarget $target The target to update timestamps for
716 * @param string $timestamp Set the update timestamp to this value
718 * @return int[] Array of user IDs the timestamp has been updated for
720 public function updateNotificationTimestamp( User
$editor, LinkTarget
$target, $timestamp ) {
721 $dbw = $this->getConnection( DB_MASTER
);
722 $uids = $dbw->selectFieldValues(
726 'wl_user != ' . intval( $editor->getId() ),
727 'wl_namespace' => $target->getNamespace(),
728 'wl_title' => $target->getDBkey(),
729 'wl_notificationtimestamp IS NULL',
733 $this->reuseConnection( $dbw );
735 $watchers = array_map( 'intval', $uids );
737 // Update wl_notificationtimestamp for all watching users except the editor
739 DeferredUpdates
::addCallableUpdate(
740 function () use ( $timestamp, $watchers, $target, $fname ) {
741 global $wgUpdateRowsPerQuery;
743 $dbw = $this->getConnection( DB_MASTER
);
744 $factory = wfGetLBFactory();
745 $ticket = $factory->getEmptyTransactionTicket( __METHOD__
);
747 $watchersChunks = array_chunk( $watchers, $wgUpdateRowsPerQuery );
748 foreach ( $watchersChunks as $watchersChunk ) {
749 $dbw->update( 'watchlist',
751 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
752 ], [ /* WHERE - TODO Use wl_id T130067 */
753 'wl_user' => $watchersChunk,
754 'wl_namespace' => $target->getNamespace(),
755 'wl_title' => $target->getDBkey(),
758 if ( count( $watchersChunks ) > 1 ) {
759 $factory->commitAndWaitForReplication(
760 __METHOD__
, $ticket, [ 'wiki' => $dbw->getWikiID() ]
764 $this->uncacheLinkTarget( $target );
766 $this->reuseConnection( $dbw );
768 DeferredUpdates
::POSTSEND
,
777 * Reset the notification timestamp of this entry
780 * @param Title $title
781 * @param string $force Whether to force the write query to be executed even if the
782 * page is not watched or the notification timestamp is already NULL.
783 * 'force' in order to force
784 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
786 * @return bool success
788 public function resetNotificationTimestamp( User
$user, Title
$title, $force = '', $oldid = 0 ) {
789 // Only loggedin user can have a watchlist
790 if ( $this->loadBalancer
->getReadOnlyReason() !== false ||
$user->isAnon() ) {
795 if ( $force != 'force' ) {
796 $item = $this->loadWatchedItem( $user, $title );
797 if ( !$item ||
$item->getNotificationTimestamp() === null ) {
802 // If the page is watched by the user (or may be watched), update the timestamp
803 $job = new ActivityUpdateJob(
806 'type' => 'updateWatchlistNotification',
807 'userid' => $user->getId(),
808 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
813 // Try to run this post-send
814 // Calls DeferredUpdates::addCallableUpdate in normal operation
816 $this->deferredUpdatesAddCallableUpdateCallback
,
817 function() use ( $job ) {
822 $this->uncache( $user, $title );
827 private function getNotificationTimestamp( User
$user, Title
$title, $item, $force, $oldid ) {
829 // No oldid given, assuming latest revision; clear the timestamp.
833 if ( !$title->getNextRevisionID( $oldid ) ) {
834 // Oldid given and is the latest revision for this title; clear the timestamp.
838 if ( $item === null ) {
839 $item = $this->loadWatchedItem( $user, $title );
843 // This can only happen if $force is enabled.
847 // Oldid given and isn't the latest; update the timestamp.
848 // This will result in no further notification emails being sent!
849 // Calls Revision::getTimestampFromId in normal operation
850 $notificationTimestamp = call_user_func(
851 $this->revisionGetTimestampFromIdCallback
,
856 // We need to go one second to the future because of various strict comparisons
857 // throughout the codebase
858 $ts = new MWTimestamp( $notificationTimestamp );
859 $ts->timestamp
->add( new DateInterval( 'PT1S' ) );
860 $notificationTimestamp = $ts->getTimestamp( TS_MW
);
862 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
863 if ( $force != 'force' ) {
866 // This is a little silly…
867 return $item->getNotificationTimestamp();
871 return $notificationTimestamp;
876 * @param int $unreadLimit
878 * @return int|bool The number of unread notifications
879 * true if greater than or equal to $unreadLimit
881 public function countUnreadNotifications( User
$user, $unreadLimit = null ) {
883 if ( $unreadLimit !== null ) {
884 $unreadLimit = (int)$unreadLimit;
885 $queryOptions['LIMIT'] = $unreadLimit;
888 $dbr = $this->getConnection( DB_SLAVE
);
889 $rowCount = $dbr->selectRowCount(
893 'wl_user' => $user->getId(),
894 'wl_notificationtimestamp IS NOT NULL',
899 $this->reuseConnection( $dbr );
901 if ( !isset( $unreadLimit ) ) {
905 if ( $rowCount >= $unreadLimit ) {
913 * Check if the given title already is watched by the user, and if so
914 * add a watch for the new title.
916 * To be used for page renames and such.
918 * @param LinkTarget $oldTarget
919 * @param LinkTarget $newTarget
921 public function duplicateAllAssociatedEntries( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
922 $oldTarget = Title
::newFromLinkTarget( $oldTarget );
923 $newTarget = Title
::newFromLinkTarget( $newTarget );
925 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
926 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
930 * Check if the given title already is watched by the user, and if so
931 * add a watch for the new title.
933 * To be used for page renames and such.
934 * This must be called separately for Subject and Talk pages
936 * @param LinkTarget $oldTarget
937 * @param LinkTarget $newTarget
939 public function duplicateEntry( LinkTarget
$oldTarget, LinkTarget
$newTarget ) {
940 $dbw = $this->getConnection( DB_MASTER
);
942 $result = $dbw->select(
944 [ 'wl_user', 'wl_notificationtimestamp' ],
946 'wl_namespace' => $oldTarget->getNamespace(),
947 'wl_title' => $oldTarget->getDBkey(),
953 $newNamespace = $newTarget->getNamespace();
954 $newDBkey = $newTarget->getDBkey();
956 # Construct array to replace into the watchlist
958 foreach ( $result as $row ) {
960 'wl_user' => $row->wl_user
,
961 'wl_namespace' => $newNamespace,
962 'wl_title' => $newDBkey,
963 'wl_notificationtimestamp' => $row->wl_notificationtimestamp
,
967 if ( !empty( $values ) ) {
969 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
970 # some other DBMSes, mostly due to poor simulation by us
973 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
979 $this->reuseConnection( $dbw );