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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query action to enumerate the recent changes that were done to the wiki.
34 * Various filters are supported.
38 class ApiQueryRecentChanges
extends ApiQueryGeneratorBase
{
40 public function __construct( $query, $moduleName ) {
41 parent
::__construct( $query, $moduleName, 'rc' );
44 private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
45 $fld_flags = false, $fld_timestamp = false, $fld_title = false, $fld_ids = false,
46 $fld_sizes = false, $fld_redirect = false, $fld_patrolled = false, $fld_loginfo = false, $fld_tags = false;
48 private $tokenFunctions;
51 * Get an array mapping token names to their handler functions.
52 * The prototype for a token function is func($pageid, $title, $rc)
53 * it should return a token or false (permission denied)
54 * @return array(tokenname => function)
56 protected function getTokenFunctions() {
57 // Don't call the hooks twice
58 if ( isset( $this->tokenFunctions
) ) {
59 return $this->tokenFunctions
;
62 // If we're in JSON callback mode, no tokens can be obtained
63 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
67 $this->tokenFunctions
= array(
68 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
70 wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions
) );
71 return $this->tokenFunctions
;
74 public static function getPatrolToken( $pageid, $title, $rc ) {
76 if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() ||
77 $rc->getAttribute( 'rc_type' ) != RC_NEW
) )
82 // The patrol token is always the same, let's exploit that
83 static $cachedPatrolToken = null;
84 if ( is_null( $cachedPatrolToken ) ) {
85 $cachedPatrolToken = $wgUser->editToken( 'patrol' );
88 return $cachedPatrolToken;
92 * Sets internal state to include the desired properties in the output.
93 * @param $prop Array associative array of properties, only keys are used here
95 public function initProperties( $prop ) {
96 $this->fld_comment
= isset( $prop['comment'] );
97 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
98 $this->fld_user
= isset( $prop['user'] );
99 $this->fld_userid
= isset( $prop['userid'] );
100 $this->fld_flags
= isset( $prop['flags'] );
101 $this->fld_timestamp
= isset( $prop['timestamp'] );
102 $this->fld_title
= isset( $prop['title'] );
103 $this->fld_ids
= isset( $prop['ids'] );
104 $this->fld_sizes
= isset( $prop['sizes'] );
105 $this->fld_redirect
= isset( $prop['redirect'] );
106 $this->fld_patrolled
= isset( $prop['patrolled'] );
107 $this->fld_loginfo
= isset( $prop['loginfo'] );
108 $this->fld_tags
= isset( $prop['tags'] );
111 public function execute() {
115 public function executeGenerator( $resultPageSet ) {
116 $this->run( $resultPageSet );
120 * Generates and outputs the result of this query based upon the provided parameters.
122 public function run( $resultPageSet = null ) {
124 /* Get the parameters of the request. */
125 $params = $this->extractRequestParams();
127 /* Build our basic query. Namely, something along the lines of:
128 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
129 * AND rc_timestamp < $end AND rc_namespace = $namespace
130 * AND rc_deleted = '0'
132 $this->addTables( 'recentchanges' );
133 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
134 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
135 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
136 $this->addWhereFld( 'rc_deleted', 0 );
138 if ( !is_null( $params['type'] ) ) {
139 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
142 if ( !is_null( $params['show'] ) ) {
143 $show = array_flip( $params['show'] );
145 /* Check for conflicting parameters. */
146 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
147 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
148 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
149 ||
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
150 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
153 $this->dieUsageMsg( array( 'show' ) );
157 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
158 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
159 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
163 /* Add additional conditions to query depending upon parameters. */
164 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
165 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
166 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
167 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
168 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
169 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
170 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
171 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
172 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
174 // Don't throw log entries out the window here
175 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) );
178 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
179 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
182 if ( !is_null( $params['user'] ) ) {
183 $this->addWhereFld( 'rc_user_text', $params['user'] );
184 $index['recentchanges'] = 'rc_user_text';
187 if ( !is_null( $params['excludeuser'] ) ) {
188 // We don't use the rc_user_text index here because
189 // * it would require us to sort by rc_user_text before rc_timestamp
190 // * the != condition doesn't throw out too many rows anyway
191 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
194 /* Add the fields we're concerned with to our query. */
195 $this->addFields( array(
206 /* Determine what properties we need to display. */
207 if ( !is_null( $params['prop'] ) ) {
208 $prop = array_flip( $params['prop'] );
210 /* Set up internal members based upon params. */
211 $this->initProperties( $prop );
213 if ( $this->fld_patrolled
&& !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
214 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
217 /* Add fields to our query if they are specified as a needed parameter. */
218 $this->addFieldsIf( 'rc_id', $this->fld_ids
);
219 $this->addFieldsIf( 'rc_this_oldid', $this->fld_ids
);
220 $this->addFieldsIf( 'rc_last_oldid', $this->fld_ids
);
221 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
222 $this->addFieldsIf( 'rc_user', $this->fld_user
);
223 $this->addFieldsIf( 'rc_user_text', $this->fld_user ||
$this->fld_userid
);
224 $this->addFieldsIf( 'rc_minor', $this->fld_flags
);
225 $this->addFieldsIf( 'rc_bot', $this->fld_flags
);
226 $this->addFieldsIf( 'rc_new', $this->fld_flags
);
227 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes
);
228 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes
);
229 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
230 $this->addFieldsIf( 'rc_logid', $this->fld_loginfo
);
231 $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo
);
232 $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo
);
233 $this->addFieldsIf( 'rc_params', $this->fld_loginfo
);
234 if ( $this->fld_redirect ||
isset( $show['redirect'] ) ||
isset( $show['!redirect'] ) ) {
235 $this->addTables( 'page' );
236 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
237 $this->addFields( 'page_is_redirect' );
241 if ( $this->fld_tags
) {
242 $this->addTables( 'tag_summary' );
243 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
244 $this->addFields( 'ts_tags' );
247 if ( !is_null( $params['tag'] ) ) {
248 $this->addTables( 'change_tag' );
249 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
250 $this->addWhereFld( 'ct_tag' , $params['tag'] );
251 global $wgOldChangeTagsIndex;
252 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
255 $this->token
= $params['token'];
256 $this->addOption( 'LIMIT', $params['limit'] +
1 );
257 $this->addOption( 'USE INDEX', $index );
260 /* Perform the actual query. */
261 $res = $this->select( __METHOD__
);
265 /* Iterate through the rows, adding data extracted from them to our query result. */
266 foreach ( $res as $row ) {
267 if ( ++
$count > $params['limit'] ) {
268 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
269 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
273 if ( is_null( $resultPageSet ) ) {
274 /* Extract the data from a single row. */
275 $vals = $this->extractRowInfo( $row );
277 /* Add that row's data to our final output. */
281 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
283 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
287 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
291 if ( is_null( $resultPageSet ) ) {
292 /* Format the result */
293 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
295 $resultPageSet->populateFromTitles( $titles );
300 * Extracts from a single sql row the data needed to describe one recent change.
302 * @param $row The row from which to extract the data.
303 * @return An array mapping strings (descriptors) to their respective string values.
306 public function extractRowInfo( $row ) {
307 /* If page was moved somewhere, get the title of the move target. */
308 $movedToTitle = false;
309 if ( isset( $row->rc_moved_to_title
) && $row->rc_moved_to_title
!== '' ) {
310 $movedToTitle = Title
::makeTitle( $row->rc_moved_to_ns
, $row->rc_moved_to_title
);
313 /* Determine the title of the page that has been changed. */
314 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
316 /* Our output data. */
319 $type = intval( $row->rc_type
);
321 /* Determine what kind of change this was. */
324 $vals['type'] = 'edit';
327 $vals['type'] = 'new';
330 $vals['type'] = 'move';
333 $vals['type'] = 'log';
335 case RC_MOVE_OVER_REDIRECT
:
336 $vals['type'] = 'move over redirect';
339 $vals['type'] = $type;
342 /* Create a new entry in the result for the title. */
343 if ( $this->fld_title
) {
344 ApiQueryBase
::addTitleInfo( $vals, $title );
345 if ( $movedToTitle ) {
346 ApiQueryBase
::addTitleInfo( $vals, $movedToTitle, 'new_' );
350 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
351 if ( $this->fld_ids
) {
352 $vals['rcid'] = intval( $row->rc_id
);
353 $vals['pageid'] = intval( $row->rc_cur_id
);
354 $vals['revid'] = intval( $row->rc_this_oldid
);
355 $vals['old_revid'] = intval( $row->rc_last_oldid
);
358 /* Add user data and 'anon' flag, if use is anonymous. */
359 if ( $this->fld_user ||
$this->fld_userid
) {
361 if ( $this->fld_user
) {
362 $vals['user'] = $row->rc_user_text
;
365 if ( $this->fld_userid
) {
366 $vals['userid'] = $row->rc_user
;
369 if ( !$row->rc_user
) {
374 /* Add flags, such as new, minor, bot. */
375 if ( $this->fld_flags
) {
376 if ( $row->rc_bot
) {
379 if ( $row->rc_new
) {
382 if ( $row->rc_minor
) {
387 /* Add sizes of each revision. (Only available on 1.10+) */
388 if ( $this->fld_sizes
) {
389 $vals['oldlen'] = intval( $row->rc_old_len
);
390 $vals['newlen'] = intval( $row->rc_new_len
);
393 /* Add the timestamp. */
394 if ( $this->fld_timestamp
) {
395 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
398 /* Add edit summary / log summary. */
399 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
400 $vals['comment'] = $row->rc_comment
;
403 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
405 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment
, $title );
408 if ( $this->fld_redirect
) {
409 if ( $row->page_is_redirect
) {
410 $vals['redirect'] = '';
414 /* Add the patrolled flag */
415 if ( $this->fld_patrolled
&& $row->rc_patrolled
== 1 ) {
416 $vals['patrolled'] = '';
419 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
420 $vals['logid'] = intval( $row->rc_logid
);
421 $vals['logtype'] = $row->rc_log_type
;
422 $vals['logaction'] = $row->rc_log_action
;
423 ApiQueryLogEvents
::addLogParams(
425 $vals, $row->rc_params
,
426 $row->rc_log_type
, $row->rc_timestamp
430 if ( $this->fld_tags
) {
431 if ( $row->ts_tags
) {
432 $tags = explode( ',', $row->ts_tags
);
433 $this->getResult()->setIndexedTagName( $tags, 'tag' );
434 $vals['tags'] = $tags;
436 $vals['tags'] = array();
440 if ( !is_null( $this->token
) ) {
441 $tokenFunctions = $this->getTokenFunctions();
442 foreach ( $this->token
as $t ) {
443 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
444 $title, RecentChange
::newFromRow( $row ) );
445 if ( $val === false ) {
446 $this->setWarning( "Action '$t' is not allowed for the current user" );
448 $vals[$t . 'token'] = $val;
456 private function parseRCType( $type ) {
457 if ( is_array( $type ) ) {
459 foreach ( $type as $t ) {
460 $retval[] = $this->parseRCType( $t );
474 public function getCacheMode( $params ) {
475 if ( isset( $params['show'] ) ) {
476 foreach ( $params['show'] as $show ) {
477 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
482 if ( isset( $params['token'] ) ) {
485 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
486 // formatComment() calls wfMsg() among other things
487 return 'anon-public-user-private';
492 public function getAllowedParams() {
495 ApiBase
::PARAM_TYPE
=> 'timestamp'
498 ApiBase
::PARAM_TYPE
=> 'timestamp'
501 ApiBase
::PARAM_DFLT
=> 'older',
502 ApiBase
::PARAM_TYPE
=> array(
507 'namespace' => array(
508 ApiBase
::PARAM_ISMULTI
=> true,
509 ApiBase
::PARAM_TYPE
=> 'namespace'
512 ApiBase
::PARAM_TYPE
=> 'user'
514 'excludeuser' => array(
515 ApiBase
::PARAM_TYPE
=> 'user'
519 ApiBase
::PARAM_ISMULTI
=> true,
520 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
521 ApiBase
::PARAM_TYPE
=> array(
538 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
539 ApiBase
::PARAM_ISMULTI
=> true
542 ApiBase
::PARAM_ISMULTI
=> true,
543 ApiBase
::PARAM_TYPE
=> array(
557 ApiBase
::PARAM_DFLT
=> 10,
558 ApiBase
::PARAM_TYPE
=> 'limit',
559 ApiBase
::PARAM_MIN
=> 1,
560 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
561 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
564 ApiBase
::PARAM_ISMULTI
=> true,
565 ApiBase
::PARAM_TYPE
=> array(
574 public function getParamDescription() {
576 'start' => 'The timestamp to start enumerating from',
577 'end' => 'The timestamp to end enumerating',
578 'dir' => 'In which direction to enumerate',
579 'namespace' => 'Filter log entries to only this namespace(s)',
580 'user' => 'Only list changes by this user',
581 'excludeuser' => 'Don\'t list changes by this user',
583 'Include additional pieces of information',
584 ' user - Adds the user responsible for the edit and tags if they are an IP',
585 ' userid - Adds the user id responsible for the edit',
586 ' comment - Adds the comment for the edit',
587 ' parsedcomment - Adds the parsed comment for the edit',
588 ' flags - Adds flags for the edit',
589 ' timestamp - Adds timestamp of the edit',
590 ' title - Adds the page title of the edit',
591 ' ids - Adds the page ID, recent changes ID and the new and old revision ID',
592 ' sizes - Adds the new and old page length in bytes',
593 ' redirect - Tags edit if page is a redirect',
594 ' patrolled - Tags edits that have been patrolled',
595 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
596 ' tags - Lists tags for the entry',
598 'token' => 'Which tokens to obtain for each change',
600 'Show only items that meet this criteria.',
601 "For example, to see only minor edits done by logged-in users, set {$this->getModulePrefix()}show=minor|!anon"
603 'type' => 'Which types of changes to show',
604 'limit' => 'How many total changes to return',
605 'tag' => 'Only list changes tagged with this tag',
609 public function getDescription() {
610 return 'Enumerate recent changes';
613 public function getPossibleErrors() {
614 return array_merge( parent
::getPossibleErrors(), array(
616 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
617 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
621 protected function getExamples() {
623 'api.php?action=query&list=recentchanges'
627 public function getVersion() {
628 return __CLASS__
. ': $Id$';