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, $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 $prop Array 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'] );
126 public function execute() {
130 public function executeGenerator( $resultPageSet ) {
131 $this->run( $resultPageSet );
135 * Generates and outputs the result of this query based upon the provided parameters.
137 * @param $resultPageSet ApiPageSet
139 public function run( $resultPageSet = null ) {
140 $user = $this->getUser();
141 /* Get the parameters of the request. */
142 $params = $this->extractRequestParams();
144 /* Build our basic query. Namely, something along the lines of:
145 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
146 * AND rc_timestamp < $end AND rc_namespace = $namespace
147 * AND rc_deleted = '0'
149 $this->addTables( 'recentchanges' );
150 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
151 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
152 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
153 $this->addWhereFld( 'rc_deleted', 0 );
155 if ( !is_null( $params['type'] ) ) {
156 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
159 if ( !is_null( $params['show'] ) ) {
160 $show = array_flip( $params['show'] );
162 /* Check for conflicting parameters. */
163 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
164 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
165 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
166 ||
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
167 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
169 $this->dieUsageMsg( 'show' );
173 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
174 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
175 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
179 /* Add additional conditions to query depending upon parameters. */
180 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
181 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
182 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
183 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
184 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
185 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
186 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
187 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
188 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
190 // Don't throw log entries out the window here
191 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) );
194 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
195 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
198 if ( !is_null( $params['user'] ) ) {
199 $this->addWhereFld( 'rc_user_text', $params['user'] );
200 $index['recentchanges'] = 'rc_user_text';
203 if ( !is_null( $params['excludeuser'] ) ) {
204 // We don't use the rc_user_text index here because
205 // * it would require us to sort by rc_user_text before rc_timestamp
206 // * the != condition doesn't throw out too many rows anyway
207 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
210 /* Add the fields we're concerned with to our query. */
211 $this->addFields( array(
222 $showRedirects = false;
223 /* Determine what properties we need to display. */
224 if ( !is_null( $params['prop'] ) ) {
225 $prop = array_flip( $params['prop'] );
227 /* Set up internal members based upon params. */
228 $this->initProperties( $prop );
230 if ( $this->fld_patrolled
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
231 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
234 /* Add fields to our query if they are specified as a needed parameter. */
235 $this->addFieldsIf( array( 'rc_id', 'rc_this_oldid', 'rc_last_oldid' ), $this->fld_ids
);
236 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
237 $this->addFieldsIf( 'rc_user', $this->fld_user
);
238 $this->addFieldsIf( 'rc_user_text', $this->fld_user ||
$this->fld_userid
);
239 $this->addFieldsIf( array( 'rc_minor', 'rc_new', 'rc_bot' ) , $this->fld_flags
);
240 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
241 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
242 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo
);
243 $showRedirects = $this->fld_redirect ||
isset( $show['redirect'] ) ||
isset( $show['!redirect'] );
246 if ( $this->fld_tags
) {
247 $this->addTables( 'tag_summary' );
248 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
249 $this->addFields( 'ts_tags' );
252 if ( $params['toponly'] ||
$showRedirects ) {
253 $this->addTables( 'page' );
254 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
255 $this->addFields( 'page_is_redirect' );
257 if ( $params['toponly'] ) {
258 $this->addWhere( 'rc_this_oldid = page_latest' );
262 if ( !is_null( $params['tag'] ) ) {
263 $this->addTables( 'change_tag' );
264 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
265 $this->addWhereFld( 'ct_tag' , $params['tag'] );
266 global $wgOldChangeTagsIndex;
267 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
270 $this->token
= $params['token'];
271 $this->addOption( 'LIMIT', $params['limit'] +
1 );
272 $this->addOption( 'USE INDEX', $index );
275 /* Perform the actual query. */
276 $res = $this->select( __METHOD__
);
280 $result = $this->getResult();
282 /* Iterate through the rows, adding data extracted from them to our query result. */
283 foreach ( $res as $row ) {
284 if ( ++
$count > $params['limit'] ) {
285 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
286 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
290 if ( is_null( $resultPageSet ) ) {
291 /* Extract the data from a single row. */
292 $vals = $this->extractRowInfo( $row );
294 /* Add that row's data to our final output. */
298 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
300 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
304 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
308 if ( is_null( $resultPageSet ) ) {
309 /* Format the result */
310 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
312 $resultPageSet->populateFromTitles( $titles );
317 * Extracts from a single sql row the data needed to describe one recent change.
319 * @param $row The row from which to extract the data.
320 * @return array An array mapping strings (descriptors) to their respective string values.
323 public function extractRowInfo( $row ) {
324 /* If page was moved somewhere, get the title of the move target. */
325 $movedToTitle = false;
326 if ( isset( $row->rc_moved_to_title
) && $row->rc_moved_to_title
!== '' ) {
327 $movedToTitle = Title
::makeTitle( $row->rc_moved_to_ns
, $row->rc_moved_to_title
);
330 /* Determine the title of the page that has been changed. */
331 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
333 /* Our output data. */
336 $type = intval( $row->rc_type
);
338 /* Determine what kind of change this was. */
341 $vals['type'] = 'edit';
344 $vals['type'] = 'new';
347 $vals['type'] = 'move';
350 $vals['type'] = 'log';
352 case RC_MOVE_OVER_REDIRECT
:
353 $vals['type'] = 'move over redirect';
356 $vals['type'] = $type;
359 /* Create a new entry in the result for the title. */
360 if ( $this->fld_title
) {
361 ApiQueryBase
::addTitleInfo( $vals, $title );
362 if ( $movedToTitle ) {
363 ApiQueryBase
::addTitleInfo( $vals, $movedToTitle, 'new_' );
367 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
368 if ( $this->fld_ids
) {
369 $vals['rcid'] = intval( $row->rc_id
);
370 $vals['pageid'] = intval( $row->rc_cur_id
);
371 $vals['revid'] = intval( $row->rc_this_oldid
);
372 $vals['old_revid'] = intval( $row->rc_last_oldid
);
375 /* Add user data and 'anon' flag, if use is anonymous. */
376 if ( $this->fld_user ||
$this->fld_userid
) {
378 if ( $this->fld_user
) {
379 $vals['user'] = $row->rc_user_text
;
382 if ( $this->fld_userid
) {
383 $vals['userid'] = $row->rc_user
;
386 if ( !$row->rc_user
) {
391 /* Add flags, such as new, minor, bot. */
392 if ( $this->fld_flags
) {
393 if ( $row->rc_bot
) {
396 if ( $row->rc_new
) {
399 if ( $row->rc_minor
) {
404 /* Add sizes of each revision. (Only available on 1.10+) */
405 if ( $this->fld_sizes
) {
406 $vals['oldlen'] = intval( $row->rc_old_len
);
407 $vals['newlen'] = intval( $row->rc_new_len
);
410 /* Add the timestamp. */
411 if ( $this->fld_timestamp
) {
412 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
415 /* Add edit summary / log summary. */
416 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
417 $vals['comment'] = $row->rc_comment
;
420 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
421 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
424 if ( $this->fld_redirect
) {
425 if ( $row->page_is_redirect
) {
426 $vals['redirect'] = '';
430 /* Add the patrolled flag */
431 if ( $this->fld_patrolled
&& $row->rc_patrolled
== 1 ) {
432 $vals['patrolled'] = '';
435 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
436 $vals['logid'] = intval( $row->rc_logid
);
437 $vals['logtype'] = $row->rc_log_type
;
438 $vals['logaction'] = $row->rc_log_action
;
439 $logEntry = DatabaseLogEntry
::newFromRow( (array)$row );
440 ApiQueryLogEvents
::addLogParams(
443 $logEntry->getParameters(),
444 $logEntry->getType(),
445 $logEntry->getSubtype(),
446 $logEntry->getTimestamp()
450 if ( $this->fld_tags
) {
451 if ( $row->ts_tags
) {
452 $tags = explode( ',', $row->ts_tags
);
453 $this->getResult()->setIndexedTagName( $tags, 'tag' );
454 $vals['tags'] = $tags;
456 $vals['tags'] = array();
460 if ( !is_null( $this->token
) ) {
461 $tokenFunctions = $this->getTokenFunctions();
462 foreach ( $this->token
as $t ) {
463 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
464 $title, RecentChange
::newFromRow( $row ) );
465 if ( $val === false ) {
466 $this->setWarning( "Action '$t' is not allowed for the current user" );
468 $vals[$t . 'token'] = $val;
476 private function parseRCType( $type ) {
477 if ( is_array( $type ) ) {
479 foreach ( $type as $t ) {
480 $retval[] = $this->parseRCType( $t );
494 public function getCacheMode( $params ) {
495 if ( isset( $params['show'] ) ) {
496 foreach ( $params['show'] as $show ) {
497 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
502 if ( isset( $params['token'] ) ) {
505 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
506 // formatComment() calls wfMsg() among other things
507 return 'anon-public-user-private';
512 public function getAllowedParams() {
515 ApiBase
::PARAM_TYPE
=> 'timestamp'
518 ApiBase
::PARAM_TYPE
=> 'timestamp'
521 ApiBase
::PARAM_DFLT
=> 'older',
522 ApiBase
::PARAM_TYPE
=> array(
527 'namespace' => array(
528 ApiBase
::PARAM_ISMULTI
=> true,
529 ApiBase
::PARAM_TYPE
=> 'namespace'
532 ApiBase
::PARAM_TYPE
=> 'user'
534 'excludeuser' => array(
535 ApiBase
::PARAM_TYPE
=> 'user'
539 ApiBase
::PARAM_ISMULTI
=> true,
540 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
541 ApiBase
::PARAM_TYPE
=> array(
558 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
559 ApiBase
::PARAM_ISMULTI
=> true
562 ApiBase
::PARAM_ISMULTI
=> true,
563 ApiBase
::PARAM_TYPE
=> array(
577 ApiBase
::PARAM_DFLT
=> 10,
578 ApiBase
::PARAM_TYPE
=> 'limit',
579 ApiBase
::PARAM_MIN
=> 1,
580 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
581 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
584 ApiBase
::PARAM_ISMULTI
=> true,
585 ApiBase
::PARAM_TYPE
=> array(
595 public function getParamDescription() {
596 $p = $this->getModulePrefix();
598 'start' => 'The timestamp to start enumerating from',
599 'end' => 'The timestamp to end enumerating',
600 'dir' => $this->getDirectionDescription( $p ),
601 'namespace' => 'Filter log entries to only this namespace(s)',
602 'user' => 'Only list changes by this user',
603 'excludeuser' => 'Don\'t list changes by this user',
605 'Include additional pieces of information',
606 ' user - Adds the user responsible for the edit and tags if they are an IP',
607 ' userid - Adds the user id responsible for the edit',
608 ' comment - Adds the comment for the edit',
609 ' parsedcomment - Adds the parsed comment for the edit',
610 ' flags - Adds flags for the edit',
611 ' timestamp - Adds timestamp of the edit',
612 ' title - Adds the page title of the edit',
613 ' ids - Adds the page ID, recent changes ID and the new and old revision ID',
614 ' sizes - Adds the new and old page length in bytes',
615 ' redirect - Tags edit if page is a redirect',
616 ' patrolled - Tags edits that have been patrolled',
617 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
618 ' tags - Lists tags for the entry',
620 'token' => 'Which tokens to obtain for each change',
622 'Show only items that meet this criteria.',
623 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
625 'type' => 'Which types of changes to show',
626 'limit' => 'How many total changes to return',
627 'tag' => 'Only list changes tagged with this tag',
628 'toponly' => 'Only list changes which are the latest revision',
632 public function getDescription() {
633 return 'Enumerate recent changes';
636 public function getPossibleErrors() {
637 return array_merge( parent
::getPossibleErrors(), array(
639 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
640 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
644 public function getExamples() {
646 'api.php?action=query&list=recentchanges'
650 public function getHelpUrls() {
651 return 'https://www.mediawiki.org/wiki/API:Recentchanges';
654 public function getVersion() {
655 return __CLASS__
. ': $Id$';