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