Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / WatchedItemQueryService.php
1 <?php
2
3 use MediaWiki\Linker\LinkTarget;
4 use Wikimedia\Assert\Assert;
5 use Wikimedia\Rdbms\LoadBalancer;
6
7 /**
8 * Class performing complex database queries related to WatchedItems.
9 *
10 * @since 1.28
11 *
12 * @file
13 * @ingroup Watchlist
14 *
15 * @license GNU GPL v2+
16 */
17 class WatchedItemQueryService {
18
19 const DIR_OLDER = 'older';
20 const DIR_NEWER = 'newer';
21
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';
29
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';
45
46 const SORT_ASC = 'ASC';
47 const SORT_DESC = 'DESC';
48
49 /**
50 * @var LoadBalancer
51 */
52 private $loadBalancer;
53
54 /** @var WatchedItemQueryServiceExtension[]|null */
55 private $extensions = null;
56
57 public function __construct( LoadBalancer $loadBalancer ) {
58 $this->loadBalancer = $loadBalancer;
59 }
60
61 /**
62 * @return WatchedItemQueryServiceExtension[]
63 */
64 private function getExtensions() {
65 if ( $this->extensions === null ) {
66 $this->extensions = [];
67 Hooks::run( 'WatchedItemQueryServiceExtensions', [ &$this->extensions, $this ] );
68 }
69 return $this->extensions;
70 }
71
72 /**
73 * @return IDatabase
74 * @throws MWException
75 */
76 private function getConnection() {
77 return $this->loadBalancer->getConnectionRef( DB_REPLICA, [ 'watchlist' ] );
78 }
79
80 /**
81 * @param User $user
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')
111 * if false (default)
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:
115 * - 'rc_id',
116 * - 'rc_namespace',
117 * - 'rc_title',
118 * - 'rc_timestamp',
119 * - 'rc_type',
120 * - 'rc_deleted',
121 * Additional keys could be added by specifying the 'includeFields' option
122 */
123 public function getWatchedItemsWithRecentChangeInfo(
124 User $user, array $options = [], &$startFrom = null
125 ) {
126 $options += [
127 'includeFields' => [],
128 'namespaceIds' => [],
129 'filters' => [],
130 'allRevisions' => false,
131 'usedInGenerator' => false
132 ];
133
134 Assert::parameter(
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'
139 );
140 Assert::parameter(
141 !isset( $options['dir'] ) || in_array( $options['dir'], [ self::DIR_OLDER, self::DIR_NEWER ] ),
142 '$options[\'dir\']',
143 'must be DIR_OLDER or DIR_NEWER'
144 );
145 Assert::parameter(
146 !isset( $options['start'] ) && !isset( $options['end'] ) && $startFrom === null
147 || isset( $options['dir'] ),
148 '$options[\'dir\']',
149 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
150 );
151 Assert::parameter(
152 !isset( $options['startFrom'] ),
153 '$options[\'startFrom\']',
154 'must not be provided, use $startFrom instead'
155 );
156 Assert::parameter(
157 !isset( $startFrom ) || ( is_array( $startFrom ) && count( $startFrom ) === 2 ),
158 '$startFrom',
159 'must be a two-element array'
160 );
161 if ( array_key_exists( 'watchlistOwner', $options ) ) {
162 Assert::parameterType(
163 User::class,
164 $options['watchlistOwner'],
165 '$options[\'watchlistOwner\']'
166 );
167 Assert::parameter(
168 isset( $options['watchlistOwnerToken'] ),
169 '$options[\'watchlistOwnerToken\']',
170 'must be provided when providing watchlistOwner option'
171 );
172 }
173
174 $tables = [ 'recentchanges', 'watchlist' ];
175 if ( !$options['allRevisions'] ) {
176 $tables[] = 'page';
177 }
178
179 $db = $this->getConnection();
180
181 $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
182 $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
183 $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
184 $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
185
186 if ( $startFrom !== null ) {
187 $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
188 }
189
190 foreach ( $this->getExtensions() as $extension ) {
191 $extension->modifyWatchedItemsWithRCInfoQuery(
192 $user, $options, $db,
193 $tables,
194 $fields,
195 $conds,
196 $dbOptions,
197 $joinConds
198 );
199 }
200
201 $res = $db->select(
202 $tables,
203 $fields,
204 $conds,
205 __METHOD__,
206 $dbOptions,
207 $joinConds
208 );
209
210 $limit = isset( $dbOptions['LIMIT'] ) ? $dbOptions['LIMIT'] : INF;
211 $items = [];
212 $startFrom = null;
213 foreach ( $res as $row ) {
214 if ( --$limit <= 0 ) {
215 $startFrom = [ $row->rc_timestamp, $row->rc_id ];
216 break;
217 }
218
219 $items[] = [
220 new WatchedItem(
221 $user,
222 new TitleValue( (int)$row->rc_namespace, $row->rc_title ),
223 $row->wl_notificationtimestamp
224 ),
225 $this->getRecentChangeFieldsFromRow( $row )
226 ];
227 }
228
229 foreach ( $this->getExtensions() as $extension ) {
230 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
231 }
232
233 return $items;
234 }
235
236 /**
237 * For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser
238 *
239 * @param User $user
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[]
254 */
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?
259 return [];
260 }
261
262 $options += [ 'namespaceIds' => [] ];
263
264 Assert::parameter(
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'
268 );
269 Assert::parameter(
270 !isset( $options['filter'] ) || in_array(
271 $options['filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
272 ),
273 '$options[\'filter\']',
274 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
275 );
276 Assert::parameter(
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'
281 );
282
283 $db = $this->getConnection();
284
285 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
286 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
287
288 $res = $db->select(
289 'watchlist',
290 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
291 $conds,
292 __METHOD__,
293 $dbOptions
294 );
295
296 $watchedItems = [];
297 foreach ( $res as $row ) {
298 // todo these could all be cached at some point?
299 $watchedItems[] = new WatchedItem(
300 $user,
301 new TitleValue( (int)$row->wl_namespace, $row->wl_title ),
302 $row->wl_notificationtimestamp
303 );
304 }
305
306 return $watchedItems;
307 }
308
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 ),
315 function( $key ) {
316 return substr( $key, 0, 3 ) === 'rc_';
317 }
318 );
319 return array_intersect_key( $allFields, array_flip( $rcKeys ) );
320 }
321
322 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
323 $fields = [
324 'rc_id',
325 'rc_namespace',
326 'rc_title',
327 'rc_timestamp',
328 'rc_type',
329 'rc_deleted',
330 'wl_notificationtimestamp'
331 ];
332
333 $rcIdFields = [
334 'rc_cur_id',
335 'rc_this_oldid',
336 'rc_last_oldid',
337 ];
338 if ( $options['usedInGenerator'] ) {
339 if ( $options['allRevisions'] ) {
340 $rcIdFields = [ 'rc_this_oldid' ];
341 } else {
342 $rcIdFields = [ 'rc_cur_id' ];
343 }
344 }
345 $fields = array_merge( $fields, $rcIdFields );
346
347 if ( in_array( self::INCLUDE_FLAGS, $options['includeFields'] ) ) {
348 $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
349 }
350 if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ) {
351 $fields[] = 'rc_user_text';
352 }
353 if ( in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ) {
354 $fields[] = 'rc_user';
355 }
356 if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
357 $fields[] = 'rc_comment';
358 }
359 if ( in_array( self::INCLUDE_PATROL_INFO, $options['includeFields'] ) ) {
360 $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
361 }
362 if ( in_array( self::INCLUDE_SIZES, $options['includeFields'] ) ) {
363 $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
364 }
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' ] );
367 }
368
369 return $fields;
370 }
371
372 private function getWatchedItemsWithRCInfoQueryConds(
373 IDatabase $db,
374 User $user,
375 array $options
376 ) {
377 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
378 $conds = [ 'wl_user' => $watchlistOwnerId ];
379
380 if ( !$options['allRevisions'] ) {
381 $conds[] = $db->makeList(
382 [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG ],
383 LIST_OR
384 );
385 }
386
387 if ( $options['namespaceIds'] ) {
388 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
389 }
390
391 if ( array_key_exists( 'rcTypes', $options ) ) {
392 $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
393 }
394
395 $conds = array_merge(
396 $conds,
397 $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
398 );
399
400 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
401
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 > ' . $db->addQuotes( '' );
406 }
407 }
408
409 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
410
411 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
412 if ( $deletedPageLogCond ) {
413 $conds[] = $deletedPageLogCond;
414 }
415
416 return $conds;
417 }
418
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' );
427 }
428 return $watchlistOwner->getId();
429 }
430 return $user->getId();
431 }
432
433 private function getWatchedItemsWithRCInfoQueryFilterConds( User $user, array $options ) {
434 $conds = [];
435
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';
440 }
441
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';
446 }
447
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';
452 }
453
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';
461 }
462 }
463
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';
469 }
470
471 return $conds;
472 }
473
474 private function getStartEndConds( IDatabase $db, array $options ) {
475 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
476 return [];
477 }
478
479 $conds = [];
480
481 if ( isset( $options['start'] ) ) {
482 $after = $options['dir'] === self::DIR_OLDER ? '<=' : '>=';
483 $conds[] = 'rc_timestamp ' . $after . ' ' .
484 $db->addQuotes( $db->timestamp( $options['start'] ) );
485 }
486 if ( isset( $options['end'] ) ) {
487 $before = $options['dir'] === self::DIR_OLDER ? '>=' : '<=';
488 $conds[] = 'rc_timestamp ' . $before . ' ' .
489 $db->addQuotes( $db->timestamp( $options['end'] ) );
490 }
491
492 return $conds;
493 }
494
495 private function getUserRelatedConds( IDatabase $db, User $user, array $options ) {
496 if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
497 return [];
498 }
499
500 $conds = [];
501
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'] );
506 }
507
508 // Avoid brute force searches (T19342)
509 $bitmask = 0;
510 if ( !$user->isAllowed( 'deletedhistory' ) ) {
511 $bitmask = Revision::DELETED_USER;
512 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
513 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
514 }
515 if ( $bitmask ) {
516 $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
517 }
518
519 return $conds;
520 }
521
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.
525 $bitmask = 0;
526 if ( !$user->isAllowed( 'deletedhistory' ) ) {
527 $bitmask = LogPage::DELETED_ACTION;
528 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
529 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
530 }
531 if ( $bitmask ) {
532 return $db->makeList( [
533 'rc_type != ' . RC_LOG,
534 $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
535 ], LIST_OR );
536 }
537 return '';
538 }
539
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 ) );
544 $rcId = (int)$rcId;
545 return $db->makeList(
546 [
547 "rc_timestamp $op $rcTimestamp",
548 $db->makeList(
549 [
550 "rc_timestamp = $rcTimestamp",
551 "rc_id $op= $rcId"
552 ],
553 LIST_AND
554 )
555 ],
556 LIST_OR
557 );
558 }
559
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'] );
564 }
565 if ( isset( $options['filter'] ) ) {
566 $filter = $options['filter'];
567 if ( $filter === self::FILTER_CHANGED ) {
568 $conds[] = 'wl_notificationtimestamp IS NOT NULL';
569 } else {
570 $conds[] = 'wl_notificationtimestamp IS NULL';
571 }
572 }
573
574 if ( isset( $options['from'] ) ) {
575 $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
576 $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
577 }
578 if ( isset( $options['until'] ) ) {
579 $op = $options['sort'] === self::SORT_ASC ? '<' : '>';
580 $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
581 }
582 if ( isset( $options['startFrom'] ) ) {
583 $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
584 $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
585 }
586
587 return $conds;
588 }
589
590 /**
591 * Creates a query condition part for getting only items before or after the given link target
592 * (while ordering using $sort mode)
593 *
594 * @param IDatabase $db
595 * @param LinkTarget $target
596 * @param string $op comparison operator to use in the conditions
597 * @return string
598 */
599 private function getFromUntilTargetConds( IDatabase $db, LinkTarget $target, $op ) {
600 return $db->makeList(
601 [
602 "wl_namespace $op " . $target->getNamespace(),
603 $db->makeList(
604 [
605 'wl_namespace = ' . $target->getNamespace(),
606 "wl_title $op= " . $db->addQuotes( $target->getDBkey() )
607 ],
608 LIST_AND
609 )
610 ],
611 LIST_OR
612 );
613 }
614
615 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
616 $dbOptions = [];
617
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 ];
621 }
622
623 if ( array_key_exists( 'limit', $options ) ) {
624 $dbOptions['LIMIT'] = (int)$options['limit'] + 1;
625 }
626
627 return $dbOptions;
628 }
629
630 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
631 $dbOptions = [];
632 if ( array_key_exists( 'sort', $options ) ) {
633 $dbOptions['ORDER BY'] = [
634 "wl_namespace {$options['sort']}",
635 "wl_title {$options['sort']}"
636 ];
637 if ( count( $options['namespaceIds'] ) === 1 ) {
638 $dbOptions['ORDER BY'] = "wl_title {$options['sort']}";
639 }
640 }
641 if ( array_key_exists( 'limit', $options ) ) {
642 $dbOptions['LIMIT'] = (int)$options['limit'];
643 }
644 return $dbOptions;
645 }
646
647 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
648 $joinConds = [
649 'watchlist' => [ 'INNER JOIN',
650 [
651 'wl_namespace=rc_namespace',
652 'wl_title=rc_title'
653 ]
654 ]
655 ];
656 if ( !$options['allRevisions'] ) {
657 $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
658 }
659 return $joinConds;
660 }
661
662 }