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';
31 // FILTER_* constants are part of public API (are used in ApiQueryWatchlist and
32 // ApiQueryWatchlistRaw classes) and should not be changed.
33 // Changing values of those constants will result in a breaking change in the API
34 const FILTER_MINOR
= 'minor';
35 const FILTER_NOT_MINOR
= '!minor';
36 const FILTER_BOT
= 'bot';
37 const FILTER_NOT_BOT
= '!bot';
38 const FILTER_ANON
= 'anon';
39 const FILTER_NOT_ANON
= '!anon';
40 const FILTER_PATROLLED
= 'patrolled';
41 const FILTER_NOT_PATROLLED
= '!patrolled';
42 const FILTER_UNREAD
= 'unread';
43 const FILTER_NOT_UNREAD
= '!unread';
44 const FILTER_CHANGED
= 'changed';
45 const FILTER_NOT_CHANGED
= '!changed';
47 const SORT_ASC
= 'ASC';
48 const SORT_DESC
= 'DESC';
53 private $loadBalancer;
55 /** @var WatchedItemQueryServiceExtension[]|null */
56 private $extensions = null;
59 * @var CommentStore|null */
60 private $commentStore = null;
62 public function __construct( LoadBalancer
$loadBalancer ) {
63 $this->loadBalancer
= $loadBalancer;
67 * @return WatchedItemQueryServiceExtension[]
69 private function getExtensions() {
70 if ( $this->extensions
=== null ) {
71 $this->extensions
= [];
72 Hooks
::run( 'WatchedItemQueryServiceExtensions', [ &$this->extensions
, $this ] );
74 return $this->extensions
;
81 private function getConnection() {
82 return $this->loadBalancer
->getConnectionRef( DB_REPLICA
, [ 'watchlist' ] );
85 private function getCommentStore() {
86 if ( !$this->commentStore
) {
87 $this->commentStore
= new CommentStore( 'rc_comment' );
89 return $this->commentStore
;
94 * @param array $options Allowed keys:
95 * 'includeFields' => string[] RecentChange fields to be included in the result,
96 * self::INCLUDE_* constants should be used
97 * 'filters' => string[] optional filters to narrow down resulted items
98 * 'namespaceIds' => int[] optional namespace IDs to filter by
99 * (defaults to all namespaces)
100 * 'allRevisions' => bool return multiple revisions of the same page if true,
101 * only the most recent if false (default)
102 * 'rcTypes' => int[] which types of RecentChanges to include
103 * (defaults to all types), allowed values: RC_EDIT, RC_NEW,
104 * RC_LOG, RC_EXTERNAL, RC_CATEGORIZE
105 * 'onlyByUser' => string only list changes by a specified user
106 * 'notByUser' => string do not incluide changes by a specified user
107 * 'dir' => string in which direction to enumerate, accepted values:
108 * - DIR_OLDER list newest first
109 * - DIR_NEWER list oldest first
110 * 'start' => string (format accepted by wfTimestamp) requires 'dir' option,
111 * timestamp to start enumerating from
112 * 'end' => string (format accepted by wfTimestamp) requires 'dir' option,
113 * timestamp to end enumerating
114 * 'watchlistOwner' => User user whose watchlist items should be listed if different
115 * than the one specified with $user param,
116 * requires 'watchlistOwnerToken' option
117 * 'watchlistOwnerToken' => string a watchlist token used to access another user's
118 * watchlist, used with 'watchlistOwnerToken' option
119 * 'limit' => int maximum numbers of items to return
120 * 'usedInGenerator' => bool include only RecentChange id field required by the
121 * generator ('rc_cur_id' or 'rc_this_oldid') if true, or all
122 * id fields ('rc_cur_id', 'rc_this_oldid', 'rc_last_oldid')
124 * @param array|null &$startFrom Continuation value: [ string $rcTimestamp, int $rcId ]
125 * @return array of pairs ( WatchedItem $watchedItem, string[] $recentChangeInfo ),
126 * where $recentChangeInfo contains the following keys:
133 * Additional keys could be added by specifying the 'includeFields' option
135 public function getWatchedItemsWithRecentChangeInfo(
136 User
$user, array $options = [], &$startFrom = null
139 'includeFields' => [],
140 'namespaceIds' => [],
142 'allRevisions' => false,
143 'usedInGenerator' => false
147 !isset( $options['rcTypes'] )
148 ||
!array_diff( $options['rcTypes'], [ RC_EDIT
, RC_NEW
, RC_LOG
, RC_EXTERNAL
, RC_CATEGORIZE
] ),
149 '$options[\'rcTypes\']',
150 'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
153 !isset( $options['dir'] ) ||
in_array( $options['dir'], [ self
::DIR_OLDER
, self
::DIR_NEWER
] ),
155 'must be DIR_OLDER or DIR_NEWER'
158 !isset( $options['start'] ) && !isset( $options['end'] ) && $startFrom === null
159 ||
isset( $options['dir'] ),
161 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
164 !isset( $options['startFrom'] ),
165 '$options[\'startFrom\']',
166 'must not be provided, use $startFrom instead'
169 !isset( $startFrom ) ||
( is_array( $startFrom ) && count( $startFrom ) === 2 ),
171 'must be a two-element array'
173 if ( array_key_exists( 'watchlistOwner', $options ) ) {
174 Assert
::parameterType(
176 $options['watchlistOwner'],
177 '$options[\'watchlistOwner\']'
180 isset( $options['watchlistOwnerToken'] ),
181 '$options[\'watchlistOwnerToken\']',
182 'must be provided when providing watchlistOwner option'
186 $db = $this->getConnection();
188 $tables = $this->getWatchedItemsWithRCInfoQueryTables( $options );
189 $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
190 $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
191 $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
192 $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
194 if ( $startFrom !== null ) {
195 $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
198 foreach ( $this->getExtensions() as $extension ) {
199 $extension->modifyWatchedItemsWithRCInfoQuery(
200 $user, $options, $db,
218 $limit = isset( $dbOptions['LIMIT'] ) ?
$dbOptions['LIMIT'] : INF
;
221 foreach ( $res as $row ) {
222 if ( --$limit <= 0 ) {
223 $startFrom = [ $row->rc_timestamp
, $row->rc_id
];
230 new TitleValue( (int)$row->rc_namespace
, $row->rc_title
),
231 $row->wl_notificationtimestamp
233 $this->getRecentChangeFieldsFromRow( $row )
237 foreach ( $this->getExtensions() as $extension ) {
238 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
245 * For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser
248 * @param array $options Allowed keys:
249 * 'sort' => string optional sorting by namespace ID and title
250 * one of the self::SORT_* constants
251 * 'namespaceIds' => int[] optional namespace IDs to filter by (defaults to all namespaces)
252 * 'limit' => int maximum number of items to return
253 * 'filter' => string optional filter, one of the self::FILTER_* contants
254 * 'from' => LinkTarget requires 'sort' key, only return items starting from
255 * those related to the link target
256 * 'until' => LinkTarget requires 'sort' key, only return items until
257 * those related to the link target
258 * 'startFrom' => LinkTarget requires 'sort' key, only return items starting from
259 * those related to the link target, allows to skip some link targets
260 * specified using the form option
261 * @return WatchedItem[]
263 public function getWatchedItemsForUser( User
$user, array $options = [] ) {
264 if ( $user->isAnon() ) {
265 // TODO: should this just return an empty array or rather complain loud at this point
266 // as e.g. ApiBase::getWatchlistUser does?
270 $options +
= [ 'namespaceIds' => [] ];
273 !isset( $options['sort'] ) ||
in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ),
274 '$options[\'sort\']',
275 'must be SORT_ASC or SORT_DESC'
278 !isset( $options['filter'] ) ||
in_array(
279 $options['filter'], [ self
::FILTER_CHANGED
, self
::FILTER_NOT_CHANGED
]
281 '$options[\'filter\']',
282 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
285 !isset( $options['from'] ) && !isset( $options['until'] ) && !isset( $options['startFrom'] )
286 ||
isset( $options['sort'] ),
287 '$options[\'sort\']',
288 'must be provided if any of "from", "until", "startFrom" options is provided'
291 $db = $this->getConnection();
293 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
294 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
298 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
305 foreach ( $res as $row ) {
306 // todo these could all be cached at some point?
307 $watchedItems[] = new WatchedItem(
309 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
310 $row->wl_notificationtimestamp
314 return $watchedItems;
317 private function getRecentChangeFieldsFromRow( stdClass
$row ) {
318 // This can be simplified to single array_filter call filtering by key value,
319 // once we stop supporting PHP 5.5
320 $allFields = get_object_vars( $row );
321 $rcKeys = array_filter(
322 array_keys( $allFields ),
324 return substr( $key, 0, 3 ) === 'rc_';
327 return array_intersect_key( $allFields, array_flip( $rcKeys ) );
330 private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
331 $tables = [ 'recentchanges', 'watchlist' ];
332 if ( !$options['allRevisions'] ) {
335 if ( in_array( self
::INCLUDE_COMMENT
, $options['includeFields'] ) ) {
336 $tables +
= $this->getCommentStore()->getJoin()['tables'];
341 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
349 'wl_notificationtimestamp'
357 if ( $options['usedInGenerator'] ) {
358 if ( $options['allRevisions'] ) {
359 $rcIdFields = [ 'rc_this_oldid' ];
361 $rcIdFields = [ 'rc_cur_id' ];
364 $fields = array_merge( $fields, $rcIdFields );
366 if ( in_array( self
::INCLUDE_FLAGS
, $options['includeFields'] ) ) {
367 $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
369 if ( in_array( self
::INCLUDE_USER
, $options['includeFields'] ) ) {
370 $fields[] = 'rc_user_text';
372 if ( in_array( self
::INCLUDE_USER_ID
, $options['includeFields'] ) ) {
373 $fields[] = 'rc_user';
375 if ( in_array( self
::INCLUDE_COMMENT
, $options['includeFields'] ) ) {
376 $fields +
= $this->getCommentStore()->getJoin()['fields'];
378 if ( in_array( self
::INCLUDE_PATROL_INFO
, $options['includeFields'] ) ) {
379 $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
381 if ( in_array( self
::INCLUDE_SIZES
, $options['includeFields'] ) ) {
382 $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
384 if ( in_array( self
::INCLUDE_LOG_INFO
, $options['includeFields'] ) ) {
385 $fields = array_merge( $fields, [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ] );
391 private function getWatchedItemsWithRCInfoQueryConds(
396 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
397 $conds = [ 'wl_user' => $watchlistOwnerId ];
399 if ( !$options['allRevisions'] ) {
400 $conds[] = $db->makeList(
401 [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG
],
406 if ( $options['namespaceIds'] ) {
407 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
410 if ( array_key_exists( 'rcTypes', $options ) ) {
411 $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
414 $conds = array_merge(
416 $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
419 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
421 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
422 if ( $db->getType() === 'mysql' ) {
423 // This is an index optimization for mysql
424 $conds[] = 'rc_timestamp > ' . $db->addQuotes( '' );
428 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
430 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
431 if ( $deletedPageLogCond ) {
432 $conds[] = $deletedPageLogCond;
438 private function getWatchlistOwnerId( User
$user, array $options ) {
439 if ( array_key_exists( 'watchlistOwner', $options ) ) {
440 /** @var User $watchlistOwner */
441 $watchlistOwner = $options['watchlistOwner'];
442 $ownersToken = $watchlistOwner->getOption( 'watchlisttoken' );
443 $token = $options['watchlistOwnerToken'];
444 if ( $ownersToken == '' ||
!hash_equals( $ownersToken, $token ) ) {
445 throw ApiUsageException
::newWithMessage( null, 'apierror-bad-watchlist-token', 'bad_wltoken' );
447 return $watchlistOwner->getId();
449 return $user->getId();
452 private function getWatchedItemsWithRCInfoQueryFilterConds( User
$user, array $options ) {
455 if ( in_array( self
::FILTER_MINOR
, $options['filters'] ) ) {
456 $conds[] = 'rc_minor != 0';
457 } elseif ( in_array( self
::FILTER_NOT_MINOR
, $options['filters'] ) ) {
458 $conds[] = 'rc_minor = 0';
461 if ( in_array( self
::FILTER_BOT
, $options['filters'] ) ) {
462 $conds[] = 'rc_bot != 0';
463 } elseif ( in_array( self
::FILTER_NOT_BOT
, $options['filters'] ) ) {
464 $conds[] = 'rc_bot = 0';
467 if ( in_array( self
::FILTER_ANON
, $options['filters'] ) ) {
468 $conds[] = 'rc_user = 0';
469 } elseif ( in_array( self
::FILTER_NOT_ANON
, $options['filters'] ) ) {
470 $conds[] = 'rc_user != 0';
473 if ( $user->useRCPatrol() ||
$user->useNPPatrol() ) {
474 // TODO: not sure if this should simply ignore patrolled filters if user does not have the patrol
475 // right, or maybe rather fail loud at this point, same as e.g. ApiQueryWatchlist does?
476 if ( in_array( self
::FILTER_PATROLLED
, $options['filters'] ) ) {
477 $conds[] = 'rc_patrolled != 0';
478 } elseif ( in_array( self
::FILTER_NOT_PATROLLED
, $options['filters'] ) ) {
479 $conds[] = 'rc_patrolled = 0';
483 if ( in_array( self
::FILTER_UNREAD
, $options['filters'] ) ) {
484 $conds[] = 'rc_timestamp >= wl_notificationtimestamp';
485 } elseif ( in_array( self
::FILTER_NOT_UNREAD
, $options['filters'] ) ) {
486 // TODO: should this be changed to use Database::makeList?
487 $conds[] = 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
493 private function getStartEndConds( IDatabase
$db, array $options ) {
494 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
500 if ( isset( $options['start'] ) ) {
501 $after = $options['dir'] === self
::DIR_OLDER ?
'<=' : '>=';
502 $conds[] = 'rc_timestamp ' . $after . ' ' .
503 $db->addQuotes( $db->timestamp( $options['start'] ) );
505 if ( isset( $options['end'] ) ) {
506 $before = $options['dir'] === self
::DIR_OLDER ?
'>=' : '<=';
507 $conds[] = 'rc_timestamp ' . $before . ' ' .
508 $db->addQuotes( $db->timestamp( $options['end'] ) );
514 private function getUserRelatedConds( IDatabase
$db, User
$user, array $options ) {
515 if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
521 if ( array_key_exists( 'onlyByUser', $options ) ) {
522 $conds['rc_user_text'] = $options['onlyByUser'];
523 } elseif ( array_key_exists( 'notByUser', $options ) ) {
524 $conds[] = 'rc_user_text != ' . $db->addQuotes( $options['notByUser'] );
527 // Avoid brute force searches (T19342)
529 if ( !$user->isAllowed( 'deletedhistory' ) ) {
530 $bitmask = Revision
::DELETED_USER
;
531 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
532 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
535 $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
541 private function getExtraDeletedPageLogEntryRelatedCond( IDatabase
$db, User
$user ) {
542 // LogPage::DELETED_ACTION hides the affected page, too. So hide those
543 // entirely from the watchlist, or someone could guess the title.
545 if ( !$user->isAllowed( 'deletedhistory' ) ) {
546 $bitmask = LogPage
::DELETED_ACTION
;
547 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
548 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
551 return $db->makeList( [
552 'rc_type != ' . RC_LOG
,
553 $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
559 private function getStartFromConds( IDatabase
$db, array $options, array $startFrom ) {
560 $op = $options['dir'] === self
::DIR_OLDER ?
'<' : '>';
561 list( $rcTimestamp, $rcId ) = $startFrom;
562 $rcTimestamp = $db->addQuotes( $db->timestamp( $rcTimestamp ) );
564 return $db->makeList(
566 "rc_timestamp $op $rcTimestamp",
569 "rc_timestamp = $rcTimestamp",
579 private function getWatchedItemsForUserQueryConds( IDatabase
$db, User
$user, array $options ) {
580 $conds = [ 'wl_user' => $user->getId() ];
581 if ( $options['namespaceIds'] ) {
582 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
584 if ( isset( $options['filter'] ) ) {
585 $filter = $options['filter'];
586 if ( $filter === self
::FILTER_CHANGED
) {
587 $conds[] = 'wl_notificationtimestamp IS NOT NULL';
589 $conds[] = 'wl_notificationtimestamp IS NULL';
593 if ( isset( $options['from'] ) ) {
594 $op = $options['sort'] === self
::SORT_ASC ?
'>' : '<';
595 $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
597 if ( isset( $options['until'] ) ) {
598 $op = $options['sort'] === self
::SORT_ASC ?
'<' : '>';
599 $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
601 if ( isset( $options['startFrom'] ) ) {
602 $op = $options['sort'] === self
::SORT_ASC ?
'>' : '<';
603 $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
610 * Creates a query condition part for getting only items before or after the given link target
611 * (while ordering using $sort mode)
613 * @param IDatabase $db
614 * @param LinkTarget $target
615 * @param string $op comparison operator to use in the conditions
618 private function getFromUntilTargetConds( IDatabase
$db, LinkTarget
$target, $op ) {
619 return $db->makeList(
621 "wl_namespace $op " . $target->getNamespace(),
624 'wl_namespace = ' . $target->getNamespace(),
625 "wl_title $op= " . $db->addQuotes( $target->getDBkey() )
634 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
637 if ( array_key_exists( 'dir', $options ) ) {
638 $sort = $options['dir'] === self
::DIR_OLDER ?
' DESC' : '';
639 $dbOptions['ORDER BY'] = [ 'rc_timestamp' . $sort, 'rc_id' . $sort ];
642 if ( array_key_exists( 'limit', $options ) ) {
643 $dbOptions['LIMIT'] = (int)$options['limit'] +
1;
649 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
651 if ( array_key_exists( 'sort', $options ) ) {
652 $dbOptions['ORDER BY'] = [
653 "wl_namespace {$options['sort']}",
654 "wl_title {$options['sort']}"
656 if ( count( $options['namespaceIds'] ) === 1 ) {
657 $dbOptions['ORDER BY'] = "wl_title {$options['sort']}";
660 if ( array_key_exists( 'limit', $options ) ) {
661 $dbOptions['LIMIT'] = (int)$options['limit'];
666 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
668 'watchlist' => [ 'INNER JOIN',
670 'wl_namespace=rc_namespace',
675 if ( !$options['allRevisions'] ) {
676 $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
678 if ( in_array( self
::INCLUDE_COMMENT
, $options['includeFields'] ) ) {
679 $joinConds +
= $this->getCommentStore()->getJoin()['joins'];