9use Wikimedia\Assert\Assert;
91 bool $expiryEnabled =
false,
92 int $maxQueryExecutionTime = 0
94 $this->loadBalancer = $loadBalancer;
95 $this->commentStore = $commentStore;
96 $this->watchedItemStore = $watchedItemStore;
97 $this->hookRunner =
new HookRunner( $hookContainer );
106 if ( $this->extensions ===
null ) {
107 $this->extensions = [];
108 $this->hookRunner->onWatchedItemQueryServiceExtensions( $this->extensions, $this );
110 return $this->extensions;
117 return $this->loadBalancer->getConnectionRef(
DB_REPLICA, [
'watchlist' ] );
164 User $user, array $options = [], &$startFrom =
null
167 'includeFields' => [],
168 'namespaceIds' => [],
170 'allRevisions' =>
false,
171 'usedInGenerator' => false
175 !isset( $options[
'rcTypes'] )
177 '$options[\'rcTypes\']',
178 'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
181 !isset( $options[
'dir'] ) || in_array( $options[
'dir'], [ self::DIR_OLDER, self::DIR_NEWER ] ),
183 'must be DIR_OLDER or DIR_NEWER'
186 !isset( $options[
'start'] ) && !isset( $options[
'end'] ) && $startFrom ===
null
187 || isset( $options[
'dir'] ),
189 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
192 !isset( $options[
'startFrom'] ),
193 '$options[\'startFrom\']',
194 'must not be provided, use $startFrom instead'
197 !isset( $startFrom ) || ( is_array( $startFrom ) && count( $startFrom ) === 2 ),
199 'must be a two-element array'
201 if ( array_key_exists(
'watchlistOwner', $options ) ) {
202 Assert::parameterType(
204 $options[
'watchlistOwner'],
205 '$options[\'watchlistOwner\']'
208 isset( $options[
'watchlistOwnerToken'] ),
209 '$options[\'watchlistOwnerToken\']',
210 'must be provided when providing watchlistOwner option'
222 if ( $startFrom !==
null ) {
227 $extension->modifyWatchedItemsWithRCInfoQuery(
228 $user, $options, $db,
246 $limit = $dbOptions[
'LIMIT'] ?? INF;
249 foreach (
$res as $row ) {
250 if ( --$limit <= 0 ) {
251 $startFrom = [ $row->rc_timestamp, $row->rc_id ];
255 $target =
new TitleValue( (
int)$row->rc_namespace, $row->rc_title );
260 $this->watchedItemStore->getLatestNotificationTimestamp(
261 $row->wl_notificationtimestamp, $user, $target
263 $row->we_expiry ??
null
270 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items,
$res, $startFrom );
302 $options += [
'namespaceIds' => [] ];
305 !isset( $options[
'sort'] ) || in_array( $options[
'sort'], [ self::SORT_ASC, self::SORT_DESC ] ),
306 '$options[\'sort\']',
307 'must be SORT_ASC or SORT_DESC'
310 !isset( $options[
'filter'] ) || in_array(
311 $options[
'filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
313 '$options[\'filter\']',
314 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
317 !isset( $options[
'from'] ) && !isset( $options[
'until'] ) && !isset( $options[
'startFrom'] )
318 || isset( $options[
'sort'] ),
319 '$options[\'sort\']',
320 'must be provided if any of "from", "until", "startFrom" options is provided'
328 $tables =
'watchlist';
330 if ( $this->expiryEnabled ) {
332 $tables = [
'watchlist',
'watchlist_expiry' ];
333 $conds[] = $db->makeList(
334 [
'we_expiry' =>
null,
'we_expiry > ' . $db->addQuotes( $db->timestamp() ) ],
337 $joinConds[
'watchlist_expiry'] = [
'LEFT JOIN',
'wl_id = we_item' ];
341 [
'wl_namespace',
'wl_title',
'wl_notificationtimestamp' ],
349 foreach (
$res as $row ) {
350 $target =
new TitleValue( (
int)$row->wl_namespace, $row->wl_title );
355 $this->watchedItemStore->getLatestNotificationTimestamp(
356 $row->wl_notificationtimestamp, $user, $target
358 $row->we_expiry ??
null
362 return $watchedItems;
367 get_object_vars( $row ),
368 static function ( $key ) {
369 return substr( $key, 0, 3 ) ===
'rc_';
376 $tables = [
'recentchanges',
'watchlist' ];
378 if ( $this->expiryEnabled ) {
379 $tables[] =
'watchlist_expiry';
382 if ( !$options[
'allRevisions'] ) {
385 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
386 $tables += $this->commentStore->getJoin(
'rc_comment' )[
'tables'];
388 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ||
389 in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ||
390 in_array( self::FILTER_ANON, $options[
'filters'] ) ||
391 in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ||
392 array_key_exists(
'onlyByUser', $options ) || array_key_exists(
'notByUser', $options )
394 $tables[
'watchlist_actor'] =
'actor';
407 'wl_notificationtimestamp'
410 if ( $this->expiryEnabled ) {
411 $fields[] =
'we_expiry';
419 if ( $options[
'usedInGenerator'] ) {
420 if ( $options[
'allRevisions'] ) {
421 $rcIdFields = [
'rc_this_oldid' ];
423 $rcIdFields = [
'rc_cur_id' ];
426 $fields = array_merge( $fields, $rcIdFields );
428 if ( in_array( self::INCLUDE_FLAGS, $options[
'includeFields'] ) ) {
429 $fields = array_merge( $fields, [
'rc_type',
'rc_minor',
'rc_bot' ] );
431 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ) {
432 $fields[
'rc_user_text'] =
'watchlist_actor.actor_name';
434 if ( in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ) {
435 $fields[
'rc_user'] =
'watchlist_actor.actor_user';
437 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
438 $fields += $this->commentStore->getJoin(
'rc_comment' )[
'fields'];
440 if ( in_array( self::INCLUDE_PATROL_INFO, $options[
'includeFields'] ) ) {
441 $fields = array_merge( $fields, [
'rc_patrolled',
'rc_log_type' ] );
443 if ( in_array( self::INCLUDE_SIZES, $options[
'includeFields'] ) ) {
444 $fields = array_merge( $fields, [
'rc_old_len',
'rc_new_len' ] );
446 if ( in_array( self::INCLUDE_LOG_INFO, $options[
'includeFields'] ) ) {
447 $fields = array_merge( $fields, [
'rc_logid',
'rc_log_type',
'rc_log_action',
'rc_params' ] );
449 if ( in_array( self::INCLUDE_TAGS, $options[
'includeFields'] ) ) {
463 $conds = [
'wl_user' => $watchlistOwnerId ];
465 if ( $this->expiryEnabled ) {
469 if ( !$options[
'allRevisions'] ) {
471 [
'rc_this_oldid=page_latest',
'rc_type=' .
RC_LOG ],
476 if ( $options[
'namespaceIds'] ) {
477 $conds[
'wl_namespace'] = array_map(
'intval', $options[
'namespaceIds'] );
480 if ( array_key_exists(
'rcTypes', $options ) ) {
481 $conds[
'rc_type'] = array_map(
'intval', $options[
'rcTypes'] );
484 $conds = array_merge(
491 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) && $db->
getType() ===
'mysql' ) {
493 $conds[] =
'rc_timestamp > ' . $db->
addQuotes(
'' );
499 if ( $deletedPageLogCond ) {
500 $conds[] = $deletedPageLogCond;
507 if ( array_key_exists(
'watchlistOwner', $options ) ) {
509 $watchlistOwner = $options[
'watchlistOwner'];
511 $watchlistOwner->getOption(
'watchlisttoken' );
512 $token = $options[
'watchlistOwnerToken'];
513 if ( $ownersToken ==
'' || !hash_equals( $ownersToken, $token ) ) {
514 throw ApiUsageException::newWithMessage(
null,
'apierror-bad-watchlist-token',
'bad_wltoken' );
516 return $watchlistOwner->getId();
518 return $user->
getId();
524 if ( in_array( self::FILTER_MINOR, $options[
'filters'] ) ) {
525 $conds[] =
'rc_minor != 0';
526 } elseif ( in_array( self::FILTER_NOT_MINOR, $options[
'filters'] ) ) {
527 $conds[] =
'rc_minor = 0';
530 if ( in_array( self::FILTER_BOT, $options[
'filters'] ) ) {
531 $conds[] =
'rc_bot != 0';
532 } elseif ( in_array( self::FILTER_NOT_BOT, $options[
'filters'] ) ) {
533 $conds[] =
'rc_bot = 0';
536 if ( in_array( self::FILTER_ANON, $options[
'filters'] ) ) {
537 $conds[] =
'watchlist_actor.actor_user IS NULL';
538 } elseif ( in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ) {
539 $conds[] =
'watchlist_actor.actor_user IS NOT NULL';
545 if ( in_array( self::FILTER_PATROLLED, $options[
'filters'] ) ) {
546 $conds[] =
'rc_patrolled != ' . RecentChange::PRC_UNPATROLLED;
547 } elseif ( in_array( self::FILTER_NOT_PATROLLED, $options[
'filters'] ) ) {
548 $conds[
'rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
551 if ( in_array( self::FILTER_AUTOPATROLLED, $options[
'filters'] ) ) {
552 $conds[
'rc_patrolled'] = RecentChange::PRC_AUTOPATROLLED;
553 } elseif ( in_array( self::FILTER_NOT_AUTOPATROLLED, $options[
'filters'] ) ) {
554 $conds[] =
'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED;
558 if ( in_array( self::FILTER_UNREAD, $options[
'filters'] ) ) {
559 $conds[] =
'rc_timestamp >= wl_notificationtimestamp';
560 } elseif ( in_array( self::FILTER_NOT_UNREAD, $options[
'filters'] ) ) {
562 $conds[] =
'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
569 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) ) {
575 if ( isset( $options[
'start'] ) ) {
576 $after = $options[
'dir'] === self::DIR_OLDER ?
'<=' :
'>=';
577 $conds[] =
'rc_timestamp ' . $after .
' ' .
580 if ( isset( $options[
'end'] ) ) {
581 $before = $options[
'dir'] === self::DIR_OLDER ?
'>=' :
'<=';
582 $conds[] =
'rc_timestamp ' . $before .
' ' .
590 if ( !array_key_exists(
'onlyByUser', $options ) && !array_key_exists(
'notByUser', $options ) ) {
596 if ( array_key_exists(
'onlyByUser', $options ) ) {
597 $conds[
'watchlist_actor.actor_name'] = $options[
'onlyByUser'];
598 } elseif ( array_key_exists(
'notByUser', $options ) ) {
599 $conds[] =
'watchlist_actor.actor_name<>' . $db->
addQuotes( $options[
'notByUser'] );
604 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
605 $bitmask = RevisionRecord::DELETED_USER;
606 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
607 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
610 $conds[] = $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask";
620 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
622 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
628 $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask",
635 $op = $options[
'dir'] === self::DIR_OLDER ?
'<' :
'>';
636 list( $rcTimestamp, $rcId ) = $startFrom;
641 "rc_timestamp $op $rcTimestamp",
644 "rc_timestamp = $rcTimestamp",
657 $conds = [
'wl_user' => $user->
getId() ];
658 if ( $options[
'namespaceIds'] ) {
659 $conds[
'wl_namespace'] = array_map(
'intval', $options[
'namespaceIds'] );
661 if ( isset( $options[
'filter'] ) ) {
662 $filter = $options[
'filter'];
663 if ( $filter === self::FILTER_CHANGED ) {
664 $conds[] =
'wl_notificationtimestamp IS NOT NULL';
666 $conds[] =
'wl_notificationtimestamp IS NULL';
670 if ( isset( $options[
'from'] ) ) {
671 $op = $options[
'sort'] === self::SORT_ASC ?
'>' :
'<';
674 if ( isset( $options[
'until'] ) ) {
675 $op = $options[
'sort'] === self::SORT_ASC ?
'<' :
'>';
678 if ( isset( $options[
'startFrom'] ) ) {
679 $op = $options[
'sort'] === self::SORT_ASC ?
'>' :
'<';
714 if ( array_key_exists(
'dir', $options ) ) {
715 $sort = $options[
'dir'] === self::DIR_OLDER ?
' DESC' :
'';
716 $dbOptions[
'ORDER BY'] = [
'rc_timestamp' . $sort,
'rc_id' . $sort ];
719 if ( array_key_exists(
'limit', $options ) ) {
720 $dbOptions[
'LIMIT'] = (int)$options[
'limit'] + 1;
722 if ( $this->maxQueryExecutionTime ) {
723 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
730 if ( array_key_exists(
'sort', $options ) ) {
731 $dbOptions[
'ORDER BY'] = [
732 "wl_namespace {$options['sort']}",
733 "wl_title {$options['sort']}"
735 if ( count( $options[
'namespaceIds'] ) === 1 ) {
736 $dbOptions[
'ORDER BY'] =
"wl_title {$options['sort']}";
739 if ( array_key_exists(
'limit', $options ) ) {
740 $dbOptions[
'LIMIT'] = (int)$options[
'limit'];
742 if ( $this->maxQueryExecutionTime ) {
743 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
750 'watchlist' => [
'JOIN',
752 'wl_namespace=rc_namespace',
758 if ( $this->expiryEnabled ) {
759 $joinConds[
'watchlist_expiry'] = [
'LEFT JOIN',
'wl_id = we_item' ];
762 if ( !$options[
'allRevisions'] ) {
763 $joinConds[
'page'] = [
'LEFT JOIN',
'rc_cur_id=page_id' ];
765 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
766 $joinConds += $this->commentStore->getJoin(
'rc_comment' )[
'joins'];
768 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ||
769 in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ||
770 in_array( self::FILTER_ANON, $options[
'filters'] ) ||
771 in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ||
772 array_key_exists(
'onlyByUser', $options ) || array_key_exists(
'notByUser', $options )
774 $joinConds[
'watchlist_actor'] = [
'JOIN',
'actor_id=rc_actor' ];
Represents a page (or page fragment) title within MediaWiki.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
useNPPatrol()
Check whether to enable new pages patrol features for this user.
useRCPatrol()
Check whether to enable recent changes patrol features for this user.
const INCLUDE_AUTOPATROL_INFO
const FILTER_NOT_AUTOPATROLLED
bool $expiryEnabled
Correlates to $wgWatchlistExpiry feature flag.
getWatchedItemsForUser(UserIdentity $user, array $options=[])
For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser.
WatchedItemQueryServiceExtension[] null $extensions
__construct(ILoadBalancer $loadBalancer, CommentStore $commentStore, WatchedItemStoreInterface $watchedItemStore, HookContainer $hookContainer, bool $expiryEnabled=false, int $maxQueryExecutionTime=0)
const INCLUDE_PATROL_INFO
getWatchedItemsWithRCInfoQueryDbOptions(array $options)
getWatchedItemsWithRCInfoQueryFilterConds(User $user, array $options)
ILoadBalancer $loadBalancer
getWatchedItemsWithRCInfoQueryTables(array $options)
getWatchedItemsWithRCInfoQueryFields(array $options)
getWatchedItemsWithRCInfoQueryConds(IDatabase $db, User $user, array $options)
WatchedItemStoreInterface $watchedItemStore
const FILTER_AUTOPATROLLED
getFromUntilTargetConds(IDatabase $db, LinkTarget $target, $op)
Creates a query condition part for getting only items before or after the given link target (while or...
int $maxQueryExecutionTime
Max query execution time.
getStartEndConds(IDatabase $db, array $options)
const FILTER_NOT_PATROLLED
getWatchlistOwnerId(UserIdentity $user, array $options)
getWatchedItemsForUserQueryConds(IDatabase $db, UserIdentity $user, array $options)
getWatchedItemsWithRecentChangeInfo(User $user, array $options=[], &$startFrom=null)
getWatchedItemsWithRCInfoQueryJoinConds(array $options)
getExtraDeletedPageLogEntryRelatedCond(IDatabase $db, Authority $user)
getWatchedItemsForUserQueryDbOptions(array $options)
getRecentChangeFieldsFromRow(stdClass $row)
getUserRelatedConds(IDatabase $db, Authority $user, array $options)
getStartFromConds(IDatabase $db, array $options, array $startFrom)
CommentStore $commentStore
Representation of a pair of user and title for watchlist entries.