Merge "Fix ParserOutput::getText 'unwrap' flag for end-of-doc comment"
[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 /** @var CommentStore */
60 private $commentStore;
61
62 public function __construct(
63 LoadBalancer $loadBalancer,
64 CommentStore $commentStore
65 ) {
66 $this->loadBalancer = $loadBalancer;
67 $this->commentStore = $commentStore;
68 }
69
70 /**
71 * @return WatchedItemQueryServiceExtension[]
72 */
73 private function getExtensions() {
74 if ( $this->extensions === null ) {
75 $this->extensions = [];
76 Hooks::run( 'WatchedItemQueryServiceExtensions', [ &$this->extensions, $this ] );
77 }
78 return $this->extensions;
79 }
80
81 /**
82 * @return IDatabase
83 * @throws MWException
84 */
85 private function getConnection() {
86 return $this->loadBalancer->getConnectionRef( DB_REPLICA, [ 'watchlist' ] );
87 }
88
89 /**
90 * @param User $user
91 * @param array $options Allowed keys:
92 * 'includeFields' => string[] RecentChange fields to be included in the result,
93 * self::INCLUDE_* constants should be used
94 * 'filters' => string[] optional filters to narrow down resulted items
95 * 'namespaceIds' => int[] optional namespace IDs to filter by
96 * (defaults to all namespaces)
97 * 'allRevisions' => bool return multiple revisions of the same page if true,
98 * only the most recent if false (default)
99 * 'rcTypes' => int[] which types of RecentChanges to include
100 * (defaults to all types), allowed values: RC_EDIT, RC_NEW,
101 * RC_LOG, RC_EXTERNAL, RC_CATEGORIZE
102 * 'onlyByUser' => string only list changes by a specified user
103 * 'notByUser' => string do not incluide changes by a specified user
104 * 'dir' => string in which direction to enumerate, accepted values:
105 * - DIR_OLDER list newest first
106 * - DIR_NEWER list oldest first
107 * 'start' => string (format accepted by wfTimestamp) requires 'dir' option,
108 * timestamp to start enumerating from
109 * 'end' => string (format accepted by wfTimestamp) requires 'dir' option,
110 * timestamp to end enumerating
111 * 'watchlistOwner' => User user whose watchlist items should be listed if different
112 * than the one specified with $user param,
113 * requires 'watchlistOwnerToken' option
114 * 'watchlistOwnerToken' => string a watchlist token used to access another user's
115 * watchlist, used with 'watchlistOwnerToken' option
116 * 'limit' => int maximum numbers of items to return
117 * 'usedInGenerator' => bool include only RecentChange id field required by the
118 * generator ('rc_cur_id' or 'rc_this_oldid') if true, or all
119 * id fields ('rc_cur_id', 'rc_this_oldid', 'rc_last_oldid')
120 * if false (default)
121 * @param array|null &$startFrom Continuation value: [ string $rcTimestamp, int $rcId ]
122 * @return array of pairs ( WatchedItem $watchedItem, string[] $recentChangeInfo ),
123 * where $recentChangeInfo contains the following keys:
124 * - 'rc_id',
125 * - 'rc_namespace',
126 * - 'rc_title',
127 * - 'rc_timestamp',
128 * - 'rc_type',
129 * - 'rc_deleted',
130 * Additional keys could be added by specifying the 'includeFields' option
131 */
132 public function getWatchedItemsWithRecentChangeInfo(
133 User $user, array $options = [], &$startFrom = null
134 ) {
135 $options += [
136 'includeFields' => [],
137 'namespaceIds' => [],
138 'filters' => [],
139 'allRevisions' => false,
140 'usedInGenerator' => false
141 ];
142
143 Assert::parameter(
144 !isset( $options['rcTypes'] )
145 || !array_diff( $options['rcTypes'], [ RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL, RC_CATEGORIZE ] ),
146 '$options[\'rcTypes\']',
147 'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
148 );
149 Assert::parameter(
150 !isset( $options['dir'] ) || in_array( $options['dir'], [ self::DIR_OLDER, self::DIR_NEWER ] ),
151 '$options[\'dir\']',
152 'must be DIR_OLDER or DIR_NEWER'
153 );
154 Assert::parameter(
155 !isset( $options['start'] ) && !isset( $options['end'] ) && $startFrom === null
156 || isset( $options['dir'] ),
157 '$options[\'dir\']',
158 'must be provided when providing the "start" or "end" options or the $startFrom parameter'
159 );
160 Assert::parameter(
161 !isset( $options['startFrom'] ),
162 '$options[\'startFrom\']',
163 'must not be provided, use $startFrom instead'
164 );
165 Assert::parameter(
166 !isset( $startFrom ) || ( is_array( $startFrom ) && count( $startFrom ) === 2 ),
167 '$startFrom',
168 'must be a two-element array'
169 );
170 if ( array_key_exists( 'watchlistOwner', $options ) ) {
171 Assert::parameterType(
172 User::class,
173 $options['watchlistOwner'],
174 '$options[\'watchlistOwner\']'
175 );
176 Assert::parameter(
177 isset( $options['watchlistOwnerToken'] ),
178 '$options[\'watchlistOwnerToken\']',
179 'must be provided when providing watchlistOwner option'
180 );
181 }
182
183 $db = $this->getConnection();
184
185 $tables = $this->getWatchedItemsWithRCInfoQueryTables( $options );
186 $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
187 $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
188 $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
189 $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
190
191 if ( $startFrom !== null ) {
192 $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
193 }
194
195 foreach ( $this->getExtensions() as $extension ) {
196 $extension->modifyWatchedItemsWithRCInfoQuery(
197 $user, $options, $db,
198 $tables,
199 $fields,
200 $conds,
201 $dbOptions,
202 $joinConds
203 );
204 }
205
206 $res = $db->select(
207 $tables,
208 $fields,
209 $conds,
210 __METHOD__,
211 $dbOptions,
212 $joinConds
213 );
214
215 $limit = isset( $dbOptions['LIMIT'] ) ? $dbOptions['LIMIT'] : INF;
216 $items = [];
217 $startFrom = null;
218 foreach ( $res as $row ) {
219 if ( --$limit <= 0 ) {
220 $startFrom = [ $row->rc_timestamp, $row->rc_id ];
221 break;
222 }
223
224 $items[] = [
225 new WatchedItem(
226 $user,
227 new TitleValue( (int)$row->rc_namespace, $row->rc_title ),
228 $row->wl_notificationtimestamp
229 ),
230 $this->getRecentChangeFieldsFromRow( $row )
231 ];
232 }
233
234 foreach ( $this->getExtensions() as $extension ) {
235 $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
236 }
237
238 return $items;
239 }
240
241 /**
242 * For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser
243 *
244 * @param User $user
245 * @param array $options Allowed keys:
246 * 'sort' => string optional sorting by namespace ID and title
247 * one of the self::SORT_* constants
248 * 'namespaceIds' => int[] optional namespace IDs to filter by (defaults to all namespaces)
249 * 'limit' => int maximum number of items to return
250 * 'filter' => string optional filter, one of the self::FILTER_* contants
251 * 'from' => LinkTarget requires 'sort' key, only return items starting from
252 * those related to the link target
253 * 'until' => LinkTarget requires 'sort' key, only return items until
254 * those related to the link target
255 * 'startFrom' => LinkTarget requires 'sort' key, only return items starting from
256 * those related to the link target, allows to skip some link targets
257 * specified using the form option
258 * @return WatchedItem[]
259 */
260 public function getWatchedItemsForUser( User $user, array $options = [] ) {
261 if ( $user->isAnon() ) {
262 // TODO: should this just return an empty array or rather complain loud at this point
263 // as e.g. ApiBase::getWatchlistUser does?
264 return [];
265 }
266
267 $options += [ 'namespaceIds' => [] ];
268
269 Assert::parameter(
270 !isset( $options['sort'] ) || in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ),
271 '$options[\'sort\']',
272 'must be SORT_ASC or SORT_DESC'
273 );
274 Assert::parameter(
275 !isset( $options['filter'] ) || in_array(
276 $options['filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
277 ),
278 '$options[\'filter\']',
279 'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
280 );
281 Assert::parameter(
282 !isset( $options['from'] ) && !isset( $options['until'] ) && !isset( $options['startFrom'] )
283 || isset( $options['sort'] ),
284 '$options[\'sort\']',
285 'must be provided if any of "from", "until", "startFrom" options is provided'
286 );
287
288 $db = $this->getConnection();
289
290 $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
291 $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
292
293 $res = $db->select(
294 'watchlist',
295 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
296 $conds,
297 __METHOD__,
298 $dbOptions
299 );
300
301 $watchedItems = [];
302 foreach ( $res as $row ) {
303 // todo these could all be cached at some point?
304 $watchedItems[] = new WatchedItem(
305 $user,
306 new TitleValue( (int)$row->wl_namespace, $row->wl_title ),
307 $row->wl_notificationtimestamp
308 );
309 }
310
311 return $watchedItems;
312 }
313
314 private function getRecentChangeFieldsFromRow( stdClass $row ) {
315 // This can be simplified to single array_filter call filtering by key value,
316 // once we stop supporting PHP 5.5
317 $allFields = get_object_vars( $row );
318 $rcKeys = array_filter(
319 array_keys( $allFields ),
320 function ( $key ) {
321 return substr( $key, 0, 3 ) === 'rc_';
322 }
323 );
324 return array_intersect_key( $allFields, array_flip( $rcKeys ) );
325 }
326
327 private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
328 $tables = [ 'recentchanges', 'watchlist' ];
329 if ( !$options['allRevisions'] ) {
330 $tables[] = 'page';
331 }
332 if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
333 $tables += $this->commentStore->getJoin( 'rc_comment' )['tables'];
334 }
335 if ( in_array( self::INCLUDE_TAGS, $options['includeFields'] ) ) {
336 $tables[] = 'tag_summary';
337 }
338 return $tables;
339 }
340
341 private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
342 $fields = [
343 'rc_id',
344 'rc_namespace',
345 'rc_title',
346 'rc_timestamp',
347 'rc_type',
348 'rc_deleted',
349 'wl_notificationtimestamp'
350 ];
351
352 $rcIdFields = [
353 'rc_cur_id',
354 'rc_this_oldid',
355 'rc_last_oldid',
356 ];
357 if ( $options['usedInGenerator'] ) {
358 if ( $options['allRevisions'] ) {
359 $rcIdFields = [ 'rc_this_oldid' ];
360 } else {
361 $rcIdFields = [ 'rc_cur_id' ];
362 }
363 }
364 $fields = array_merge( $fields, $rcIdFields );
365
366 if ( in_array( self::INCLUDE_FLAGS, $options['includeFields'] ) ) {
367 $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
368 }
369 if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ) {
370 $fields[] = 'rc_user_text';
371 }
372 if ( in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ) {
373 $fields[] = 'rc_user';
374 }
375 if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
376 $fields += $this->commentStore->getJoin( 'rc_comment' )['fields'];
377 }
378 if ( in_array( self::INCLUDE_PATROL_INFO, $options['includeFields'] ) ) {
379 $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
380 }
381 if ( in_array( self::INCLUDE_SIZES, $options['includeFields'] ) ) {
382 $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
383 }
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' ] );
386 }
387 if ( in_array( self::INCLUDE_TAGS, $options['includeFields'] ) ) {
388 // prefixed with rc_ to include the field in getRecentChangeFieldsFromRow
389 $fields['rc_tags'] = 'ts_tags';
390 }
391
392 return $fields;
393 }
394
395 private function getWatchedItemsWithRCInfoQueryConds(
396 IDatabase $db,
397 User $user,
398 array $options
399 ) {
400 $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
401 $conds = [ 'wl_user' => $watchlistOwnerId ];
402
403 if ( !$options['allRevisions'] ) {
404 $conds[] = $db->makeList(
405 [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG ],
406 LIST_OR
407 );
408 }
409
410 if ( $options['namespaceIds'] ) {
411 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
412 }
413
414 if ( array_key_exists( 'rcTypes', $options ) ) {
415 $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
416 }
417
418 $conds = array_merge(
419 $conds,
420 $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
421 );
422
423 $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
424
425 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
426 if ( $db->getType() === 'mysql' ) {
427 // This is an index optimization for mysql
428 $conds[] = 'rc_timestamp > ' . $db->addQuotes( '' );
429 }
430 }
431
432 $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
433
434 $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
435 if ( $deletedPageLogCond ) {
436 $conds[] = $deletedPageLogCond;
437 }
438
439 return $conds;
440 }
441
442 private function getWatchlistOwnerId( User $user, array $options ) {
443 if ( array_key_exists( 'watchlistOwner', $options ) ) {
444 /** @var User $watchlistOwner */
445 $watchlistOwner = $options['watchlistOwner'];
446 $ownersToken = $watchlistOwner->getOption( 'watchlisttoken' );
447 $token = $options['watchlistOwnerToken'];
448 if ( $ownersToken == '' || !hash_equals( $ownersToken, $token ) ) {
449 throw ApiUsageException::newWithMessage( null, 'apierror-bad-watchlist-token', 'bad_wltoken' );
450 }
451 return $watchlistOwner->getId();
452 }
453 return $user->getId();
454 }
455
456 private function getWatchedItemsWithRCInfoQueryFilterConds( User $user, array $options ) {
457 $conds = [];
458
459 if ( in_array( self::FILTER_MINOR, $options['filters'] ) ) {
460 $conds[] = 'rc_minor != 0';
461 } elseif ( in_array( self::FILTER_NOT_MINOR, $options['filters'] ) ) {
462 $conds[] = 'rc_minor = 0';
463 }
464
465 if ( in_array( self::FILTER_BOT, $options['filters'] ) ) {
466 $conds[] = 'rc_bot != 0';
467 } elseif ( in_array( self::FILTER_NOT_BOT, $options['filters'] ) ) {
468 $conds[] = 'rc_bot = 0';
469 }
470
471 if ( in_array( self::FILTER_ANON, $options['filters'] ) ) {
472 $conds[] = 'rc_user = 0';
473 } elseif ( in_array( self::FILTER_NOT_ANON, $options['filters'] ) ) {
474 $conds[] = 'rc_user != 0';
475 }
476
477 if ( $user->useRCPatrol() || $user->useNPPatrol() ) {
478 // TODO: not sure if this should simply ignore patrolled filters if user does not have the patrol
479 // right, or maybe rather fail loud at this point, same as e.g. ApiQueryWatchlist does?
480 if ( in_array( self::FILTER_PATROLLED, $options['filters'] ) ) {
481 $conds[] = 'rc_patrolled != 0';
482 } elseif ( in_array( self::FILTER_NOT_PATROLLED, $options['filters'] ) ) {
483 $conds[] = 'rc_patrolled = 0';
484 }
485 }
486
487 if ( in_array( self::FILTER_UNREAD, $options['filters'] ) ) {
488 $conds[] = 'rc_timestamp >= wl_notificationtimestamp';
489 } elseif ( in_array( self::FILTER_NOT_UNREAD, $options['filters'] ) ) {
490 // TODO: should this be changed to use Database::makeList?
491 $conds[] = 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
492 }
493
494 return $conds;
495 }
496
497 private function getStartEndConds( IDatabase $db, array $options ) {
498 if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
499 return [];
500 }
501
502 $conds = [];
503
504 if ( isset( $options['start'] ) ) {
505 $after = $options['dir'] === self::DIR_OLDER ? '<=' : '>=';
506 $conds[] = 'rc_timestamp ' . $after . ' ' .
507 $db->addQuotes( $db->timestamp( $options['start'] ) );
508 }
509 if ( isset( $options['end'] ) ) {
510 $before = $options['dir'] === self::DIR_OLDER ? '>=' : '<=';
511 $conds[] = 'rc_timestamp ' . $before . ' ' .
512 $db->addQuotes( $db->timestamp( $options['end'] ) );
513 }
514
515 return $conds;
516 }
517
518 private function getUserRelatedConds( IDatabase $db, User $user, array $options ) {
519 if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
520 return [];
521 }
522
523 $conds = [];
524
525 if ( array_key_exists( 'onlyByUser', $options ) ) {
526 $conds['rc_user_text'] = $options['onlyByUser'];
527 } elseif ( array_key_exists( 'notByUser', $options ) ) {
528 $conds[] = 'rc_user_text != ' . $db->addQuotes( $options['notByUser'] );
529 }
530
531 // Avoid brute force searches (T19342)
532 $bitmask = 0;
533 if ( !$user->isAllowed( 'deletedhistory' ) ) {
534 $bitmask = Revision::DELETED_USER;
535 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
536 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
537 }
538 if ( $bitmask ) {
539 $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
540 }
541
542 return $conds;
543 }
544
545 private function getExtraDeletedPageLogEntryRelatedCond( IDatabase $db, User $user ) {
546 // LogPage::DELETED_ACTION hides the affected page, too. So hide those
547 // entirely from the watchlist, or someone could guess the title.
548 $bitmask = 0;
549 if ( !$user->isAllowed( 'deletedhistory' ) ) {
550 $bitmask = LogPage::DELETED_ACTION;
551 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
552 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
553 }
554 if ( $bitmask ) {
555 return $db->makeList( [
556 'rc_type != ' . RC_LOG,
557 $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
558 ], LIST_OR );
559 }
560 return '';
561 }
562
563 private function getStartFromConds( IDatabase $db, array $options, array $startFrom ) {
564 $op = $options['dir'] === self::DIR_OLDER ? '<' : '>';
565 list( $rcTimestamp, $rcId ) = $startFrom;
566 $rcTimestamp = $db->addQuotes( $db->timestamp( $rcTimestamp ) );
567 $rcId = (int)$rcId;
568 return $db->makeList(
569 [
570 "rc_timestamp $op $rcTimestamp",
571 $db->makeList(
572 [
573 "rc_timestamp = $rcTimestamp",
574 "rc_id $op= $rcId"
575 ],
576 LIST_AND
577 )
578 ],
579 LIST_OR
580 );
581 }
582
583 private function getWatchedItemsForUserQueryConds( IDatabase $db, User $user, array $options ) {
584 $conds = [ 'wl_user' => $user->getId() ];
585 if ( $options['namespaceIds'] ) {
586 $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
587 }
588 if ( isset( $options['filter'] ) ) {
589 $filter = $options['filter'];
590 if ( $filter === self::FILTER_CHANGED ) {
591 $conds[] = 'wl_notificationtimestamp IS NOT NULL';
592 } else {
593 $conds[] = 'wl_notificationtimestamp IS NULL';
594 }
595 }
596
597 if ( isset( $options['from'] ) ) {
598 $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
599 $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
600 }
601 if ( isset( $options['until'] ) ) {
602 $op = $options['sort'] === self::SORT_ASC ? '<' : '>';
603 $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
604 }
605 if ( isset( $options['startFrom'] ) ) {
606 $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
607 $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
608 }
609
610 return $conds;
611 }
612
613 /**
614 * Creates a query condition part for getting only items before or after the given link target
615 * (while ordering using $sort mode)
616 *
617 * @param IDatabase $db
618 * @param LinkTarget $target
619 * @param string $op comparison operator to use in the conditions
620 * @return string
621 */
622 private function getFromUntilTargetConds( IDatabase $db, LinkTarget $target, $op ) {
623 return $db->makeList(
624 [
625 "wl_namespace $op " . $target->getNamespace(),
626 $db->makeList(
627 [
628 'wl_namespace = ' . $target->getNamespace(),
629 "wl_title $op= " . $db->addQuotes( $target->getDBkey() )
630 ],
631 LIST_AND
632 )
633 ],
634 LIST_OR
635 );
636 }
637
638 private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
639 $dbOptions = [];
640
641 if ( array_key_exists( 'dir', $options ) ) {
642 $sort = $options['dir'] === self::DIR_OLDER ? ' DESC' : '';
643 $dbOptions['ORDER BY'] = [ 'rc_timestamp' . $sort, 'rc_id' . $sort ];
644 }
645
646 if ( array_key_exists( 'limit', $options ) ) {
647 $dbOptions['LIMIT'] = (int)$options['limit'] + 1;
648 }
649
650 return $dbOptions;
651 }
652
653 private function getWatchedItemsForUserQueryDbOptions( array $options ) {
654 $dbOptions = [];
655 if ( array_key_exists( 'sort', $options ) ) {
656 $dbOptions['ORDER BY'] = [
657 "wl_namespace {$options['sort']}",
658 "wl_title {$options['sort']}"
659 ];
660 if ( count( $options['namespaceIds'] ) === 1 ) {
661 $dbOptions['ORDER BY'] = "wl_title {$options['sort']}";
662 }
663 }
664 if ( array_key_exists( 'limit', $options ) ) {
665 $dbOptions['LIMIT'] = (int)$options['limit'];
666 }
667 return $dbOptions;
668 }
669
670 private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
671 $joinConds = [
672 'watchlist' => [ 'INNER JOIN',
673 [
674 'wl_namespace=rc_namespace',
675 'wl_title=rc_title'
676 ]
677 ]
678 ];
679 if ( !$options['allRevisions'] ) {
680 $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
681 }
682 if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
683 $joinConds += $this->commentStore->getJoin( 'rc_comment' )['joins'];
684 }
685 if ( in_array( self::INCLUDE_TAGS, $options['includeFields'] ) ) {
686 $joinConds['tag_summary'] = [ 'LEFT JOIN', [ 'rc_id=ts_rc_id' ] ];
687 }
688 return $joinConds;
689 }
690
691 }