71 private $extensions =
null;
74 private $commentStore;
77 private $watchedItemStore;
83 private $userOptionsLookup;
86 private $tempUserConfig;
91 private $expiryEnabled;
96 private $maxQueryExecutionTime;
105 bool $expiryEnabled =
false,
106 int $maxQueryExecutionTime = 0
108 $this->dbProvider = $dbProvider;
109 $this->commentStore = $commentStore;
110 $this->watchedItemStore = $watchedItemStore;
111 $this->hookRunner =
new HookRunner( $hookContainer );
112 $this->userOptionsLookup = $userOptionsLookup;
113 $this->tempUserConfig = $tempUserConfig;
114 $this->expiryEnabled = $expiryEnabled;
115 $this->maxQueryExecutionTime = $maxQueryExecutionTime;
121 private function getExtensions() {
122 if ( $this->extensions ===
null ) {
123 $this->extensions = [];
124 $this->hookRunner->onWatchedItemQueryServiceExtensions( $this->extensions, $this );
126 return $this->extensions;
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->dbProvider->getReplicaDatabase();
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->dbProvider->getReplicaDatabase();
334 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
335 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
337 $tables =
'watchlist';
339 if ( $this->expiryEnabled ) {
341 $tables = [
'watchlist',
'watchlist_expiry' ];
342 $conds[] = $db->
expr(
'we_expiry',
'>', $db->
timestamp() )->or(
'we_expiry',
'=',
null );
343 $joinConds[
'watchlist_expiry'] = [
'LEFT JOIN',
'wl_id = we_item' ];
347 [
'wl_namespace',
'wl_title',
'wl_notificationtimestamp' ],
355 foreach ( $res as $row ) {
356 $target =
new TitleValue( (
int)$row->wl_namespace, $row->wl_title );
361 $this->watchedItemStore->getLatestNotificationTimestamp(
362 $row->wl_notificationtimestamp, $user, $target
364 $row->we_expiry ??
null
368 return $watchedItems;
371 private function getRecentChangeFieldsFromRow( stdClass $row ) {
373 get_object_vars( $row ),
374 static function ( $key ) {
375 return str_starts_with( $key,
'rc_' );
381 private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
382 $tables = [
'recentchanges',
'watchlist' ];
384 if ( $this->expiryEnabled ) {
385 $tables[] =
'watchlist_expiry';
388 if ( !$options[
'allRevisions'] ) {
391 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
392 $tables += $this->commentStore->getJoin(
'rc_comment' )[
'tables'];
394 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ||
395 in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ||
396 in_array( self::FILTER_ANON, $options[
'filters'] ) ||
397 in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ||
398 array_key_exists(
'onlyByUser', $options ) || array_key_exists(
'notByUser', $options )
400 $tables[
'watchlist_actor'] =
'actor';
405 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
413 'wl_notificationtimestamp'
416 if ( $this->expiryEnabled ) {
417 $fields[] =
'we_expiry';
425 if ( $options[
'usedInGenerator'] ) {
426 if ( $options[
'allRevisions'] ) {
427 $rcIdFields = [
'rc_this_oldid' ];
429 $rcIdFields = [
'rc_cur_id' ];
432 $fields = array_merge( $fields, $rcIdFields );
434 if ( in_array( self::INCLUDE_FLAGS, $options[
'includeFields'] ) ) {
435 $fields = array_merge( $fields, [
'rc_type',
'rc_minor',
'rc_bot' ] );
437 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ) {
438 $fields[
'rc_user_text'] =
'watchlist_actor.actor_name';
440 if ( in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ) {
441 $fields[
'rc_user'] =
'watchlist_actor.actor_user';
443 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
444 $fields += $this->commentStore->getJoin(
'rc_comment' )[
'fields'];
446 if ( in_array( self::INCLUDE_PATROL_INFO, $options[
'includeFields'] ) ) {
447 $fields = array_merge( $fields, [
'rc_patrolled',
'rc_log_type' ] );
449 if ( in_array( self::INCLUDE_SIZES, $options[
'includeFields'] ) ) {
450 $fields = array_merge( $fields, [
'rc_old_len',
'rc_new_len' ] );
452 if ( in_array( self::INCLUDE_LOG_INFO, $options[
'includeFields'] ) ) {
453 $fields = array_merge( $fields, [
'rc_logid',
'rc_log_type',
'rc_log_action',
'rc_params' ] );
455 if ( in_array( self::INCLUDE_TAGS, $options[
'includeFields'] ) ) {
463 private function getWatchedItemsWithRCInfoQueryConds(
468 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
469 $conds = [
'wl_user' => $watchlistOwnerId ];
471 if ( $this->expiryEnabled ) {
472 $conds[] = $db->
expr(
'we_expiry',
'=',
null )->or(
'we_expiry',
'>', $db->
timestamp() );
475 if ( !$options[
'allRevisions'] ) {
477 [
'rc_this_oldid=page_latest',
'rc_type=' .
RC_LOG ],
482 if ( $options[
'namespaceIds'] ) {
483 $conds[
'wl_namespace'] = array_map(
'intval', $options[
'namespaceIds'] );
486 if ( array_key_exists(
'rcTypes', $options ) ) {
487 $conds[
'rc_type'] = array_map(
'intval', $options[
'rcTypes'] );
490 $conds = array_merge(
492 $this->getWatchedItemsWithRCInfoQueryFilterConds( $db, $user, $options )
495 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
497 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) && $db->
getType() ===
'mysql' ) {
499 $conds[] = $db->
expr(
'rc_timestamp',
'>',
'' );
502 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
504 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
505 if ( $deletedPageLogCond ) {
506 $conds[] = $deletedPageLogCond;
512 private function getWatchlistOwnerId(
UserIdentity $user, array $options ) {
513 if ( array_key_exists(
'watchlistOwner', $options ) ) {
515 $watchlistOwner = $options[
'watchlistOwner'];
517 $this->userOptionsLookup->getOption( $watchlistOwner,
'watchlisttoken' );
518 $token = $options[
'watchlistOwnerToken'];
519 if ( $ownersToken ==
'' || !hash_equals( $ownersToken, $token ) ) {
522 return $watchlistOwner->getId();
524 return $user->
getId();
527 private function getWatchedItemsWithRCInfoQueryFilterConds(
534 if ( in_array( self::FILTER_MINOR, $options[
'filters'] ) ) {
535 $conds[] =
'rc_minor != 0';
536 } elseif ( in_array( self::FILTER_NOT_MINOR, $options[
'filters'] ) ) {
537 $conds[] =
'rc_minor = 0';
540 if ( in_array( self::FILTER_BOT, $options[
'filters'] ) ) {
541 $conds[] =
'rc_bot != 0';
542 } elseif ( in_array( self::FILTER_NOT_BOT, $options[
'filters'] ) ) {
543 $conds[] =
'rc_bot = 0';
547 if ( in_array( self::FILTER_ANON, $options[
'filters'] ) ) {
548 if ( $this->tempUserConfig->isEnabled() ) {
549 $conds[] = $dbr->
expr(
'watchlist_actor.actor_user',
'=',
null )
550 ->orExpr( $this->tempUserConfig->getMatchCondition( $dbr,
551 'watchlist_actor.actor_name', IExpression::LIKE ) );
553 $conds[] =
'watchlist_actor.actor_user IS NULL';
555 } elseif ( in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ) {
556 $conds[] =
'watchlist_actor.actor_user IS NOT NULL';
557 if ( $this->tempUserConfig->isEnabled() ) {
558 $conds[] = $this->tempUserConfig->getMatchCondition( $dbr,
559 'watchlist_actor.actor_name', IExpression::NOT_LIKE );
566 if ( in_array( self::FILTER_PATROLLED, $options[
'filters'] ) ) {
568 } elseif ( in_array( self::FILTER_NOT_PATROLLED, $options[
'filters'] ) ) {
572 if ( in_array( self::FILTER_AUTOPATROLLED, $options[
'filters'] ) ) {
574 } elseif ( in_array( self::FILTER_NOT_AUTOPATROLLED, $options[
'filters'] ) ) {
579 if ( in_array( self::FILTER_UNREAD, $options[
'filters'] ) ) {
580 $conds[] =
'rc_timestamp >= wl_notificationtimestamp';
581 } elseif ( in_array( self::FILTER_NOT_UNREAD, $options[
'filters'] ) ) {
583 $conds[] =
'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
590 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) ) {
596 if ( isset( $options[
'start'] ) ) {
597 $after = $options[
'dir'] === self::DIR_OLDER ?
'<=' :
'>=';
598 $conds[] =
'rc_timestamp ' . $after .
' ' .
601 if ( isset( $options[
'end'] ) ) {
602 $before = $options[
'dir'] === self::DIR_OLDER ?
'>=' :
'<=';
603 $conds[] =
'rc_timestamp ' . $before .
' ' .
611 if ( !array_key_exists(
'onlyByUser', $options ) && !array_key_exists(
'notByUser', $options ) ) {
617 if ( array_key_exists(
'onlyByUser', $options ) ) {
618 $conds[
'watchlist_actor.actor_name'] = $options[
'onlyByUser'];
619 } elseif ( array_key_exists(
'notByUser', $options ) ) {
620 $conds[] =
'watchlist_actor.actor_name<>' . $db->
addQuotes( $options[
'notByUser'] );
625 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
626 $bitmask = RevisionRecord::DELETED_USER;
627 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
628 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
631 $conds[] = $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask";
641 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
643 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
649 $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask",
655 private function getStartFromConds(
IReadableDatabase $db, array $options, array $startFrom ) {
656 $op = $options[
'dir'] === self::DIR_OLDER ?
'<=' :
'>=';
657 [ $rcTimestamp, $rcId ] = $startFrom;
658 $rcTimestamp = $db->
timestamp( $rcTimestamp );
661 'rc_timestamp' => $rcTimestamp,
666 private function getWatchedItemsForUserQueryConds(
669 $conds = [
'wl_user' => $user->
getId() ];
670 if ( $options[
'namespaceIds'] ) {
671 $conds[
'wl_namespace'] = array_map(
'intval', $options[
'namespaceIds'] );
673 if ( isset( $options[
'filter'] ) ) {
674 $filter = $options[
'filter'];
675 if ( $filter === self::FILTER_CHANGED ) {
676 $conds[] =
'wl_notificationtimestamp IS NOT NULL';
678 $conds[] =
'wl_notificationtimestamp IS NULL';
682 if ( isset( $options[
'from'] ) ) {
683 $op = $options[
'sort'] === self::SORT_ASC ?
'>=' :
'<=';
684 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'from'], $op );
686 if ( isset( $options[
'until'] ) ) {
687 $op = $options[
'sort'] === self::SORT_ASC ?
'<=' :
'>=';
688 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'until'], $op );
690 if ( isset( $options[
'startFrom'] ) ) {
691 $op = $options[
'sort'] === self::SORT_ASC ?
'>=' :
'<=';
692 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'startFrom'], $op );
714 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
717 if ( array_key_exists(
'dir', $options ) ) {
718 $sort = $options[
'dir'] === self::DIR_OLDER ?
' DESC' :
'';
719 $dbOptions[
'ORDER BY'] = [
'rc_timestamp' . $sort,
'rc_id' . $sort ];
722 if ( array_key_exists(
'limit', $options ) ) {
723 $dbOptions[
'LIMIT'] = (int)$options[
'limit'] + 1;
725 if ( $this->maxQueryExecutionTime ) {
726 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
731 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
733 if ( array_key_exists(
'sort', $options ) ) {
734 $dbOptions[
'ORDER BY'] = [
735 "wl_namespace {$options['sort']}",
736 "wl_title {$options['sort']}"
738 if ( count( $options[
'namespaceIds'] ) === 1 ) {
739 $dbOptions[
'ORDER BY'] =
"wl_title {$options['sort']}";
742 if ( array_key_exists(
'limit', $options ) ) {
743 $dbOptions[
'LIMIT'] = (int)$options[
'limit'];
745 if ( $this->maxQueryExecutionTime ) {
746 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
751 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
753 'watchlist' => [
'JOIN',
755 'wl_namespace=rc_namespace',
761 if ( $this->expiryEnabled ) {
762 $joinConds[
'watchlist_expiry'] = [
'LEFT JOIN',
'wl_id = we_item' ];
765 if ( !$options[
'allRevisions'] ) {
766 $joinConds[
'page'] = [
'LEFT JOIN',
'rc_cur_id=page_id' ];
768 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
769 $joinConds += $this->commentStore->getJoin(
'rc_comment' )[
'joins'];
771 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ||
772 in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ||
773 in_array( self::FILTER_ANON, $options[
'filters'] ) ||
774 in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ||
775 array_key_exists(
'onlyByUser', $options ) || array_key_exists(
'notByUser', $options )
777 $joinConds[
'watchlist_actor'] = [
'JOIN',
'actor_id=rc_actor' ];