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( $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 = array();
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 * @return array array(tokenname => function)
52 protected function getTokenFunctions() {
53 // Don't call the hooks twice
54 if ( isset( $this->tokenFunctions
) ) {
55 return $this->tokenFunctions
;
58 // If we're in JSON callback mode, no tokens can be obtained
59 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
63 $this->tokenFunctions
= array(
64 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
66 wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions
) );
67 return $this->tokenFunctions
;
73 * @param $rc RecentChange (optional)
76 public static function getPatrolToken( $pageid, $title, $rc = null ) {
79 $validTokenUser = false;
82 if ( ( $wgUser->useRCPatrol() && $rc->getAttribute( 'rc_type' ) == RC_EDIT
) ||
83 ( $wgUser->useNPPatrol() && $rc->getAttribute( 'rc_type' ) == RC_NEW
) )
85 $validTokenUser = true;
88 if ( $wgUser->useRCPatrol() ||
$wgUser->useNPPatrol() ) {
89 $validTokenUser = true;
93 if ( $validTokenUser ) {
94 // The patrol token is always the same, let's exploit that
95 static $cachedPatrolToken = null;
96 if ( is_null( $cachedPatrolToken ) ) {
97 $cachedPatrolToken = $wgUser->getEditToken( 'patrol' );
99 return $cachedPatrolToken;
107 * Sets internal state to include the desired properties in the output.
108 * @param array $prop associative array of properties, only keys are used here
110 public function initProperties( $prop ) {
111 $this->fld_comment
= isset( $prop['comment'] );
112 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
113 $this->fld_user
= isset( $prop['user'] );
114 $this->fld_userid
= isset( $prop['userid'] );
115 $this->fld_flags
= isset( $prop['flags'] );
116 $this->fld_timestamp
= isset( $prop['timestamp'] );
117 $this->fld_title
= isset( $prop['title'] );
118 $this->fld_ids
= isset( $prop['ids'] );
119 $this->fld_sizes
= isset( $prop['sizes'] );
120 $this->fld_redirect
= isset( $prop['redirect'] );
121 $this->fld_patrolled
= isset( $prop['patrolled'] );
122 $this->fld_loginfo
= isset( $prop['loginfo'] );
123 $this->fld_tags
= isset( $prop['tags'] );
124 $this->fld_sha1
= isset( $prop['sha1'] );
127 public function execute() {
131 public function executeGenerator( $resultPageSet ) {
132 $this->run( $resultPageSet );
136 * Generates and outputs the result of this query based upon the provided parameters.
138 * @param $resultPageSet ApiPageSet
140 public function run( $resultPageSet = null ) {
141 $user = $this->getUser();
142 /* Get the parameters of the request. */
143 $params = $this->extractRequestParams();
145 /* Build our basic query. Namely, something along the lines of:
146 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
147 * AND rc_timestamp < $end AND rc_namespace = $namespace
150 $this->addTables( 'recentchanges' );
151 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
152 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
154 if ( !is_null( $params['continue'] ) ) {
155 $cont = explode( '|', $params['continue'] );
156 if ( count( $cont ) != 2 ) {
157 $this->dieUsage( 'Invalid continue param. You should pass the ' .
158 'original value returned by the previous query', '_badcontinue' );
161 $timestamp = $this->getDB()->addQuotes( wfTimestamp( TS_MW
, $cont[0] ) );
162 $id = intval( $cont[1] );
163 $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', array(
174 "rc_timestamp $order",
178 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
179 $this->addWhereFld( 'rc_deleted', 0 );
181 if ( !is_null( $params['type'] ) ) {
182 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
185 if ( !is_null( $params['show'] ) ) {
186 $show = array_flip( $params['show'] );
188 /* Check for conflicting parameters. */
189 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
190 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
191 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
192 ||
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
193 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
195 $this->dieUsageMsg( 'show' );
199 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
200 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
201 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
205 /* Add additional conditions to query depending upon parameters. */
206 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
207 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
208 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
209 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
210 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
211 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
212 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
213 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
214 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
216 // Don't throw log entries out the window here
217 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) );
220 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
221 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
224 if ( !is_null( $params['user'] ) ) {
225 $this->addWhereFld( 'rc_user_text', $params['user'] );
226 $index['recentchanges'] = 'rc_user_text';
229 if ( !is_null( $params['excludeuser'] ) ) {
230 // We don't use the rc_user_text index here because
231 // * it would require us to sort by rc_user_text before rc_timestamp
232 // * the != condition doesn't throw out too many rows anyway
233 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
236 /* Add the fields we're concerned with to our query. */
237 $this->addFields( array(
246 $showRedirects = false;
247 /* Determine what properties we need to display. */
248 if ( !is_null( $params['prop'] ) ) {
249 $prop = array_flip( $params['prop'] );
251 /* Set up internal members based upon params. */
252 $this->initProperties( $prop );
254 if ( $this->fld_patrolled
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
255 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
258 $this->addFields( 'rc_id' );
259 /* Add fields to our query if they are specified as a needed parameter. */
260 $this->addFieldsIf( array( 'rc_this_oldid', 'rc_last_oldid' ), $this->fld_ids
);
261 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
262 $this->addFieldsIf( 'rc_user', $this->fld_user
);
263 $this->addFieldsIf( 'rc_user_text', $this->fld_user ||
$this->fld_userid
);
264 $this->addFieldsIf( array( 'rc_minor', 'rc_type', 'rc_bot' ), $this->fld_flags
);
265 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
266 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
267 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo
);
268 $showRedirects = $this->fld_redirect ||
isset( $show['redirect'] ) ||
isset( $show['!redirect'] );
271 if ( $this->fld_tags
) {
272 $this->addTables( 'tag_summary' );
273 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
274 $this->addFields( 'ts_tags' );
277 if ( $this->fld_sha1
) {
278 $this->addTables( 'revision' );
279 $this->addJoinConds( array( 'revision' => array( 'LEFT JOIN', array( 'rc_this_oldid=rev_id' ) ) ) );
280 $this->addFields( array( 'rev_sha1', 'rev_deleted' ) );
283 if ( $params['toponly'] ||
$showRedirects ) {
284 $this->addTables( 'page' );
285 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
286 $this->addFields( 'page_is_redirect' );
288 if ( $params['toponly'] ) {
289 $this->addWhere( 'rc_this_oldid = page_latest' );
293 if ( !is_null( $params['tag'] ) ) {
294 $this->addTables( 'change_tag' );
295 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
296 $this->addWhereFld( 'ct_tag', $params['tag'] );
297 $index['change_tag'] = 'change_tag_tag_id';
300 $this->token
= $params['token'];
301 $this->addOption( 'LIMIT', $params['limit'] +
1 );
302 $this->addOption( 'USE INDEX', $index );
305 /* Perform the actual query. */
306 $res = $this->select( __METHOD__
);
310 $result = $this->getResult();
312 /* Iterate through the rows, adding data extracted from them to our query result. */
313 foreach ( $res as $row ) {
314 if ( ++
$count > $params['limit'] ) {
315 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
316 $this->setContinueEnumParameter( 'continue', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) . '|' . $row->rc_id
);
320 if ( is_null( $resultPageSet ) ) {
321 /* Extract the data from a single row. */
322 $vals = $this->extractRowInfo( $row );
324 /* Add that row's data to our final output. */
328 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
330 $this->setContinueEnumParameter( 'continue', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) . '|' . $row->rc_id
);
334 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
338 if ( is_null( $resultPageSet ) ) {
339 /* Format the result */
340 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
342 $resultPageSet->populateFromTitles( $titles );
347 * Extracts from a single sql row the data needed to describe one recent change.
349 * @param mixed $row The row from which to extract the data.
350 * @return array An array mapping strings (descriptors) to their respective string values.
353 public function extractRowInfo( $row ) {
354 /* Determine the title of the page that has been changed. */
355 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
357 /* Our output data. */
360 $type = intval( $row->rc_type
);
362 /* Determine what kind of change this was. */
365 $vals['type'] = 'edit';
368 $vals['type'] = 'new';
371 $vals['type'] = 'move';
374 $vals['type'] = 'log';
377 $vals['type'] = 'external';
379 case RC_MOVE_OVER_REDIRECT
:
380 $vals['type'] = 'move over redirect';
383 $vals['type'] = $type;
386 /* Create a new entry in the result for the title. */
387 if ( $this->fld_title
) {
388 ApiQueryBase
::addTitleInfo( $vals, $title );
391 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
392 if ( $this->fld_ids
) {
393 $vals['rcid'] = intval( $row->rc_id
);
394 $vals['pageid'] = intval( $row->rc_cur_id
);
395 $vals['revid'] = intval( $row->rc_this_oldid
);
396 $vals['old_revid'] = intval( $row->rc_last_oldid
);
399 /* Add user data and 'anon' flag, if use is anonymous. */
400 if ( $this->fld_user ||
$this->fld_userid
) {
402 if ( $this->fld_user
) {
403 $vals['user'] = $row->rc_user_text
;
406 if ( $this->fld_userid
) {
407 $vals['userid'] = $row->rc_user
;
410 if ( !$row->rc_user
) {
415 /* Add flags, such as new, minor, bot. */
416 if ( $this->fld_flags
) {
417 if ( $row->rc_bot
) {
420 if ( $row->rc_type
== RC_NEW
) {
423 if ( $row->rc_minor
) {
428 /* Add sizes of each revision. (Only available on 1.10+) */
429 if ( $this->fld_sizes
) {
430 $vals['oldlen'] = intval( $row->rc_old_len
);
431 $vals['newlen'] = intval( $row->rc_new_len
);
434 /* Add the timestamp. */
435 if ( $this->fld_timestamp
) {
436 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
439 /* Add edit summary / log summary. */
440 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
441 $vals['comment'] = $row->rc_comment
;
444 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
445 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
448 if ( $this->fld_redirect
) {
449 if ( $row->page_is_redirect
) {
450 $vals['redirect'] = '';
454 /* Add the patrolled flag */
455 if ( $this->fld_patrolled
&& $row->rc_patrolled
== 1 ) {
456 $vals['patrolled'] = '';
459 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
460 $vals['logid'] = intval( $row->rc_logid
);
461 $vals['logtype'] = $row->rc_log_type
;
462 $vals['logaction'] = $row->rc_log_action
;
463 $logEntry = DatabaseLogEntry
::newFromRow( (array)$row );
464 ApiQueryLogEvents
::addLogParams(
467 $logEntry->getParameters(),
468 $logEntry->getType(),
469 $logEntry->getSubtype(),
470 $logEntry->getTimestamp()
474 if ( $this->fld_tags
) {
475 if ( $row->ts_tags
) {
476 $tags = explode( ',', $row->ts_tags
);
477 $this->getResult()->setIndexedTagName( $tags, 'tag' );
478 $vals['tags'] = $tags;
480 $vals['tags'] = array();
484 if ( $this->fld_sha1
&& $row->rev_sha1
!== null ) {
485 // The RevDel check should currently never pass due to the
486 // rc_deleted = 0 condition in the WHERE clause, but in case that
487 // ever changes we check it here too.
488 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
489 $vals['sha1hidden'] = '';
490 } elseif ( $row->rev_sha1
!== '' ) {
491 $vals['sha1'] = wfBaseConvert( $row->rev_sha1
, 36, 16, 40 );
497 if ( !is_null( $this->token
) ) {
498 $tokenFunctions = $this->getTokenFunctions();
499 foreach ( $this->token
as $t ) {
500 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
501 $title, RecentChange
::newFromRow( $row ) );
502 if ( $val === false ) {
503 $this->setWarning( "Action '$t' is not allowed for the current user" );
505 $vals[$t . 'token'] = $val;
513 private function parseRCType( $type ) {
514 if ( is_array( $type ) ) {
516 foreach ( $type as $t ) {
517 $retval[] = $this->parseRCType( $t );
533 public function getCacheMode( $params ) {
534 if ( isset( $params['show'] ) ) {
535 foreach ( $params['show'] as $show ) {
536 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
541 if ( isset( $params['token'] ) ) {
544 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
545 // formatComment() calls wfMessage() among other things
546 return 'anon-public-user-private';
551 public function getAllowedParams() {
554 ApiBase
::PARAM_TYPE
=> 'timestamp'
557 ApiBase
::PARAM_TYPE
=> 'timestamp'
560 ApiBase
::PARAM_DFLT
=> 'older',
561 ApiBase
::PARAM_TYPE
=> array(
566 'namespace' => array(
567 ApiBase
::PARAM_ISMULTI
=> true,
568 ApiBase
::PARAM_TYPE
=> 'namespace'
571 ApiBase
::PARAM_TYPE
=> 'user'
573 'excludeuser' => array(
574 ApiBase
::PARAM_TYPE
=> 'user'
578 ApiBase
::PARAM_ISMULTI
=> true,
579 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
580 ApiBase
::PARAM_TYPE
=> array(
598 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
599 ApiBase
::PARAM_ISMULTI
=> true
602 ApiBase
::PARAM_ISMULTI
=> true,
603 ApiBase
::PARAM_TYPE
=> array(
617 ApiBase
::PARAM_DFLT
=> 10,
618 ApiBase
::PARAM_TYPE
=> 'limit',
619 ApiBase
::PARAM_MIN
=> 1,
620 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
621 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
624 ApiBase
::PARAM_ISMULTI
=> true,
625 ApiBase
::PARAM_TYPE
=> array(
637 public function getParamDescription() {
638 $p = $this->getModulePrefix();
640 'start' => 'The timestamp to start enumerating from',
641 'end' => 'The timestamp to end enumerating',
642 'dir' => $this->getDirectionDescription( $p ),
643 'namespace' => 'Filter log entries to only this namespace(s)',
644 'user' => 'Only list changes by this user',
645 'excludeuser' => 'Don\'t list changes by this user',
647 'Include additional pieces of information',
648 ' user - Adds the user responsible for the edit and tags if they are an IP',
649 ' userid - Adds the user id responsible for the edit',
650 ' comment - Adds the comment for the edit',
651 ' parsedcomment - Adds the parsed comment for the edit',
652 ' flags - Adds flags for the edit',
653 ' timestamp - Adds timestamp of the edit',
654 ' title - Adds the page title of the edit',
655 ' ids - Adds the page ID, recent changes ID and the new and old revision ID',
656 ' sizes - Adds the new and old page length in bytes',
657 ' redirect - Tags edit if page is a redirect',
658 ' patrolled - Tags edits that have been patrolled',
659 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
660 ' tags - Lists tags for the entry',
661 ' sha1 - Adds the content checksum for entries associated with a revision',
663 'token' => 'Which tokens to obtain for each change',
665 'Show only items that meet this criteria.',
666 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
668 'type' => 'Which types of changes to show',
669 'limit' => 'How many total changes to return',
670 'tag' => 'Only list changes tagged with this tag',
671 'toponly' => 'Only list changes which are the latest revision',
672 'continue' => 'When more results are available, use this to continue',
676 public function getResultProperties() {
681 ApiBase
::PROP_TYPE
=> array(
694 ApiBase
::PROP_TYPE
=> 'namespace',
695 ApiBase
::PROP_NULLABLE
=> true
697 'new_title' => array(
698 ApiBase
::PROP_TYPE
=> 'string',
699 ApiBase
::PROP_NULLABLE
=> true
704 'pageid' => 'integer',
705 'revid' => 'integer',
706 'old_revid' => 'integer'
713 'userid' => 'integer',
722 'oldlen' => 'integer',
723 'newlen' => 'integer'
725 'timestamp' => array(
726 'timestamp' => 'timestamp'
730 ApiBase
::PROP_TYPE
=> 'string',
731 ApiBase
::PROP_NULLABLE
=> true
734 'parsedcomment' => array(
735 'parsedcomment' => array(
736 ApiBase
::PROP_TYPE
=> 'string',
737 ApiBase
::PROP_NULLABLE
=> true
741 'redirect' => 'boolean'
743 'patrolled' => array(
744 'patrolled' => 'boolean'
748 ApiBase
::PROP_TYPE
=> 'integer',
749 ApiBase
::PROP_NULLABLE
=> true
752 ApiBase
::PROP_TYPE
=> $wgLogTypes,
753 ApiBase
::PROP_NULLABLE
=> true
755 'logaction' => array(
756 ApiBase
::PROP_TYPE
=> 'string',
757 ApiBase
::PROP_NULLABLE
=> true
762 ApiBase
::PROP_TYPE
=> 'string',
763 ApiBase
::PROP_NULLABLE
=> true
765 'sha1hidden' => array(
766 ApiBase
::PROP_TYPE
=> 'boolean',
767 ApiBase
::PROP_NULLABLE
=> true
772 self
::addTokenProperties( $props, $this->getTokenFunctions() );
777 public function getDescription() {
778 return 'Enumerate recent changes';
781 public function getPossibleErrors() {
782 return array_merge( parent
::getPossibleErrors(), array(
784 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
785 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
789 public function getExamples() {
791 'api.php?action=query&list=recentchanges'
795 public function getHelpUrls() {
796 return 'https://www.mediawiki.org/wiki/API:Recentchanges';