63 private $loadBalancer;
66 private $extensions =
null;
69 private $commentStore;
72 private $watchedItemStore;
78 private $userOptionsLookup;
83 private $expiryEnabled;
88 private $maxQueryExecutionTime;
96 bool $expiryEnabled =
false,
97 int $maxQueryExecutionTime = 0
99 $this->loadBalancer = $loadBalancer;
100 $this->commentStore = $commentStore;
101 $this->watchedItemStore = $watchedItemStore;
102 $this->hookRunner =
new HookRunner( $hookContainer );
103 $this->userOptionsLookup = $userOptionsLookup;
104 $this->expiryEnabled = $expiryEnabled;
105 $this->maxQueryExecutionTime = $maxQueryExecutionTime;
111 private function getExtensions() {
112 if ( $this->extensions ===
null ) {
113 $this->extensions = [];
114 $this->hookRunner->onWatchedItemQueryServiceExtensions( $this->extensions, $this );
116 return $this->extensions;
122 private function getConnection() {
123 return $this->loadBalancer->getConnectionRef(
DB_REPLICA );
170 User $user, array $options = [], &$startFrom =
null
173 'includeFields' => [],
174 'namespaceIds' => [],
176 'allRevisions' =>
false,
177 'usedInGenerator' => false
181 !isset( $options[
'rcTypes'] )
183 '$options[\'rcTypes\']',
184 'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
187 !isset( $options[
'dir'] ) || in_array( $options[
'dir'], [ self::DIR_OLDER, self::DIR_NEWER ] ),
189 'must be DIR_OLDER or DIR_NEWER'
192 !isset( $options[
'start'] ) && !isset( $options[
'end'] ) && $startFrom ===
null
193 || isset( $options[
'dir'] ),
195 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
198 !isset( $options[
'startFrom'] ),
199 '$options[\'startFrom\']',
200 'must not be provided, use $startFrom instead'
203 !isset( $startFrom ) || ( is_array( $startFrom ) && count( $startFrom ) === 2 ),
205 'must be a two-element array'
207 if ( array_key_exists(
'watchlistOwner', $options ) ) {
208 Assert::parameterType(
210 $options[
'watchlistOwner'],
211 '$options[\'watchlistOwner\']'
214 isset( $options[
'watchlistOwnerToken'] ),
215 '$options[\'watchlistOwnerToken\']',
216 'must be provided when providing watchlistOwner option'
220 $db = $this->getConnection();
222 $tables = $this->getWatchedItemsWithRCInfoQueryTables( $options );
223 $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
224 $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
225 $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
226 $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
228 if ( $startFrom !==
null ) {
229 $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
232 foreach ( $this->getExtensions() as $extension ) {
233 $extension->modifyWatchedItemsWithRCInfoQuery(
234 $user, $options, $db,
252 $limit = $dbOptions[
'LIMIT'] ?? INF;
255 foreach (
$res as $row ) {
256 if ( --$limit <= 0 ) {
257 $startFrom = [ $row->rc_timestamp, $row->rc_id ];
261 $target =
new TitleValue( (
int)$row->rc_namespace, $row->rc_title );
266 $this->watchedItemStore->getLatestNotificationTimestamp(
267 $row->wl_notificationtimestamp, $user, $target
269 $row->we_expiry ??
null
271 $this->getRecentChangeFieldsFromRow( $row )
275 foreach ( $this->getExtensions() as $extension ) {
276 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items,
$res, $startFrom );
308 $options += [
'namespaceIds' => [] ];
311 !isset( $options[
'sort'] ) || in_array( $options[
'sort'], [ self::SORT_ASC, self::SORT_DESC ] ),
312 '$options[\'sort\']',
313 'must be SORT_ASC or SORT_DESC'
316 !isset( $options[
'filter'] ) || in_array(
317 $options[
'filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
319 '$options[\'filter\']',
320 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
323 !isset( $options[
'from'] ) && !isset( $options[
'until'] ) && !isset( $options[
'startFrom'] )
324 || isset( $options[
'sort'] ),
325 '$options[\'sort\']',
326 'must be provided if any of "from", "until", "startFrom" options is provided'
329 $db = $this->getConnection();
331 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
332 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
334 $tables =
'watchlist';
336 if ( $this->expiryEnabled ) {
338 $tables = [
'watchlist',
'watchlist_expiry' ];
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 substr( $key, 0, 3 ) ===
'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 ) {
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( $user, $options )
495 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
497 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) && $db->
getType() ===
'mysql' ) {
499 $conds[] =
'rc_timestamp > ' . $db->
addQuotes(
'' );
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(
User $user, array $options ) {
530 if ( in_array( self::FILTER_MINOR, $options[
'filters'] ) ) {
531 $conds[] =
'rc_minor != 0';
532 } elseif ( in_array( self::FILTER_NOT_MINOR, $options[
'filters'] ) ) {
533 $conds[] =
'rc_minor = 0';
536 if ( in_array( self::FILTER_BOT, $options[
'filters'] ) ) {
537 $conds[] =
'rc_bot != 0';
538 } elseif ( in_array( self::FILTER_NOT_BOT, $options[
'filters'] ) ) {
539 $conds[] =
'rc_bot = 0';
542 if ( in_array( self::FILTER_ANON, $options[
'filters'] ) ) {
543 $conds[] =
'watchlist_actor.actor_user IS NULL';
544 } elseif ( in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ) {
545 $conds[] =
'watchlist_actor.actor_user IS NOT NULL';
551 if ( in_array( self::FILTER_PATROLLED, $options[
'filters'] ) ) {
553 } elseif ( in_array( self::FILTER_NOT_PATROLLED, $options[
'filters'] ) ) {
557 if ( in_array( self::FILTER_AUTOPATROLLED, $options[
'filters'] ) ) {
559 } elseif ( in_array( self::FILTER_NOT_AUTOPATROLLED, $options[
'filters'] ) ) {
564 if ( in_array( self::FILTER_UNREAD, $options[
'filters'] ) ) {
565 $conds[] =
'rc_timestamp >= wl_notificationtimestamp';
566 } elseif ( in_array( self::FILTER_NOT_UNREAD, $options[
'filters'] ) ) {
568 $conds[] =
'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
574 private function getStartEndConds(
IDatabase $db, array $options ) {
575 if ( !isset( $options[
'start'] ) && !isset( $options[
'end'] ) ) {
581 if ( isset( $options[
'start'] ) ) {
582 $after = $options[
'dir'] === self::DIR_OLDER ?
'<=' :
'>=';
583 $conds[] =
'rc_timestamp ' . $after .
' ' .
586 if ( isset( $options[
'end'] ) ) {
587 $before = $options[
'dir'] === self::DIR_OLDER ?
'>=' :
'<=';
588 $conds[] =
'rc_timestamp ' . $before .
' ' .
595 private function getUserRelatedConds(
IDatabase $db,
Authority $user, array $options ) {
596 if ( !array_key_exists(
'onlyByUser', $options ) && !array_key_exists(
'notByUser', $options ) ) {
602 if ( array_key_exists(
'onlyByUser', $options ) ) {
603 $conds[
'watchlist_actor.actor_name'] = $options[
'onlyByUser'];
604 } elseif ( array_key_exists(
'notByUser', $options ) ) {
605 $conds[] =
'watchlist_actor.actor_name<>' . $db->
addQuotes( $options[
'notByUser'] );
610 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
611 $bitmask = RevisionRecord::DELETED_USER;
612 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
613 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
616 $conds[] = $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask";
622 private function getExtraDeletedPageLogEntryRelatedCond(
IDatabase $db,
Authority $user ) {
626 if ( !$user->
isAllowed(
'deletedhistory' ) ) {
628 } elseif ( !$user->
isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
634 $db->
bitAnd(
'rc_deleted', $bitmask ) .
" != $bitmask",
640 private function getStartFromConds(
IDatabase $db, array $options, array $startFrom ) {
641 $op = $options[
'dir'] === self::DIR_OLDER ?
'<' :
'>';
642 list( $rcTimestamp, $rcId ) = $startFrom;
647 "rc_timestamp $op $rcTimestamp",
650 "rc_timestamp = $rcTimestamp",
660 private function getWatchedItemsForUserQueryConds(
663 $conds = [
'wl_user' => $user->
getId() ];
664 if ( $options[
'namespaceIds'] ) {
665 $conds[
'wl_namespace'] = array_map(
'intval', $options[
'namespaceIds'] );
667 if ( isset( $options[
'filter'] ) ) {
668 $filter = $options[
'filter'];
669 if ( $filter === self::FILTER_CHANGED ) {
670 $conds[] =
'wl_notificationtimestamp IS NOT NULL';
672 $conds[] =
'wl_notificationtimestamp IS NULL';
676 if ( isset( $options[
'from'] ) ) {
677 $op = $options[
'sort'] === self::SORT_ASC ?
'>' :
'<';
678 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'from'], $op );
680 if ( isset( $options[
'until'] ) ) {
681 $op = $options[
'sort'] === self::SORT_ASC ?
'<' :
'>';
682 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'until'], $op );
684 if ( isset( $options[
'startFrom'] ) ) {
685 $op = $options[
'sort'] === self::SORT_ASC ?
'>' :
'<';
686 $conds[] = $this->getFromUntilTargetConds( $db, $options[
'startFrom'], $op );
717 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
720 if ( array_key_exists(
'dir', $options ) ) {
721 $sort = $options[
'dir'] === self::DIR_OLDER ?
' DESC' :
'';
722 $dbOptions[
'ORDER BY'] = [
'rc_timestamp' . $sort,
'rc_id' . $sort ];
725 if ( array_key_exists(
'limit', $options ) ) {
726 $dbOptions[
'LIMIT'] = (int)$options[
'limit'] + 1;
728 if ( $this->maxQueryExecutionTime ) {
729 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
734 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
736 if ( array_key_exists(
'sort', $options ) ) {
737 $dbOptions[
'ORDER BY'] = [
738 "wl_namespace {$options['sort']}",
739 "wl_title {$options['sort']}"
741 if ( count( $options[
'namespaceIds'] ) === 1 ) {
742 $dbOptions[
'ORDER BY'] =
"wl_title {$options['sort']}";
745 if ( array_key_exists(
'limit', $options ) ) {
746 $dbOptions[
'LIMIT'] = (int)$options[
'limit'];
748 if ( $this->maxQueryExecutionTime ) {
749 $dbOptions[
'MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
754 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
756 'watchlist' => [
'JOIN',
758 'wl_namespace=rc_namespace',
764 if ( $this->expiryEnabled ) {
765 $joinConds[
'watchlist_expiry'] = [
'LEFT JOIN',
'wl_id = we_item' ];
768 if ( !$options[
'allRevisions'] ) {
769 $joinConds[
'page'] = [
'LEFT JOIN',
'rc_cur_id=page_id' ];
771 if ( in_array( self::INCLUDE_COMMENT, $options[
'includeFields'] ) ) {
772 $joinConds += $this->commentStore->getJoin(
'rc_comment' )[
'joins'];
774 if ( in_array( self::INCLUDE_USER, $options[
'includeFields'] ) ||
775 in_array( self::INCLUDE_USER_ID, $options[
'includeFields'] ) ||
776 in_array( self::FILTER_ANON, $options[
'filters'] ) ||
777 in_array( self::FILTER_NOT_ANON, $options[
'filters'] ) ||
778 array_key_exists(
'onlyByUser', $options ) || array_key_exists(
'notByUser', $options )
780 $joinConds[
'watchlist_actor'] = [
'JOIN',
'actor_id=rc_actor' ];