MediaWiki  master
ApiQueryRecentChanges.php
Go to the documentation of this file.
1 <?php
27 
35 
37  private $commentStore;
38 
44  public function __construct(
45  ApiQuery $query,
46  $moduleName,
48  ) {
49  parent::__construct( $query, $moduleName, 'rc' );
50  $this->commentStore = $commentStore;
51  }
52 
53  private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
54  $fld_flags = false, $fld_timestamp = false, $fld_title = false, $fld_ids = false,
55  $fld_sizes = false, $fld_redirect = false, $fld_patrolled = false, $fld_loginfo = false,
56  $fld_tags = false, $fld_sha1 = false, $token = [];
57 
58  private $tokenFunctions;
59 
67  protected function getTokenFunctions() {
68  if ( isset( $this->tokenFunctions ) ) {
69  return $this->tokenFunctions;
70  }
71 
72  // If we're in a mode that breaks the same-origin policy, no tokens can
73  // be obtained
74  if ( $this->lacksSameOriginSecurity() ) {
75  return [];
76  }
77 
78  $this->tokenFunctions = [
79  'patrol' => [ self::class, 'getPatrolToken' ]
80  ];
81 
82  return $this->tokenFunctions;
83  }
84 
92  public static function getPatrolToken( User $user, $rc = null ) {
93  $validTokenUser = false;
94 
95  if ( $rc ) {
96  if ( ( $user->useRCPatrol() && $rc->getAttribute( 'rc_type' ) == RC_EDIT ) ||
97  ( $user->useNPPatrol() && $rc->getAttribute( 'rc_type' ) == RC_NEW )
98  ) {
99  $validTokenUser = true;
100  }
101  } elseif ( $user->useRCPatrol() || $user->useNPPatrol() ) {
102  $validTokenUser = true;
103  }
104 
105  if ( $validTokenUser ) {
106  // The patrol token is always the same, let's exploit that
107  static $cachedPatrolToken = null;
108 
109  if ( $cachedPatrolToken === null ) {
110  $cachedPatrolToken = $user->getEditToken( 'patrol' );
111  }
112 
113  return $cachedPatrolToken;
114  }
115 
116  return false;
117  }
118 
123  public function initProperties( $prop ) {
124  $this->fld_comment = isset( $prop['comment'] );
125  $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
126  $this->fld_user = isset( $prop['user'] );
127  $this->fld_userid = isset( $prop['userid'] );
128  $this->fld_flags = isset( $prop['flags'] );
129  $this->fld_timestamp = isset( $prop['timestamp'] );
130  $this->fld_title = isset( $prop['title'] );
131  $this->fld_ids = isset( $prop['ids'] );
132  $this->fld_sizes = isset( $prop['sizes'] );
133  $this->fld_redirect = isset( $prop['redirect'] );
134  $this->fld_patrolled = isset( $prop['patrolled'] );
135  $this->fld_loginfo = isset( $prop['loginfo'] );
136  $this->fld_tags = isset( $prop['tags'] );
137  $this->fld_sha1 = isset( $prop['sha1'] );
138  }
139 
140  public function execute() {
141  $this->run();
142  }
143 
144  public function executeGenerator( $resultPageSet ) {
145  $this->run( $resultPageSet );
146  }
147 
153  public function run( $resultPageSet = null ) {
154  $user = $this->getUser();
155  /* Get the parameters of the request. */
156  $params = $this->extractRequestParams();
157 
158  /* Build our basic query. Namely, something along the lines of:
159  * SELECT * FROM recentchanges WHERE rc_timestamp > $start
160  * AND rc_timestamp < $end AND rc_namespace = $namespace
161  */
162  $this->addTables( 'recentchanges' );
163  $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
164 
165  if ( $params['continue'] !== null ) {
166  $cont = explode( '|', $params['continue'] );
167  $this->dieContinueUsageIf( count( $cont ) != 2 );
168  $db = $this->getDB();
169  $timestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
170  $id = (int)$cont[1];
171  $this->dieContinueUsageIf( $id != $cont[1] );
172  $op = $params['dir'] === 'older' ? '<' : '>';
173  $this->addWhere(
174  "rc_timestamp $op $timestamp OR " .
175  "(rc_timestamp = $timestamp AND " .
176  "rc_id $op= $id)"
177  );
178  }
179 
180  $order = $params['dir'] === 'older' ? 'DESC' : 'ASC';
181  $this->addOption( 'ORDER BY', [
182  "rc_timestamp $order",
183  "rc_id $order",
184  ] );
185 
186  $this->addWhereFld( 'rc_namespace', $params['namespace'] );
187 
188  if ( $params['type'] !== null ) {
189  try {
190  $this->addWhereFld( 'rc_type', RecentChange::parseToRCType( $params['type'] ) );
191  } catch ( Exception $e ) {
192  ApiBase::dieDebug( __METHOD__, $e->getMessage() );
193  }
194  }
195 
196  $title = $params['title'];
197  if ( $title !== null ) {
198  $titleObj = Title::newFromText( $title );
199  if ( $titleObj === null ) {
200  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] );
201  }
202  $this->addWhereFld( 'rc_namespace', $titleObj->getNamespace() );
203  $this->addWhereFld( 'rc_title', $titleObj->getDBkey() );
204  }
205 
206  if ( $params['show'] !== null ) {
207  $show = array_fill_keys( $params['show'], true );
208 
209  /* Check for conflicting parameters. */
210  if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
211  || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
212  || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
213  || ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
214  || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
215  || ( isset( $show['patrolled'] ) && isset( $show['unpatrolled'] ) )
216  || ( isset( $show['!patrolled'] ) && isset( $show['unpatrolled'] ) )
217  || ( isset( $show['autopatrolled'] ) && isset( $show['!autopatrolled'] ) )
218  || ( isset( $show['autopatrolled'] ) && isset( $show['unpatrolled'] ) )
219  || ( isset( $show['autopatrolled'] ) && isset( $show['!patrolled'] ) )
220  ) {
221  $this->dieWithError( 'apierror-show' );
222  }
223 
224  // Check permissions
225  if ( $this->includesPatrollingFlags( $show ) ) {
226  if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
227  $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
228  }
229  }
230 
231  /* Add additional conditions to query depending upon parameters. */
232  $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
233  $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
234  $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
235  $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
236  if ( isset( $show['anon'] ) || isset( $show['!anon'] ) ) {
237  $this->addTables( 'actor', 'actor' );
238  $this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id=rc_actor' ] ] );
239  $this->addWhereIf(
240  'actor_user IS NULL', isset( $show['anon'] )
241  );
242  $this->addWhereIf(
243  'actor_user IS NOT NULL', isset( $show['!anon'] )
244  );
245  }
246  $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
247  $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
248  $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
249 
250  if ( isset( $show['unpatrolled'] ) ) {
251  // See ChangesList::isUnpatrolled
252  if ( $user->useRCPatrol() ) {
253  $this->addWhere( 'rc_patrolled = ' . RecentChange::PRC_UNPATROLLED );
254  } elseif ( $user->useNPPatrol() ) {
255  $this->addWhere( 'rc_patrolled = ' . RecentChange::PRC_UNPATROLLED );
256  $this->addWhereFld( 'rc_type', RC_NEW );
257  }
258  }
259 
260  $this->addWhereIf(
261  'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED,
262  isset( $show['!autopatrolled'] )
263  );
264  $this->addWhereIf(
265  'rc_patrolled = ' . RecentChange::PRC_AUTOPATROLLED,
266  isset( $show['autopatrolled'] )
267  );
268 
269  // Don't throw log entries out the window here
270  $this->addWhereIf(
271  'page_is_redirect = 0 OR page_is_redirect IS NULL',
272  isset( $show['!redirect'] )
273  );
274  }
275 
276  $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
277 
278  if ( $params['prop'] !== null ) {
279  $prop = array_fill_keys( $params['prop'], true );
280 
281  /* Set up internal members based upon params. */
282  $this->initProperties( $prop );
283  }
284 
285  if ( $this->fld_user
286  || $this->fld_userid
287  // Token needs actor_user for RecentChange::newFromRow/User::newFromAnyId (T228425)
288  || $this->token !== null
289  || $params['user'] !== null
290  || $params['excludeuser'] !== null
291  ) {
292  $this->addTables( 'actor', 'actor' );
293  $this->addFields( [ 'actor_name', 'actor_user', 'rc_actor' ] );
294  $this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id=rc_actor' ] ] );
295  }
296 
297  if ( $params['user'] !== null ) {
298  $this->addWhereFld( 'actor_name', $params['user'] );
299  }
300 
301  if ( $params['excludeuser'] !== null ) {
302  $this->addWhere( 'actor_name<>' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
303  }
304 
305  /* Add the fields we're concerned with to our query. */
306  $this->addFields( [
307  'rc_id',
308  'rc_timestamp',
309  'rc_namespace',
310  'rc_title',
311  'rc_cur_id',
312  'rc_type',
313  'rc_deleted'
314  ] );
315 
316  $showRedirects = false;
317  /* Determine what properties we need to display. */
318  if ( $params['prop'] !== null ) {
319  if ( $this->fld_patrolled && !$user->useRCPatrol() && !$user->useNPPatrol() ) {
320  $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
321  }
322 
323  /* Add fields to our query if they are specified as a needed parameter. */
324  $this->addFieldsIf( [ 'rc_this_oldid', 'rc_last_oldid' ], $this->fld_ids );
325  $this->addFieldsIf( [ 'rc_minor', 'rc_type', 'rc_bot' ], $this->fld_flags );
326  $this->addFieldsIf( [ 'rc_old_len', 'rc_new_len' ], $this->fld_sizes );
327  $this->addFieldsIf( [ 'rc_patrolled', 'rc_log_type' ], $this->fld_patrolled );
328  $this->addFieldsIf(
329  [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ],
330  $this->fld_loginfo
331  );
332  $showRedirects = $this->fld_redirect || isset( $show['redirect'] )
333  || isset( $show['!redirect'] );
334  }
335  $this->addFieldsIf( [ 'rc_this_oldid' ],
336  $resultPageSet && $params['generaterevisions'] );
337 
338  if ( $this->fld_tags ) {
339  $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'recentchanges' ) ] );
340  }
341 
342  if ( $this->fld_sha1 ) {
343  $this->addTables( 'revision' );
344  $this->addJoinConds( [ 'revision' => [ 'LEFT JOIN',
345  [ 'rc_this_oldid=rev_id' ] ] ] );
346  $this->addFields( [ 'rev_sha1', 'rev_deleted' ] );
347  }
348 
349  if ( $params['toponly'] || $showRedirects ) {
350  $this->addTables( 'page' );
351  $this->addJoinConds( [ 'page' => [ 'LEFT JOIN',
352  [ 'rc_namespace=page_namespace', 'rc_title=page_title' ] ] ] );
353  $this->addFields( 'page_is_redirect' );
354 
355  if ( $params['toponly'] ) {
356  $this->addWhere( 'rc_this_oldid = page_latest' );
357  }
358  }
359 
360  if ( $params['tag'] !== null ) {
361  $this->addTables( 'change_tag' );
362  $this->addJoinConds( [ 'change_tag' => [ 'JOIN', [ 'rc_id=ct_rc_id' ] ] ] );
363  $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
364  try {
365  $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
366  } catch ( NameTableAccessException $exception ) {
367  // Return nothing.
368  $this->addWhere( '1=0' );
369  }
370  }
371 
372  // Paranoia: avoid brute force searches (T19342)
373  if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
374  if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
375  $bitmask = RevisionRecord::DELETED_USER;
376  } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
377  $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
378  } else {
379  $bitmask = 0;
380  }
381  if ( $bitmask ) {
382  $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
383  }
384  }
385  if ( $this->getRequest()->getCheck( 'namespace' ) ) {
386  // LogPage::DELETED_ACTION hides the affected page, too.
387  if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
388  $bitmask = LogPage::DELETED_ACTION;
389  } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
391  } else {
392  $bitmask = 0;
393  }
394  if ( $bitmask ) {
395  $this->addWhere( $this->getDB()->makeList( [
396  'rc_type != ' . RC_LOG,
397  $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
398  ], LIST_OR ) );
399  }
400  }
401 
402  $this->token = $params['token'];
403 
404  if ( $this->fld_comment || $this->fld_parsedcomment || $this->token ) {
405  $commentQuery = $this->commentStore->getJoin( 'rc_comment' );
406  $this->addTables( $commentQuery['tables'] );
407  $this->addFields( $commentQuery['fields'] );
408  $this->addJoinConds( $commentQuery['joins'] );
409  }
410 
411  if ( $params['slot'] !== null ) {
412  try {
413  $slotId = MediaWikiServices::getInstance()->getSlotRoleStore()->getId(
414  $params['slot']
415  );
416  } catch ( \Exception $e ) {
417  $slotId = null;
418  }
419 
420  $this->addTables( [
421  'slot' => 'slots', 'parent_slot' => 'slots'
422  ] );
423  $this->addJoinConds( [
424  'slot' => [ 'LEFT JOIN', [
425  'rc_this_oldid = slot.slot_revision_id',
426  'slot.slot_role_id' => $slotId,
427  ] ],
428  'parent_slot' => [ 'LEFT JOIN', [
429  'rc_last_oldid = parent_slot.slot_revision_id',
430  'parent_slot.slot_role_id' => $slotId,
431  ] ]
432  ] );
433  // Detecting whether the slot has been touched as follows:
434  // 1. if slot_origin=slot_revision_id then the slot has been newly created or edited
435  // with this revision
436  // 2. otherwise if the content of a slot is different to the content of its parent slot,
437  // then the content of the slot has been changed in this revision
438  // (probably by a revert)
439  $this->addWhere(
440  'slot.slot_origin = slot.slot_revision_id OR ' .
441  'slot.slot_content_id != parent_slot.slot_content_id OR ' .
442  '(slot.slot_content_id IS NULL AND parent_slot.slot_content_id IS NOT NULL) OR ' .
443  '(slot.slot_content_id IS NOT NULL AND parent_slot.slot_content_id IS NULL)'
444  );
445  // Only include changes that touch page content (i.e. RC_NEW, RC_EDIT)
446  $changeTypes = RecentChange::parseToRCType(
447  array_intersect( $params['type'], [ 'new', 'edit' ] )
448  );
449  if ( count( $changeTypes ) ) {
450  $this->addWhereFld( 'rc_type', $changeTypes );
451  } else {
452  // Calling $this->addWhere() with an empty array does nothing, so explicitly
453  // add an unsatisfiable condition
454  $this->addWhere( 'rc_type IS NULL' );
455  }
456  }
457 
458  $this->addOption( 'LIMIT', $params['limit'] + 1 );
459 
460  $hookData = [];
461  $count = 0;
462  /* Perform the actual query. */
463  $res = $this->select( __METHOD__, [], $hookData );
464 
465  if ( $this->fld_title && $resultPageSet === null ) {
466  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__, 'rc' );
467  }
468 
469  $revids = [];
470  $titles = [];
471 
472  $result = $this->getResult();
473 
474  /* Iterate through the rows, adding data extracted from them to our query result. */
475  foreach ( $res as $row ) {
476  if ( $count === 0 && $resultPageSet !== null ) {
477  // Set the non-continue since the list of recentchanges is
478  // prone to having entries added at the start frequently.
479  $this->getContinuationManager()->addGeneratorNonContinueParam(
480  $this, 'continue', "$row->rc_timestamp|$row->rc_id"
481  );
482  }
483  if ( ++$count > $params['limit'] ) {
484  // We've reached the one extra which shows that there are
485  // additional pages to be had. Stop here...
486  $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
487  break;
488  }
489 
490  if ( $resultPageSet === null ) {
491  /* Extract the data from a single row. */
492  $vals = $this->extractRowInfo( $row );
493 
494  /* Add that row's data to our final output. */
495  $fit = $this->processRow( $row, $vals, $hookData ) &&
496  $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
497  if ( !$fit ) {
498  $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
499  break;
500  }
501  } elseif ( $params['generaterevisions'] ) {
502  $revid = (int)$row->rc_this_oldid;
503  if ( $revid > 0 ) {
504  $revids[] = $revid;
505  }
506  } else {
507  $titles[] = Title::makeTitle( $row->rc_namespace, $row->rc_title );
508  }
509  }
510 
511  if ( $resultPageSet === null ) {
512  /* Format the result */
513  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'rc' );
514  } elseif ( $params['generaterevisions'] ) {
515  $resultPageSet->populateFromRevisionIDs( $revids );
516  } else {
517  $resultPageSet->populateFromTitles( $titles );
518  }
519  }
520 
527  public function extractRowInfo( $row ) {
528  /* Determine the title of the page that has been changed. */
529  $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
530  $user = $this->getUser();
531 
532  /* Our output data. */
533  $vals = [];
534 
535  $type = (int)$row->rc_type;
536  $vals['type'] = RecentChange::parseFromRCType( $type );
537 
538  $anyHidden = false;
539 
540  /* Create a new entry in the result for the title. */
541  if ( $this->fld_title || $this->fld_ids ) {
542  if ( $type === RC_LOG && ( $row->rc_deleted & LogPage::DELETED_ACTION ) ) {
543  $vals['actionhidden'] = true;
544  $anyHidden = true;
545  }
546  if ( $type !== RC_LOG ||
547  LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user )
548  ) {
549  if ( $this->fld_title ) {
551  }
552  if ( $this->fld_ids ) {
553  $vals['pageid'] = (int)$row->rc_cur_id;
554  $vals['revid'] = (int)$row->rc_this_oldid;
555  $vals['old_revid'] = (int)$row->rc_last_oldid;
556  }
557  }
558  }
559 
560  if ( $this->fld_ids ) {
561  $vals['rcid'] = (int)$row->rc_id;
562  }
563 
564  /* Add user data and 'anon' flag, if user is anonymous. */
565  if ( $this->fld_user || $this->fld_userid ) {
566  if ( $row->rc_deleted & RevisionRecord::DELETED_USER ) {
567  $vals['userhidden'] = true;
568  $anyHidden = true;
569  }
570  if ( RevisionRecord::userCanBitfield( $row->rc_deleted, RevisionRecord::DELETED_USER, $user ) ) {
571  if ( $this->fld_user ) {
572  $vals['user'] = $row->actor_name;
573  }
574 
575  if ( $this->fld_userid ) {
576  $vals['userid'] = (int)$row->actor_user;
577  }
578 
579  if ( !$row->actor_user ) {
580  $vals['anon'] = true;
581  }
582  }
583  }
584 
585  /* Add flags, such as new, minor, bot. */
586  if ( $this->fld_flags ) {
587  $vals['bot'] = (bool)$row->rc_bot;
588  $vals['new'] = $row->rc_type == RC_NEW;
589  $vals['minor'] = (bool)$row->rc_minor;
590  }
591 
592  /* Add sizes of each revision. (Only available on 1.10+) */
593  if ( $this->fld_sizes ) {
594  $vals['oldlen'] = (int)$row->rc_old_len;
595  $vals['newlen'] = (int)$row->rc_new_len;
596  }
597 
598  /* Add the timestamp. */
599  if ( $this->fld_timestamp ) {
600  $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
601  }
602 
603  /* Add edit summary / log summary. */
604  if ( $this->fld_comment || $this->fld_parsedcomment ) {
605  if ( $row->rc_deleted & RevisionRecord::DELETED_COMMENT ) {
606  $vals['commenthidden'] = true;
607  $anyHidden = true;
608  }
609  if ( RevisionRecord::userCanBitfield(
610  $row->rc_deleted, RevisionRecord::DELETED_COMMENT, $user
611  ) ) {
612  $comment = $this->commentStore->getComment( 'rc_comment', $row )->text;
613  if ( $this->fld_comment ) {
614  $vals['comment'] = $comment;
615  }
616 
617  if ( $this->fld_parsedcomment ) {
618  $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
619  }
620  }
621  }
622 
623  if ( $this->fld_redirect ) {
624  $vals['redirect'] = (bool)$row->page_is_redirect;
625  }
626 
627  /* Add the patrolled flag */
628  if ( $this->fld_patrolled ) {
629  $vals['patrolled'] = $row->rc_patrolled != RecentChange::PRC_UNPATROLLED;
630  $vals['unpatrolled'] = ChangesList::isUnpatrolled( $row, $user );
631  $vals['autopatrolled'] = $row->rc_patrolled == RecentChange::PRC_AUTOPATROLLED;
632  }
633 
634  if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
635  if ( $row->rc_deleted & LogPage::DELETED_ACTION ) {
636  $vals['actionhidden'] = true;
637  $anyHidden = true;
638  }
639  if ( LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user ) ) {
640  $vals['logid'] = (int)$row->rc_logid;
641  $vals['logtype'] = $row->rc_log_type;
642  $vals['logaction'] = $row->rc_log_action;
643  $vals['logparams'] = LogFormatter::newFromRow( $row )->formatParametersForApi();
644  }
645  }
646 
647  if ( $this->fld_tags ) {
648  if ( $row->ts_tags ) {
649  $tags = explode( ',', $row->ts_tags );
650  ApiResult::setIndexedTagName( $tags, 'tag' );
651  $vals['tags'] = $tags;
652  } else {
653  $vals['tags'] = [];
654  }
655  }
656 
657  if ( $this->fld_sha1 && $row->rev_sha1 !== null ) {
658  if ( $row->rev_deleted & RevisionRecord::DELETED_TEXT ) {
659  $vals['sha1hidden'] = true;
660  $anyHidden = true;
661  }
662  if ( RevisionRecord::userCanBitfield(
663  $row->rev_deleted, RevisionRecord::DELETED_TEXT, $user
664  ) ) {
665  if ( $row->rev_sha1 !== '' ) {
666  $vals['sha1'] = Wikimedia\base_convert( $row->rev_sha1, 36, 16, 40 );
667  } else {
668  $vals['sha1'] = '';
669  }
670  }
671  }
672 
673  if ( $this->token !== null ) {
675  foreach ( $this->token as $t ) {
676  $val = call_user_func(
678  $this->getUser(),
680  );
681  if ( $val === false ) {
682  $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
683  } else {
684  $vals[$t . 'token'] = $val;
685  }
686  }
687  }
688 
689  if ( $anyHidden && ( $row->rc_deleted & RevisionRecord::DELETED_RESTRICTED ) ) {
690  $vals['suppressed'] = true;
691  }
692 
693  return $vals;
694  }
695 
700  private function includesPatrollingFlags( array $flagsArray ) {
701  return isset( $flagsArray['patrolled'] ) ||
702  isset( $flagsArray['!patrolled'] ) ||
703  isset( $flagsArray['unpatrolled'] ) ||
704  isset( $flagsArray['autopatrolled'] ) ||
705  isset( $flagsArray['!autopatrolled'] );
706  }
707 
708  public function getCacheMode( $params ) {
709  if ( isset( $params['show'] ) &&
710  $this->includesPatrollingFlags( array_fill_keys( $params['show'], true ) )
711  ) {
712  return 'private';
713  }
714  if ( isset( $params['token'] ) ) {
715  return 'private';
716  }
717  if ( $this->userCanSeeRevDel() ) {
718  return 'private';
719  }
720  if ( $params['prop'] !== null && in_array( 'parsedcomment', $params['prop'] ) ) {
721  // formatComment() calls wfMessage() among other things
722  return 'anon-public-user-private';
723  }
724 
725  return 'public';
726  }
727 
728  public function getAllowedParams() {
729  $slotRoles = MediaWikiServices::getInstance()->getSlotRoleRegistry()->getKnownRoles();
730  sort( $slotRoles, SORT_STRING );
731 
732  return [
733  'start' => [
734  ApiBase::PARAM_TYPE => 'timestamp'
735  ],
736  'end' => [
737  ApiBase::PARAM_TYPE => 'timestamp'
738  ],
739  'dir' => [
740  ApiBase::PARAM_DFLT => 'older',
742  'newer',
743  'older'
744  ],
745  ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
746  ],
747  'namespace' => [
748  ApiBase::PARAM_ISMULTI => true,
749  ApiBase::PARAM_TYPE => 'namespace',
751  ],
752  'user' => [
753  ApiBase::PARAM_TYPE => 'user',
754  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
755  UserDef::PARAM_RETURN_OBJECT => true,
756  ],
757  'excludeuser' => [
758  ApiBase::PARAM_TYPE => 'user',
759  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
760  UserDef::PARAM_RETURN_OBJECT => true,
761  ],
762  'tag' => null,
763  'prop' => [
764  ApiBase::PARAM_ISMULTI => true,
765  ApiBase::PARAM_DFLT => 'title|timestamp|ids',
767  'user',
768  'userid',
769  'comment',
770  'parsedcomment',
771  'flags',
772  'timestamp',
773  'title',
774  'ids',
775  'sizes',
776  'redirect',
777  'patrolled',
778  'loginfo',
779  'tags',
780  'sha1',
781  ],
783  ],
784  'token' => [
786  ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
788  ],
789  'show' => [
790  ApiBase::PARAM_ISMULTI => true,
792  'minor',
793  '!minor',
794  'bot',
795  '!bot',
796  'anon',
797  '!anon',
798  'redirect',
799  '!redirect',
800  'patrolled',
801  '!patrolled',
802  'unpatrolled',
803  'autopatrolled',
804  '!autopatrolled',
805  ]
806  ],
807  'limit' => [
808  ApiBase::PARAM_DFLT => 10,
809  ApiBase::PARAM_TYPE => 'limit',
810  ApiBase::PARAM_MIN => 1,
813  ],
814  'type' => [
815  ApiBase::PARAM_DFLT => 'edit|new|log|categorize',
816  ApiBase::PARAM_ISMULTI => true,
818  ],
819  'toponly' => false,
820  'title' => null,
821  'continue' => [
822  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
823  ],
824  'generaterevisions' => false,
825  'slot' => [
826  ApiBase::PARAM_TYPE => $slotRoles
827  ],
828  ];
829  }
830 
831  protected function getExamplesMessages() {
832  return [
833  'action=query&list=recentchanges'
834  => 'apihelp-query+recentchanges-example-simple',
835  'action=query&generator=recentchanges&grcshow=!patrolled&prop=info'
836  => 'apihelp-query+recentchanges-example-generator',
837  ];
838  }
839 
840  public function getHelpUrls() {
841  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Recentchanges';
842  }
843 }
ApiQueryRecentChanges\run
run( $resultPageSet=null)
Generates and outputs the result of this query based upon the provided parameters.
Definition: ApiQueryRecentChanges.php:153
LIST_OR
const LIST_OR
Definition: Defines.php:46
ApiQueryRecentChanges\__construct
__construct(ApiQuery $query, $moduleName, CommentStore $commentStore)
Definition: ApiQueryRecentChanges.php:44
ApiQueryRecentChanges\$fld_comment
$fld_comment
Definition: ApiQueryRecentChanges.php:53
ChangeTags\makeTagSummarySubquery
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
Definition: ChangeTags.php:970
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:385
ApiQueryBase\processRow
processRow( $row, array &$data, array &$hookData)
Call the ApiQueryBaseProcessRow hook.
Definition: ApiQueryBase.php:449
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:212
ApiQueryRecentChanges\$fld_ids
$fld_ids
Definition: ApiQueryRecentChanges.php:54
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
ApiBase\addWarning
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition: ApiBase.php:1301
ApiQueryRecentChanges
A query action to enumerate the recent changes that were done to the wiki.
Definition: ApiQueryRecentChanges.php:34
ApiQueryRecentChanges\extractRowInfo
extractRowInfo( $row)
Extracts from a single sql row the data needed to describe one recent change.
Definition: ApiQueryRecentChanges.php:527
RC_EDIT
const RC_EDIT
Definition: Defines.php:115
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:180
if
if(ini_get( 'mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition: Setup.php:85
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
ApiQueryBase\addTimestampWhereRange
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
Definition: ApiQueryBase.php:366
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:105
true
return true
Definition: router.php:90
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1692
ApiQueryRecentChanges\getTokenFunctions
getTokenFunctions()
Get an array mapping token names to their handler functions.
Definition: ApiQueryRecentChanges.php:67
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
LogEventsList\userCanBitfield
static userCanBitfield( $bitfield, $field, Authority $performer)
Determine if the current user is allowed to view a particular field of this log row,...
Definition: LogEventsList.php:545
ApiQueryRecentChanges\$fld_sizes
$fld_sizes
Definition: ApiQueryRecentChanges.php:55
ApiQueryRecentChanges\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryRecentChanges.php:140
RecentChange\getChangeTypes
static getChangeTypes()
Get an array of all change types.
Definition: RecentChange.php:203
CommentStore
Handle database storage of comments such as edit summaries and log reasons.
Definition: CommentStore.php:42
ApiQueryRecentChanges\$fld_userid
$fld_userid
Definition: ApiQueryRecentChanges.php:53
User\useNPPatrol
useNPPatrol()
Check whether to enable new pages patrol features for this user.
Definition: User.php:3002
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:80
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:378
RecentChange\parseToRCType
static parseToRCType( $type)
Parsing text to RC_* constants.
Definition: RecentChange.php:170
RC_NEW
const RC_NEW
Definition: Defines.php:116
$res
$res
Definition: testCompression.php:57
User\useRCPatrol
useRCPatrol()
Check whether to enable recent changes patrol features for this user.
Definition: User.php:2993
ContextSource\getUser
getUser()
Definition: ContextSource.php:135
ApiBase\lacksSameOriginSecurity
lacksSameOriginSecurity()
Returns true if the current request breaks the same-origin policy.
Definition: ApiBase.php:502
ApiQueryRecentChanges\getPatrolToken
static getPatrolToken(User $user, $rc=null)
Definition: ApiQueryRecentChanges.php:92
ApiQueryBase\addFieldsIf
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
Definition: ApiQueryBase.php:222
ApiQueryRecentChanges\includesPatrollingFlags
includesPatrollingFlags(array $flagsArray)
Definition: ApiQueryRecentChanges.php:700
ApiQueryRecentChanges\$fld_redirect
$fld_redirect
Definition: ApiQueryRecentChanges.php:55
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:24
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
RC_LOG
const RC_LOG
Definition: Defines.php:117
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
Definition: ApiBase.php:77
ApiQueryRecentChanges\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryRecentChanges.php:728
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:75
ApiQueryBase\executeGenderCacheFromResultWrapper
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
Definition: ApiQueryBase.php:626
ApiQueryRecentChanges\$fld_flags
$fld_flags
Definition: ApiQueryRecentChanges.php:54
LogFormatter\newFromRow
static newFromRow( $row)
Handy shortcut for constructing a formatter directly from database row.
Definition: LogFormatter.php:74
ApiQueryRecentChanges\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryRecentChanges.php:144
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:163
ApiQueryRecentChanges\$fld_title
$fld_title
Definition: ApiQueryRecentChanges.php:54
ApiQueryRecentChanges\$fld_parsedcomment
$fld_parsedcomment
Definition: ApiQueryRecentChanges.php:53
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:117
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:73
RecentChange\newFromRow
static newFromRow( $row)
Definition: RecentChange.php:156
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:182
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:399
ApiQueryRecentChanges\$fld_user
$fld_user
Definition: ApiQueryRecentChanges.php:53
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:718
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:650
ApiBase\PARAM_EXTRA_NAMESPACES
const PARAM_EXTRA_NAMESPACES
Definition: ApiBase.php:82
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:39
ApiBase\getContinuationManager
getContinuationManager()
Definition: ApiBase.php:605
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:607
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:52
ApiQueryRecentChanges\$commentStore
CommentStore $commentStore
Definition: ApiQueryRecentChanges.php:37
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:890
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1565
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:143
ApiQueryRecentChanges\initProperties
initProperties( $prop)
Sets internal state to include the desired properties in the output.
Definition: ApiQueryRecentChanges.php:123
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:201
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1456
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
Definition: ApiQueryBase.php:282
RecentChange\PRC_AUTOPATROLLED
const PRC_AUTOPATROLLED
Definition: RecentChange.php:93
Linker\formatComment
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition: Linker.php:1208
ApiQueryGeneratorBase
Definition: ApiQueryGeneratorBase.php:28
ApiQueryRecentChanges\$fld_tags
$fld_tags
Definition: ApiQueryRecentChanges.php:56
ApiQueryRecentChanges\$fld_patrolled
$fld_patrolled
Definition: ApiQueryRecentChanges.php:55
ApiQueryRecentChanges\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryRecentChanges.php:840
ApiQueryRecentChanges\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryRecentChanges.php:831
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:165
ApiQueryRecentChanges\$fld_loginfo
$fld_loginfo
Definition: ApiQueryRecentChanges.php:55
RecentChange\parseFromRCType
static parseFromRCType( $rcType)
Parsing RC_* constants to human-readable test.
Definition: RecentChange.php:192
ApiQueryRecentChanges\$fld_timestamp
$fld_timestamp
Definition: ApiQueryRecentChanges.php:54
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:70
RecentChange\PRC_UNPATROLLED
const PRC_UNPATROLLED
Definition: RecentChange.php:91
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
MediaWiki\Storage\NameTableAccessException
Exception representing a failure to look up a row from a name table.
Definition: NameTableAccessException.php:33
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:74
LogPage\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: LogPage.php:42
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:245
ChangesList\isUnpatrolled
static isUnpatrolled( $rc, User $user)
Definition: ChangesList.php:878
$t
$t
Definition: testCompression.php:74
ApiQueryRecentChanges\$token
$token
Definition: ApiQueryRecentChanges.php:56
ApiBase\PARAM_HELP_MSG_PER_VALUE
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:138
User\getEditToken
getEditToken( $salt='', $request=null)
Initialize (if necessary) and return a session token value which can be used in edit forms to show th...
Definition: User.php:3665
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:67
ApiQueryBase\userCanSeeRevDel
userCanSeeRevDel()
Check whether the current user has permission to view revision-deleted fields.
Definition: ApiQueryBase.php:607
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:1577
ApiQueryRecentChanges\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryRecentChanges.php:708
ApiQueryBase\addWhereIf
addWhereIf( $value, $condition)
Same as addWhere(), but add the WHERE clauses only if a condition is met.
Definition: ApiQueryBase.php:263
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:466
$type
$type
Definition: testCompression.php:52
ApiQueryRecentChanges\$fld_sha1
$fld_sha1
Definition: ApiQueryRecentChanges.php:56
ApiQueryRecentChanges\$tokenFunctions
$tokenFunctions
Definition: ApiQueryRecentChanges.php:58