5 * Created on Oct 19, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * A query action to enumerate the recent changes that were done to the wiki.
29 * Various filters are supported.
33 class ApiQueryRecentChanges
extends ApiQueryGeneratorBase
{
35 public function __construct( ApiQuery
$query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'rc' );
39 private $commentStore;
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 = [];
46 private $tokenFunctions;
49 * Get an array mapping token names to their handler functions.
50 * The prototype for a token function is func($pageid, $title, $rc)
51 * it should return a token or false (permission denied)
52 * @deprecated since 1.24
53 * @return array [ tokenname => function ]
55 protected function getTokenFunctions() {
56 // Don't call the hooks twice
57 if ( isset( $this->tokenFunctions
) ) {
58 return $this->tokenFunctions
;
61 // If we're in a mode that breaks the same-origin policy, no tokens can
63 if ( $this->lacksSameOriginSecurity() ) {
67 $this->tokenFunctions
= [
68 'patrol' => [ 'ApiQueryRecentChanges', 'getPatrolToken' ]
70 Hooks
::run( 'APIQueryRecentChangesTokens', [ &$this->tokenFunctions
] );
72 return $this->tokenFunctions
;
76 * @deprecated since 1.24
79 * @param RecentChange|null $rc
82 public static function getPatrolToken( $pageid, $title, $rc = null ) {
85 $validTokenUser = false;
88 if ( ( $wgUser->useRCPatrol() && $rc->getAttribute( 'rc_type' ) == RC_EDIT
) ||
89 ( $wgUser->useNPPatrol() && $rc->getAttribute( 'rc_type' ) == RC_NEW
)
91 $validTokenUser = true;
93 } elseif ( $wgUser->useRCPatrol() ||
$wgUser->useNPPatrol() ) {
94 $validTokenUser = true;
97 if ( $validTokenUser ) {
98 // The patrol token is always the same, let's exploit that
99 static $cachedPatrolToken = null;
101 if ( is_null( $cachedPatrolToken ) ) {
102 $cachedPatrolToken = $wgUser->getEditToken( 'patrol' );
105 return $cachedPatrolToken;
112 * Sets internal state to include the desired properties in the output.
113 * @param array $prop Associative array of properties, only keys are used here
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'] );
132 public function execute() {
136 public function executeGenerator( $resultPageSet ) {
137 $this->run( $resultPageSet );
141 * Generates and outputs the result of this query based upon the provided parameters.
143 * @param ApiPageSet $resultPageSet
145 public function run( $resultPageSet = null ) {
146 $user = $this->getUser();
147 /* Get the parameters of the request. */
148 $params = $this->extractRequestParams();
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
154 $this->addTables( 'recentchanges' );
155 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
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 = intval( $cont[1] );
163 $this->dieContinueUsageIf( $id != $cont[1] );
164 $op = $params['dir'] === 'older' ?
'<' : '>';
166 "rc_timestamp $op $timestamp OR " .
167 "(rc_timestamp = $timestamp AND " .
172 $order = $params['dir'] === 'older' ?
'DESC' : 'ASC';
173 $this->addOption( 'ORDER BY', [
174 "rc_timestamp $order",
178 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
180 if ( !is_null( $params['type'] ) ) {
182 $this->addWhereFld( 'rc_type', RecentChange
::parseToRCType( $params['type'] ) );
183 } catch ( Exception
$e ) {
184 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
188 if ( !is_null( $params['show'] ) ) {
189 $show = array_flip( $params['show'] );
191 /* Check for conflicting parameters. */
192 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
193 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
194 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
195 ||
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
196 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
197 ||
( isset( $show['patrolled'] ) && isset( $show['unpatrolled'] ) )
198 ||
( isset( $show['!patrolled'] ) && isset( $show['unpatrolled'] ) )
200 $this->dieWithError( 'apierror-show' );
204 if ( isset( $show['patrolled'] )
205 ||
isset( $show['!patrolled'] )
206 ||
isset( $show['unpatrolled'] )
208 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
209 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
213 /* Add additional conditions to query depending upon parameters. */
214 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
215 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
216 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
217 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
218 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
219 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
220 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
221 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
222 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
224 if ( isset( $show['unpatrolled'] ) ) {
225 // See ChangesList::isUnpatrolled
226 if ( $user->useRCPatrol() ) {
227 $this->addWhere( 'rc_patrolled = 0' );
228 } elseif ( $user->useNPPatrol() ) {
229 $this->addWhere( 'rc_patrolled = 0' );
230 $this->addWhereFld( 'rc_type', RC_NEW
);
234 // Don't throw log entries out the window here
236 'page_is_redirect = 0 OR page_is_redirect IS NULL',
237 isset( $show['!redirect'] )
241 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
243 if ( !is_null( $params['user'] ) ) {
244 $this->addWhereFld( 'rc_user_text', $params['user'] );
247 if ( !is_null( $params['excludeuser'] ) ) {
248 // We don't use the rc_user_text index here because
249 // * it would require us to sort by rc_user_text before rc_timestamp
250 // * the != condition doesn't throw out too many rows anyway
251 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
254 /* Add the fields we're concerned with to our query. */
265 $showRedirects = false;
266 /* Determine what properties we need to display. */
267 if ( !is_null( $params['prop'] ) ) {
268 $prop = array_flip( $params['prop'] );
270 /* Set up internal members based upon params. */
271 $this->initProperties( $prop );
273 if ( $this->fld_patrolled
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
274 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
277 /* Add fields to our query if they are specified as a needed parameter. */
278 $this->addFieldsIf( [ 'rc_this_oldid', 'rc_last_oldid' ], $this->fld_ids
);
279 $this->addFieldsIf( 'rc_user', $this->fld_user ||
$this->fld_userid
);
280 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
281 $this->addFieldsIf( [ 'rc_minor', 'rc_type', 'rc_bot' ], $this->fld_flags
);
282 $this->addFieldsIf( [ 'rc_old_len', 'rc_new_len' ], $this->fld_sizes
);
283 $this->addFieldsIf( [ 'rc_patrolled', 'rc_log_type' ], $this->fld_patrolled
);
285 [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ],
288 $showRedirects = $this->fld_redirect ||
isset( $show['redirect'] )
289 ||
isset( $show['!redirect'] );
291 $this->addFieldsIf( [ 'rc_this_oldid' ],
292 $resultPageSet && $params['generaterevisions'] );
294 if ( $this->fld_tags
) {
295 $this->addTables( 'tag_summary' );
296 $this->addJoinConds( [ 'tag_summary' => [ 'LEFT JOIN', [ 'rc_id=ts_rc_id' ] ] ] );
297 $this->addFields( 'ts_tags' );
300 if ( $this->fld_sha1
) {
301 $this->addTables( 'revision' );
302 $this->addJoinConds( [ 'revision' => [ 'LEFT JOIN',
303 [ 'rc_this_oldid=rev_id' ] ] ] );
304 $this->addFields( [ 'rev_sha1', 'rev_deleted' ] );
307 if ( $params['toponly'] ||
$showRedirects ) {
308 $this->addTables( 'page' );
309 $this->addJoinConds( [ 'page' => [ 'LEFT JOIN',
310 [ 'rc_namespace=page_namespace', 'rc_title=page_title' ] ] ] );
311 $this->addFields( 'page_is_redirect' );
313 if ( $params['toponly'] ) {
314 $this->addWhere( 'rc_this_oldid = page_latest' );
318 if ( !is_null( $params['tag'] ) ) {
319 $this->addTables( 'change_tag' );
320 $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN', [ 'rc_id=ct_rc_id' ] ] ] );
321 $this->addWhereFld( 'ct_tag', $params['tag'] );
324 // Paranoia: avoid brute force searches (T19342)
325 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
326 if ( !$user->isAllowed( 'deletedhistory' ) ) {
327 $bitmask = Revision
::DELETED_USER
;
328 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
329 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
334 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
337 if ( $this->getRequest()->getCheck( 'namespace' ) ) {
338 // LogPage::DELETED_ACTION hides the affected page, too.
339 if ( !$user->isAllowed( 'deletedhistory' ) ) {
340 $bitmask = LogPage
::DELETED_ACTION
;
341 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
342 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
347 $this->addWhere( $this->getDB()->makeList( [
348 'rc_type != ' . RC_LOG
,
349 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
354 $this->token
= $params['token'];
356 if ( $this->fld_comment ||
$this->fld_parsedcomment ||
$this->token
) {
357 $this->commentStore
= new CommentStore( 'rc_comment' );
358 $commentQuery = $this->commentStore
->getJoin();
359 $this->addTables( $commentQuery['tables'] );
360 $this->addFields( $commentQuery['fields'] );
361 $this->addJoinConds( $commentQuery['joins'] );
364 $this->addOption( 'LIMIT', $params['limit'] +
1 );
368 /* Perform the actual query. */
369 $res = $this->select( __METHOD__
, [], $hookData );
374 $result = $this->getResult();
376 /* Iterate through the rows, adding data extracted from them to our query result. */
377 foreach ( $res as $row ) {
378 if ( $count === 0 && $resultPageSet !== null ) {
379 // Set the non-continue since the list of recentchanges is
380 // prone to having entries added at the start frequently.
381 $this->getContinuationManager()->addGeneratorNonContinueParam(
382 $this, 'continue', "$row->rc_timestamp|$row->rc_id"
385 if ( ++
$count > $params['limit'] ) {
386 // We've reached the one extra which shows that there are
387 // additional pages to be had. Stop here...
388 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
392 if ( is_null( $resultPageSet ) ) {
393 /* Extract the data from a single row. */
394 $vals = $this->extractRowInfo( $row );
396 /* Add that row's data to our final output. */
397 $fit = $this->processRow( $row, $vals, $hookData ) &&
398 $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
400 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
403 } elseif ( $params['generaterevisions'] ) {
404 $revid = (int)$row->rc_this_oldid
;
409 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
413 if ( is_null( $resultPageSet ) ) {
414 /* Format the result */
415 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'rc' );
416 } elseif ( $params['generaterevisions'] ) {
417 $resultPageSet->populateFromRevisionIDs( $revids );
419 $resultPageSet->populateFromTitles( $titles );
424 * Extracts from a single sql row the data needed to describe one recent change.
426 * @param stdClass $row The row from which to extract the data.
427 * @return array An array mapping strings (descriptors) to their respective string values.
430 public function extractRowInfo( $row ) {
431 /* Determine the title of the page that has been changed. */
432 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
433 $user = $this->getUser();
435 /* Our output data. */
438 $type = intval( $row->rc_type
);
439 $vals['type'] = RecentChange
::parseFromRCType( $type );
443 /* Create a new entry in the result for the title. */
444 if ( $this->fld_title ||
$this->fld_ids
) {
445 if ( $type === RC_LOG
&& ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) ) {
446 $vals['actionhidden'] = true;
449 if ( $type !== RC_LOG ||
450 LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user )
452 if ( $this->fld_title
) {
453 ApiQueryBase
::addTitleInfo( $vals, $title );
455 if ( $this->fld_ids
) {
456 $vals['pageid'] = intval( $row->rc_cur_id
);
457 $vals['revid'] = intval( $row->rc_this_oldid
);
458 $vals['old_revid'] = intval( $row->rc_last_oldid
);
463 if ( $this->fld_ids
) {
464 $vals['rcid'] = intval( $row->rc_id
);
467 /* Add user data and 'anon' flag, if user is anonymous. */
468 if ( $this->fld_user ||
$this->fld_userid
) {
469 if ( $row->rc_deleted
& Revision
::DELETED_USER
) {
470 $vals['userhidden'] = true;
473 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_USER
, $user ) ) {
474 if ( $this->fld_user
) {
475 $vals['user'] = $row->rc_user_text
;
478 if ( $this->fld_userid
) {
479 $vals['userid'] = (int)$row->rc_user
;
482 if ( !$row->rc_user
) {
483 $vals['anon'] = true;
488 /* Add flags, such as new, minor, bot. */
489 if ( $this->fld_flags
) {
490 $vals['bot'] = (bool)$row->rc_bot
;
491 $vals['new'] = $row->rc_type
== RC_NEW
;
492 $vals['minor'] = (bool)$row->rc_minor
;
495 /* Add sizes of each revision. (Only available on 1.10+) */
496 if ( $this->fld_sizes
) {
497 $vals['oldlen'] = intval( $row->rc_old_len
);
498 $vals['newlen'] = intval( $row->rc_new_len
);
501 /* Add the timestamp. */
502 if ( $this->fld_timestamp
) {
503 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
506 /* Add edit summary / log summary. */
507 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
508 if ( $row->rc_deleted
& Revision
::DELETED_COMMENT
) {
509 $vals['commenthidden'] = true;
512 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
513 $comment = $this->commentStore
->getComment( $row )->text
;
514 if ( $this->fld_comment
) {
515 $vals['comment'] = $comment;
518 if ( $this->fld_parsedcomment
) {
519 $vals['parsedcomment'] = Linker
::formatComment( $comment, $title );
524 if ( $this->fld_redirect
) {
525 $vals['redirect'] = (bool)$row->page_is_redirect
;
528 /* Add the patrolled flag */
529 if ( $this->fld_patrolled
) {
530 $vals['patrolled'] = $row->rc_patrolled
== 1;
531 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( $row, $user );
534 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
535 if ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) {
536 $vals['actionhidden'] = true;
539 if ( LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user ) ) {
540 $vals['logid'] = intval( $row->rc_logid
);
541 $vals['logtype'] = $row->rc_log_type
;
542 $vals['logaction'] = $row->rc_log_action
;
543 $vals['logparams'] = LogFormatter
::newFromRow( $row )->formatParametersForApi();
547 if ( $this->fld_tags
) {
548 if ( $row->ts_tags
) {
549 $tags = explode( ',', $row->ts_tags
);
550 ApiResult
::setIndexedTagName( $tags, 'tag' );
551 $vals['tags'] = $tags;
557 if ( $this->fld_sha1
&& $row->rev_sha1
!== null ) {
558 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
559 $vals['sha1hidden'] = true;
562 if ( Revision
::userCanBitfield( $row->rev_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
563 if ( $row->rev_sha1
!== '' ) {
564 $vals['sha1'] = Wikimedia\base_convert
( $row->rev_sha1
, 36, 16, 40 );
571 if ( !is_null( $this->token
) ) {
572 $tokenFunctions = $this->getTokenFunctions();
573 foreach ( $this->token
as $t ) {
574 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
575 $title, RecentChange
::newFromRow( $row ) );
576 if ( $val === false ) {
577 $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
579 $vals[$t . 'token'] = $val;
584 if ( $anyHidden && ( $row->rc_deleted
& Revision
::DELETED_RESTRICTED
) ) {
585 $vals['suppressed'] = true;
591 public function getCacheMode( $params ) {
592 if ( isset( $params['show'] ) ) {
593 foreach ( $params['show'] as $show ) {
594 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
599 if ( isset( $params['token'] ) ) {
602 if ( $this->userCanSeeRevDel() ) {
605 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
606 // formatComment() calls wfMessage() among other things
607 return 'anon-public-user-private';
613 public function getAllowedParams() {
616 ApiBase
::PARAM_TYPE
=> 'timestamp'
619 ApiBase
::PARAM_TYPE
=> 'timestamp'
622 ApiBase
::PARAM_DFLT
=> 'older',
623 ApiBase
::PARAM_TYPE
=> [
627 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
630 ApiBase
::PARAM_ISMULTI
=> true,
631 ApiBase
::PARAM_TYPE
=> 'namespace',
632 ApiBase
::PARAM_EXTRA_NAMESPACES
=> [ NS_MEDIA
, NS_SPECIAL
],
635 ApiBase
::PARAM_TYPE
=> 'user'
638 ApiBase
::PARAM_TYPE
=> 'user'
642 ApiBase
::PARAM_ISMULTI
=> true,
643 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
644 ApiBase
::PARAM_TYPE
=> [
660 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
663 ApiBase
::PARAM_DEPRECATED
=> true,
664 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
665 ApiBase
::PARAM_ISMULTI
=> true
668 ApiBase
::PARAM_ISMULTI
=> true,
669 ApiBase
::PARAM_TYPE
=> [
684 ApiBase
::PARAM_DFLT
=> 10,
685 ApiBase
::PARAM_TYPE
=> 'limit',
686 ApiBase
::PARAM_MIN
=> 1,
687 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
688 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
691 ApiBase
::PARAM_DFLT
=> 'edit|new|log|categorize',
692 ApiBase
::PARAM_ISMULTI
=> true,
693 ApiBase
::PARAM_TYPE
=> RecentChange
::getChangeTypes()
697 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
699 'generaterevisions' => false,
703 protected function getExamplesMessages() {
705 'action=query&list=recentchanges'
706 => 'apihelp-query+recentchanges-example-simple',
707 'action=query&generator=recentchanges&grcshow=!patrolled&prop=info'
708 => 'apihelp-query+recentchanges-example-generator',
712 public function getHelpUrls() {
713 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Recentchanges';