4 * Created on Oct 19, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
32 * A query action to enumerate the recent changes that were done to the wiki.
33 * Various filters are supported.
37 class ApiQueryRecentChanges
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
:: __construct( $query, $moduleName, 'rc' );
43 private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_flags = false,
44 $fld_timestamp = false, $fld_title = false, $fld_ids = false,
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(tokenname => function)
52 protected function getTokenFunctions() {
53 // Don't call the hooks twice
54 if ( isset( $this->tokenFunctions
) )
55 return $this->tokenFunctions
;
57 // If we're in JSON callback mode, no tokens can be obtained
58 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) )
61 $this->tokenFunctions
= array(
62 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
64 wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions
) );
65 return $this->tokenFunctions
;
68 public static function getPatrolToken( $pageid, $title, $rc )
71 if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() ||
72 $rc->getAttribute( 'rc_type' ) != RC_NEW
) )
75 // The patrol token is always the same, let's exploit that
76 static $cachedPatrolToken = null;
77 if ( !is_null( $cachedPatrolToken ) )
78 return $cachedPatrolToken;
80 $cachedPatrolToken = $wgUser->editToken();
81 return $cachedPatrolToken;
85 * Sets internal state to include the desired properties in the output.
86 * @param $prop associative array of properties, only keys are used here
88 public function initProperties( $prop ) {
89 $this->fld_comment
= isset ( $prop['comment'] );
90 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
91 $this->fld_user
= isset ( $prop['user'] );
92 $this->fld_flags
= isset ( $prop['flags'] );
93 $this->fld_timestamp
= isset ( $prop['timestamp'] );
94 $this->fld_title
= isset ( $prop['title'] );
95 $this->fld_ids
= isset ( $prop['ids'] );
96 $this->fld_sizes
= isset ( $prop['sizes'] );
97 $this->fld_redirect
= isset( $prop['redirect'] );
98 $this->fld_patrolled
= isset( $prop['patrolled'] );
99 $this->fld_loginfo
= isset( $prop['loginfo'] );
100 $this->fld_tags
= isset( $prop['tags'] );
104 * Generates and outputs the result of this query based upon the provided parameters.
106 public function execute() {
107 /* Get the parameters of the request. */
108 $params = $this->extractRequestParams();
110 /* Build our basic query. Namely, something along the lines of:
111 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
112 * AND rc_timestamp < $end AND rc_namespace = $namespace
113 * AND rc_deleted = '0'
115 $db = $this->getDB();
116 $this->addTables( 'recentchanges' );
117 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
118 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
119 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
120 $this->addWhereFld( 'rc_deleted', 0 );
122 if ( !is_null( $params['type'] ) )
123 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
125 if ( !is_null( $params['show'] ) ) {
126 $show = array_flip( $params['show'] );
128 /* Check for conflicting parameters. */
129 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
130 ||
( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
131 ||
( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
132 ||
( isset ( $show['redirect'] ) && isset ( $show['!redirect'] ) )
133 ||
( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) ) ) {
135 $this->dieUsage( "Incorrect parameter - mutually exclusive values may not be supplied", 'show' );
140 if ( ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
141 $this->dieUsage( "You need the patrol right to request the patrolled flag", 'permissiondenied' );
143 /* Add additional conditions to query depending upon parameters. */
144 $this->addWhereIf( 'rc_minor = 0', isset ( $show['!minor'] ) );
145 $this->addWhereIf( 'rc_minor != 0', isset ( $show['minor'] ) );
146 $this->addWhereIf( 'rc_bot = 0', isset ( $show['!bot'] ) );
147 $this->addWhereIf( 'rc_bot != 0', isset ( $show['bot'] ) );
148 $this->addWhereIf( 'rc_user = 0', isset ( $show['anon'] ) );
149 $this->addWhereIf( 'rc_user != 0', isset ( $show['!anon'] ) );
150 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
151 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
152 $this->addWhereIf( 'page_is_redirect = 1', isset ( $show['redirect'] ) );
154 // Don't throw log entries out the window here
155 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset ( $show['!redirect'] ) );
158 if ( !is_null( $params['user'] ) && !is_null( $param['excludeuser'] ) )
159 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
161 if ( !is_null( $params['user'] ) )
163 $this->addWhereFld( 'rc_user_text', $params['user'] );
164 $index['recentchanges'] = 'rc_user_text';
167 if ( !is_null( $params['excludeuser'] ) )
168 // We don't use the rc_user_text index here because
169 // * it would require us to sort by rc_user_text before rc_timestamp
170 // * the != condition doesn't throw out too many rows anyway
171 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
173 /* Add the fields we're concerned with to our query. */
174 $this->addFields( array (
184 /* Determine what properties we need to display. */
185 if ( !is_null( $params['prop'] ) ) {
186 $prop = array_flip( $params['prop'] );
188 /* Set up internal members based upon params. */
189 $this->initProperties( $prop );
192 if ( $this->fld_patrolled
&& !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
193 $this->dieUsage( "You need the patrol right to request the patrolled flag", 'permissiondenied' );
195 /* Add fields to our query if they are specified as a needed parameter. */
196 $this->addFieldsIf( 'rc_id', $this->fld_ids
);
197 $this->addFieldsIf( 'rc_this_oldid', $this->fld_ids
);
198 $this->addFieldsIf( 'rc_last_oldid', $this->fld_ids
);
199 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
200 $this->addFieldsIf( 'rc_user', $this->fld_user
);
201 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
202 $this->addFieldsIf( 'rc_minor', $this->fld_flags
);
203 $this->addFieldsIf( 'rc_bot', $this->fld_flags
);
204 $this->addFieldsIf( 'rc_new', $this->fld_flags
);
205 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes
);
206 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes
);
207 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
208 $this->addFieldsIf( 'rc_logid', $this->fld_loginfo
);
209 $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo
);
210 $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo
);
211 $this->addFieldsIf( 'rc_params', $this->fld_loginfo
);
212 if ( $this->fld_redirect ||
isset( $show['redirect'] ) ||
isset( $show['!redirect'] ) )
214 $this->addTables( 'page' );
215 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
216 $this->addFields( 'page_is_redirect' );
220 if ( $this->fld_tags
) {
221 $this->addTables( 'tag_summary' );
222 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
223 $this->addFields( 'ts_tags' );
226 if ( !is_null( $params['tag'] ) ) {
227 $this->addTables( 'change_tag' );
228 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
229 $this->addWhereFld( 'ct_tag' , $params['tag'] );
230 global $wgOldChangeTagsIndex;
231 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
234 $this->token
= $params['token'];
235 $this->addOption( 'LIMIT', $params['limit'] +
1 );
236 $this->addOption( 'USE INDEX', $index );
239 /* Perform the actual query. */
240 $db = $this->getDB();
241 $res = $this->select( __METHOD__
);
243 /* Iterate through the rows, adding data extracted from them to our query result. */
244 while ( $row = $db->fetchObject( $res ) ) {
245 if ( ++
$count > $params['limit'] ) {
246 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
247 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
251 /* Extract the data from a single row. */
252 $vals = $this->extractRowInfo( $row );
254 /* Add that row's data to our final output. */
257 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
260 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
265 $db->freeResult( $res );
267 /* Format the result */
268 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
272 * Extracts from a single sql row the data needed to describe one recent change.
274 * @param $row The row from which to extract the data.
275 * @return An array mapping strings (descriptors) to their respective string values.
278 public function extractRowInfo( $row ) {
279 /* If page was moved somewhere, get the title of the move target. */
280 $movedToTitle = false;
281 if ( isset( $row->rc_moved_to_title
) && $row->rc_moved_to_title
!== '' )
282 $movedToTitle = Title
:: makeTitle( $row->rc_moved_to_ns
, $row->rc_moved_to_title
);
284 /* Determine the title of the page that has been changed. */
285 $title = Title
:: makeTitle( $row->rc_namespace
, $row->rc_title
);
287 /* Our output data. */
290 $type = intval ( $row->rc_type
);
292 /* Determine what kind of change this was. */
295 $vals['type'] = 'edit';
298 $vals['type'] = 'new';
301 $vals['type'] = 'move';
304 $vals['type'] = 'log';
306 case RC_MOVE_OVER_REDIRECT
:
307 $vals['type'] = 'move over redirect';
310 $vals['type'] = $type;
313 /* Create a new entry in the result for the title. */
314 if ( $this->fld_title
) {
315 ApiQueryBase
:: addTitleInfo( $vals, $title );
317 ApiQueryBase
:: addTitleInfo( $vals, $movedToTitle, "new_" );
320 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
321 if ( $this->fld_ids
) {
322 $vals['rcid'] = intval( $row->rc_id
);
323 $vals['pageid'] = intval( $row->rc_cur_id
);
324 $vals['revid'] = intval( $row->rc_this_oldid
);
325 $vals['old_revid'] = intval( $row->rc_last_oldid
);
328 /* Add user data and 'anon' flag, if use is anonymous. */
329 if ( $this->fld_user
) {
330 $vals['user'] = $row->rc_user_text
;
331 if ( !$row->rc_user
)
335 /* Add flags, such as new, minor, bot. */
336 if ( $this->fld_flags
) {
341 if ( $row->rc_minor
)
345 /* Add sizes of each revision. (Only available on 1.10+) */
346 if ( $this->fld_sizes
) {
347 $vals['oldlen'] = intval( $row->rc_old_len
);
348 $vals['newlen'] = intval( $row->rc_new_len
);
351 /* Add the timestamp. */
352 if ( $this->fld_timestamp
)
353 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
355 /* Add edit summary / log summary. */
356 if ( $this->fld_comment
&& isset( $row->rc_comment
) )
357 $vals['comment'] = $row->rc_comment
;
359 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
361 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment
, $title );
364 if ( $this->fld_redirect
)
365 if ( $row->page_is_redirect
)
366 $vals['redirect'] = '';
368 /* Add the patrolled flag */
369 if ( $this->fld_patrolled
&& $row->rc_patrolled
== 1 )
370 $vals['patrolled'] = '';
372 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
373 $vals['logid'] = intval( $row->rc_logid
);
374 $vals['logtype'] = $row->rc_log_type
;
375 $vals['logaction'] = $row->rc_log_action
;
376 ApiQueryLogEvents
::addLogParams( $this->getResult(),
377 $vals, $row->rc_params
,
378 $row->rc_log_type
, $row->rc_timestamp
);
381 if ( $this->fld_tags
) {
382 if ( $row->ts_tags
) {
383 $tags = explode( ',', $row->ts_tags
);
384 $this->getResult()->setIndexedTagName( $tags, 'tag' );
385 $vals['tags'] = $tags;
387 $vals['tags'] = array();
391 if ( !is_null( $this->token
) )
393 $tokenFunctions = $this->getTokenFunctions();
394 foreach ( $this->token
as $t )
396 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
397 $title, RecentChange
::newFromRow( $row ) );
398 if ( $val === false )
399 $this->setWarning( "Action '$t' is not allowed for the current user" );
401 $vals[$t . 'token'] = $val;
408 private function parseRCType( $type )
410 if ( is_array( $type ) )
413 foreach ( $type as $t )
414 $retval[] = $this->parseRCType( $t );
419 case 'edit': return RC_EDIT
;
420 case 'new': return RC_NEW
;
421 case 'log': return RC_LOG
;
425 public function getAllowedParams() {
428 ApiBase
:: PARAM_TYPE
=> 'timestamp'
431 ApiBase
:: PARAM_TYPE
=> 'timestamp'
434 ApiBase
:: PARAM_DFLT
=> 'older',
435 ApiBase
:: PARAM_TYPE
=> array (
440 'namespace' => array (
441 ApiBase
:: PARAM_ISMULTI
=> true,
442 ApiBase
:: PARAM_TYPE
=> 'namespace'
445 ApiBase
:: PARAM_TYPE
=> 'user'
447 'excludeuser' => array(
448 ApiBase
:: PARAM_TYPE
=> 'user'
452 ApiBase
:: PARAM_ISMULTI
=> true,
453 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
454 ApiBase
:: PARAM_TYPE
=> array (
470 ApiBase
:: PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
471 ApiBase
:: PARAM_ISMULTI
=> true
474 ApiBase
:: PARAM_ISMULTI
=> true,
475 ApiBase
:: PARAM_TYPE
=> array (
489 ApiBase
:: PARAM_DFLT
=> 10,
490 ApiBase
:: PARAM_TYPE
=> 'limit',
491 ApiBase
:: PARAM_MIN
=> 1,
492 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
493 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
496 ApiBase
:: PARAM_ISMULTI
=> true,
497 ApiBase
:: PARAM_TYPE
=> array (
506 public function getParamDescription() {
508 'start' => 'The timestamp to start enumerating from.',
509 'end' => 'The timestamp to end enumerating.',
510 'dir' => 'In which direction to enumerate.',
511 'namespace' => 'Filter log entries to only this namespace(s)',
512 'user' => 'Only list changes by this user',
513 'excludeuser' => 'Don\'t list changes by this user',
514 'prop' => 'Include additional pieces of information',
515 'token' => 'Which tokens to obtain for each change',
517 'Show only items that meet this criteria.',
518 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
520 'type' => 'Which types of changes to show.',
521 'limit' => 'How many total changes to return.',
522 'tag' => 'Only list changes tagged with this tag.',
526 public function getDescription() {
527 return 'Enumerate recent changes';
530 protected function getExamples() {
532 'api.php?action=query&list=recentchanges'
536 public function getVersion() {
537 return __CLASS__
. ': $Id$';