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 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->dieUsageMsg( 'show' );
202 if ( isset( $show['patrolled'] )
203 ||
isset( $show['!patrolled'] )
204 ||
isset( $show['unpatrolled'] )
206 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
208 'You need patrol or patrolmarks permission to request the patrolled flag',
214 /* Add additional conditions to query depending upon parameters. */
215 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
216 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
217 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
218 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
219 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
220 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
221 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
222 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
223 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
225 if ( isset( $show['unpatrolled'] ) ) {
226 // See ChangesList::isUnpatrolled
227 if ( $user->useRCPatrol() ) {
228 $this->addWhere( 'rc_patrolled = 0' );
229 } elseif ( $user->useNPPatrol() ) {
230 $this->addWhere( 'rc_patrolled = 0' );
231 $this->addWhereFld( 'rc_type', RC_NEW
);
235 // Don't throw log entries out the window here
237 'page_is_redirect = 0 OR page_is_redirect IS NULL',
238 isset( $show['!redirect'] )
242 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
243 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
246 if ( !is_null( $params['user'] ) ) {
247 $this->addWhereFld( 'rc_user_text', $params['user'] );
250 if ( !is_null( $params['excludeuser'] ) ) {
251 // We don't use the rc_user_text index here because
252 // * it would require us to sort by rc_user_text before rc_timestamp
253 // * the != condition doesn't throw out too many rows anyway
254 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
257 /* Add the fields we're concerned with to our query. */
268 $showRedirects = false;
269 /* Determine what properties we need to display. */
270 if ( !is_null( $params['prop'] ) ) {
271 $prop = array_flip( $params['prop'] );
273 /* Set up internal members based upon params. */
274 $this->initProperties( $prop );
276 if ( $this->fld_patrolled
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
278 'You need patrol or patrolmarks permission to request the patrolled flag',
283 /* Add fields to our query if they are specified as a needed parameter. */
284 $this->addFieldsIf( [ 'rc_this_oldid', 'rc_last_oldid' ], $this->fld_ids
);
285 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
286 $this->addFieldsIf( 'rc_user', $this->fld_user ||
$this->fld_userid
);
287 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
288 $this->addFieldsIf( [ 'rc_minor', 'rc_type', 'rc_bot' ], $this->fld_flags
);
289 $this->addFieldsIf( [ 'rc_old_len', 'rc_new_len' ], $this->fld_sizes
);
290 $this->addFieldsIf( [ 'rc_patrolled', 'rc_log_type' ], $this->fld_patrolled
);
292 [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ],
295 $showRedirects = $this->fld_redirect ||
isset( $show['redirect'] )
296 ||
isset( $show['!redirect'] );
298 $this->addFieldsIf( [ 'rc_this_oldid' ],
299 $resultPageSet && $params['generaterevisions'] );
301 if ( $this->fld_tags
) {
302 $this->addTables( 'tag_summary' );
303 $this->addJoinConds( [ 'tag_summary' => [ 'LEFT JOIN', [ 'rc_id=ts_rc_id' ] ] ] );
304 $this->addFields( 'ts_tags' );
307 if ( $this->fld_sha1
) {
308 $this->addTables( 'revision' );
309 $this->addJoinConds( [ 'revision' => [ 'LEFT JOIN',
310 [ 'rc_this_oldid=rev_id' ] ] ] );
311 $this->addFields( [ 'rev_sha1', 'rev_deleted' ] );
314 if ( $params['toponly'] ||
$showRedirects ) {
315 $this->addTables( 'page' );
316 $this->addJoinConds( [ 'page' => [ 'LEFT JOIN',
317 [ 'rc_namespace=page_namespace', 'rc_title=page_title' ] ] ] );
318 $this->addFields( 'page_is_redirect' );
320 if ( $params['toponly'] ) {
321 $this->addWhere( 'rc_this_oldid = page_latest' );
325 if ( !is_null( $params['tag'] ) ) {
326 $this->addTables( 'change_tag' );
327 $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN', [ 'rc_id=ct_rc_id' ] ] ] );
328 $this->addWhereFld( 'ct_tag', $params['tag'] );
331 // Paranoia: avoid brute force searches (bug 17342)
332 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
333 if ( !$user->isAllowed( 'deletedhistory' ) ) {
334 $bitmask = Revision
::DELETED_USER
;
335 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
336 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
341 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
344 if ( $this->getRequest()->getCheck( 'namespace' ) ) {
345 // LogPage::DELETED_ACTION hides the affected page, too.
346 if ( !$user->isAllowed( 'deletedhistory' ) ) {
347 $bitmask = LogPage
::DELETED_ACTION
;
348 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
349 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
354 $this->addWhere( $this->getDB()->makeList( [
355 'rc_type != ' . RC_LOG
,
356 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
361 $this->token
= $params['token'];
362 $this->addOption( 'LIMIT', $params['limit'] +
1 );
365 /* Perform the actual query. */
366 $res = $this->select( __METHOD__
);
371 $result = $this->getResult();
373 /* Iterate through the rows, adding data extracted from them to our query result. */
374 foreach ( $res as $row ) {
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 = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
389 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
392 } elseif ( $params['generaterevisions'] ) {
393 $revid = (int)$row->rc_this_oldid
;
398 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
402 if ( is_null( $resultPageSet ) ) {
403 /* Format the result */
404 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'rc' );
405 } elseif ( $params['generaterevisions'] ) {
406 $resultPageSet->populateFromRevisionIDs( $revids );
408 $resultPageSet->populateFromTitles( $titles );
413 * Extracts from a single sql row the data needed to describe one recent change.
415 * @param stdClass $row The row from which to extract the data.
416 * @return array An array mapping strings (descriptors) to their respective string values.
419 public function extractRowInfo( $row ) {
420 /* Determine the title of the page that has been changed. */
421 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
422 $user = $this->getUser();
424 /* Our output data. */
427 $type = intval( $row->rc_type
);
428 $vals['type'] = RecentChange
::parseFromRCType( $type );
432 /* Create a new entry in the result for the title. */
433 if ( $this->fld_title ||
$this->fld_ids
) {
434 if ( $type === RC_LOG
&& ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) ) {
435 $vals['actionhidden'] = true;
438 if ( $type !== RC_LOG ||
439 LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user )
441 if ( $this->fld_title
) {
442 ApiQueryBase
::addTitleInfo( $vals, $title );
444 if ( $this->fld_ids
) {
445 $vals['pageid'] = intval( $row->rc_cur_id
);
446 $vals['revid'] = intval( $row->rc_this_oldid
);
447 $vals['old_revid'] = intval( $row->rc_last_oldid
);
452 if ( $this->fld_ids
) {
453 $vals['rcid'] = intval( $row->rc_id
);
456 /* Add user data and 'anon' flag, if user is anonymous. */
457 if ( $this->fld_user ||
$this->fld_userid
) {
458 if ( $row->rc_deleted
& Revision
::DELETED_USER
) {
459 $vals['userhidden'] = true;
462 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_USER
, $user ) ) {
463 if ( $this->fld_user
) {
464 $vals['user'] = $row->rc_user_text
;
467 if ( $this->fld_userid
) {
468 $vals['userid'] = (int)$row->rc_user
;
471 if ( !$row->rc_user
) {
472 $vals['anon'] = true;
477 /* Add flags, such as new, minor, bot. */
478 if ( $this->fld_flags
) {
479 $vals['bot'] = (bool)$row->rc_bot
;
480 $vals['new'] = $row->rc_type
== RC_NEW
;
481 $vals['minor'] = (bool)$row->rc_minor
;
484 /* Add sizes of each revision. (Only available on 1.10+) */
485 if ( $this->fld_sizes
) {
486 $vals['oldlen'] = intval( $row->rc_old_len
);
487 $vals['newlen'] = intval( $row->rc_new_len
);
490 /* Add the timestamp. */
491 if ( $this->fld_timestamp
) {
492 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
495 /* Add edit summary / log summary. */
496 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
497 if ( $row->rc_deleted
& Revision
::DELETED_COMMENT
) {
498 $vals['commenthidden'] = true;
501 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
502 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
503 $vals['comment'] = $row->rc_comment
;
506 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
507 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
512 if ( $this->fld_redirect
) {
513 $vals['redirect'] = (bool)$row->page_is_redirect
;
516 /* Add the patrolled flag */
517 if ( $this->fld_patrolled
) {
518 $vals['patrolled'] = $row->rc_patrolled
== 1;
519 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( $row, $user );
522 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
523 if ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) {
524 $vals['actionhidden'] = true;
527 if ( LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user ) ) {
528 $vals['logid'] = intval( $row->rc_logid
);
529 $vals['logtype'] = $row->rc_log_type
;
530 $vals['logaction'] = $row->rc_log_action
;
531 $vals['logparams'] = LogFormatter
::newFromRow( $row )->formatParametersForApi();
535 if ( $this->fld_tags
) {
536 if ( $row->ts_tags
) {
537 $tags = explode( ',', $row->ts_tags
);
538 ApiResult
::setIndexedTagName( $tags, 'tag' );
539 $vals['tags'] = $tags;
545 if ( $this->fld_sha1
&& $row->rev_sha1
!== null ) {
546 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
547 $vals['sha1hidden'] = true;
550 if ( Revision
::userCanBitfield( $row->rev_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
551 if ( $row->rev_sha1
!== '' ) {
552 $vals['sha1'] = Wikimedia\base_convert
( $row->rev_sha1
, 36, 16, 40 );
559 if ( !is_null( $this->token
) ) {
560 $tokenFunctions = $this->getTokenFunctions();
561 foreach ( $this->token
as $t ) {
562 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
563 $title, RecentChange
::newFromRow( $row ) );
564 if ( $val === false ) {
565 $this->setWarning( "Action '$t' is not allowed for the current user" );
567 $vals[$t . 'token'] = $val;
572 if ( $anyHidden && ( $row->rc_deleted
& Revision
::DELETED_RESTRICTED
) ) {
573 $vals['suppressed'] = true;
579 public function getCacheMode( $params ) {
580 if ( isset( $params['show'] ) ) {
581 foreach ( $params['show'] as $show ) {
582 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
587 if ( isset( $params['token'] ) ) {
590 if ( $this->userCanSeeRevDel() ) {
593 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
594 // formatComment() calls wfMessage() among other things
595 return 'anon-public-user-private';
601 public function getAllowedParams() {
604 ApiBase
::PARAM_TYPE
=> 'timestamp'
607 ApiBase
::PARAM_TYPE
=> 'timestamp'
610 ApiBase
::PARAM_DFLT
=> 'older',
611 ApiBase
::PARAM_TYPE
=> [
615 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
618 ApiBase
::PARAM_ISMULTI
=> true,
619 ApiBase
::PARAM_TYPE
=> 'namespace'
622 ApiBase
::PARAM_TYPE
=> 'user'
625 ApiBase
::PARAM_TYPE
=> 'user'
629 ApiBase
::PARAM_ISMULTI
=> true,
630 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
631 ApiBase
::PARAM_TYPE
=> [
647 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
650 ApiBase
::PARAM_DEPRECATED
=> true,
651 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
652 ApiBase
::PARAM_ISMULTI
=> true
655 ApiBase
::PARAM_ISMULTI
=> true,
656 ApiBase
::PARAM_TYPE
=> [
671 ApiBase
::PARAM_DFLT
=> 10,
672 ApiBase
::PARAM_TYPE
=> 'limit',
673 ApiBase
::PARAM_MIN
=> 1,
674 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
675 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
678 ApiBase
::PARAM_DFLT
=> 'edit|new|log|categorize',
679 ApiBase
::PARAM_ISMULTI
=> true,
680 ApiBase
::PARAM_TYPE
=> RecentChange
::getChangeTypes()
684 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
686 'generaterevisions' => false,
690 protected function getExamplesMessages() {
692 'action=query&list=recentchanges'
693 => 'apihelp-query+recentchanges-example-simple',
694 'action=query&generator=recentchanges&grcshow=!patrolled&prop=info'
695 => 'apihelp-query+recentchanges-example-generator',
699 public function getHelpUrls() {
700 return 'https://www.mediawiki.org/wiki/API:Recentchanges';