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;
10 use Wikimedia\Rdbms\DBUnexpectedError;
13 * Storage layer class for WatchedItems.
14 * Database interaction.
16 * Uses database because this uses User::isAnon
23 class WatchedItemStore implements StatsdAwareInterface {
25 const SORT_DESC = 'DESC';
26 const SORT_ASC = 'ASC';
31 private $loadBalancer;
36 private $readOnlyMode;
44 * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key'
45 * The index is needed so that on mass changes all relevant items can be un-cached.
46 * For example: Clearing a users watchlist of all items or updating notification timestamps
47 * for all users watching a single target.
49 private $cacheIndex = [];
54 private $deferredUpdatesAddCallableUpdateCallback;
59 private $revisionGetTimestampFromIdCallback;
62 * @var StatsdDataFactoryInterface
67 * @param LoadBalancer $loadBalancer
68 * @param HashBagOStuff $cache
69 * @param ReadOnlyMode $readOnlyMode
71 public function __construct(
72 LoadBalancer $loadBalancer,
74 ReadOnlyMode $readOnlyMode
76 $this->loadBalancer = $loadBalancer;
77 $this->cache = $cache;
78 $this->readOnlyMode = $readOnlyMode;
79 $this->stats = new NullStatsdDataFactory();
80 $this->deferredUpdatesAddCallableUpdateCallback = [ 'DeferredUpdates', 'addCallableUpdate' ];
81 $this->revisionGetTimestampFromIdCallback = [ 'Revision', 'getTimestampFromId' ];
84 public function setStatsdDataFactory( StatsdDataFactoryInterface $stats ) {
85 $this->stats = $stats;
89 * Overrides the DeferredUpdates::addCallableUpdate callback
90 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
92 * @param callable $callback
94 * @see DeferredUpdates::addCallableUpdate for callback signiture
96 * @return ScopedCallback to reset the overridden value
99 public function overrideDeferredUpdatesAddCallableUpdateCallback( callable $callback ) {
100 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
101 throw new MWException(
102 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
105 $previousValue = $this->deferredUpdatesAddCallableUpdateCallback;
106 $this->deferredUpdatesAddCallableUpdateCallback = $callback;
107 return new ScopedCallback( function () use ( $previousValue ) {
108 $this->deferredUpdatesAddCallableUpdateCallback = $previousValue;
113 * Overrides the Revision::getTimestampFromId callback
114 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
116 * @param callable $callback
117 * @see Revision::getTimestampFromId for callback signiture
119 * @return ScopedCallback to reset the overridden value
120 * @throws MWException
122 public function overrideRevisionGetTimestampFromIdCallback( callable $callback ) {
123 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
124 throw new MWException(
125 'Cannot override Revision::getTimestampFromId callback in operation.'
128 $previousValue = $this->revisionGetTimestampFromIdCallback;
129 $this->revisionGetTimestampFromIdCallback = $callback;
130 return new ScopedCallback( function () use ( $previousValue ) {
131 $this->revisionGetTimestampFromIdCallback = $previousValue;
135 private function getCacheKey( User $user, LinkTarget $target ) {
136 return $this->cache->makeKey(
137 (string)$target->getNamespace(),
139 (string)$user->getId()
143 private function cache( WatchedItem $item ) {
144 $user = $item->getUser();
145 $target = $item->getLinkTarget();
146 $key = $this->getCacheKey( $user, $target );
147 $this->cache->set( $key, $item );
148 $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
149 $this->stats->increment( 'WatchedItemStore.cache' );
152 private function uncache( User $user, LinkTarget $target ) {
153 $this->cache->delete( $this->getCacheKey( $user, $target ) );
154 unset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] );
155 $this->stats->increment( 'WatchedItemStore.uncache' );
158 private function uncacheLinkTarget( LinkTarget $target ) {
159 $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget' );
160 if ( !isset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] ) ) {
163 foreach ( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] as $key ) {
164 $this->stats->increment( 'WatchedItemStore.uncacheLinkTarget.items' );
165 $this->cache->delete( $key );
169 private function uncacheUser( User $user ) {
170 $this->stats->increment( 'WatchedItemStore.uncacheUser' );
171 foreach ( $this->cacheIndex as $ns => $dbKeyArray ) {
172 foreach ( $dbKeyArray as $dbKey => $userArray ) {
173 if ( isset( $userArray[$user->getId()] ) ) {
174 $this->stats->increment( 'WatchedItemStore.uncacheUser.items' );
175 $this->cache->delete( $userArray[$user->getId()] );
183 * @param LinkTarget $target
185 * @return WatchedItem|false
187 private function getCached( User $user, LinkTarget $target ) {
188 return $this->cache->get( $this->getCacheKey( $user, $target ) );
192 * Return an array of conditions to select or update the appropriate database
196 * @param LinkTarget $target
200 private function dbCond( User $user, LinkTarget $target ) {
202 'wl_user' => $user->getId(),
203 'wl_namespace' => $target->getNamespace(),
204 'wl_title' => $target->getDBkey(),
209 * @param int $dbIndex DB_MASTER or DB_REPLICA
212 * @throws MWException
214 private function getConnectionRef( $dbIndex ) {
215 return $this->loadBalancer->getConnectionRef( $dbIndex, [ 'watchlist' ] );
219 * Count the number of individual items that are watched by the user.
220 * If a subject and corresponding talk page are watched this will return 2.
226 public function countWatchedItems( User $user ) {
227 $dbr = $this->getConnectionRef( DB_REPLICA );
228 $return = (int)$dbr->selectField(
232 'wl_user' => $user->getId()
241 * @param LinkTarget $target
245 public function countWatchers( LinkTarget $target ) {
246 $dbr = $this->getConnectionRef( DB_REPLICA );
247 $return = (int)$dbr->selectField(
251 'wl_namespace' => $target->getNamespace(),
252 'wl_title' => $target->getDBkey(),
261 * Number of page watchers who also visited a "recent" edit
263 * @param LinkTarget $target
264 * @param mixed $threshold timestamp accepted by wfTimestamp
267 * @throws DBUnexpectedError
268 * @throws MWException
270 public function countVisitingWatchers( LinkTarget $target, $threshold ) {
271 $dbr = $this->getConnectionRef( DB_REPLICA );
272 $visitingWatchers = (int)$dbr->selectField(
276 'wl_namespace' => $target->getNamespace(),
277 'wl_title' => $target->getDBkey(),
278 'wl_notificationtimestamp >= ' .
279 $dbr->addQuotes( $dbr->timestamp( $threshold ) ) .
280 ' OR wl_notificationtimestamp IS NULL'
285 return $visitingWatchers;
289 * @param LinkTarget[] $targets
290 * @param array $options Allowed keys:
291 * 'minimumWatchers' => int
293 * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers
294 * All targets will be present in the result. 0 either means no watchers or the number
295 * of watchers was below the minimumWatchers option if passed.
297 public function countWatchersMultiple( array $targets, array $options = [] ) {
298 if ( $targets === [] ) {
299 // No titles requested => no results returned
303 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
305 $dbr = $this->getConnectionRef( DB_REPLICA );
307 if ( array_key_exists( 'minimumWatchers', $options ) ) {
308 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers'];
311 $lb = new LinkBatch( $targets );
314 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
315 [ $lb->constructSet( 'wl', $dbr ) ],
321 foreach ( $targets as $linkTarget ) {
322 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
325 foreach ( $res as $row ) {
326 $watchCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
333 * Number of watchers of each page who have visited recent edits to that page
335 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed $threshold),
337 * - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp)
338 * - null if $target doesn't exist
339 * @param int|null $minimumWatchers
340 * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers,
341 * where $watchers is an int:
342 * - if the page exists, number of users watching who have visited the page recently
343 * - if the page doesn't exist, number of users that have the page on their watchlist
344 * - 0 means there are no visiting watchers or their number is below the minimumWatchers
345 * option (if passed).
347 public function countVisitingWatchersMultiple(
348 array $targetsWithVisitThresholds,
349 $minimumWatchers = null
351 $dbr = $this->getConnectionRef( DB_REPLICA );
353 $conds = $this->getVisitingWatchersCondition( $dbr, $targetsWithVisitThresholds );
355 $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ];
356 if ( $minimumWatchers !== null ) {
357 $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$minimumWatchers;
361 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
368 foreach ( $targetsWithVisitThresholds as list( $target ) ) {
369 /* @var LinkTarget $target */
370 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
373 foreach ( $res as $row ) {
374 $watcherCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
377 return $watcherCounts;
381 * Generates condition for the query used in a batch count visiting watchers.
383 * @param IDatabase $db
384 * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold)
387 private function getVisitingWatchersCondition(
389 array $targetsWithVisitThresholds
391 $missingTargets = [];
392 $namespaceConds = [];
393 foreach ( $targetsWithVisitThresholds as list( $target, $threshold ) ) {
394 if ( $threshold === null ) {
395 $missingTargets[] = $target;
398 /* @var LinkTarget $target */
399 $namespaceConds[$target->getNamespace()][] = $db->makeList( [
400 'wl_title = ' . $db->addQuotes( $target->getDBkey() ),
402 'wl_notificationtimestamp >= ' . $db->addQuotes( $db->timestamp( $threshold ) ),
403 'wl_notificationtimestamp IS NULL'
409 foreach ( $namespaceConds as $namespace => $pageConds ) {
410 $conds[] = $db->makeList( [
411 'wl_namespace = ' . $namespace,
412 '(' . $db->makeList( $pageConds, LIST_OR ) . ')'
416 if ( $missingTargets ) {
417 $lb = new LinkBatch( $missingTargets );
418 $conds[] = $lb->constructSet( 'wl', $db );
421 return $db->makeList( $conds, LIST_OR );
425 * Get an item (may be cached)
428 * @param LinkTarget $target
430 * @return WatchedItem|false
432 public function getWatchedItem( User $user, LinkTarget $target ) {
433 if ( $user->isAnon() ) {
437 $cached = $this->getCached( $user, $target );
439 $this->stats->increment( 'WatchedItemStore.getWatchedItem.cached' );
442 $this->stats->increment( 'WatchedItemStore.getWatchedItem.load' );
443 return $this->loadWatchedItem( $user, $target );
447 * Loads an item from the db
450 * @param LinkTarget $target
452 * @return WatchedItem|false
454 public function loadWatchedItem( User $user, LinkTarget $target ) {
455 // Only loggedin user can have a watchlist
456 if ( $user->isAnon() ) {
460 $dbr = $this->getConnectionRef( DB_REPLICA );
461 $row = $dbr->selectRow(
463 'wl_notificationtimestamp',
464 $this->dbCond( $user, $target ),
472 $item = new WatchedItem(
475 wfTimestampOrNull( TS_MW, $row->wl_notificationtimestamp )
477 $this->cache( $item );
484 * @param array $options Allowed keys:
485 * 'forWrite' => bool defaults to false
486 * 'sort' => string optional sorting by namespace ID and title
487 * one of the self::SORT_* constants
489 * @return WatchedItem[]
491 public function getWatchedItemsForUser( User $user, array $options = [] ) {
492 $options += [ 'forWrite' => false ];
495 if ( array_key_exists( 'sort', $options ) ) {
497 ( in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ) ),
498 '$options[\'sort\']',
499 'must be SORT_ASC or SORT_DESC'
501 $dbOptions['ORDER BY'] = [
502 "wl_namespace {$options['sort']}",
503 "wl_title {$options['sort']}"
506 $db = $this->getConnectionRef( $options['forWrite'] ? DB_MASTER : DB_REPLICA );
510 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
511 [ 'wl_user' => $user->getId() ],
517 foreach ( $res as $row ) {
518 // @todo: Should we add these to the process cache?
519 $watchedItems[] = new WatchedItem(
521 new TitleValue( (int)$row->wl_namespace, $row->wl_title ),
522 $row->wl_notificationtimestamp
526 return $watchedItems;
530 * Must be called separately for Subject & Talk namespaces
533 * @param LinkTarget $target
537 public function isWatched( User $user, LinkTarget $target ) {
538 return (bool)$this->getWatchedItem( $user, $target );
543 * @param LinkTarget[] $targets
545 * @return array multi-dimensional like $return[$namespaceId][$titleString] = $timestamp,
546 * where $timestamp is:
547 * - string|null value of wl_notificationtimestamp,
548 * - false if $target is not watched by $user.
550 public function getNotificationTimestampsBatch( User $user, array $targets ) {
552 foreach ( $targets as $target ) {
553 $timestamps[$target->getNamespace()][$target->getDBkey()] = false;
556 if ( $user->isAnon() ) {
561 foreach ( $targets as $target ) {
562 $cachedItem = $this->getCached( $user, $target );
564 $timestamps[$target->getNamespace()][$target->getDBkey()] =
565 $cachedItem->getNotificationTimestamp();
567 $targetsToLoad[] = $target;
571 if ( !$targetsToLoad ) {
575 $dbr = $this->getConnectionRef( DB_REPLICA );
577 $lb = new LinkBatch( $targetsToLoad );
580 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
582 $lb->constructSet( 'wl', $dbr ),
583 'wl_user' => $user->getId(),
588 foreach ( $res as $row ) {
589 $timestamps[$row->wl_namespace][$row->wl_title] =
590 wfTimestampOrNull( TS_MW, $row->wl_notificationtimestamp );
597 * Must be called separately for Subject & Talk namespaces
600 * @param LinkTarget $target
602 public function addWatch( User $user, LinkTarget $target ) {
603 $this->addWatchBatchForUser( $user, [ $target ] );
608 * @param LinkTarget[] $targets
610 * @return bool success
612 public function addWatchBatchForUser( User $user, array $targets ) {
613 if ( $this->readOnlyMode->isReadOnly() ) {
616 // Only loggedin user can have a watchlist
617 if ( $user->isAnon() ) {
627 foreach ( $targets as $target ) {
629 'wl_user' => $user->getId(),
630 'wl_namespace' => $target->getNamespace(),
631 'wl_title' => $target->getDBkey(),
632 'wl_notificationtimestamp' => null,
634 $items[] = new WatchedItem(
639 $this->uncache( $user, $target );
642 $dbw = $this->getConnectionRef( DB_MASTER );
643 foreach ( array_chunk( $rows, 100 ) as $toInsert ) {
644 // Use INSERT IGNORE to avoid overwriting the notification timestamp
645 // if there's already an entry for this page
646 $dbw->insert( 'watchlist', $toInsert, __METHOD__, 'IGNORE' );
648 // Update process cache to ensure skin doesn't claim that the current
649 // page is unwatched in the response of action=watch itself (T28292).
650 // This would otherwise be re-queried from a slave by isWatched().
651 foreach ( $items as $item ) {
652 $this->cache( $item );
659 * Removes the an entry for the User watching the LinkTarget
660 * Must be called separately for Subject & Talk namespaces
663 * @param LinkTarget $target
665 * @return bool success
666 * @throws DBUnexpectedError
667 * @throws MWException
669 public function removeWatch( User $user, LinkTarget $target ) {
670 // Only logged in user can have a watchlist
671 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
675 $this->uncache( $user, $target );
677 $dbw = $this->getConnectionRef( DB_MASTER );
678 $dbw->delete( 'watchlist',
680 'wl_user' => $user->getId(),
681 'wl_namespace' => $target->getNamespace(),
682 'wl_title' => $target->getDBkey(),
685 $success = (bool)$dbw->affectedRows();
691 * @param User $user The user to set the timestamp for
692 * @param string|null $timestamp Set the update timestamp to this value
693 * @param LinkTarget[] $targets List of targets to update. Default to all targets
695 * @return bool success
697 public function setNotificationTimestampsForUser( User $user, $timestamp, array $targets = [] ) {
698 // Only loggedin user can have a watchlist
699 if ( $user->isAnon() ) {
703 $dbw = $this->getConnectionRef( DB_MASTER );
705 $conds = [ 'wl_user' => $user->getId() ];
707 $batch = new LinkBatch( $targets );
708 $conds[] = $batch->constructSet( 'wl', $dbw );
711 if ( $timestamp !== null ) {
712 $timestamp = $dbw->timestamp( $timestamp );
715 $success = $dbw->update(
717 [ 'wl_notificationtimestamp' => $timestamp ],
722 $this->uncacheUser( $user );
728 * @param User $editor The editor that triggered the update. Their notification
729 * timestamp will not be updated(they have already seen it)
730 * @param LinkTarget $target The target to update timestamps for
731 * @param string $timestamp Set the update timestamp to this value
733 * @return int[] Array of user IDs the timestamp has been updated for
735 public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
736 $dbw = $this->getConnectionRef( DB_MASTER );
737 $uids = $dbw->selectFieldValues(
741 'wl_user != ' . intval( $editor->getId() ),
742 'wl_namespace' => $target->getNamespace(),
743 'wl_title' => $target->getDBkey(),
744 'wl_notificationtimestamp IS NULL',
749 $watchers = array_map( 'intval', $uids );
751 // Update wl_notificationtimestamp for all watching users except the editor
753 DeferredUpdates::addCallableUpdate(
754 function () use ( $timestamp, $watchers, $target, $fname ) {
755 global $wgUpdateRowsPerQuery;
757 $dbw = $this->getConnectionRef( DB_MASTER );
758 $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
759 $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
761 $watchersChunks = array_chunk( $watchers, $wgUpdateRowsPerQuery );
762 foreach ( $watchersChunks as $watchersChunk ) {
763 $dbw->update( 'watchlist',
765 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
766 ], [ /* WHERE - TODO Use wl_id T130067 */
767 'wl_user' => $watchersChunk,
768 'wl_namespace' => $target->getNamespace(),
769 'wl_title' => $target->getDBkey(),
772 if ( count( $watchersChunks ) > 1 ) {
773 $factory->commitAndWaitForReplication(
774 __METHOD__, $ticket, [ 'domain' => $dbw->getDomainID() ]
778 $this->uncacheLinkTarget( $target );
780 DeferredUpdates::POSTSEND,
789 * Reset the notification timestamp of this entry
792 * @param Title $title
793 * @param string $force Whether to force the write query to be executed even if the
794 * page is not watched or the notification timestamp is already NULL.
795 * 'force' in order to force
796 * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
798 * @return bool success
800 public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) {
801 // Only loggedin user can have a watchlist
802 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
807 if ( $force != 'force' ) {
808 $item = $this->loadWatchedItem( $user, $title );
809 if ( !$item || $item->getNotificationTimestamp() === null ) {
814 // If the page is watched by the user (or may be watched), update the timestamp
815 $job = new ActivityUpdateJob(
818 'type' => 'updateWatchlistNotification',
819 'userid' => $user->getId(),
820 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
825 // Try to run this post-send
826 // Calls DeferredUpdates::addCallableUpdate in normal operation
828 $this->deferredUpdatesAddCallableUpdateCallback,
829 function () use ( $job ) {
834 $this->uncache( $user, $title );
839 private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) {
841 // No oldid given, assuming latest revision; clear the timestamp.
845 if ( !$title->getNextRevisionID( $oldid ) ) {
846 // Oldid given and is the latest revision for this title; clear the timestamp.
850 if ( $item === null ) {
851 $item = $this->loadWatchedItem( $user, $title );
855 // This can only happen if $force is enabled.
859 // Oldid given and isn't the latest; update the timestamp.
860 // This will result in no further notification emails being sent!
861 // Calls Revision::getTimestampFromId in normal operation
862 $notificationTimestamp = call_user_func(
863 $this->revisionGetTimestampFromIdCallback,
868 // We need to go one second to the future because of various strict comparisons
869 // throughout the codebase
870 $ts = new MWTimestamp( $notificationTimestamp );
871 $ts->timestamp->add( new DateInterval( 'PT1S' ) );
872 $notificationTimestamp = $ts->getTimestamp( TS_MW );
874 if ( $notificationTimestamp < $item->getNotificationTimestamp() ) {
875 if ( $force != 'force' ) {
878 // This is a little silly…
879 return $item->getNotificationTimestamp();
883 return $notificationTimestamp;
888 * @param int $unreadLimit
890 * @return int|bool The number of unread notifications
891 * true if greater than or equal to $unreadLimit
893 public function countUnreadNotifications( User $user, $unreadLimit = null ) {
895 if ( $unreadLimit !== null ) {
896 $unreadLimit = (int)$unreadLimit;
897 $queryOptions['LIMIT'] = $unreadLimit;
900 $dbr = $this->getConnectionRef( DB_REPLICA );
901 $rowCount = $dbr->selectRowCount(
905 'wl_user' => $user->getId(),
906 'wl_notificationtimestamp IS NOT NULL',
912 if ( !isset( $unreadLimit ) ) {
916 if ( $rowCount >= $unreadLimit ) {
924 * Check if the given title already is watched by the user, and if so
925 * add a watch for the new title.
927 * To be used for page renames and such.
929 * @param LinkTarget $oldTarget
930 * @param LinkTarget $newTarget
932 public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) {
933 $oldTarget = Title::newFromLinkTarget( $oldTarget );
934 $newTarget = Title::newFromLinkTarget( $newTarget );
936 $this->duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
937 $this->duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
941 * Check if the given title already is watched by the user, and if so
942 * add a watch for the new title.
944 * To be used for page renames and such.
945 * This must be called separately for Subject and Talk pages
947 * @param LinkTarget $oldTarget
948 * @param LinkTarget $newTarget
950 public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) {
951 $dbw = $this->getConnectionRef( DB_MASTER );
953 $result = $dbw->select(
955 [ 'wl_user', 'wl_notificationtimestamp' ],
957 'wl_namespace' => $oldTarget->getNamespace(),
958 'wl_title' => $oldTarget->getDBkey(),
964 $newNamespace = $newTarget->getNamespace();
965 $newDBkey = $newTarget->getDBkey();
967 # Construct array to replace into the watchlist
969 foreach ( $result as $row ) {
971 'wl_user' => $row->wl_user,
972 'wl_namespace' => $newNamespace,
973 'wl_title' => $newDBkey,
974 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
978 if ( !empty( $values ) ) {
980 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
981 # some other DBMSes, mostly due to poor simulation by us
984 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],