Merge "Add collation for Abkhaz (ab)"
[lhc/web/wiklou.git] / includes / watcheditem / WatchedItemQueryService.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4 use MediaWiki\Linker\LinkTarget;
5 use Wikimedia\Assert\Assert;
6 use Wikimedia\Rdbms\LoadBalancer;
7
8 /**
9 * Class performing complex database queries related to WatchedItems.
10 *
11 * @since 1.28
12 *
13 * @file
14 * @ingroup Watchlist
15 *
16 * @license GNU GPL v2+
17 */
18 class WatchedItemQueryService {
19
20 const DIR_OLDER = 'older';
21 const DIR_NEWER = 'newer';
22
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';
31
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';
47
48 const SORT_ASC = 'ASC';
49 const SORT_DESC = 'DESC';
50
51 /**
52 * @var LoadBalancer
53 */
54 private $loadBalancer;
55
56 /** @var WatchedItemQueryServiceExtension[]|null */
57 private $extensions = null;
58
59 /**
60 * @var CommentStore|null */
61 private $commentStore = null;
62
63 public function __construct( LoadBalancer $loadBalancer ) {
64 $this->loadBalancer = $loadBalancer;
65 }
66
67 /**
68 * @return WatchedItemQueryServiceExtension[]
69 */
70 private function getExtensions() {
71 if ( $this->extensions === null ) {
72 $this->extensions = [];
73 Hooks::run( 'WatchedItemQueryServiceExtensions', [ &$this->extensions, $this ] );
74 }
75 return $this->extensions;
76 }
77
78 /**
79 * @return IDatabase
80 * @throws MWException
81 */
82 private function getConnection() {
83 return $this->loadBalancer->getConnectionRef( DB_REPLICA, [ 'watchlist' ] );
84 }
85
86 private function getCommentStore() {
87 if ( !$this->commentStore ) {
88 $this->commentStore = new CommentStore( 'rc_comment' );
89 }
90 return $this->commentStore;
91 }
92
93 /**
94 * @param User $user
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')
124 * if false (default)
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:
128 * - 'rc_id',
129 * - 'rc_namespace',
130 * - 'rc_title',
131 * - 'rc_timestamp',
132 * - 'rc_type',
133 * - 'rc_deleted',
134 * Additional keys could be added by specifying the 'includeFields' option
135 */
136 public function getWatchedItemsWithRecentChangeInfo(
137 User $user, array $options = [], &$startFrom = null
138 ) {
139 $options += [
140 'includeFields' => [],
141 'namespaceIds' => [],
142 'filters' => [],
143 'allRevisions' => false,
144 'usedInGenerator' => false
145 ];
146
147 Assert::parameter(
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'
152 );
153 Assert::parameter(
154 !isset( $options['dir'] ) || in_array( $options['dir'], [ self::DIR_OLDER, self::DIR_NEWER ] ),
155 '$options[\'dir\']',
156 'must be DIR_OLDER or DIR_NEWER'
157 );
158 Assert::parameter(
159 !isset( $options['start'] ) && !isset( $options['end'] ) && $startFrom === null
160 || isset( $options['dir'] ),
161 '$options[\'dir\']',
162 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
163 );
164 Assert::parameter(
165 !isset( $options['startFrom'] ),
166 '$options[\'startFrom\']',
167 'must not be provided, use $startFrom instead'
168 );
169 Assert::parameter(
170 !isset( $startFrom ) || ( is_array( $startFrom ) && count( $startFrom ) === 2 ),
171 '$startFrom',
172 'must be a two-element array'
173 );
174 if ( array_key_exists( 'watchlistOwner', $options ) ) {
175 Assert::parameterType(
176 User::class,
177 $options['watchlistOwner'],
178 '$options[\'watchlistOwner\']'
179 );
180 Assert::parameter(
181 isset( $options['watchlistOwnerToken'] ),
182 '$options[\'watchlistOwnerToken\']',
183 'must be provided when providing watchlistOwner option'
184 );
185 }
186
187 $db = $this->getConnection();
188
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 );
194
195 if ( $startFrom !== null ) {
196 $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
197 }
198
199 foreach ( $this->getExtensions() as $extension ) {
200 $extension->modifyWatchedItemsWithRCInfoQuery(
201 $user, $options, $db,
202 $tables,
203 $fields,
204 $conds,
205 $dbOptions,
206 $joinConds
207 );
208 }
209
210 $res = $db->select(
211 $tables,
212 $fields,
213 $conds,
214 __METHOD__,
215 $dbOptions,
216 $joinConds
217 );
218
219 $limit = isset( $dbOptions['LIMIT'] ) ? $dbOptions['LIMIT'] : INF;
220 $items = [];
221 $startFrom = null;
222 foreach ( $res as $row ) {
223 if ( --$limit <= 0 ) {
224 $startFrom = [ $row->rc_timestamp, $row->rc_id ];
225 break;
226 }
227
228 $items[] = [
229 new WatchedItem(
230 $user,
231 new TitleValue( (int)$row->rc_namespace, $row->rc_title ),
232 $row->wl_notificationtimestamp
233 ),
234 $this->getRecentChangeFieldsFromRow( $row )
235 ];
236 }
237
238 foreach ( $this->getExtensions() as $extension ) {
239 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
240 }
241
242 return $items;
243 }
244
245 /**
246 * For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser
247 *
248 * @param User $user
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[]
263 */
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?
268 return [];
269 }
270
271 $options += [ 'namespaceIds' => [] ];
272
273 Assert::parameter(
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'
277 );
278 Assert::parameter(
279 !isset( $options['filter'] ) || in_array(
280 $options['filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
281 ),
282 '$options[\'filter\']',
283 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
284 );
285 Assert::parameter(
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'
290 );
291
292 $db = $this->getConnection();
293
294 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
295 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
296
297 $res = $db->select(
298 'watchlist',
299 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
300 $conds,
301 __METHOD__,
302 $dbOptions
303 );
304
305 $watchedItems = [];
306 foreach ( $res as $row ) {
307 // todo these could all be cached at some point?
308 $watchedItems[] = new WatchedItem(
309 $user,
310 new TitleValue( (int)$row->wl_namespace, $row->wl_title ),
311 $row->wl_notificationtimestamp
312 );
313 }
314
315 return $watchedItems;
316 }
317
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 ),
324 function ( $key ) {
325 return substr( $key, 0, 3 ) === 'rc_';
326 }
327 );
328 return array_intersect_key( $allFields, array_flip( $rcKeys ) );
329 }
330
331 private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
332 $tables = [ 'recentchanges', 'watchlist' ];
333 if ( !$options['allRevisions'] ) {
334 $tables[] = 'page';
335 }
336 if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
337 $tables += $this->getCommentStore()->getJoin()['tables'];
338 }
339 if ( in_array( self::INCLUDE_TAGS, $options['includeFields'] ) ) {
340 $tables[] = 'tag_summary';
341 }
342 return $tables;
343 }
344
345 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
346 $fields = [
347 'rc_id',
348 'rc_namespace',
349 'rc_title',
350 'rc_timestamp',
351 'rc_type',
352 'rc_deleted',
353 'wl_notificationtimestamp'
354 ];
355
356 $rcIdFields = [
357 'rc_cur_id',
358 'rc_this_oldid',
359 'rc_last_oldid',
360 ];
361 if ( $options['usedInGenerator'] ) {
362 if ( $options['allRevisions'] ) {
363 $rcIdFields = [ 'rc_this_oldid' ];
364 } else {
365 $rcIdFields = [ 'rc_cur_id' ];
366 }
367 }
368 $fields = array_merge( $fields, $rcIdFields );
369
370 if ( in_array( self::INCLUDE_FLAGS, $options['includeFields'] ) ) {
371 $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
372 }
373 if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ) {
374 $fields[] = 'rc_user_text';
375 }
376 if ( in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ) {
377 $fields[] = 'rc_user';
378 }
379 if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
380 $fields += $this->getCommentStore()->getJoin()['fields'];
381 }
382 if ( in_array( self::INCLUDE_PATROL_INFO, $options['includeFields'] ) ) {
383 $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
384 }
385 if ( in_array( self::INCLUDE_SIZES, $options['includeFields'] ) ) {
386 $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
387 }
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' ] );
390 }
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';
394 }
395
396 return $fields;
397 }
398
399 private function getWatchedItemsWithRCInfoQueryConds(
400 IDatabase $db,
401 User $user,
402 array $options
403 ) {
404 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
405 $conds = [ 'wl_user' => $watchlistOwnerId ];
406
407 if ( !$options['allRevisions'] ) {
408 $conds[] = $db->makeList(
409 [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG ],
410 LIST_OR
411 );
412 }
413
414 if ( $options['namespaceIds'] ) {
415 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
416 }
417
418 if ( array_key_exists( 'rcTypes', $options ) ) {
419 $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
420 }
421
422 $conds = array_merge(
423 $conds,
424 $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
425 );
426
427 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
428
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( '' );
433 }
434 }
435
436 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
437
438 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
439 if ( $deletedPageLogCond ) {
440 $conds[] = $deletedPageLogCond;
441 }
442
443 return $conds;
444 }
445
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' );
454 }
455 return $watchlistOwner->getId();
456 }
457 return $user->getId();
458 }
459
460 private function getWatchedItemsWithRCInfoQueryFilterConds( User $user, array $options ) {
461 $conds = [];
462
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';
467 }
468
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';
473 }
474
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';
479 }
480
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';
488 }
489 }
490
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';
496 }
497
498 return $conds;
499 }
500
501 private function getStartEndConds( IDatabase $db, array $options ) {
502 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
503 return [];
504 }
505
506 $conds = [];
507
508 if ( isset( $options['start'] ) ) {
509 $after = $options['dir'] === self::DIR_OLDER ? '<=' : '>=';
510 $conds[] = 'rc_timestamp ' . $after . ' ' .
511 $db->addQuotes( $db->timestamp( $options['start'] ) );
512 }
513 if ( isset( $options['end'] ) ) {
514 $before = $options['dir'] === self::DIR_OLDER ? '>=' : '<=';
515 $conds[] = 'rc_timestamp ' . $before . ' ' .
516 $db->addQuotes( $db->timestamp( $options['end'] ) );
517 }
518
519 return $conds;
520 }
521
522 private function getUserRelatedConds( IDatabase $db, User $user, array $options ) {
523 if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
524 return [];
525 }
526
527 $conds = [];
528
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'] );
533 }
534
535 // Avoid brute force searches (T19342)
536 $bitmask = 0;
537 if ( !$user->isAllowed( 'deletedhistory' ) ) {
538 $bitmask = Revision::DELETED_USER;
539 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
540 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
541 }
542 if ( $bitmask ) {
543 $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
544 }
545
546 return $conds;
547 }
548
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.
552 $bitmask = 0;
553 if ( !$user->isAllowed( 'deletedhistory' ) ) {
554 $bitmask = LogPage::DELETED_ACTION;
555 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
556 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
557 }
558 if ( $bitmask ) {
559 return $db->makeList( [
560 'rc_type != ' . RC_LOG,
561 $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
562 ], LIST_OR );
563 }
564 return '';
565 }
566
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 ) );
571 $rcId = (int)$rcId;
572 return $db->makeList(
573 [
574 "rc_timestamp $op $rcTimestamp",
575 $db->makeList(
576 [
577 "rc_timestamp = $rcTimestamp",
578 "rc_id $op= $rcId"
579 ],
580 LIST_AND
581 )
582 ],
583 LIST_OR
584 );
585 }
586
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'] );
591 }
592 if ( isset( $options['filter'] ) ) {
593 $filter = $options['filter'];
594 if ( $filter === self::FILTER_CHANGED ) {
595 $conds[] = 'wl_notificationtimestamp IS NOT NULL';
596 } else {
597 $conds[] = 'wl_notificationtimestamp IS NULL';
598 }
599 }
600
601 if ( isset( $options['from'] ) ) {
602 $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
603 $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
604 }
605 if ( isset( $options['until'] ) ) {
606 $op = $options['sort'] === self::SORT_ASC ? '<' : '>';
607 $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
608 }
609 if ( isset( $options['startFrom'] ) ) {
610 $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
611 $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
612 }
613
614 return $conds;
615 }
616
617 /**
618 * Creates a query condition part for getting only items before or after the given link target
619 * (while ordering using $sort mode)
620 *
621 * @param IDatabase $db
622 * @param LinkTarget $target
623 * @param string $op comparison operator to use in the conditions
624 * @return string
625 */
626 private function getFromUntilTargetConds( IDatabase $db, LinkTarget $target, $op ) {
627 return $db->makeList(
628 [
629 "wl_namespace $op " . $target->getNamespace(),
630 $db->makeList(
631 [
632 'wl_namespace = ' . $target->getNamespace(),
633 "wl_title $op= " . $db->addQuotes( $target->getDBkey() )
634 ],
635 LIST_AND
636 )
637 ],
638 LIST_OR
639 );
640 }
641
642 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
643 $dbOptions = [];
644
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 ];
648 }
649
650 if ( array_key_exists( 'limit', $options ) ) {
651 $dbOptions['LIMIT'] = (int)$options['limit'] + 1;
652 }
653
654 return $dbOptions;
655 }
656
657 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
658 $dbOptions = [];
659 if ( array_key_exists( 'sort', $options ) ) {
660 $dbOptions['ORDER BY'] = [
661 "wl_namespace {$options['sort']}",
662 "wl_title {$options['sort']}"
663 ];
664 if ( count( $options['namespaceIds'] ) === 1 ) {
665 $dbOptions['ORDER BY'] = "wl_title {$options['sort']}";
666 }
667 }
668 if ( array_key_exists( 'limit', $options ) ) {
669 $dbOptions['LIMIT'] = (int)$options['limit'];
670 }
671 return $dbOptions;
672 }
673
674 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
675 $joinConds = [
676 'watchlist' => [ 'INNER JOIN',
677 [
678 'wl_namespace=rc_namespace',
679 'wl_title=rc_title'
680 ]
681 ]
682 ];
683 if ( !$options['allRevisions'] ) {
684 $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
685 }
686 if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
687 $joinConds += $this->getCommentStore()->getJoin()['joins'];
688 }
689 if ( in_array( self::INCLUDE_TAGS, $options['includeFields'] ) ) {
690 $joinConds['tag_summary'] = [ 'LEFT JOIN', [ 'rc_id=ts_rc_id' ] ];
691 }
692 return $joinConds;
693 }
694
695 }