13 use Wikimedia\Assert\Assert;
66 private $loadBalancer;
69 private $extensions =
null;
72 private $commentStore;
75 private $watchedItemStore;
81 private $userOptionsLookup;
86 private $expiryEnabled;
91 private $maxQueryExecutionTime;
99 bool $expiryEnabled =
false,
100 int $maxQueryExecutionTime = 0
102 $this->loadBalancer = $loadBalancer;
103 $this->commentStore = $commentStore;
104 $this->watchedItemStore = $watchedItemStore;
105 $this->hookRunner =
new HookRunner( $hookContainer );
106 $this->userOptionsLookup = $userOptionsLookup;
107 $this->expiryEnabled = $expiryEnabled;
108 $this->maxQueryExecutionTime = $maxQueryExecutionTime;
114 private function getExtensions() {
115 if ( $this->extensions ===
null ) {
116 $this->extensions = [];
117 $this->hookRunner->onWatchedItemQueryServiceExtensions( $this->extensions, $this );
119 return $this->extensions;
125 private function getConnection() {
126 return $this->loadBalancer->getConnectionRef(
DB_REPLICA );
173 User $user, array $options = [], &$startFrom =
null
176 'includeFields' => [],
177 'namespaceIds' => [],
179 'allRevisions' =>
false,
180 'usedInGenerator' => false
184 !isset( $options[
'rcTypes'] )
186 '$options[\'rcTypes\']',
187 'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
190 !isset( $options[
'dir'] ) || in_array( $options[
'dir'], [ self::DIR_OLDER, self::DIR_NEWER ] ),
192 'must be DIR_OLDER or DIR_NEWER'
195 !isset( $options[
'start'] ) && !isset( $options[
'end'] ) && $startFrom ===
null
196 || isset( $options[
'dir'] ),
198 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
201 !isset( $options[
'startFrom'] ),
202 '$options[\'startFrom\']',
203 'must not be provided, use $startFrom instead'
206 !isset( $startFrom ) || ( is_array( $startFrom ) && count( $startFrom ) === 2 ),
208 'must be a two-element array'
210 if ( array_key_exists(
'watchlistOwner', $options ) ) {
211 Assert::parameterType(
213 $options[
'watchlistOwner'],
214 '$options[\'watchlistOwner\']'
217 isset( $options[
'watchlistOwnerToken'] ),
218 '$options[\'watchlistOwnerToken\']',
219 'must be provided when providing watchlistOwner option'
223 $db = $this->getConnection();
225 $tables = $this->getWatchedItemsWithRCInfoQueryTables( $options );
226 $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
227 $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
228 $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
229 $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
231 if ( $startFrom !==
null ) {
232 $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
235 foreach ( $this->getExtensions() as $extension ) {
236 $extension->modifyWatchedItemsWithRCInfoQuery(
237 $user, $options, $db,
255 $limit = $dbOptions[
'LIMIT'] ?? INF;
258 foreach ( $res as $row ) {
259 if ( --$limit <= 0 ) {
260 $startFrom = [ $row->rc_timestamp, $row->rc_id ];
264 $target =
new TitleValue( (
int)$row->rc_namespace, $row->rc_title );
269 $this->watchedItemStore->getLatestNotificationTimestamp(
270 $row->wl_notificationtimestamp, $user, $target
272 $row->we_expiry ??
null
274 $this->getRecentChangeFieldsFromRow( $row )
278 foreach ( $this->getExtensions() as $extension ) {
279 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
311 $options += [
'namespaceIds' => [] ];
314 !isset( $options[
'sort'] ) || in_array( $options[
'sort'], [ self::SORT_ASC, self::SORT_DESC ] ),
315 '$options[\'sort\']',
316 'must be SORT_ASC or SORT_DESC'
319 !isset( $options[
'filter'] ) || in_array(
320 $options[
'filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
322 '$options[\'filter\']',
323 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
326 !isset( $options[
'from'] ) && !isset( $options[
'until'] ) && !isset( $options[
'startFrom'] )
327 || isset( $options[
'sort'] ),
328 '$options[\'sort\']',
329 'must be provided if any of "from", "until", "startFrom" options is provided'
332 $db = $this->getConnection();
334 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
335 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
337 $tables =
'watchlist';
339 if ( $this->expiryEnabled ) {
341 $tables = [
'watchlist',
'watchlist_expiry' ];
346 $joinConds[
'watchlist_expiry'] = [
'LEFT JOIN',
'wl_id = we_item' ];
350 [
'wl_namespace',
'wl_title',
'wl_notificationtimestamp' ],
358 foreach ( $res as $row ) {
359 $target =
new TitleValue( (
int)$row->wl_namespace, $row->wl_title );
364 $this->watchedItemStore->getLatestNotificationTimestamp(
365 $row->wl_notificationtimestamp, $user, $target
367 $row->we_expiry ??
null
371 return $watchedItems;
374 private function getRecentChangeFieldsFromRow( stdClass $row ) {
376 get_object_vars( $row ),
377 static function ( $key ) {
378 return str_starts_with( $key,
'rc_' );
384 private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
385 $tables = [
'recentchanges',
'watchlist' ];
387 if ( $this->expiryEnabled ) {
388 $tables[] =
'watchlist_expiry';
391 if ( !$options[
'allRevisions'] ) {
394 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
395 $tables += $this->commentStore->getJoin(
'rc_comment' )[
'tables'];
397 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ||
398 in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ||
399 in_array( self::FILTER_ANON, $options[
'filters'] ) ||
400 in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ||
401 array_key_exists(
'onlyByUser', $options ) || array_key_exists(
'notByUser', $options )
403 $tables[
'watchlist_actor'] =
'actor';
408 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
416 'wl_notificationtimestamp'
419 if ( $this->expiryEnabled ) {
420 $fields[] =
'we_expiry';
428 if ( $options[
'usedInGenerator'] ) {
429 if ( $options[
'allRevisions'] ) {
430 $rcIdFields = [
'rc_this_oldid' ];
432 $rcIdFields = [
'rc_cur_id' ];
435 $fields = array_merge( $fields, $rcIdFields );
437 if ( in_array( self::INCLUDE_FLAGS, $options[
'includeFields'] ) ) {
438 $fields = array_merge( $fields, [
'rc_type',
'rc_minor',
'rc_bot' ] );
440 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ) {
441 $fields[
'rc_user_text'] =
'watchlist_actor.actor_name';
443 if ( in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ) {
444 $fields[
'rc_user'] =
'watchlist_actor.actor_user';
446 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
447 $fields += $this->commentStore->getJoin(
'rc_comment' )[
'fields'];
449 if ( in_array( self::INCLUDE_PATROL_INFO, $options[
'includeFields'] ) ) {
450 $fields = array_merge( $fields, [
'rc_patrolled',
'rc_log_type' ] );
452 if ( in_array( self::INCLUDE_SIZES, $options[
'includeFields'] ) ) {
453 $fields = array_merge( $fields, [
'rc_old_len',
'rc_new_len' ] );
455 if ( in_array( self::INCLUDE_LOG_INFO, $options[
'includeFields'] ) ) {
456 $fields = array_merge( $fields, [
'rc_logid',
'rc_log_type',
'rc_log_action',
'rc_params' ] );
458 if ( in_array( self::INCLUDE_TAGS, $options[
'includeFields'] ) ) {
466 private function getWatchedItemsWithRCInfoQueryConds(
471 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
472 $conds = [
'wl_user' => $watchlistOwnerId ];
474 if ( $this->expiryEnabled ) {
478 if ( !$options[
'allRevisions'] ) {
480 [
'rc_this_oldid=page_latest',
'rc_type=' .
RC_LOG ],
485 if ( $options[
'namespaceIds'] ) {
486 $conds[
'wl_namespace'] = array_map(
'intval', $options[
'namespaceIds'] );
489 if ( array_key_exists(
'rcTypes', $options ) ) {
490 $conds[
'rc_type'] = array_map(
'intval', $options[
'rcTypes'] );
493 $conds = array_merge(
495 $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
498 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
500 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) && $db->
getType() ===
'mysql' ) {
502 $conds[] =
'rc_timestamp > ' . $db->
addQuotes(
'' );
505 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
507 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
508 if ( $deletedPageLogCond ) {
509 $conds[] = $deletedPageLogCond;
515 private function getWatchlistOwnerId(
UserIdentity $user, array $options ) {
516 if ( array_key_exists(
'watchlistOwner', $options ) ) {
518 $watchlistOwner = $options[
'watchlistOwner'];
520 $this->userOptionsLookup->getOption( $watchlistOwner,
'watchlisttoken' );
521 $token = $options[
'watchlistOwnerToken'];
522 if ( $ownersToken ==
'' || !hash_equals( $ownersToken, $token ) ) {
525 return $watchlistOwner->getId();
527 return $user->
getId();
530 private function getWatchedItemsWithRCInfoQueryFilterConds(
User $user, array $options ) {
533 if ( in_array( self::FILTER_MINOR, $options[
'filters'] ) ) {
534 $conds[] =
'rc_minor != 0';
535 } elseif ( in_array( self::FILTER_NOT_MINOR, $options[
'filters'] ) ) {
536 $conds[] =
'rc_minor = 0';
539 if ( in_array( self::FILTER_BOT, $options[
'filters'] ) ) {
540 $conds[] =
'rc_bot != 0';
541 } elseif ( in_array( self::FILTER_NOT_BOT, $options[
'filters'] ) ) {
542 $conds[] =
'rc_bot = 0';
545 if ( in_array( self::FILTER_ANON, $options[
'filters'] ) ) {
546 $conds[] =
'watchlist_actor.actor_user IS NULL';
547 } elseif ( in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ) {
548 $conds[] =
'watchlist_actor.actor_user IS NOT NULL';
554 if ( in_array( self::FILTER_PATROLLED, $options[
'filters'] ) ) {
556 } elseif ( in_array( self::FILTER_NOT_PATROLLED, $options[
'filters'] ) ) {
560 if ( in_array( self::FILTER_AUTOPATROLLED, $options[
'filters'] ) ) {
562 } elseif ( in_array( self::FILTER_NOT_AUTOPATROLLED, $options[
'filters'] ) ) {
567 if ( in_array( self::FILTER_UNREAD, $options[
'filters'] ) ) {
568 $conds[] =
'rc_timestamp >= wl_notificationtimestamp';
569 } elseif ( in_array( self::FILTER_NOT_UNREAD, $options[
'filters'] ) ) {
571 $conds[] =
'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
577 private function getStartEndConds(
IDatabase $db, array $options ) {
578 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) ) {
584 if ( isset( $options[
'start'] ) ) {
585 $after = $options[
'dir'] === self::DIR_OLDER ?
'<=' :
'>=';
586 $conds[] =
'rc_timestamp ' . $after .
' ' .
589 if ( isset( $options[
'end'] ) ) {
590 $before = $options[
'dir'] === self::DIR_OLDER ?
'>=' :
'<=';
591 $conds[] =
'rc_timestamp ' . $before .
' ' .
598 private function getUserRelatedConds(
IDatabase $db,
Authority $user, array $options ) {
599 if ( !array_key_exists(
'onlyByUser', $options ) && !array_key_exists(
'notByUser', $options ) ) {
605 if ( array_key_exists(
'onlyByUser', $options ) ) {
606 $conds[
'watchlist_actor.actor_name'] = $options[
'onlyByUser'];
607 } elseif ( array_key_exists(
'notByUser', $options ) ) {
608 $conds[] =
'watchlist_actor.actor_name<>' . $db->
addQuotes( $options[
'notByUser'] );
613 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
614 $bitmask = RevisionRecord::DELETED_USER;
615 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
616 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
619 $conds[] = $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask";
625 private function getExtraDeletedPageLogEntryRelatedCond(
IDatabase $db,
Authority $user ) {
629 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
631 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
637 $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask",
643 private function getStartFromConds(
IDatabase $db, array $options, array $startFrom ) {
644 $op = $options[
'dir'] === self::DIR_OLDER ?
'<=' :
'>=';
645 [ $rcTimestamp, $rcId ] = $startFrom;
646 $rcTimestamp = $db->
timestamp( $rcTimestamp );
649 'rc_timestamp' => $rcTimestamp,
654 private function getWatchedItemsForUserQueryConds(
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 ?
'>=' :
'<=';
672 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'from'], $op );
674 if ( isset( $options[
'until'] ) ) {
675 $op = $options[
'sort'] === self::SORT_ASC ?
'<=' :
'>=';
676 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'until'], $op );
678 if ( isset( $options[
'startFrom'] ) ) {
679 $op = $options[
'sort'] === self::SORT_ASC ?
'>=' :
'<=';
680 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'startFrom'], $op );
702 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
705 if ( array_key_exists(
'dir', $options ) ) {
706 $sort = $options[
'dir'] === self::DIR_OLDER ?
' DESC' :
'';
707 $dbOptions[
'ORDER BY'] = [
'rc_timestamp' . $sort,
'rc_id' . $sort ];
710 if ( array_key_exists(
'limit', $options ) ) {
711 $dbOptions[
'LIMIT'] = (int)$options[
'limit'] + 1;
713 if ( $this->maxQueryExecutionTime ) {
714 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
719 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
721 if ( array_key_exists(
'sort', $options ) ) {
722 $dbOptions[
'ORDER BY'] = [
723 "wl_namespace {$options['sort']}",
724 "wl_title {$options['sort']}"
726 if ( count( $options[
'namespaceIds'] ) === 1 ) {
727 $dbOptions[
'ORDER BY'] =
"wl_title {$options['sort']}";
730 if ( array_key_exists(
'limit', $options ) ) {
731 $dbOptions[
'LIMIT'] = (int)$options[
'limit'];
733 if ( $this->maxQueryExecutionTime ) {
734 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
739 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
741 'watchlist' => [
'JOIN',
743 'wl_namespace=rc_namespace',
749 if ( $this->expiryEnabled ) {
750 $joinConds[
'watchlist_expiry'] = [
'LEFT JOIN',
'wl_id = we_item' ];
753 if ( !$options[
'allRevisions'] ) {
754 $joinConds[
'page'] = [
'LEFT JOIN',
'rc_cur_id=page_id' ];
756 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
757 $joinConds += $this->commentStore->getJoin(
'rc_comment' )[
'joins'];
759 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ||
760 in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ||
761 in_array( self::FILTER_ANON, $options[
'filters'] ) ||
762 in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ||
763 array_key_exists(
'onlyByUser', $options ) || array_key_exists(
'notByUser', $options )
765 $joinConds[
'watchlist_actor'] = [
'JOIN',
'actor_id=rc_actor' ];
static newWithMessage(?ApiBase $module, $msg, $code=null, $data=null, $httpCode=0, Throwable $previous=null)
__construct(ILoadBalancer $loadBalancer, CommentStore $commentStore, WatchedItemStoreInterface $watchedItemStore, HookContainer $hookContainer, UserOptionsLookup $userOptionsLookup, bool $expiryEnabled=false, int $maxQueryExecutionTime=0)
const INCLUDE_AUTOPATROL_INFO
const FILTER_NOT_AUTOPATROLLED
getWatchedItemsForUser(UserIdentity $user, array $options=[])
For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser.
const INCLUDE_PATROL_INFO
const FILTER_AUTOPATROLLED
const FILTER_NOT_PATROLLED
getWatchedItemsWithRecentChangeInfo(User $user, array $options=[], &$startFrom=null)
Representation of a pair of user and title for watchlist entries.