4use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
6use Wikimedia\Assert\Assert;
7use Wikimedia\ScopedCallback;
86 $this->deferredUpdatesAddCallableUpdateCallback =
87 [ DeferredUpdates::class,
'addCallableUpdate' ];
88 $this->revisionGetTimestampFromIdCallback =
89 [ Revision::class,
'getTimestampFromId' ];
112 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
114 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
118 $this->deferredUpdatesAddCallableUpdateCallback = $callback;
119 return new ScopedCallback(
function () use ( $previousValue ) {
120 $this->deferredUpdatesAddCallableUpdateCallback = $previousValue;
135 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
137 'Cannot override Revision::getTimestampFromId callback in operation.'
141 $this->revisionGetTimestampFromIdCallback = $callback;
142 return new ScopedCallback(
function () use ( $previousValue ) {
143 $this->revisionGetTimestampFromIdCallback = $previousValue;
148 return $this->
cache->makeKey(
151 (
string)$user->getId()
156 $user = $item->getUser();
157 $target = $item->getLinkTarget();
159 $this->
cache->set( $key, $item );
160 $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key;
161 $this->stats->increment(
'WatchedItemStore.cache' );
167 $this->stats->increment(
'WatchedItemStore.uncache' );
171 $this->stats->increment(
'WatchedItemStore.uncacheLinkTarget' );
176 $this->stats->increment(
'WatchedItemStore.uncacheLinkTarget.items' );
177 $this->
cache->delete( $key );
182 $this->stats->increment(
'WatchedItemStore.uncacheUser' );
183 foreach ( $this->cacheIndex as $ns => $dbKeyArray ) {
184 foreach ( $dbKeyArray as $dbKey => $userArray ) {
185 if ( isset( $userArray[$user->getId()] ) ) {
186 $this->stats->increment(
'WatchedItemStore.uncacheUser.items' );
187 $this->
cache->delete( $userArray[$user->getId()] );
214 'wl_user' => $user->getId(),
227 return $this->loadBalancer->getConnectionRef( $dbIndex, [
'watchlist' ] );
245 $dbw = $this->loadBalancer->getConnectionRef(
DB_MASTER );
248 [
'wl_user' => $user->getId() ],
257 $userId = $user->getId();
258 foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
259 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
260 if ( array_key_exists( $userId, $userIndex ) ) {
261 $this->
cache->delete( $userIndex[$userId] );
262 unset( $this->cacheIndex[$ns][$dbKey][$userId] );
268 foreach ( $this->cacheIndex as $ns => $dbKeyIndex ) {
269 foreach ( $dbKeyIndex as $dbKey => $userIndex ) {
270 if ( empty( $this->cacheIndex[$ns][$dbKey] ) ) {
271 unset( $this->cacheIndex[$ns][$dbKey] );
274 if ( empty( $this->cacheIndex[$ns] ) ) {
275 unset( $this->cacheIndex[$ns] );
290 JobQueueGroup::singleton()->push(
$job );
299 return (
int)
$dbr->selectField(
314 $return = (int)
$dbr->selectField(
318 'wl_user' => $user->getId()
333 $return = (int)
$dbr->selectField(
354 $visitingWatchers = (int)
$dbr->selectField(
360 'wl_notificationtimestamp >= ' .
361 $dbr->addQuotes(
$dbr->timestamp( $threshold ) ) .
362 ' OR wl_notificationtimestamp IS NULL'
367 return $visitingWatchers;
377 $dbOptions = [
'GROUP BY' => [
'wl_namespace',
'wl_title' ] ];
381 if ( array_key_exists(
'minimumWatchers',
$options ) ) {
382 $dbOptions[
'HAVING'] =
'COUNT(*) >= ' . (int)
$options[
'minimumWatchers'];
388 [
'wl_title',
'wl_namespace',
'watchers' =>
'COUNT(*)' ],
389 [ $lb->constructSet(
'wl',
$dbr ) ],
395 foreach ( $targets as $linkTarget ) {
396 $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0;
399 foreach (
$res as $row ) {
400 $watchCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
413 array $targetsWithVisitThresholds,
414 $minimumWatchers =
null
416 if ( $targetsWithVisitThresholds === [] ) {
425 $dbOptions = [
'GROUP BY' => [
'wl_namespace',
'wl_title' ] ];
426 if ( $minimumWatchers !==
null ) {
427 $dbOptions[
'HAVING'] =
'COUNT(*) >= ' . (int)$minimumWatchers;
431 [
'wl_namespace',
'wl_title',
'watchers' =>
'COUNT(*)' ],
438 foreach ( $targetsWithVisitThresholds as
list( $target ) ) {
440 $watcherCounts[$target->getNamespace()][$target->getDBkey()] = 0;
443 foreach (
$res as $row ) {
444 $watcherCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers;
447 return $watcherCounts;
459 array $targetsWithVisitThresholds
461 $missingTargets = [];
462 $namespaceConds = [];
463 foreach ( $targetsWithVisitThresholds as
list( $target, $threshold ) ) {
464 if ( $threshold ===
null ) {
465 $missingTargets[] = $target;
469 $namespaceConds[$target->getNamespace()][] = $db->
makeList( [
470 'wl_title = ' . $db->
addQuotes( $target->getDBkey() ),
473 'wl_notificationtimestamp IS NULL'
479 foreach ( $namespaceConds as $namespace => $pageConds ) {
481 'wl_namespace = ' . $namespace,
486 if ( $missingTargets ) {
488 $conds[] = $lb->constructSet(
'wl', $db );
501 if ( $user->isAnon() ) {
505 $cached = $this->
getCached( $user, $target );
507 $this->stats->increment(
'WatchedItemStore.getWatchedItem.cached' );
510 $this->stats->increment(
'WatchedItemStore.getWatchedItem.load' );
522 if ( $user->isAnon() ) {
527 $row =
$dbr->selectRow(
529 'wl_notificationtimestamp',
530 $this->
dbCond( $user, $target ),
543 $this->
cache( $item );
558 if ( array_key_exists(
'sort',
$options ) ) {
560 ( in_array(
$options[
'sort'], [ self::SORT_ASC, self::SORT_DESC ] ) ),
561 '$options[\'sort\']',
562 'must be SORT_ASC or SORT_DESC'
564 $dbOptions[
'ORDER BY'] = [
565 "wl_namespace {$options['sort']}",
566 "wl_title {$options['sort']}"
573 [
'wl_namespace',
'wl_title',
'wl_notificationtimestamp' ],
574 [
'wl_user' => $user->getId() ],
580 foreach (
$res as $row ) {
584 new TitleValue( (
int)$row->wl_namespace, $row->wl_title ),
585 $row->wl_notificationtimestamp
589 return $watchedItems;
610 foreach ( $targets as $target ) {
611 $timestamps[$target->getNamespace()][$target->getDBkey()] =
false;
614 if ( $user->isAnon() ) {
619 foreach ( $targets as $target ) {
620 $cachedItem = $this->
getCached( $user, $target );
622 $timestamps[$target->getNamespace()][$target->getDBkey()] =
623 $cachedItem->getNotificationTimestamp();
625 $targetsToLoad[] = $target;
629 if ( !$targetsToLoad ) {
638 [
'wl_namespace',
'wl_title',
'wl_notificationtimestamp' ],
640 $lb->constructSet(
'wl',
$dbr ),
641 'wl_user' => $user->getId(),
646 foreach (
$res as $row ) {
647 $timestamps[$row->wl_namespace][$row->wl_title] =
670 if ( $this->readOnlyMode->isReadOnly() ) {
674 if ( $user->isAnon() ) {
684 foreach ( $targets as $target ) {
686 'wl_user' => $user->getId(),
687 'wl_namespace' => $target->getNamespace(),
688 'wl_title' => $target->getDBkey(),
689 'wl_notificationtimestamp' =>
null,
696 $this->
uncache( $user, $target );
700 foreach ( array_chunk(
$rows, 100 ) as $toInsert ) {
703 $dbw->insert(
'watchlist', $toInsert, __METHOD__,
'IGNORE' );
708 foreach ( $items as $item ) {
709 $this->
cache( $item );
723 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
727 $this->
uncache( $user, $target );
730 $dbw->delete(
'watchlist',
732 'wl_user' => $user->getId(),
737 $success = (bool)$dbw->affectedRows();
751 if ( $user->isAnon() ) {
757 $conds = [
'wl_user' => $user->getId() ];
760 $conds[] =
$batch->constructSet(
'wl', $dbw );
763 if ( $timestamp !==
null ) {
764 $timestamp = $dbw->timestamp( $timestamp );
769 [
'wl_notificationtimestamp' => $timestamp ],
781 if ( $user->isAnon() ) {
787 $user->getUserPage(),
788 [
'userId' => $user->getId(),
'casTime' => time() ]
794 $this->deferredUpdatesAddCallableUpdateCallback,
795 function () use (
$job ) {
810 $uids = $dbw->selectFieldValues(
814 'wl_user != ' . intval(
$editor->getId() ),
817 'wl_notificationtimestamp IS NULL',
822 $watchers = array_map(
'intval', $uids );
826 DeferredUpdates::addCallableUpdate(
827 function () use ( $timestamp, $watchers, $target,
$fname ) {
829 $ticket = $this->lbFactory->getEmptyTransactionTicket(
$fname );
831 $watchersChunks = array_chunk( $watchers, $this->updateRowsPerQuery );
832 foreach ( $watchersChunks as $watchersChunk ) {
833 $dbw->update(
'watchlist',
835 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
837 'wl_user' => $watchersChunk,
842 if ( count( $watchersChunks ) > 1 ) {
843 $this->lbFactory->commitAndWaitForReplication(
844 $fname, $ticket, [
'domain' => $dbw->getDomainID() ]
850 DeferredUpdates::POSTSEND,
868 if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
873 if ( $force !=
'force' ) {
875 if ( !$item || $item->getNotificationTimestamp() ===
null ) {
884 'type' =>
'updateWatchlistNotification',
885 'userid' => $user->getId(),
886 'notifTime' => $this->getNotificationTimestamp( $user, $title, $item, $force, $oldid ),
894 $this->deferredUpdatesAddCallableUpdateCallback,
895 function () use (
$job ) {
900 $this->
uncache( $user, $title );
911 if ( !$title->getNextRevisionID( $oldid ) ) {
916 if ( $item ===
null ) {
928 $notificationTimestamp = call_user_func(
929 $this->revisionGetTimestampFromIdCallback,
937 $ts->timestamp->add(
new DateInterval(
'PT1S' ) );
938 $notificationTimestamp = $ts->getTimestamp( TS_MW );
941 if ( $force !=
'force' ) {
945 return $item->getNotificationTimestamp();
949 return $notificationTimestamp;
960 if ( $unreadLimit !==
null ) {
961 $unreadLimit = (int)$unreadLimit;
962 $queryOptions[
'LIMIT'] = $unreadLimit;
966 $rowCount =
$dbr->selectRowCount(
970 'wl_user' => $user->getId(),
971 'wl_notificationtimestamp IS NOT NULL',
977 if ( !isset( $unreadLimit ) ) {
981 if ( $rowCount >= $unreadLimit ) {
994 $oldTarget = Title::newFromLinkTarget( $oldTarget );
995 $newTarget = Title::newFromLinkTarget( $newTarget );
997 $this->
duplicateEntry( $oldTarget->getSubjectPage(), $newTarget->getSubjectPage() );
998 $this->
duplicateEntry( $oldTarget->getTalkPage(), $newTarget->getTalkPage() );
1009 $result = $dbw->select(
1011 [
'wl_user',
'wl_notificationtimestamp' ],
1013 'wl_namespace' => $oldTarget->getNamespace(),
1014 'wl_title' => $oldTarget->getDBkey(),
1020 $newNamespace = $newTarget->getNamespace();
1021 $newDBkey = $newTarget->getDBkey();
1023 # Construct array to replace into the watchlist
1025 foreach ( $result as $row ) {
1027 'wl_user' => $row->wl_user,
1028 'wl_namespace' => $newNamespace,
1029 'wl_title' => $newDBkey,
1030 'wl_notificationtimestamp' => $row->wl_notificationtimestamp,
1034 if ( !empty( $values ) ) {
1036 # Note that multi-row replace is very efficient for MySQL but may be inefficient for
1037 # some other DBMSes, mostly due to poor simulation by us
1040 [ [
'wl_user',
'wl_namespace',
'wl_title' ] ],
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.
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)
__construct(ILBFactory $lbFactory, HashBagOStuff $cache, ReadOnlyMode $readOnlyMode, $updateRowsPerQuery)
getConnectionRef( $dbIndex)
countWatchers(LinkTarget $target)
overrideRevisionGetTimestampFromIdCallback(callable $callback)
Overrides the Revision::getTimestampFromId callback This is intended for use while testing and will f...
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
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
if(count( $args)< 1) $job