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