3 use Wikimedia\Rdbms\IDatabase
;
4 use MediaWiki\Linker\LinkTarget
;
5 use Wikimedia\Assert\Assert
;
6 use Wikimedia\Rdbms\LoadBalancer
;
9 * Class performing complex database queries related to WatchedItems.
16 * @license GNU GPL v2+
18 class WatchedItemQueryService
{
20 const DIR_OLDER
= 'older';
21 const DIR_NEWER
= 'newer';
23 const INCLUDE_FLAGS
= 'flags';
24 const INCLUDE_USER
= 'user';
25 const INCLUDE_USER_ID
= 'userid';
26 const INCLUDE_COMMENT
= 'comment';
27 const INCLUDE_PATROL_INFO
= 'patrol';
28 const INCLUDE_SIZES
= 'sizes';
29 const INCLUDE_LOG_INFO
= 'loginfo';
30 const INCLUDE_TAGS
= 'tags';
32 // FILTER_* constants are part of public API (are used in ApiQueryWatchlist and
33 // ApiQueryWatchlistRaw classes) and should not be changed.
34 // Changing values of those constants will result in a breaking change in the API
35 const FILTER_MINOR
= 'minor';
36 const FILTER_NOT_MINOR
= '!minor';
37 const FILTER_BOT
= 'bot';
38 const FILTER_NOT_BOT
= '!bot';
39 const FILTER_ANON
= 'anon';
40 const FILTER_NOT_ANON
= '!anon';
41 const FILTER_PATROLLED
= 'patrolled';
42 const FILTER_NOT_PATROLLED
= '!patrolled';
43 const FILTER_UNREAD
= 'unread';
44 const FILTER_NOT_UNREAD
= '!unread';
45 const FILTER_CHANGED
= 'changed';
46 const FILTER_NOT_CHANGED
= '!changed';
48 const SORT_ASC
= 'ASC';
49 const SORT_DESC
= 'DESC';
54 private $loadBalancer;
56 /** @var WatchedItemQueryServiceExtension[]|null */
57 private $extensions = null;
60 * @var CommentStore|null */
61 private $commentStore = null;
63 public function __construct( LoadBalancer
$loadBalancer ) {
64 $this->loadBalancer
= $loadBalancer;
68 * @return WatchedItemQueryServiceExtension[]
70 private function getExtensions() {
71 if ( $this->extensions
=== null ) {
72 $this->extensions
= [];
73 Hooks
::run( 'WatchedItemQueryServiceExtensions', [ &$this->extensions
, $this ] );
75 return $this->extensions
;
82 private function getConnection() {
83 return $this->loadBalancer
->getConnectionRef( DB_REPLICA
, [ 'watchlist' ] );
86 private function getCommentStore() {
87 if ( !$this->commentStore
) {
88 $this->commentStore
= new CommentStore( 'rc_comment' );
90 return $this->commentStore
;
95 * @param array $options Allowed keys:
96 * 'includeFields' => string[] RecentChange fields to be included in the result,
97 * self::INCLUDE_* constants should be used
98 * 'filters' => string[] optional filters to narrow down resulted items
99 * 'namespaceIds' => int[] optional namespace IDs to filter by
100 * (defaults to all namespaces)
101 * 'allRevisions' => bool return multiple revisions of the same page if true,
102 * only the most recent if false (default)
103 * 'rcTypes' => int[] which types of RecentChanges to include
104 * (defaults to all types), allowed values: RC_EDIT, RC_NEW,
105 * RC_LOG, RC_EXTERNAL, RC_CATEGORIZE
106 * 'onlyByUser' => string only list changes by a specified user
107 * 'notByUser' => string do not incluide changes by a specified user
108 * 'dir' => string in which direction to enumerate, accepted values:
109 * - DIR_OLDER list newest first
110 * - DIR_NEWER list oldest first
111 * 'start' => string (format accepted by wfTimestamp) requires 'dir' option,
112 * timestamp to start enumerating from
113 * 'end' => string (format accepted by wfTimestamp) requires 'dir' option,
114 * timestamp to end enumerating
115 * 'watchlistOwner' => User user whose watchlist items should be listed if different
116 * than the one specified with $user param,
117 * requires 'watchlistOwnerToken' option
118 * 'watchlistOwnerToken' => string a watchlist token used to access another user's
119 * watchlist, used with 'watchlistOwnerToken' option
120 * 'limit' => int maximum numbers of items to return
121 * 'usedInGenerator' => bool include only RecentChange id field required by the
122 * generator ('rc_cur_id' or 'rc_this_oldid') if true, or all
123 * id fields ('rc_cur_id', 'rc_this_oldid', 'rc_last_oldid')
125 * @param array|null &$startFrom Continuation value: [ string $rcTimestamp, int $rcId ]
126 * @return array of pairs ( WatchedItem $watchedItem, string[] $recentChangeInfo ),
127 * where $recentChangeInfo contains the following keys:
134 * Additional keys could be added by specifying the 'includeFields' option
136 public function getWatchedItemsWithRecentChangeInfo(
137 User
$user, array $options = [], &$startFrom = null
140 'includeFields' => [],
141 'namespaceIds' => [],
143 'allRevisions' => false,
144 'usedInGenerator' => false
148 !isset( $options['rcTypes'] )
149 ||
!array_diff( $options['rcTypes'], [ RC_EDIT
, RC_NEW
, RC_LOG
, RC_EXTERNAL
, RC_CATEGORIZE
] ),
150 '$options[\'rcTypes\']',
151 'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
154 !isset( $options['dir'] ) ||
in_array( $options['dir'], [ self
::DIR_OLDER
, self
::DIR_NEWER
] ),
156 'must be DIR_OLDER or DIR_NEWER'
159 !isset( $options['start'] ) && !isset( $options['end'] ) && $startFrom === null
160 ||
isset( $options['dir'] ),
162 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
165 !isset( $options['startFrom'] ),
166 '$options[\'startFrom\']',
167 'must not be provided, use $startFrom instead'
170 !isset( $startFrom ) ||
( is_array( $startFrom ) && count( $startFrom ) === 2 ),
172 'must be a two-element array'
174 if ( array_key_exists( 'watchlistOwner', $options ) ) {
175 Assert
::parameterType(
177 $options['watchlistOwner'],
178 '$options[\'watchlistOwner\']'
181 isset( $options['watchlistOwnerToken'] ),
182 '$options[\'watchlistOwnerToken\']',
183 'must be provided when providing watchlistOwner option'
187 $db = $this->getConnection();
189 $tables = $this->getWatchedItemsWithRCInfoQueryTables( $options );
190 $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
191 $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
192 $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
193 $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
195 if ( $startFrom !== null ) {
196 $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
199 foreach ( $this->getExtensions() as $extension ) {
200 $extension->modifyWatchedItemsWithRCInfoQuery(
201 $user, $options, $db,
219 $limit = isset( $dbOptions['LIMIT'] ) ?
$dbOptions['LIMIT'] : INF
;
222 foreach ( $res as $row ) {
223 if ( --$limit <= 0 ) {
224 $startFrom = [ $row->rc_timestamp
, $row->rc_id
];
231 new TitleValue( (int)$row->rc_namespace
, $row->rc_title
),
232 $row->wl_notificationtimestamp
234 $this->getRecentChangeFieldsFromRow( $row )
238 foreach ( $this->getExtensions() as $extension ) {
239 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
246 * For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser
249 * @param array $options Allowed keys:
250 * 'sort' => string optional sorting by namespace ID and title
251 * one of the self::SORT_* constants
252 * 'namespaceIds' => int[] optional namespace IDs to filter by (defaults to all namespaces)
253 * 'limit' => int maximum number of items to return
254 * 'filter' => string optional filter, one of the self::FILTER_* contants
255 * 'from' => LinkTarget requires 'sort' key, only return items starting from
256 * those related to the link target
257 * 'until' => LinkTarget requires 'sort' key, only return items until
258 * those related to the link target
259 * 'startFrom' => LinkTarget requires 'sort' key, only return items starting from
260 * those related to the link target, allows to skip some link targets
261 * specified using the form option
262 * @return WatchedItem[]
264 public function getWatchedItemsForUser( User
$user, array $options = [] ) {
265 if ( $user->isAnon() ) {
266 // TODO: should this just return an empty array or rather complain loud at this point
267 // as e.g. ApiBase::getWatchlistUser does?
271 $options +
= [ 'namespaceIds' => [] ];
274 !isset( $options['sort'] ) ||
in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ),
275 '$options[\'sort\']',
276 'must be SORT_ASC or SORT_DESC'
279 !isset( $options['filter'] ) ||
in_array(
280 $options['filter'], [ self
::FILTER_CHANGED
, self
::FILTER_NOT_CHANGED
]
282 '$options[\'filter\']',
283 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
286 !isset( $options['from'] ) && !isset( $options['until'] ) && !isset( $options['startFrom'] )
287 ||
isset( $options['sort'] ),
288 '$options[\'sort\']',
289 'must be provided if any of "from", "until", "startFrom" options is provided'
292 $db = $this->getConnection();
294 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
295 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
299 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
306 foreach ( $res as $row ) {
307 // todo these could all be cached at some point?
308 $watchedItems[] = new WatchedItem(
310 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
311 $row->wl_notificationtimestamp
315 return $watchedItems;
318 private function getRecentChangeFieldsFromRow( stdClass
$row ) {
319 // This can be simplified to single array_filter call filtering by key value,
320 // once we stop supporting PHP 5.5
321 $allFields = get_object_vars( $row );
322 $rcKeys = array_filter(
323 array_keys( $allFields ),
325 return substr( $key, 0, 3 ) === 'rc_';
328 return array_intersect_key( $allFields, array_flip( $rcKeys ) );
331 private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
332 $tables = [ 'recentchanges', 'watchlist' ];
333 if ( !$options['allRevisions'] ) {
336 if ( in_array( self
::INCLUDE_COMMENT
, $options['includeFields'] ) ) {
337 $tables +
= $this->getCommentStore()->getJoin()['tables'];
339 if ( in_array( self
::INCLUDE_TAGS
, $options['includeFields'] ) ) {
340 $tables[] = 'tag_summary';
345 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
353 'wl_notificationtimestamp'
361 if ( $options['usedInGenerator'] ) {
362 if ( $options['allRevisions'] ) {
363 $rcIdFields = [ 'rc_this_oldid' ];
365 $rcIdFields = [ 'rc_cur_id' ];
368 $fields = array_merge( $fields, $rcIdFields );
370 if ( in_array( self
::INCLUDE_FLAGS
, $options['includeFields'] ) ) {
371 $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
373 if ( in_array( self
::INCLUDE_USER
, $options['includeFields'] ) ) {
374 $fields[] = 'rc_user_text';
376 if ( in_array( self
::INCLUDE_USER_ID
, $options['includeFields'] ) ) {
377 $fields[] = 'rc_user';
379 if ( in_array( self
::INCLUDE_COMMENT
, $options['includeFields'] ) ) {
380 $fields +
= $this->getCommentStore()->getJoin()['fields'];
382 if ( in_array( self
::INCLUDE_PATROL_INFO
, $options['includeFields'] ) ) {
383 $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
385 if ( in_array( self
::INCLUDE_SIZES
, $options['includeFields'] ) ) {
386 $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
388 if ( in_array( self
::INCLUDE_LOG_INFO
, $options['includeFields'] ) ) {
389 $fields = array_merge( $fields, [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ] );
391 if ( in_array( self
::INCLUDE_TAGS
, $options['includeFields'] ) ) {
392 // prefixed with rc_ to include the field in getRecentChangeFieldsFromRow
393 $fields['rc_tags'] = 'ts_tags';
399 private function getWatchedItemsWithRCInfoQueryConds(
404 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
405 $conds = [ 'wl_user' => $watchlistOwnerId ];
407 if ( !$options['allRevisions'] ) {
408 $conds[] = $db->makeList(
409 [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG
],
414 if ( $options['namespaceIds'] ) {
415 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
418 if ( array_key_exists( 'rcTypes', $options ) ) {
419 $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
422 $conds = array_merge(
424 $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
427 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
429 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
430 if ( $db->getType() === 'mysql' ) {
431 // This is an index optimization for mysql
432 $conds[] = 'rc_timestamp > ' . $db->addQuotes( '' );
436 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
438 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
439 if ( $deletedPageLogCond ) {
440 $conds[] = $deletedPageLogCond;
446 private function getWatchlistOwnerId( User
$user, array $options ) {
447 if ( array_key_exists( 'watchlistOwner', $options ) ) {
448 /** @var User $watchlistOwner */
449 $watchlistOwner = $options['watchlistOwner'];
450 $ownersToken = $watchlistOwner->getOption( 'watchlisttoken' );
451 $token = $options['watchlistOwnerToken'];
452 if ( $ownersToken == '' ||
!hash_equals( $ownersToken, $token ) ) {
453 throw ApiUsageException
::newWithMessage( null, 'apierror-bad-watchlist-token', 'bad_wltoken' );
455 return $watchlistOwner->getId();
457 return $user->getId();
460 private function getWatchedItemsWithRCInfoQueryFilterConds( User
$user, array $options ) {
463 if ( in_array( self
::FILTER_MINOR
, $options['filters'] ) ) {
464 $conds[] = 'rc_minor != 0';
465 } elseif ( in_array( self
::FILTER_NOT_MINOR
, $options['filters'] ) ) {
466 $conds[] = 'rc_minor = 0';
469 if ( in_array( self
::FILTER_BOT
, $options['filters'] ) ) {
470 $conds[] = 'rc_bot != 0';
471 } elseif ( in_array( self
::FILTER_NOT_BOT
, $options['filters'] ) ) {
472 $conds[] = 'rc_bot = 0';
475 if ( in_array( self
::FILTER_ANON
, $options['filters'] ) ) {
476 $conds[] = 'rc_user = 0';
477 } elseif ( in_array( self
::FILTER_NOT_ANON
, $options['filters'] ) ) {
478 $conds[] = 'rc_user != 0';
481 if ( $user->useRCPatrol() ||
$user->useNPPatrol() ) {
482 // TODO: not sure if this should simply ignore patrolled filters if user does not have the patrol
483 // right, or maybe rather fail loud at this point, same as e.g. ApiQueryWatchlist does?
484 if ( in_array( self
::FILTER_PATROLLED
, $options['filters'] ) ) {
485 $conds[] = 'rc_patrolled != 0';
486 } elseif ( in_array( self
::FILTER_NOT_PATROLLED
, $options['filters'] ) ) {
487 $conds[] = 'rc_patrolled = 0';
491 if ( in_array( self
::FILTER_UNREAD
, $options['filters'] ) ) {
492 $conds[] = 'rc_timestamp >= wl_notificationtimestamp';
493 } elseif ( in_array( self
::FILTER_NOT_UNREAD
, $options['filters'] ) ) {
494 // TODO: should this be changed to use Database::makeList?
495 $conds[] = 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
501 private function getStartEndConds( IDatabase
$db, array $options ) {
502 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
508 if ( isset( $options['start'] ) ) {
509 $after = $options['dir'] === self
::DIR_OLDER ?
'<=' : '>=';
510 $conds[] = 'rc_timestamp ' . $after . ' ' .
511 $db->addQuotes( $db->timestamp( $options['start'] ) );
513 if ( isset( $options['end'] ) ) {
514 $before = $options['dir'] === self
::DIR_OLDER ?
'>=' : '<=';
515 $conds[] = 'rc_timestamp ' . $before . ' ' .
516 $db->addQuotes( $db->timestamp( $options['end'] ) );
522 private function getUserRelatedConds( IDatabase
$db, User
$user, array $options ) {
523 if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
529 if ( array_key_exists( 'onlyByUser', $options ) ) {
530 $conds['rc_user_text'] = $options['onlyByUser'];
531 } elseif ( array_key_exists( 'notByUser', $options ) ) {
532 $conds[] = 'rc_user_text != ' . $db->addQuotes( $options['notByUser'] );
535 // Avoid brute force searches (T19342)
537 if ( !$user->isAllowed( 'deletedhistory' ) ) {
538 $bitmask = Revision
::DELETED_USER
;
539 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
540 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
543 $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
549 private function getExtraDeletedPageLogEntryRelatedCond( IDatabase
$db, User
$user ) {
550 // LogPage::DELETED_ACTION hides the affected page, too. So hide those
551 // entirely from the watchlist, or someone could guess the title.
553 if ( !$user->isAllowed( 'deletedhistory' ) ) {
554 $bitmask = LogPage
::DELETED_ACTION
;
555 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
556 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
559 return $db->makeList( [
560 'rc_type != ' . RC_LOG
,
561 $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
567 private function getStartFromConds( IDatabase
$db, array $options, array $startFrom ) {
568 $op = $options['dir'] === self
::DIR_OLDER ?
'<' : '>';
569 list( $rcTimestamp, $rcId ) = $startFrom;
570 $rcTimestamp = $db->addQuotes( $db->timestamp( $rcTimestamp ) );
572 return $db->makeList(
574 "rc_timestamp $op $rcTimestamp",
577 "rc_timestamp = $rcTimestamp",
587 private function getWatchedItemsForUserQueryConds( IDatabase
$db, User
$user, array $options ) {
588 $conds = [ 'wl_user' => $user->getId() ];
589 if ( $options['namespaceIds'] ) {
590 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
592 if ( isset( $options['filter'] ) ) {
593 $filter = $options['filter'];
594 if ( $filter === self
::FILTER_CHANGED
) {
595 $conds[] = 'wl_notificationtimestamp IS NOT NULL';
597 $conds[] = 'wl_notificationtimestamp IS NULL';
601 if ( isset( $options['from'] ) ) {
602 $op = $options['sort'] === self
::SORT_ASC ?
'>' : '<';
603 $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
605 if ( isset( $options['until'] ) ) {
606 $op = $options['sort'] === self
::SORT_ASC ?
'<' : '>';
607 $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
609 if ( isset( $options['startFrom'] ) ) {
610 $op = $options['sort'] === self
::SORT_ASC ?
'>' : '<';
611 $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
618 * Creates a query condition part for getting only items before or after the given link target
619 * (while ordering using $sort mode)
621 * @param IDatabase $db
622 * @param LinkTarget $target
623 * @param string $op comparison operator to use in the conditions
626 private function getFromUntilTargetConds( IDatabase
$db, LinkTarget
$target, $op ) {
627 return $db->makeList(
629 "wl_namespace $op " . $target->getNamespace(),
632 'wl_namespace = ' . $target->getNamespace(),
633 "wl_title $op= " . $db->addQuotes( $target->getDBkey() )
642 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
645 if ( array_key_exists( 'dir', $options ) ) {
646 $sort = $options['dir'] === self
::DIR_OLDER ?
' DESC' : '';
647 $dbOptions['ORDER BY'] = [ 'rc_timestamp' . $sort, 'rc_id' . $sort ];
650 if ( array_key_exists( 'limit', $options ) ) {
651 $dbOptions['LIMIT'] = (int)$options['limit'] +
1;
657 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
659 if ( array_key_exists( 'sort', $options ) ) {
660 $dbOptions['ORDER BY'] = [
661 "wl_namespace {$options['sort']}",
662 "wl_title {$options['sort']}"
664 if ( count( $options['namespaceIds'] ) === 1 ) {
665 $dbOptions['ORDER BY'] = "wl_title {$options['sort']}";
668 if ( array_key_exists( 'limit', $options ) ) {
669 $dbOptions['LIMIT'] = (int)$options['limit'];
674 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
676 'watchlist' => [ 'INNER JOIN',
678 'wl_namespace=rc_namespace',
683 if ( !$options['allRevisions'] ) {
684 $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
686 if ( in_array( self
::INCLUDE_COMMENT
, $options['includeFields'] ) ) {
687 $joinConds +
= $this->getCommentStore()->getJoin()['joins'];
689 if ( in_array( self
::INCLUDE_TAGS
, $options['includeFields'] ) ) {
690 $joinConds['tag_summary'] = [ 'LEFT JOIN', [ 'rc_id=ts_rc_id' ] ];