3 use MediaWiki\Linker\LinkTarget
;
4 use Wikimedia\Assert\Assert
;
7 * Class performing complex database queries related to WatchedItems.
14 * @license GNU GPL v2+
16 class WatchedItemQueryService
{
18 const DIR_OLDER
= 'older';
19 const DIR_NEWER
= 'newer';
21 const INCLUDE_FLAGS
= 'flags';
22 const INCLUDE_USER
= 'user';
23 const INCLUDE_USER_ID
= 'userid';
24 const INCLUDE_COMMENT
= 'comment';
25 const INCLUDE_PATROL_INFO
= 'patrol';
26 const INCLUDE_SIZES
= 'sizes';
27 const INCLUDE_LOG_INFO
= 'loginfo';
29 // FILTER_* constants are part of public API (are used in ApiQueryWatchlist and
30 // ApiQueryWatchlistRaw classes) and should not be changed.
31 // Changing values of those constants will result in a breaking change in the API
32 const FILTER_MINOR
= 'minor';
33 const FILTER_NOT_MINOR
= '!minor';
34 const FILTER_BOT
= 'bot';
35 const FILTER_NOT_BOT
= '!bot';
36 const FILTER_ANON
= 'anon';
37 const FILTER_NOT_ANON
= '!anon';
38 const FILTER_PATROLLED
= 'patrolled';
39 const FILTER_NOT_PATROLLED
= '!patrolled';
40 const FILTER_UNREAD
= 'unread';
41 const FILTER_NOT_UNREAD
= '!unread';
42 const FILTER_CHANGED
= 'changed';
43 const FILTER_NOT_CHANGED
= '!changed';
45 const SORT_ASC
= 'ASC';
46 const SORT_DESC
= 'DESC';
51 private $loadBalancer;
53 public function __construct( LoadBalancer
$loadBalancer ) {
54 $this->loadBalancer
= $loadBalancer;
58 * @return DatabaseBase
61 private function getConnection() {
62 return $this->loadBalancer
->getConnection( DB_SLAVE
, [ 'watchlist' ] );
66 * @param DatabaseBase $connection
69 private function reuseConnection( DatabaseBase
$connection ) {
70 $this->loadBalancer
->reuseConnection( $connection );
75 * @param array $options Allowed keys:
76 * 'includeFields' => string[] RecentChange fields to be included in the result,
77 * self::INCLUDE_* constants should be used
78 * 'filters' => string[] optional filters to narrow down resulted items
79 * 'namespaceIds' => int[] optional namespace IDs to filter by
80 * (defaults to all namespaces)
81 * 'allRevisions' => bool return multiple revisions of the same page if true,
82 * only the most recent if false (default)
83 * 'rcTypes' => int[] which types of RecentChanges to include
84 * (defaults to all types), allowed values: RC_EDIT, RC_NEW,
85 * RC_LOG, RC_EXTERNAL, RC_CATEGORIZE
86 * 'onlyByUser' => string only list changes by a specified user
87 * 'notByUser' => string do not incluide changes by a specified user
88 * 'dir' => string in which direction to enumerate, accepted values:
89 * - DIR_OLDER list newest first
90 * - DIR_NEWER list oldest first
91 * 'start' => string (format accepted by wfTimestamp) requires 'dir' option,
92 * timestamp to start enumerating from
93 * 'end' => string (format accepted by wfTimestamp) requires 'dir' option,
94 * timestamp to end enumerating
95 * 'startFrom' => [ string $rcTimestamp, int $rcId ] requires 'dir' option,
96 * return items starting from the RecentChange specified by this,
97 * $rcTimestamp should be in the format accepted by wfTimestamp
98 * 'watchlistOwner' => User user whose watchlist items should be listed if different
99 * than the one specified with $user param,
100 * requires 'watchlistOwnerToken' option
101 * 'watchlistOwnerToken' => string a watchlist token used to access another user's
102 * watchlist, used with 'watchlistOwnerToken' option
103 * 'limit' => int maximum numbers of items to return
104 * 'usedInGenerator' => bool include only RecentChange id field required by the
105 * generator ('rc_cur_id' or 'rc_this_oldid') if true, or all
106 * id fields ('rc_cur_id', 'rc_this_oldid', 'rc_last_oldid')
108 * @return array of pairs ( WatchedItem $watchedItem, string[] $recentChangeInfo ),
109 * where $recentChangeInfo contains the following keys:
116 * Additional keys could be added by specifying the 'includeFields' option
118 public function getWatchedItemsWithRecentChangeInfo( User
$user, array $options = [] ) {
120 'includeFields' => [],
121 'namespaceIds' => [],
123 'allRevisions' => false,
124 'usedInGenerator' => false
128 !isset( $options['rcTypes'] )
129 ||
!array_diff( $options['rcTypes'], [ RC_EDIT
, RC_NEW
, RC_LOG
, RC_EXTERNAL
, RC_CATEGORIZE
] ),
130 '$options[\'rcTypes\']',
131 'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
134 !isset( $options['dir'] ) ||
in_array( $options['dir'], [ self
::DIR_OLDER
, self
::DIR_NEWER
] ),
136 'must be DIR_OLDER or DIR_NEWER'
139 !isset( $options['start'] ) && !isset( $options['end'] ) && !isset( $options['startFrom'] )
140 ||
isset( $options['dir'] ),
142 'must be provided when providing any of options: start, end, startFrom'
145 !isset( $options['startFrom'] )
146 ||
( is_array( $options['startFrom'] ) && count( $options['startFrom'] ) === 2 ),
147 '$options[\'startFrom\']',
148 'must be a two-element array'
150 if ( array_key_exists( 'watchlistOwner', $options ) ) {
151 Assert
::parameterType(
153 $options['watchlistOwner'],
154 '$options[\'watchlistOwner\']'
157 isset( $options['watchlistOwnerToken'] ),
158 '$options[\'watchlistOwnerToken\']',
159 'must be provided when providing watchlistOwner option'
163 $tables = [ 'recentchanges', 'watchlist' ];
164 if ( !$options['allRevisions'] ) {
168 $db = $this->getConnection();
170 $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
171 $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
172 $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
173 $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
184 $this->reuseConnection( $db );
187 foreach ( $res as $row ) {
191 new TitleValue( (int)$row->rc_namespace
, $row->rc_title
),
192 $row->wl_notificationtimestamp
194 $this->getRecentChangeFieldsFromRow( $row )
202 * For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser
205 * @param array $options Allowed keys:
206 * 'sort' => string optional sorting by namespace ID and title
207 * one of the self::SORT_* constants
208 * 'namespaceIds' => int[] optional namespace IDs to filter by (defaults to all namespaces)
209 * 'limit' => int maximum number of items to return
210 * 'filter' => string optional filter, one of the self::FILTER_* contants
211 * 'from' => LinkTarget requires 'sort' key, only return items starting from
212 * those related to the link target
213 * 'until' => LinkTarget requires 'sort' key, only return items until
214 * those related to the link target
215 * 'startFrom' => LinkTarget requires 'sort' key, only return items starting from
216 * those related to the link target, allows to skip some link targets
217 * specified using the form option
218 * @return WatchedItem[]
220 public function getWatchedItemsForUser( User
$user, array $options = [] ) {
221 if ( $user->isAnon() ) {
222 // TODO: should this just return an empty array or rather complain loud at this point
223 // as e.g. ApiBase::getWatchlistUser does?
227 $options +
= [ 'namespaceIds' => [] ];
230 !isset( $options['sort'] ) ||
in_array( $options['sort'], [ self
::SORT_ASC
, self
::SORT_DESC
] ),
231 '$options[\'sort\']',
232 'must be SORT_ASC or SORT_DESC'
235 !isset( $options['filter'] ) ||
in_array(
236 $options['filter'], [ self
::FILTER_CHANGED
, self
::FILTER_NOT_CHANGED
]
238 '$options[\'filter\']',
239 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
242 !isset( $options['from'] ) && !isset( $options['until'] ) && !isset( $options['startFrom'] )
243 ||
isset( $options['sort'] ),
244 '$options[\'sort\']',
245 'must be provided if any of "from", "until", "startFrom" options is provided'
248 $db = $this->getConnection();
250 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
251 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
255 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
261 $this->reuseConnection( $db );
264 foreach ( $res as $row ) {
265 // todo these could all be cached at some point?
266 $watchedItems[] = new WatchedItem(
268 new TitleValue( (int)$row->wl_namespace
, $row->wl_title
),
269 $row->wl_notificationtimestamp
273 return $watchedItems;
276 private function getRecentChangeFieldsFromRow( stdClass
$row ) {
277 // This can be simplified to single array_filter call filtering by key value,
278 // once we stop supporting PHP 5.5
279 $allFields = get_object_vars( $row );
280 $rcKeys = array_filter(
281 array_keys( $allFields ),
283 return substr( $key, 0, 3 ) === 'rc_';
286 return array_intersect_key( $allFields, array_flip( $rcKeys ) );
289 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
297 'wl_notificationtimestamp'
305 if ( $options['usedInGenerator'] ) {
306 if ( $options['allRevisions'] ) {
307 $rcIdFields = [ 'rc_this_oldid' ];
309 $rcIdFields = [ 'rc_cur_id' ];
312 $fields = array_merge( $fields, $rcIdFields );
314 if ( in_array( self
::INCLUDE_FLAGS
, $options['includeFields'] ) ) {
315 $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
317 if ( in_array( self
::INCLUDE_USER
, $options['includeFields'] ) ) {
318 $fields[] = 'rc_user_text';
320 if ( in_array( self
::INCLUDE_USER_ID
, $options['includeFields'] ) ) {
321 $fields[] = 'rc_user';
323 if ( in_array( self
::INCLUDE_COMMENT
, $options['includeFields'] ) ) {
324 $fields[] = 'rc_comment';
326 if ( in_array( self
::INCLUDE_PATROL_INFO
, $options['includeFields'] ) ) {
327 $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
329 if ( in_array( self
::INCLUDE_SIZES
, $options['includeFields'] ) ) {
330 $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
332 if ( in_array( self
::INCLUDE_LOG_INFO
, $options['includeFields'] ) ) {
333 $fields = array_merge( $fields, [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ] );
339 private function getWatchedItemsWithRCInfoQueryConds(
344 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
345 $conds = [ 'wl_user' => $watchlistOwnerId ];
347 if ( !$options['allRevisions'] ) {
348 $conds[] = $db->makeList(
349 [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG
],
354 if ( $options['namespaceIds'] ) {
355 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
358 if ( array_key_exists( 'rcTypes', $options ) ) {
359 $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
362 $conds = array_merge(
364 $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
367 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
369 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
370 if ( $db->getType() === 'mysql' ) {
371 // This is an index optimization for mysql
372 $conds[] = "rc_timestamp > ''";
376 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
378 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
379 if ( $deletedPageLogCond ) {
380 $conds[] = $deletedPageLogCond;
383 if ( array_key_exists( 'startFrom', $options ) ) {
384 $conds[] = $this->getStartFromConds( $db, $options );
390 private function getWatchlistOwnerId( User
$user, array $options ) {
391 if ( array_key_exists( 'watchlistOwner', $options ) ) {
392 /** @var User $watchlistOwner */
393 $watchlistOwner = $options['watchlistOwner'];
394 $ownersToken = $watchlistOwner->getOption( 'watchlisttoken' );
395 $token = $options['watchlistOwnerToken'];
396 if ( $ownersToken == '' ||
!hash_equals( $ownersToken, $token ) ) {
397 throw new UsageException(
398 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences',
402 return $watchlistOwner->getId();
404 return $user->getId();
407 private function getWatchedItemsWithRCInfoQueryFilterConds( User
$user, array $options ) {
410 if ( in_array( self
::FILTER_MINOR
, $options['filters'] ) ) {
411 $conds[] = 'rc_minor != 0';
412 } elseif ( in_array( self
::FILTER_NOT_MINOR
, $options['filters'] ) ) {
413 $conds[] = 'rc_minor = 0';
416 if ( in_array( self
::FILTER_BOT
, $options['filters'] ) ) {
417 $conds[] = 'rc_bot != 0';
418 } elseif ( in_array( self
::FILTER_NOT_BOT
, $options['filters'] ) ) {
419 $conds[] = 'rc_bot = 0';
422 if ( in_array( self
::FILTER_ANON
, $options['filters'] ) ) {
423 $conds[] = 'rc_user = 0';
424 } elseif ( in_array( self
::FILTER_NOT_ANON
, $options['filters'] ) ) {
425 $conds[] = 'rc_user != 0';
428 if ( $user->useRCPatrol() ||
$user->useNPPatrol() ) {
429 // TODO: not sure if this should simply ignore patrolled filters if user does not have the patrol
430 // right, or maybe rather fail loud at this point, same as e.g. ApiQueryWatchlist does?
431 if ( in_array( self
::FILTER_PATROLLED
, $options['filters'] ) ) {
432 $conds[] = 'rc_patrolled != 0';
433 } elseif ( in_array( self
::FILTER_NOT_PATROLLED
, $options['filters'] ) ) {
434 $conds[] = 'rc_patrolled = 0';
438 if ( in_array( self
::FILTER_UNREAD
, $options['filters'] ) ) {
439 $conds[] = 'rc_timestamp >= wl_notificationtimestamp';
440 } elseif ( in_array( self
::FILTER_NOT_UNREAD
, $options['filters'] ) ) {
441 // TODO: should this be changed to use Database::makeList?
442 $conds[] = 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
448 private function getStartEndConds( DatabaseBase
$db, array $options ) {
449 if ( !isset( $options['start'] ) && ! isset( $options['end'] ) ) {
455 if ( isset( $options['start'] ) ) {
456 $after = $options['dir'] === self
::DIR_OLDER ?
'<=' : '>=';
457 $conds[] = 'rc_timestamp ' . $after . ' ' . $db->addQuotes( $options['start'] );
459 if ( isset( $options['end'] ) ) {
460 $before = $options['dir'] === self
::DIR_OLDER ?
'>=' : '<=';
461 $conds[] = 'rc_timestamp ' . $before . ' ' . $db->addQuotes( $options['end'] );
467 private function getUserRelatedConds( DatabaseBase
$db, User
$user, array $options ) {
468 if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
474 if ( array_key_exists( 'onlyByUser', $options ) ) {
475 $conds['rc_user_text'] = $options['onlyByUser'];
476 } elseif ( array_key_exists( 'notByUser', $options ) ) {
477 $conds[] = 'rc_user_text != ' . $db->addQuotes( $options['notByUser'] );
480 // Avoid brute force searches (bug 17342)
482 if ( !$user->isAllowed( 'deletedhistory' ) ) {
483 $bitmask = Revision
::DELETED_USER
;
484 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
485 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
488 $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
494 private function getExtraDeletedPageLogEntryRelatedCond( DatabaseBase
$db, User
$user ) {
495 // LogPage::DELETED_ACTION hides the affected page, too. So hide those
496 // entirely from the watchlist, or someone could guess the title.
498 if ( !$user->isAllowed( 'deletedhistory' ) ) {
499 $bitmask = LogPage
::DELETED_ACTION
;
500 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
501 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
504 return $db->makeList( [
505 'rc_type != ' . RC_LOG
,
506 $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
512 private function getStartFromConds( DatabaseBase
$db, array $options ) {
513 $op = $options['dir'] === self
::DIR_OLDER ?
'<' : '>';
514 list( $rcTimestamp, $rcId ) = $options['startFrom'];
515 $rcTimestamp = $db->addQuotes( $db->timestamp( $rcTimestamp ) );
517 return $db->makeList(
519 "rc_timestamp $op $rcTimestamp",
522 "rc_timestamp = $rcTimestamp",
532 private function getWatchedItemsForUserQueryConds( DatabaseBase
$db, User
$user, array $options ) {
533 $conds = [ 'wl_user' => $user->getId() ];
534 if ( $options['namespaceIds'] ) {
535 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
537 if ( isset( $options['filter'] ) ) {
538 $filter = $options['filter'];
539 if ( $filter === self
::FILTER_CHANGED
) {
540 $conds[] = 'wl_notificationtimestamp IS NOT NULL';
542 $conds[] = 'wl_notificationtimestamp IS NULL';
546 if ( isset( $options['from'] ) ) {
547 $op = $options['sort'] === self
::SORT_ASC ?
'>' : '<';
548 $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
550 if ( isset( $options['until'] ) ) {
551 $op = $options['sort'] === self
::SORT_ASC ?
'<' : '>';
552 $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
554 if ( isset( $options['startFrom'] ) ) {
555 $op = $options['sort'] === self
::SORT_ASC ?
'>' : '<';
556 $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
563 * Creates a query condition part for getting only items before or after the given link target
564 * (while ordering using $sort mode)
566 * @param DatabaseBase $db
567 * @param LinkTarget $target
568 * @param string $op comparison operator to use in the conditions
571 private function getFromUntilTargetConds( DatabaseBase
$db, LinkTarget
$target, $op ) {
572 return $db->makeList(
574 "wl_namespace $op " . $target->getNamespace(),
577 'wl_namespace = ' . $target->getNamespace(),
578 "wl_title $op= " . $db->addQuotes( $target->getDBkey() )
587 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
590 if ( array_key_exists( 'dir', $options ) ) {
591 $sort = $options['dir'] === self
::DIR_OLDER ?
' DESC' : '';
592 $dbOptions['ORDER BY'] = [ 'rc_timestamp' . $sort, 'rc_id' . $sort ];
595 if ( array_key_exists( 'limit', $options ) ) {
596 $dbOptions['LIMIT'] = (int)$options['limit'];
602 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
604 if ( array_key_exists( 'sort', $options ) ) {
605 $dbOptions['ORDER BY'] = [
606 "wl_namespace {$options['sort']}",
607 "wl_title {$options['sort']}"
609 if ( count( $options['namespaceIds'] ) === 1 ) {
610 $dbOptions['ORDER BY'] = "wl_title {$options['sort']}";
613 if ( array_key_exists( 'limit', $options ) ) {
614 $dbOptions['LIMIT'] = (int)$options['limit'];
619 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
621 'watchlist' => [ 'INNER JOIN',
623 'wl_namespace=rc_namespace',
628 if ( !$options['allRevisions'] ) {
629 $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];