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