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 $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
40 $fld_flags = false, $fld_timestamp = false, $fld_title = false, $fld_ids = false,
41 $fld_sizes = false, $fld_redirect = false, $fld_patrolled = false, $fld_loginfo = false,
42 $fld_tags = false, $fld_sha1 = false, $token = [];
44 private $tokenFunctions;
47 * Get an array mapping token names to their handler functions.
48 * The prototype for a token function is func($pageid, $title, $rc)
49 * it should return a token or false (permission denied)
50 * @deprecated since 1.24
51 * @return array [ tokenname => function ]
53 protected function getTokenFunctions() {
54 // Don't call the hooks twice
55 if ( isset( $this->tokenFunctions
) ) {
56 return $this->tokenFunctions
;
59 // If we're in a mode that breaks the same-origin policy, no tokens can
61 if ( $this->lacksSameOriginSecurity() ) {
65 $this->tokenFunctions
= [
66 'patrol' => [ 'ApiQueryRecentChanges', 'getPatrolToken' ]
68 Hooks
::run( 'APIQueryRecentChangesTokens', [ &$this->tokenFunctions
] );
70 return $this->tokenFunctions
;
74 * @deprecated since 1.24
77 * @param RecentChange|null $rc
80 public static function getPatrolToken( $pageid, $title, $rc = null ) {
83 $validTokenUser = false;
86 if ( ( $wgUser->useRCPatrol() && $rc->getAttribute( 'rc_type' ) == RC_EDIT
) ||
87 ( $wgUser->useNPPatrol() && $rc->getAttribute( 'rc_type' ) == RC_NEW
)
89 $validTokenUser = true;
91 } elseif ( $wgUser->useRCPatrol() ||
$wgUser->useNPPatrol() ) {
92 $validTokenUser = true;
95 if ( $validTokenUser ) {
96 // The patrol token is always the same, let's exploit that
97 static $cachedPatrolToken = null;
99 if ( is_null( $cachedPatrolToken ) ) {
100 $cachedPatrolToken = $wgUser->getEditToken( 'patrol' );
103 return $cachedPatrolToken;
110 * Sets internal state to include the desired properties in the output.
111 * @param array $prop Associative array of properties, only keys are used here
113 public function initProperties( $prop ) {
114 $this->fld_comment
= isset( $prop['comment'] );
115 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
116 $this->fld_user
= isset( $prop['user'] );
117 $this->fld_userid
= isset( $prop['userid'] );
118 $this->fld_flags
= isset( $prop['flags'] );
119 $this->fld_timestamp
= isset( $prop['timestamp'] );
120 $this->fld_title
= isset( $prop['title'] );
121 $this->fld_ids
= isset( $prop['ids'] );
122 $this->fld_sizes
= isset( $prop['sizes'] );
123 $this->fld_redirect
= isset( $prop['redirect'] );
124 $this->fld_patrolled
= isset( $prop['patrolled'] );
125 $this->fld_loginfo
= isset( $prop['loginfo'] );
126 $this->fld_tags
= isset( $prop['tags'] );
127 $this->fld_sha1
= isset( $prop['sha1'] );
130 public function execute() {
134 public function executeGenerator( $resultPageSet ) {
135 $this->run( $resultPageSet );
139 * Generates and outputs the result of this query based upon the provided parameters.
141 * @param ApiPageSet $resultPageSet
143 public function run( $resultPageSet = null ) {
144 $user = $this->getUser();
145 /* Get the parameters of the request. */
146 $params = $this->extractRequestParams();
148 /* Build our basic query. Namely, something along the lines of:
149 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
150 * AND rc_timestamp < $end AND rc_namespace = $namespace
152 $this->addTables( 'recentchanges' );
153 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
155 if ( !is_null( $params['continue'] ) ) {
156 $cont = explode( '|', $params['continue'] );
157 $this->dieContinueUsageIf( count( $cont ) != 2 );
158 $db = $this->getDB();
159 $timestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
160 $id = intval( $cont[1] );
161 $this->dieContinueUsageIf( $id != $cont[1] );
162 $op = $params['dir'] === 'older' ?
'<' : '>';
164 "rc_timestamp $op $timestamp OR " .
165 "(rc_timestamp = $timestamp AND " .
170 $order = $params['dir'] === 'older' ?
'DESC' : 'ASC';
171 $this->addOption( 'ORDER BY', [
172 "rc_timestamp $order",
176 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
178 if ( !is_null( $params['type'] ) ) {
180 $this->addWhereFld( 'rc_type', RecentChange
::parseToRCType( $params['type'] ) );
181 } catch ( Exception
$e ) {
182 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
186 if ( !is_null( $params['show'] ) ) {
187 $show = array_flip( $params['show'] );
189 /* Check for conflicting parameters. */
190 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
191 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
192 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
193 ||
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
194 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
195 ||
( isset( $show['patrolled'] ) && isset( $show['unpatrolled'] ) )
196 ||
( isset( $show['!patrolled'] ) && isset( $show['unpatrolled'] ) )
198 $this->dieWithError( 'apierror-show' );
202 if ( isset( $show['patrolled'] )
203 ||
isset( $show['!patrolled'] )
204 ||
isset( $show['unpatrolled'] )
206 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
207 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
211 /* Add additional conditions to query depending upon parameters. */
212 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
213 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
214 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
215 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
216 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
217 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
218 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
219 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
220 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
222 if ( isset( $show['unpatrolled'] ) ) {
223 // See ChangesList::isUnpatrolled
224 if ( $user->useRCPatrol() ) {
225 $this->addWhere( 'rc_patrolled = 0' );
226 } elseif ( $user->useNPPatrol() ) {
227 $this->addWhere( 'rc_patrolled = 0' );
228 $this->addWhereFld( 'rc_type', RC_NEW
);
232 // Don't throw log entries out the window here
234 'page_is_redirect = 0 OR page_is_redirect IS NULL',
235 isset( $show['!redirect'] )
239 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
241 if ( !is_null( $params['user'] ) ) {
242 $this->addWhereFld( 'rc_user_text', $params['user'] );
245 if ( !is_null( $params['excludeuser'] ) ) {
246 // We don't use the rc_user_text index here because
247 // * it would require us to sort by rc_user_text before rc_timestamp
248 // * the != condition doesn't throw out too many rows anyway
249 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
252 /* Add the fields we're concerned with to our query. */
263 $showRedirects = false;
264 /* Determine what properties we need to display. */
265 if ( !is_null( $params['prop'] ) ) {
266 $prop = array_flip( $params['prop'] );
268 /* Set up internal members based upon params. */
269 $this->initProperties( $prop );
271 if ( $this->fld_patrolled
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
272 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
275 /* Add fields to our query if they are specified as a needed parameter. */
276 $this->addFieldsIf( [ 'rc_this_oldid', 'rc_last_oldid' ], $this->fld_ids
);
277 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
278 $this->addFieldsIf( 'rc_user', $this->fld_user ||
$this->fld_userid
);
279 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
280 $this->addFieldsIf( [ 'rc_minor', 'rc_type', 'rc_bot' ], $this->fld_flags
);
281 $this->addFieldsIf( [ 'rc_old_len', 'rc_new_len' ], $this->fld_sizes
);
282 $this->addFieldsIf( [ 'rc_patrolled', 'rc_log_type' ], $this->fld_patrolled
);
284 [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ],
287 $showRedirects = $this->fld_redirect ||
isset( $show['redirect'] )
288 ||
isset( $show['!redirect'] );
290 $this->addFieldsIf( [ 'rc_this_oldid' ],
291 $resultPageSet && $params['generaterevisions'] );
293 if ( $this->fld_tags
) {
294 $this->addTables( 'tag_summary' );
295 $this->addJoinConds( [ 'tag_summary' => [ 'LEFT JOIN', [ 'rc_id=ts_rc_id' ] ] ] );
296 $this->addFields( 'ts_tags' );
299 if ( $this->fld_sha1
) {
300 $this->addTables( 'revision' );
301 $this->addJoinConds( [ 'revision' => [ 'LEFT JOIN',
302 [ 'rc_this_oldid=rev_id' ] ] ] );
303 $this->addFields( [ 'rev_sha1', 'rev_deleted' ] );
306 if ( $params['toponly'] ||
$showRedirects ) {
307 $this->addTables( 'page' );
308 $this->addJoinConds( [ 'page' => [ 'LEFT JOIN',
309 [ 'rc_namespace=page_namespace', 'rc_title=page_title' ] ] ] );
310 $this->addFields( 'page_is_redirect' );
312 if ( $params['toponly'] ) {
313 $this->addWhere( 'rc_this_oldid = page_latest' );
317 if ( !is_null( $params['tag'] ) ) {
318 $this->addTables( 'change_tag' );
319 $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN', [ 'rc_id=ct_rc_id' ] ] ] );
320 $this->addWhereFld( 'ct_tag', $params['tag'] );
323 // Paranoia: avoid brute force searches (bug 17342)
324 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
325 if ( !$user->isAllowed( 'deletedhistory' ) ) {
326 $bitmask = Revision
::DELETED_USER
;
327 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
328 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
333 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
336 if ( $this->getRequest()->getCheck( 'namespace' ) ) {
337 // LogPage::DELETED_ACTION hides the affected page, too.
338 if ( !$user->isAllowed( 'deletedhistory' ) ) {
339 $bitmask = LogPage
::DELETED_ACTION
;
340 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
341 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
346 $this->addWhere( $this->getDB()->makeList( [
347 'rc_type != ' . RC_LOG
,
348 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
353 $this->token
= $params['token'];
354 $this->addOption( 'LIMIT', $params['limit'] +
1 );
358 /* Perform the actual query. */
359 $res = $this->select( __METHOD__
, [], $hookData );
364 $result = $this->getResult();
366 /* Iterate through the rows, adding data extracted from them to our query result. */
367 foreach ( $res as $row ) {
368 if ( $count === 0 && $resultPageSet !== null ) {
369 // Set the non-continue since the list of recentchanges is
370 // prone to having entries added at the start frequently.
371 $this->getContinuationManager()->addGeneratorNonContinueParam(
372 $this, 'continue', "$row->rc_timestamp|$row->rc_id"
375 if ( ++
$count > $params['limit'] ) {
376 // We've reached the one extra which shows that there are
377 // additional pages to be had. Stop here...
378 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
382 if ( is_null( $resultPageSet ) ) {
383 /* Extract the data from a single row. */
384 $vals = $this->extractRowInfo( $row );
386 /* Add that row's data to our final output. */
387 $fit = $this->processRow( $row, $vals, $hookData ) &&
388 $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
390 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
393 } elseif ( $params['generaterevisions'] ) {
394 $revid = (int)$row->rc_this_oldid
;
399 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
403 if ( is_null( $resultPageSet ) ) {
404 /* Format the result */
405 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'rc' );
406 } elseif ( $params['generaterevisions'] ) {
407 $resultPageSet->populateFromRevisionIDs( $revids );
409 $resultPageSet->populateFromTitles( $titles );
414 * Extracts from a single sql row the data needed to describe one recent change.
416 * @param stdClass $row The row from which to extract the data.
417 * @return array An array mapping strings (descriptors) to their respective string values.
420 public function extractRowInfo( $row ) {
421 /* Determine the title of the page that has been changed. */
422 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
423 $user = $this->getUser();
425 /* Our output data. */
428 $type = intval( $row->rc_type
);
429 $vals['type'] = RecentChange
::parseFromRCType( $type );
433 /* Create a new entry in the result for the title. */
434 if ( $this->fld_title ||
$this->fld_ids
) {
435 if ( $type === RC_LOG
&& ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) ) {
436 $vals['actionhidden'] = true;
439 if ( $type !== RC_LOG ||
440 LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user )
442 if ( $this->fld_title
) {
443 ApiQueryBase
::addTitleInfo( $vals, $title );
445 if ( $this->fld_ids
) {
446 $vals['pageid'] = intval( $row->rc_cur_id
);
447 $vals['revid'] = intval( $row->rc_this_oldid
);
448 $vals['old_revid'] = intval( $row->rc_last_oldid
);
453 if ( $this->fld_ids
) {
454 $vals['rcid'] = intval( $row->rc_id
);
457 /* Add user data and 'anon' flag, if user is anonymous. */
458 if ( $this->fld_user ||
$this->fld_userid
) {
459 if ( $row->rc_deleted
& Revision
::DELETED_USER
) {
460 $vals['userhidden'] = true;
463 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_USER
, $user ) ) {
464 if ( $this->fld_user
) {
465 $vals['user'] = $row->rc_user_text
;
468 if ( $this->fld_userid
) {
469 $vals['userid'] = (int)$row->rc_user
;
472 if ( !$row->rc_user
) {
473 $vals['anon'] = true;
478 /* Add flags, such as new, minor, bot. */
479 if ( $this->fld_flags
) {
480 $vals['bot'] = (bool)$row->rc_bot
;
481 $vals['new'] = $row->rc_type
== RC_NEW
;
482 $vals['minor'] = (bool)$row->rc_minor
;
485 /* Add sizes of each revision. (Only available on 1.10+) */
486 if ( $this->fld_sizes
) {
487 $vals['oldlen'] = intval( $row->rc_old_len
);
488 $vals['newlen'] = intval( $row->rc_new_len
);
491 /* Add the timestamp. */
492 if ( $this->fld_timestamp
) {
493 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
496 /* Add edit summary / log summary. */
497 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
498 if ( $row->rc_deleted
& Revision
::DELETED_COMMENT
) {
499 $vals['commenthidden'] = true;
502 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
503 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
504 $vals['comment'] = $row->rc_comment
;
507 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
508 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
513 if ( $this->fld_redirect
) {
514 $vals['redirect'] = (bool)$row->page_is_redirect
;
517 /* Add the patrolled flag */
518 if ( $this->fld_patrolled
) {
519 $vals['patrolled'] = $row->rc_patrolled
== 1;
520 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( $row, $user );
523 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
524 if ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) {
525 $vals['actionhidden'] = true;
528 if ( LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user ) ) {
529 $vals['logid'] = intval( $row->rc_logid
);
530 $vals['logtype'] = $row->rc_log_type
;
531 $vals['logaction'] = $row->rc_log_action
;
532 $vals['logparams'] = LogFormatter
::newFromRow( $row )->formatParametersForApi();
536 if ( $this->fld_tags
) {
537 if ( $row->ts_tags
) {
538 $tags = explode( ',', $row->ts_tags
);
539 ApiResult
::setIndexedTagName( $tags, 'tag' );
540 $vals['tags'] = $tags;
546 if ( $this->fld_sha1
&& $row->rev_sha1
!== null ) {
547 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
548 $vals['sha1hidden'] = true;
551 if ( Revision
::userCanBitfield( $row->rev_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
552 if ( $row->rev_sha1
!== '' ) {
553 $vals['sha1'] = Wikimedia\base_convert
( $row->rev_sha1
, 36, 16, 40 );
560 if ( !is_null( $this->token
) ) {
561 $tokenFunctions = $this->getTokenFunctions();
562 foreach ( $this->token
as $t ) {
563 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
564 $title, RecentChange
::newFromRow( $row ) );
565 if ( $val === false ) {
566 $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
568 $vals[$t . 'token'] = $val;
573 if ( $anyHidden && ( $row->rc_deleted
& Revision
::DELETED_RESTRICTED
) ) {
574 $vals['suppressed'] = true;
580 public function getCacheMode( $params ) {
581 if ( isset( $params['show'] ) ) {
582 foreach ( $params['show'] as $show ) {
583 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
588 if ( isset( $params['token'] ) ) {
591 if ( $this->userCanSeeRevDel() ) {
594 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
595 // formatComment() calls wfMessage() among other things
596 return 'anon-public-user-private';
602 public function getAllowedParams() {
605 ApiBase
::PARAM_TYPE
=> 'timestamp'
608 ApiBase
::PARAM_TYPE
=> 'timestamp'
611 ApiBase
::PARAM_DFLT
=> 'older',
612 ApiBase
::PARAM_TYPE
=> [
616 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
619 ApiBase
::PARAM_ISMULTI
=> true,
620 ApiBase
::PARAM_TYPE
=> 'namespace',
621 ApiBase
::PARAM_EXTRA_NAMESPACES
=> [ NS_MEDIA
, NS_SPECIAL
],
624 ApiBase
::PARAM_TYPE
=> 'user'
627 ApiBase
::PARAM_TYPE
=> 'user'
631 ApiBase
::PARAM_ISMULTI
=> true,
632 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
633 ApiBase
::PARAM_TYPE
=> [
649 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
652 ApiBase
::PARAM_DEPRECATED
=> true,
653 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
654 ApiBase
::PARAM_ISMULTI
=> true
657 ApiBase
::PARAM_ISMULTI
=> true,
658 ApiBase
::PARAM_TYPE
=> [
673 ApiBase
::PARAM_DFLT
=> 10,
674 ApiBase
::PARAM_TYPE
=> 'limit',
675 ApiBase
::PARAM_MIN
=> 1,
676 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
677 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
680 ApiBase
::PARAM_DFLT
=> 'edit|new|log|categorize',
681 ApiBase
::PARAM_ISMULTI
=> true,
682 ApiBase
::PARAM_TYPE
=> RecentChange
::getChangeTypes()
686 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
688 'generaterevisions' => false,
692 protected function getExamplesMessages() {
694 'action=query&list=recentchanges'
695 => 'apihelp-query+recentchanges-example-simple',
696 'action=query&generator=recentchanges&grcshow=!patrolled&prop=info'
697 => 'apihelp-query+recentchanges-example-generator',
701 public function getHelpUrls() {
702 return 'https://www.mediawiki.org/wiki/API:Recentchanges';