3 use MediaWiki\Linker\LinkTarget
;
4 use Wikimedia\Assert\Assert
;
5 use Wikimedia\Rdbms\LoadBalancer
;
8 * Class performing complex database queries related to WatchedItems.
15 * @license GNU GPL v2+
17 class WatchedItemQueryService
{
19 const DIR_OLDER
= 'older';
20 const DIR_NEWER
= 'newer';
22 const INCLUDE_FLAGS
= 'flags';
23 const INCLUDE_USER
= 'user';
24 const INCLUDE_USER_ID
= 'userid';
25 const INCLUDE_COMMENT
= 'comment';
26 const INCLUDE_PATROL_INFO
= 'patrol';
27 const INCLUDE_SIZES
= 'sizes';
28 const INCLUDE_LOG_INFO
= 'loginfo';
30 // FILTER_* constants are part of public API (are used in ApiQueryWatchlist and
31 // ApiQueryWatchlistRaw classes) and should not be changed.
32 // Changing values of those constants will result in a breaking change in the API
33 const FILTER_MINOR
= 'minor';
34 const FILTER_NOT_MINOR
= '!minor';
35 const FILTER_BOT
= 'bot';
36 const FILTER_NOT_BOT
= '!bot';
37 const FILTER_ANON
= 'anon';
38 const FILTER_NOT_ANON
= '!anon';
39 const FILTER_PATROLLED
= 'patrolled';
40 const FILTER_NOT_PATROLLED
= '!patrolled';
41 const FILTER_UNREAD
= 'unread';
42 const FILTER_NOT_UNREAD
= '!unread';
43 const FILTER_CHANGED
= 'changed';
44 const FILTER_NOT_CHANGED
= '!changed';
46 const SORT_ASC
= 'ASC';
47 const SORT_DESC
= 'DESC';
52 private $loadBalancer;
54 /** @var WatchedItemQueryServiceExtension[]|null */
55 private $extensions = null;
57 public function __construct( LoadBalancer
$loadBalancer ) {
58 $this->loadBalancer
= $loadBalancer;
62 * @return WatchedItemQueryServiceExtension[]
64 private function getExtensions() {
65 if ( $this->extensions
=== null ) {
66 $this->extensions
= [];
67 Hooks
::run( 'WatchedItemQueryServiceExtensions', [ &$this->extensions
, $this ] );
69 return $this->extensions
;
76 private function getConnection() {
77 return $this->loadBalancer
->getConnectionRef( DB_REPLICA
, [ 'watchlist' ] );
82 * @param array $options Allowed keys:
83 * 'includeFields' => string[] RecentChange fields to be included in the result,
84 * self::INCLUDE_* constants should be used
85 * 'filters' => string[] optional filters to narrow down resulted items
86 * 'namespaceIds' => int[] optional namespace IDs to filter by
87 * (defaults to all namespaces)
88 * 'allRevisions' => bool return multiple revisions of the same page if true,
89 * only the most recent if false (default)
90 * 'rcTypes' => int[] which types of RecentChanges to include
91 * (defaults to all types), allowed values: RC_EDIT, RC_NEW,
92 * RC_LOG, RC_EXTERNAL, RC_CATEGORIZE
93 * 'onlyByUser' => string only list changes by a specified user
94 * 'notByUser' => string do not incluide changes by a specified user
95 * 'dir' => string in which direction to enumerate, accepted values:
96 * - DIR_OLDER list newest first
97 * - DIR_NEWER list oldest first
98 * 'start' => string (format accepted by wfTimestamp) requires 'dir' option,
99 * timestamp to start enumerating from
100 * 'end' => string (format accepted by wfTimestamp) requires 'dir' option,
101 * timestamp to end enumerating
102 * 'watchlistOwner' => User user whose watchlist items should be listed if different
103 * than the one specified with $user param,
104 * requires 'watchlistOwnerToken' option
105 * 'watchlistOwnerToken' => string a watchlist token used to access another user's
106 * watchlist, used with 'watchlistOwnerToken' option
107 * 'limit' => int maximum numbers of items to return
108 * 'usedInGenerator' => bool include only RecentChange id field required by the
109 * generator ('rc_cur_id' or 'rc_this_oldid') if true, or all
110 * id fields ('rc_cur_id', 'rc_this_oldid', 'rc_last_oldid')
112 * @param array|null &$startFrom Continuation value: [ string $rcTimestamp, int $rcId ]
113 * @return array of pairs ( WatchedItem $watchedItem, string[] $recentChangeInfo ),
114 * where $recentChangeInfo contains the following keys:
121 * Additional keys could be added by specifying the 'includeFields' option
123 public function getWatchedItemsWithRecentChangeInfo(
124 User
$user, array $options = [], &$startFrom = null
127 'includeFields' => [],
128 'namespaceIds' => [],
130 'allRevisions' => false,
131 'usedInGenerator' => false
135 !isset( $options['rcTypes'] )
136 ||
!array_diff( $options['rcTypes'], [ RC_EDIT
, RC_NEW
, RC_LOG
, RC_EXTERNAL
, RC_CATEGORIZE
] ),
137 '$options[\'rcTypes\']',
138 'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
141 !isset( $options['dir'] ) ||
in_array( $options['dir'], [ self
::DIR_OLDER
, self
::DIR_NEWER
] ),
143 'must be DIR_OLDER or DIR_NEWER'
146 !isset( $options['start'] ) && !isset( $options['end'] ) && $startFrom === null
147 ||
isset( $options['dir'] ),
149 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
152 !isset( $options['startFrom'] ),
153 '$options[\'startFrom\']',
154 'must not be provided, use $startFrom instead'
157 !isset( $startFrom ) ||
( is_array( $startFrom ) && count( $startFrom ) === 2 ),
159 'must be a two-element array'
161 if ( array_key_exists( 'watchlistOwner', $options ) ) {
162 Assert
::parameterType(
164 $options['watchlistOwner'],
165 '$options[\'watchlistOwner\']'
168 isset( $options['watchlistOwnerToken'] ),
169 '$options[\'watchlistOwnerToken\']',
170 'must be provided when providing watchlistOwner option'
174 $tables = [ 'recentchanges', 'watchlist' ];
175 if ( !$options['allRevisions'] ) {
179 $db = $this->getConnection();
181 $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
182 $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
183 $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
184 $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
186 if ( $startFrom !== null ) {
187 $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
190 foreach ( $this->getExtensions() as $extension ) {
191 $extension->modifyWatchedItemsWithRCInfoQuery(
192 $user, $options, $db,
210 $limit = isset( $dbOptions['LIMIT'] ) ?
$dbOptions['LIMIT'] : INF
;
213 foreach ( $res as $row ) {
214 if ( --$limit <= 0 ) {
215 $startFrom = [ $row->rc_timestamp
, $row->rc_id
];
222 new TitleValue( (int)$row->rc_namespace
, $row->rc_title
),
223 $row->wl_notificationtimestamp
225 $this->getRecentChangeFieldsFromRow( $row )
229 foreach ( $this->getExtensions() as $extension ) {
230 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
237 * For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser
240 * @param array $options Allowed keys:
241 * 'sort' => string optional sorting by namespace ID and title
242 * one of the self::SORT_* constants
243 * 'namespaceIds' => int[] optional namespace IDs to filter by (defaults to all namespaces)
244 * 'limit' => int maximum number of items to return
245 * 'filter' => string optional filter, one of the self::FILTER_* contants
246 * 'from' => LinkTarget requires 'sort' key, only return items starting from
247 * those related to the link target
248 * 'until' => LinkTarget requires 'sort' key, only return items until
249 * those related to the link target
250 * 'startFrom' => LinkTarget requires 'sort' key, only return items starting from
251 * those related to the link target, allows to skip some link targets
252 * specified using the form option
253 * @return WatchedItem[]
255 public function getWatchedItemsForUser( User
$user, array $options = [] ) {
256 if ( $user->isAnon() ) {
257 // TODO: should this just return an empty array or rather complain loud at this point
258 // as e.g. ApiBase::getWatchlistUser does?
262 $options +
= [ 'namespaceIds' => [] ];
265 !isset( $options['sort'] ) ||
in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ),
266 '$options[\'sort\']',
267 'must be SORT_ASC or SORT_DESC'
270 !isset( $options['filter'] ) ||
in_array(
271 $options['filter'], [ self
::FILTER_CHANGED
, self
::FILTER_NOT_CHANGED
]
273 '$options[\'filter\']',
274 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
277 !isset( $options['from'] ) && !isset( $options['until'] ) && !isset( $options['startFrom'] )
278 ||
isset( $options['sort'] ),
279 '$options[\'sort\']',
280 'must be provided if any of "from", "until", "startFrom" options is provided'
283 $db = $this->getConnection();
285 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
286 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
290 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
297 foreach ( $res as $row ) {
298 // todo these could all be cached at some point?
299 $watchedItems[] = new WatchedItem(
301 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
302 $row->wl_notificationtimestamp
306 return $watchedItems;
309 private function getRecentChangeFieldsFromRow( stdClass
$row ) {
310 // This can be simplified to single array_filter call filtering by key value,
311 // once we stop supporting PHP 5.5
312 $allFields = get_object_vars( $row );
313 $rcKeys = array_filter(
314 array_keys( $allFields ),
316 return substr( $key, 0, 3 ) === 'rc_';
319 return array_intersect_key( $allFields, array_flip( $rcKeys ) );
322 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
330 'wl_notificationtimestamp'
338 if ( $options['usedInGenerator'] ) {
339 if ( $options['allRevisions'] ) {
340 $rcIdFields = [ 'rc_this_oldid' ];
342 $rcIdFields = [ 'rc_cur_id' ];
345 $fields = array_merge( $fields, $rcIdFields );
347 if ( in_array( self
::INCLUDE_FLAGS
, $options['includeFields'] ) ) {
348 $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
350 if ( in_array( self
::INCLUDE_USER
, $options['includeFields'] ) ) {
351 $fields[] = 'rc_user_text';
353 if ( in_array( self
::INCLUDE_USER_ID
, $options['includeFields'] ) ) {
354 $fields[] = 'rc_user';
356 if ( in_array( self
::INCLUDE_COMMENT
, $options['includeFields'] ) ) {
357 $fields[] = 'rc_comment';
359 if ( in_array( self
::INCLUDE_PATROL_INFO
, $options['includeFields'] ) ) {
360 $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
362 if ( in_array( self
::INCLUDE_SIZES
, $options['includeFields'] ) ) {
363 $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
365 if ( in_array( self
::INCLUDE_LOG_INFO
, $options['includeFields'] ) ) {
366 $fields = array_merge( $fields, [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ] );
372 private function getWatchedItemsWithRCInfoQueryConds(
377 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
378 $conds = [ 'wl_user' => $watchlistOwnerId ];
380 if ( !$options['allRevisions'] ) {
381 $conds[] = $db->makeList(
382 [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG
],
387 if ( $options['namespaceIds'] ) {
388 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
391 if ( array_key_exists( 'rcTypes', $options ) ) {
392 $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
395 $conds = array_merge(
397 $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
400 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
402 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
403 if ( $db->getType() === 'mysql' ) {
404 // This is an index optimization for mysql
405 $conds[] = "rc_timestamp > ''";
409 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
411 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
412 if ( $deletedPageLogCond ) {
413 $conds[] = $deletedPageLogCond;
419 private function getWatchlistOwnerId( User
$user, array $options ) {
420 if ( array_key_exists( 'watchlistOwner', $options ) ) {
421 /** @var User $watchlistOwner */
422 $watchlistOwner = $options['watchlistOwner'];
423 $ownersToken = $watchlistOwner->getOption( 'watchlisttoken' );
424 $token = $options['watchlistOwnerToken'];
425 if ( $ownersToken == '' ||
!hash_equals( $ownersToken, $token ) ) {
426 throw ApiUsageException
::newWithMessage( null, 'apierror-bad-watchlist-token', 'bad_wltoken' );
428 return $watchlistOwner->getId();
430 return $user->getId();
433 private function getWatchedItemsWithRCInfoQueryFilterConds( User
$user, array $options ) {
436 if ( in_array( self
::FILTER_MINOR
, $options['filters'] ) ) {
437 $conds[] = 'rc_minor != 0';
438 } elseif ( in_array( self
::FILTER_NOT_MINOR
, $options['filters'] ) ) {
439 $conds[] = 'rc_minor = 0';
442 if ( in_array( self
::FILTER_BOT
, $options['filters'] ) ) {
443 $conds[] = 'rc_bot != 0';
444 } elseif ( in_array( self
::FILTER_NOT_BOT
, $options['filters'] ) ) {
445 $conds[] = 'rc_bot = 0';
448 if ( in_array( self
::FILTER_ANON
, $options['filters'] ) ) {
449 $conds[] = 'rc_user = 0';
450 } elseif ( in_array( self
::FILTER_NOT_ANON
, $options['filters'] ) ) {
451 $conds[] = 'rc_user != 0';
454 if ( $user->useRCPatrol() ||
$user->useNPPatrol() ) {
455 // TODO: not sure if this should simply ignore patrolled filters if user does not have the patrol
456 // right, or maybe rather fail loud at this point, same as e.g. ApiQueryWatchlist does?
457 if ( in_array( self
::FILTER_PATROLLED
, $options['filters'] ) ) {
458 $conds[] = 'rc_patrolled != 0';
459 } elseif ( in_array( self
::FILTER_NOT_PATROLLED
, $options['filters'] ) ) {
460 $conds[] = 'rc_patrolled = 0';
464 if ( in_array( self
::FILTER_UNREAD
, $options['filters'] ) ) {
465 $conds[] = 'rc_timestamp >= wl_notificationtimestamp';
466 } elseif ( in_array( self
::FILTER_NOT_UNREAD
, $options['filters'] ) ) {
467 // TODO: should this be changed to use Database::makeList?
468 $conds[] = 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
474 private function getStartEndConds( IDatabase
$db, array $options ) {
475 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
481 if ( isset( $options['start'] ) ) {
482 $after = $options['dir'] === self
::DIR_OLDER ?
'<=' : '>=';
483 $conds[] = 'rc_timestamp ' . $after . ' ' .
484 $db->addQuotes( $db->timestamp( $options['start'] ) );
486 if ( isset( $options['end'] ) ) {
487 $before = $options['dir'] === self
::DIR_OLDER ?
'>=' : '<=';
488 $conds[] = 'rc_timestamp ' . $before . ' ' .
489 $db->addQuotes( $db->timestamp( $options['end'] ) );
495 private function getUserRelatedConds( IDatabase
$db, User
$user, array $options ) {
496 if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
502 if ( array_key_exists( 'onlyByUser', $options ) ) {
503 $conds['rc_user_text'] = $options['onlyByUser'];
504 } elseif ( array_key_exists( 'notByUser', $options ) ) {
505 $conds[] = 'rc_user_text != ' . $db->addQuotes( $options['notByUser'] );
508 // Avoid brute force searches (bug 17342)
510 if ( !$user->isAllowed( 'deletedhistory' ) ) {
511 $bitmask = Revision
::DELETED_USER
;
512 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
513 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
516 $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
522 private function getExtraDeletedPageLogEntryRelatedCond( IDatabase
$db, User
$user ) {
523 // LogPage::DELETED_ACTION hides the affected page, too. So hide those
524 // entirely from the watchlist, or someone could guess the title.
526 if ( !$user->isAllowed( 'deletedhistory' ) ) {
527 $bitmask = LogPage
::DELETED_ACTION
;
528 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
529 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
532 return $db->makeList( [
533 'rc_type != ' . RC_LOG
,
534 $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
540 private function getStartFromConds( IDatabase
$db, array $options, array $startFrom ) {
541 $op = $options['dir'] === self
::DIR_OLDER ?
'<' : '>';
542 list( $rcTimestamp, $rcId ) = $startFrom;
543 $rcTimestamp = $db->addQuotes( $db->timestamp( $rcTimestamp ) );
545 return $db->makeList(
547 "rc_timestamp $op $rcTimestamp",
550 "rc_timestamp = $rcTimestamp",
560 private function getWatchedItemsForUserQueryConds( IDatabase
$db, User
$user, array $options ) {
561 $conds = [ 'wl_user' => $user->getId() ];
562 if ( $options['namespaceIds'] ) {
563 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
565 if ( isset( $options['filter'] ) ) {
566 $filter = $options['filter'];
567 if ( $filter === self
::FILTER_CHANGED
) {
568 $conds[] = 'wl_notificationtimestamp IS NOT NULL';
570 $conds[] = 'wl_notificationtimestamp IS NULL';
574 if ( isset( $options['from'] ) ) {
575 $op = $options['sort'] === self
::SORT_ASC ?
'>' : '<';
576 $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
578 if ( isset( $options['until'] ) ) {
579 $op = $options['sort'] === self
::SORT_ASC ?
'<' : '>';
580 $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
582 if ( isset( $options['startFrom'] ) ) {
583 $op = $options['sort'] === self
::SORT_ASC ?
'>' : '<';
584 $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
591 * Creates a query condition part for getting only items before or after the given link target
592 * (while ordering using $sort mode)
594 * @param IDatabase $db
595 * @param LinkTarget $target
596 * @param string $op comparison operator to use in the conditions
599 private function getFromUntilTargetConds( IDatabase
$db, LinkTarget
$target, $op ) {
600 return $db->makeList(
602 "wl_namespace $op " . $target->getNamespace(),
605 'wl_namespace = ' . $target->getNamespace(),
606 "wl_title $op= " . $db->addQuotes( $target->getDBkey() )
615 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
618 if ( array_key_exists( 'dir', $options ) ) {
619 $sort = $options['dir'] === self
::DIR_OLDER ?
' DESC' : '';
620 $dbOptions['ORDER BY'] = [ 'rc_timestamp' . $sort, 'rc_id' . $sort ];
623 if ( array_key_exists( 'limit', $options ) ) {
624 $dbOptions['LIMIT'] = (int)$options['limit'] +
1;
630 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
632 if ( array_key_exists( 'sort', $options ) ) {
633 $dbOptions['ORDER BY'] = [
634 "wl_namespace {$options['sort']}",
635 "wl_title {$options['sort']}"
637 if ( count( $options['namespaceIds'] ) === 1 ) {
638 $dbOptions['ORDER BY'] = "wl_title {$options['sort']}";
641 if ( array_key_exists( 'limit', $options ) ) {
642 $dbOptions['LIMIT'] = (int)$options['limit'];
647 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
649 'watchlist' => [ 'INNER JOIN',
651 'wl_namespace=rc_namespace',
656 if ( !$options['allRevisions'] ) {
657 $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];