64 private $loadBalancer;
67 private $extensions =
null;
70 private $commentStore;
73 private $watchedItemStore;
79 private $userOptionsLookup;
84 private $expiryEnabled;
89 private $maxQueryExecutionTime;
97 bool $expiryEnabled =
false,
98 int $maxQueryExecutionTime = 0
100 $this->loadBalancer = $loadBalancer;
101 $this->commentStore = $commentStore;
102 $this->watchedItemStore = $watchedItemStore;
103 $this->hookRunner =
new HookRunner( $hookContainer );
104 $this->userOptionsLookup = $userOptionsLookup;
105 $this->expiryEnabled = $expiryEnabled;
106 $this->maxQueryExecutionTime = $maxQueryExecutionTime;
112 private function getExtensions() {
113 if ( $this->extensions ===
null ) {
114 $this->extensions = [];
115 $this->hookRunner->onWatchedItemQueryServiceExtensions( $this->extensions, $this );
117 return $this->extensions;
123 private function getConnection() {
124 return $this->loadBalancer->getConnectionRef(
DB_REPLICA );
171 User $user, array $options = [], &$startFrom =
null
174 'includeFields' => [],
175 'namespaceIds' => [],
177 'allRevisions' =>
false,
178 'usedInGenerator' => false
182 !isset( $options[
'rcTypes'] )
184 '$options[\'rcTypes\']',
185 'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
188 !isset( $options[
'dir'] ) || in_array( $options[
'dir'], [ self::DIR_OLDER, self::DIR_NEWER ] ),
190 'must be DIR_OLDER or DIR_NEWER'
193 !isset( $options[
'start'] ) && !isset( $options[
'end'] ) && $startFrom ===
null
194 || isset( $options[
'dir'] ),
196 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
199 !isset( $options[
'startFrom'] ),
200 '$options[\'startFrom\']',
201 'must not be provided, use $startFrom instead'
204 !isset( $startFrom ) || ( is_array( $startFrom ) && count( $startFrom ) === 2 ),
206 'must be a two-element array'
208 if ( array_key_exists(
'watchlistOwner', $options ) ) {
209 Assert::parameterType(
211 $options[
'watchlistOwner'],
212 '$options[\'watchlistOwner\']'
215 isset( $options[
'watchlistOwnerToken'] ),
216 '$options[\'watchlistOwnerToken\']',
217 'must be provided when providing watchlistOwner option'
221 $db = $this->getConnection();
223 $tables = $this->getWatchedItemsWithRCInfoQueryTables( $options );
224 $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
225 $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
226 $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
227 $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
229 if ( $startFrom !==
null ) {
230 $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
233 foreach ( $this->getExtensions() as $extension ) {
234 $extension->modifyWatchedItemsWithRCInfoQuery(
235 $user, $options, $db,
253 $limit = $dbOptions[
'LIMIT'] ?? INF;
256 foreach (
$res as $row ) {
257 if ( --$limit <= 0 ) {
258 $startFrom = [ $row->rc_timestamp, $row->rc_id ];
262 $target =
new TitleValue( (
int)$row->rc_namespace, $row->rc_title );
267 $this->watchedItemStore->getLatestNotificationTimestamp(
268 $row->wl_notificationtimestamp, $user, $target
270 $row->we_expiry ??
null
272 $this->getRecentChangeFieldsFromRow( $row )
276 foreach ( $this->getExtensions() as $extension ) {
277 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items,
$res, $startFrom );
309 $options += [
'namespaceIds' => [] ];
312 !isset( $options[
'sort'] ) || in_array( $options[
'sort'], [ self::SORT_ASC, self::SORT_DESC ] ),
313 '$options[\'sort\']',
314 'must be SORT_ASC or SORT_DESC'
317 !isset( $options[
'filter'] ) || in_array(
318 $options[
'filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
320 '$options[\'filter\']',
321 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
324 !isset( $options[
'from'] ) && !isset( $options[
'until'] ) && !isset( $options[
'startFrom'] )
325 || isset( $options[
'sort'] ),
326 '$options[\'sort\']',
327 'must be provided if any of "from", "until", "startFrom" options is provided'
330 $db = $this->getConnection();
332 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
333 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
335 $tables =
'watchlist';
337 if ( $this->expiryEnabled ) {
339 $tables = [
'watchlist',
'watchlist_expiry' ];
344 $joinConds[
'watchlist_expiry'] = [
'LEFT JOIN',
'wl_id = we_item' ];
348 [
'wl_namespace',
'wl_title',
'wl_notificationtimestamp' ],
356 foreach (
$res as $row ) {
357 $target =
new TitleValue( (
int)$row->wl_namespace, $row->wl_title );
362 $this->watchedItemStore->getLatestNotificationTimestamp(
363 $row->wl_notificationtimestamp, $user, $target
365 $row->we_expiry ??
null
369 return $watchedItems;
372 private function getRecentChangeFieldsFromRow( stdClass $row ) {
374 get_object_vars( $row ),
375 static function ( $key ) {
376 return str_starts_with( $key,
'rc_' );
382 private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
383 $tables = [
'recentchanges',
'watchlist' ];
385 if ( $this->expiryEnabled ) {
386 $tables[] =
'watchlist_expiry';
389 if ( !$options[
'allRevisions'] ) {
392 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
393 $tables += $this->commentStore->getJoin(
'rc_comment' )[
'tables'];
395 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ||
396 in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ||
397 in_array( self::FILTER_ANON, $options[
'filters'] ) ||
398 in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ||
399 array_key_exists(
'onlyByUser', $options ) || array_key_exists(
'notByUser', $options )
401 $tables[
'watchlist_actor'] =
'actor';
406 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
414 'wl_notificationtimestamp'
417 if ( $this->expiryEnabled ) {
418 $fields[] =
'we_expiry';
426 if ( $options[
'usedInGenerator'] ) {
427 if ( $options[
'allRevisions'] ) {
428 $rcIdFields = [
'rc_this_oldid' ];
430 $rcIdFields = [
'rc_cur_id' ];
433 $fields = array_merge( $fields, $rcIdFields );
435 if ( in_array( self::INCLUDE_FLAGS, $options[
'includeFields'] ) ) {
436 $fields = array_merge( $fields, [
'rc_type',
'rc_minor',
'rc_bot' ] );
438 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ) {
439 $fields[
'rc_user_text'] =
'watchlist_actor.actor_name';
441 if ( in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ) {
442 $fields[
'rc_user'] =
'watchlist_actor.actor_user';
444 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
445 $fields += $this->commentStore->getJoin(
'rc_comment' )[
'fields'];
447 if ( in_array( self::INCLUDE_PATROL_INFO, $options[
'includeFields'] ) ) {
448 $fields = array_merge( $fields, [
'rc_patrolled',
'rc_log_type' ] );
450 if ( in_array( self::INCLUDE_SIZES, $options[
'includeFields'] ) ) {
451 $fields = array_merge( $fields, [
'rc_old_len',
'rc_new_len' ] );
453 if ( in_array( self::INCLUDE_LOG_INFO, $options[
'includeFields'] ) ) {
454 $fields = array_merge( $fields, [
'rc_logid',
'rc_log_type',
'rc_log_action',
'rc_params' ] );
456 if ( in_array( self::INCLUDE_TAGS, $options[
'includeFields'] ) ) {
464 private function getWatchedItemsWithRCInfoQueryConds(
469 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
470 $conds = [
'wl_user' => $watchlistOwnerId ];
472 if ( $this->expiryEnabled ) {
476 if ( !$options[
'allRevisions'] ) {
478 [
'rc_this_oldid=page_latest',
'rc_type=' .
RC_LOG ],
483 if ( $options[
'namespaceIds'] ) {
484 $conds[
'wl_namespace'] = array_map(
'intval', $options[
'namespaceIds'] );
487 if ( array_key_exists(
'rcTypes', $options ) ) {
488 $conds[
'rc_type'] = array_map(
'intval', $options[
'rcTypes'] );
491 $conds = array_merge(
493 $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
496 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
498 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) && $db->
getType() ===
'mysql' ) {
500 $conds[] =
'rc_timestamp > ' . $db->
addQuotes(
'' );
503 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
505 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
506 if ( $deletedPageLogCond ) {
507 $conds[] = $deletedPageLogCond;
513 private function getWatchlistOwnerId(
UserIdentity $user, array $options ) {
514 if ( array_key_exists(
'watchlistOwner', $options ) ) {
516 $watchlistOwner = $options[
'watchlistOwner'];
518 $this->userOptionsLookup->getOption( $watchlistOwner,
'watchlisttoken' );
519 $token = $options[
'watchlistOwnerToken'];
520 if ( $ownersToken ==
'' || !hash_equals( $ownersToken, $token ) ) {
523 return $watchlistOwner->getId();
525 return $user->
getId();
528 private function getWatchedItemsWithRCInfoQueryFilterConds(
User $user, array $options ) {
531 if ( in_array( self::FILTER_MINOR, $options[
'filters'] ) ) {
532 $conds[] =
'rc_minor != 0';
533 } elseif ( in_array( self::FILTER_NOT_MINOR, $options[
'filters'] ) ) {
534 $conds[] =
'rc_minor = 0';
537 if ( in_array( self::FILTER_BOT, $options[
'filters'] ) ) {
538 $conds[] =
'rc_bot != 0';
539 } elseif ( in_array( self::FILTER_NOT_BOT, $options[
'filters'] ) ) {
540 $conds[] =
'rc_bot = 0';
543 if ( in_array( self::FILTER_ANON, $options[
'filters'] ) ) {
544 $conds[] =
'watchlist_actor.actor_user IS NULL';
545 } elseif ( in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ) {
546 $conds[] =
'watchlist_actor.actor_user IS NOT NULL';
552 if ( in_array( self::FILTER_PATROLLED, $options[
'filters'] ) ) {
554 } elseif ( in_array( self::FILTER_NOT_PATROLLED, $options[
'filters'] ) ) {
558 if ( in_array( self::FILTER_AUTOPATROLLED, $options[
'filters'] ) ) {
560 } elseif ( in_array( self::FILTER_NOT_AUTOPATROLLED, $options[
'filters'] ) ) {
565 if ( in_array( self::FILTER_UNREAD, $options[
'filters'] ) ) {
566 $conds[] =
'rc_timestamp >= wl_notificationtimestamp';
567 } elseif ( in_array( self::FILTER_NOT_UNREAD, $options[
'filters'] ) ) {
569 $conds[] =
'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
575 private function getStartEndConds(
IDatabase $db, array $options ) {
576 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) ) {
582 if ( isset( $options[
'start'] ) ) {
583 $after = $options[
'dir'] === self::DIR_OLDER ?
'<=' :
'>=';
584 $conds[] =
'rc_timestamp ' . $after .
' ' .
587 if ( isset( $options[
'end'] ) ) {
588 $before = $options[
'dir'] === self::DIR_OLDER ?
'>=' :
'<=';
589 $conds[] =
'rc_timestamp ' . $before .
' ' .
596 private function getUserRelatedConds(
IDatabase $db,
Authority $user, array $options ) {
597 if ( !array_key_exists(
'onlyByUser', $options ) && !array_key_exists(
'notByUser', $options ) ) {
603 if ( array_key_exists(
'onlyByUser', $options ) ) {
604 $conds[
'watchlist_actor.actor_name'] = $options[
'onlyByUser'];
605 } elseif ( array_key_exists(
'notByUser', $options ) ) {
606 $conds[] =
'watchlist_actor.actor_name<>' . $db->
addQuotes( $options[
'notByUser'] );
611 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
612 $bitmask = RevisionRecord::DELETED_USER;
613 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
614 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
617 $conds[] = $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask";
623 private function getExtraDeletedPageLogEntryRelatedCond(
IDatabase $db,
Authority $user ) {
627 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
629 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
635 $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask",
641 private function getStartFromConds(
IDatabase $db, array $options, array $startFrom ) {
642 $op = $options[
'dir'] === self::DIR_OLDER ?
'<=' :
'>=';
643 [ $rcTimestamp, $rcId ] = $startFrom;
644 $rcTimestamp = $db->
timestamp( $rcTimestamp );
647 'rc_timestamp' => $rcTimestamp,
652 private function getWatchedItemsForUserQueryConds(
655 $conds = [
'wl_user' => $user->
getId() ];
656 if ( $options[
'namespaceIds'] ) {
657 $conds[
'wl_namespace'] = array_map(
'intval', $options[
'namespaceIds'] );
659 if ( isset( $options[
'filter'] ) ) {
660 $filter = $options[
'filter'];
661 if ( $filter === self::FILTER_CHANGED ) {
662 $conds[] =
'wl_notificationtimestamp IS NOT NULL';
664 $conds[] =
'wl_notificationtimestamp IS NULL';
668 if ( isset( $options[
'from'] ) ) {
669 $op = $options[
'sort'] === self::SORT_ASC ?
'>=' :
'<=';
670 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'from'], $op );
672 if ( isset( $options[
'until'] ) ) {
673 $op = $options[
'sort'] === self::SORT_ASC ?
'<=' :
'>=';
674 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'until'], $op );
676 if ( isset( $options[
'startFrom'] ) ) {
677 $op = $options[
'sort'] === self::SORT_ASC ?
'>=' :
'<=';
678 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'startFrom'], $op );
700 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
703 if ( array_key_exists(
'dir', $options ) ) {
704 $sort = $options[
'dir'] === self::DIR_OLDER ?
' DESC' :
'';
705 $dbOptions[
'ORDER BY'] = [
'rc_timestamp' . $sort,
'rc_id' . $sort ];
708 if ( array_key_exists(
'limit', $options ) ) {
709 $dbOptions[
'LIMIT'] = (int)$options[
'limit'] + 1;
711 if ( $this->maxQueryExecutionTime ) {
712 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
717 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
719 if ( array_key_exists(
'sort', $options ) ) {
720 $dbOptions[
'ORDER BY'] = [
721 "wl_namespace {$options['sort']}",
722 "wl_title {$options['sort']}"
724 if ( count( $options[
'namespaceIds'] ) === 1 ) {
725 $dbOptions[
'ORDER BY'] =
"wl_title {$options['sort']}";
728 if ( array_key_exists(
'limit', $options ) ) {
729 $dbOptions[
'LIMIT'] = (int)$options[
'limit'];
731 if ( $this->maxQueryExecutionTime ) {
732 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
737 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
739 'watchlist' => [
'JOIN',
741 'wl_namespace=rc_namespace',
747 if ( $this->expiryEnabled ) {
748 $joinConds[
'watchlist_expiry'] = [
'LEFT JOIN',
'wl_id = we_item' ];
751 if ( !$options[
'allRevisions'] ) {
752 $joinConds[
'page'] = [
'LEFT JOIN',
'rc_cur_id=page_id' ];
754 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
755 $joinConds += $this->commentStore->getJoin(
'rc_comment' )[
'joins'];
757 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ||
758 in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ||
759 in_array( self::FILTER_ANON, $options[
'filters'] ) ||
760 in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ||
761 array_key_exists(
'onlyByUser', $options ) || array_key_exists(
'notByUser', $options )
763 $joinConds[
'watchlist_actor'] = [
'JOIN',
'actor_id=rc_actor' ];