4use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
7use Wikimedia\Assert\Assert;
8use Wikimedia\ScopedCallback;
80 $this->deferredUpdatesAddCallableUpdateCallback =
81 [ DeferredUpdates::class,
'addCallableUpdate' ];
82 $this->revisionGetTimestampFromIdCallback =
83 [ Revision::class,
'getTimestampFromId' ];
106 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
108 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
112 $this->deferredUpdatesAddCallableUpdateCallback = $callback;
113 return new ScopedCallback(
function () use ( $previousValue ) {
114 $this->deferredUpdatesAddCallableUpdateCallback = $previousValue;
129 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
131 'Cannot override Revision::getTimestampFromId callback in operation.'
135 $this->revisionGetTimestampFromIdCallback = $callback;
136 return new ScopedCallback(
function () use ( $previousValue ) {
137 $this->revisionGetTimestampFromIdCallback = $previousValue;
142 return $this->
cache->makeKey(
145 (
string)$user->getId()
150 $user = $item->getUser();
151 $target = $item->getLinkTarget();
153 $this->
cache->set( $key, $item );
154 $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
155 $this->stats->increment(
'WatchedItemStore.cache' );
161 $this->stats->increment(
'WatchedItemStore.uncache' );
165 $this->stats->increment(
'WatchedItemStore.uncacheLinkTarget' );
170 $this->stats->increment(
'WatchedItemStore.uncacheLinkTarget.items' );
171 $this->
cache->delete( $key );
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()] );
208 'wl_user' => $user->getId(),
221 return $this->loadBalancer->getConnectionRef( $dbIndex, [
'watchlist' ] );
239 $dbw = $this->loadBalancer->getConnectionRef(
DB_MASTER );
242 [
'wl_user' => $user->getId() ],
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] );
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] );
293 return (
int)
$dbr->selectField(
308 $return = (int)
$dbr->selectField(
312 'wl_user' => $user->getId()
327 $return = (int)
$dbr->selectField(
348 $visitingWatchers = (int)
$dbr->selectField(
354 'wl_notificationtimestamp >= ' .
355 $dbr->addQuotes(
$dbr->timestamp( $threshold ) ) .
356 ' OR wl_notificationtimestamp IS NULL'
361 return $visitingWatchers;
371 $dbOptions = [
'GROUP BY' => [
'wl_namespace',
'wl_title' ] ];
375 if ( array_key_exists(
'minimumWatchers',
$options ) ) {
376 $dbOptions[
'HAVING'] =
'COUNT(*) >= ' . (int)
$options[
'minimumWatchers'];
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;
407 array $targetsWithVisitThresholds,
408 $minimumWatchers =
null
410 if ( $targetsWithVisitThresholds === [] ) {
419 $dbOptions = [
'GROUP BY' => [
'wl_namespace',
'wl_title' ] ];
420 if ( $minimumWatchers !==
null ) {
421 $dbOptions[
'HAVING'] =
'COUNT(*) >= ' . (int)$minimumWatchers;
425 [
'wl_namespace',
'wl_title',
'watchers' =>
'COUNT(*)' ],
432 foreach ( $targetsWithVisitThresholds as
list( $target ) ) {
434 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
437 foreach (
$res as $row ) {
438 $watcherCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
441 return $watcherCounts;
453 array $targetsWithVisitThresholds
455 $missingTargets = [];
456 $namespaceConds = [];
457 foreach ( $targetsWithVisitThresholds as
list( $target, $threshold ) ) {
458 if ( $threshold ===
null ) {
459 $missingTargets[] = $target;
463 $namespaceConds[$target->getNamespace()][] = $db->
makeList( [
464 'wl_title = ' . $db->
addQuotes( $target->getDBkey() ),
467 'wl_notificationtimestamp IS NULL'
473 foreach ( $namespaceConds as $namespace => $pageConds ) {
475 'wl_namespace = ' . $namespace,
480 if ( $missingTargets ) {
482 $conds[] = $lb->constructSet(
'wl', $db );
495 if ( $user->isAnon() ) {
499 $cached = $this->
getCached( $user, $target );
501 $this->stats->increment(
'WatchedItemStore.getWatchedItem.cached' );
504 $this->stats->increment(
'WatchedItemStore.getWatchedItem.load' );
516 if ( $user->isAnon() ) {
521 $row =
$dbr->selectRow(
523 'wl_notificationtimestamp',
524 $this->
dbCond( $user, $target ),
537 $this->
cache( $item );
552 if ( array_key_exists(
'sort',
$options ) ) {
554 ( in_array(
$options[
'sort'], [ self::SORT_ASC, self::SORT_DESC ] ) ),
555 '$options[\'sort\']',
556 'must be SORT_ASC or SORT_DESC'
558 $dbOptions[
'ORDER BY'] = [
559 "wl_namespace {$options['sort']}",
560 "wl_title {$options['sort']}"
567 [
'wl_namespace',
'wl_title',
'wl_notificationtimestamp' ],
568 [
'wl_user' => $user->getId() ],
574 foreach (
$res as $row ) {
578 new TitleValue( (
int)$row->wl_namespace, $row->wl_title ),
579 $row->wl_notificationtimestamp
583 return $watchedItems;
604 foreach ( $targets as $target ) {
605 $timestamps[$target->getNamespace()][$target->getDBkey()] =
false;
608 if ( $user->isAnon() ) {
613 foreach ( $targets as $target ) {
614 $cachedItem = $this->
getCached( $user, $target );
616 $timestamps[$target->getNamespace()][$target->getDBkey()] =
617 $cachedItem->getNotificationTimestamp();
619 $targetsToLoad[] = $target;
623 if ( !$targetsToLoad ) {
632 [
'wl_namespace',
'wl_title',
'wl_notificationtimestamp' ],
634 $lb->constructSet(
'wl',
$dbr ),
635 'wl_user' => $user->getId(),
640 foreach (
$res as $row ) {
641 $timestamps[$row->wl_namespace][$row->wl_title] =
664 if ( $this->readOnlyMode->isReadOnly() ) {
668 if ( $user->isAnon() ) {
678 foreach ( $targets as $target ) {
680 'wl_user' => $user->getId(),
681 'wl_namespace' => $target->getNamespace(),
682 'wl_title' => $target->getDBkey(),
683 'wl_notificationtimestamp' =>
null,
690 $this->
uncache( $user, $target );
694 foreach ( array_chunk(
$rows, 100 ) as $toInsert ) {
697 $dbw->insert(
'watchlist', $toInsert, __METHOD__,
'IGNORE' );
702 foreach ( $items as $item ) {
703 $this->
cache( $item );
717 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
721 $this->
uncache( $user, $target );
724 $dbw->delete(
'watchlist',
726 'wl_user' => $user->getId(),
731 $success = (bool)$dbw->affectedRows();
745 if ( $user->isAnon() ) {
751 $conds = [
'wl_user' => $user->getId() ];
754 $conds[] =
$batch->constructSet(
'wl', $dbw );
757 if ( $timestamp !==
null ) {
758 $timestamp = $dbw->timestamp( $timestamp );
763 [
'wl_notificationtimestamp' => $timestamp ],
775 if ( $user->isAnon() ) {
781 $user->getUserPage(),
782 [
'userId' => $user->getId(),
'casTime' => time() ]
788 $this->deferredUpdatesAddCallableUpdateCallback,
789 function () use (
$job ) {
804 $uids = $dbw->selectFieldValues(
808 'wl_user != ' . intval(
$editor->getId() ),
811 'wl_notificationtimestamp IS NULL',
816 $watchers = array_map(
'intval', $uids );
820 DeferredUpdates::addCallableUpdate(
821 function () use ( $timestamp, $watchers, $target,
$fname ) {
825 $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
826 $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
829 foreach ( $watchersChunks as $watchersChunk ) {
830 $dbw->update(
'watchlist',
832 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
834 'wl_user' => $watchersChunk,
839 if ( count( $watchersChunks ) > 1 ) {
840 $factory->commitAndWaitForReplication(
841 __METHOD__, $ticket, [
'domain' => $dbw->getDomainID() ]
847 DeferredUpdates::POSTSEND,
865 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
870 if ( $force !=
'force' ) {
872 if ( !$item || $item->getNotificationTimestamp() ===
null ) {
881 'type' =>
'updateWatchlistNotification',
882 'userid' => $user->getId(),
883 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
891 $this->deferredUpdatesAddCallableUpdateCallback,
892 function () use (
$job ) {
897 $this->
uncache( $user, $title );
908 if ( !$title->getNextRevisionID( $oldid ) ) {
913 if ( $item ===
null ) {
925 $notificationTimestamp = call_user_func(
926 $this->revisionGetTimestampFromIdCallback,
934 $ts->timestamp->add(
new DateInterval(
'PT1S' ) );
935 $notificationTimestamp = $ts->getTimestamp( TS_MW );
938 if ( $force !=
'force' ) {
942 return $item->getNotificationTimestamp();
946 return $notificationTimestamp;
957 if ( $unreadLimit !==
null ) {
958 $unreadLimit = (int)$unreadLimit;
959 $queryOptions[
'LIMIT'] = $unreadLimit;
963 $rowCount =
$dbr->selectRowCount(
967 'wl_user' => $user->getId(),
968 'wl_notificationtimestamp IS NOT NULL',
974 if ( !isset( $unreadLimit ) ) {
978 if ( $rowCount >= $unreadLimit ) {
991 $oldTarget = Title::newFromLinkTarget( $oldTarget );
992 $newTarget = Title::newFromLinkTarget( $newTarget );
994 $this->
duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
995 $this->
duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
1006 $result = $dbw->select(
1008 [
'wl_user',
'wl_notificationtimestamp' ],
1010 'wl_namespace' => $oldTarget->getNamespace(),
1011 'wl_title' => $oldTarget->getDBkey(),
1017 $newNamespace = $newTarget->getNamespace();
1018 $newDBkey = $newTarget->getDBkey();
1020 # Construct array to replace into the watchlist
1022 foreach ( $result as $row ) {
1024 'wl_user' => $row->wl_user,
1025 'wl_namespace' => $newNamespace,
1026 'wl_title' => $newDBkey,
1027 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
1031 if ( !empty( $values ) ) {
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
1037 [ [
'wl_user',
'wl_namespace',
'wl_title' ] ],
$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,...
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 ...
Library for creating and parsing MW-style timestamps.
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.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
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)
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.
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.
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
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
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
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
processing should stop and the error should be shown to the user * false
Describes a Statsd aware interface.
you have access to all of the normal MediaWiki so you can get a DB use the cache
if(count( $args)< 1) $job