fe53b1a4cd32f1ac03787fe3658273a3a3952588
3 * API for MediaWiki 1.8+
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 ApiQueryBase
{
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 * Get an array mapping token names to their handler functions.
49 * The prototype for a token function is func($pageid, $title, $rc)
50 * it should return a token or false (permission denied)
51 * @return 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 JSON callback mode, no tokens can be obtained
60 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
64 $this->tokenFunctions
= array(
65 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
67 wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions
) );
68 return $this->tokenFunctions
;
71 public static function getPatrolToken( $pageid, $title, $rc ) {
73 if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() ||
74 $rc->getAttribute( 'rc_type' ) != RC_NEW
) )
79 // The patrol token is always the same, let's exploit that
80 static $cachedPatrolToken = null;
81 if ( !is_null( $cachedPatrolToken ) ) {
82 return $cachedPatrolToken;
85 $cachedPatrolToken = $wgUser->editToken();
86 return $cachedPatrolToken;
90 * Sets internal state to include the desired properties in the output.
91 * @param $prop associative array of properties, only keys are used here
93 public function initProperties( $prop ) {
94 $this->fld_comment
= isset( $prop['comment'] );
95 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
96 $this->fld_user
= isset( $prop['user'] );
97 $this->fld_userid
= isset( $prop['userid'] );
98 $this->fld_flags
= isset( $prop['flags'] );
99 $this->fld_timestamp
= isset( $prop['timestamp'] );
100 $this->fld_title
= isset( $prop['title'] );
101 $this->fld_ids
= isset( $prop['ids'] );
102 $this->fld_sizes
= isset( $prop['sizes'] );
103 $this->fld_redirect
= isset( $prop['redirect'] );
104 $this->fld_patrolled
= isset( $prop['patrolled'] );
105 $this->fld_loginfo
= isset( $prop['loginfo'] );
106 $this->fld_tags
= isset( $prop['tags'] );
110 * Generates and outputs the result of this query based upon the provided parameters.
112 public function execute() {
114 /* Get the parameters of the request. */
115 $params = $this->extractRequestParams();
117 /* Build our basic query. Namely, something along the lines of:
118 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
119 * AND rc_timestamp < $end AND rc_namespace = $namespace
120 * AND rc_deleted = '0'
122 $this->addTables( 'recentchanges' );
123 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
124 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
125 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
126 $this->addWhereFld( 'rc_deleted', 0 );
128 if ( !is_null( $params['type'] ) ) {
129 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
132 if ( !is_null( $params['show'] ) ) {
133 $show = array_flip( $params['show'] );
135 /* Check for conflicting parameters. */
136 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
137 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
138 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
139 ||
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
140 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
143 $this->dieUsageMsg( array( 'show' ) );
147 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
148 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
149 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
153 /* Add additional conditions to query depending upon parameters. */
154 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
155 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
156 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
157 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
158 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
159 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
160 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
161 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
162 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
164 // Don't throw log entries out the window here
165 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) );
168 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
169 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
172 if ( !is_null( $params['user'] ) ) {
173 $this->addWhereFld( 'rc_user_text', $params['user'] );
174 $index['recentchanges'] = 'rc_user_text';
177 if ( !is_null( $params['excludeuser'] ) ) {
178 // We don't use the rc_user_text index here because
179 // * it would require us to sort by rc_user_text before rc_timestamp
180 // * the != condition doesn't throw out too many rows anyway
181 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
184 /* Add the fields we're concerned with to our query. */
185 $this->addFields( array(
195 /* Determine what properties we need to display. */
196 if ( !is_null( $params['prop'] ) ) {
197 $prop = array_flip( $params['prop'] );
199 /* Set up internal members based upon params. */
200 $this->initProperties( $prop );
202 if ( $this->fld_patrolled
&& !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
203 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
206 /* Add fields to our query if they are specified as a needed parameter. */
207 $this->addFieldsIf( 'rc_id', $this->fld_ids
);
208 $this->addFieldsIf( 'rc_this_oldid', $this->fld_ids
);
209 $this->addFieldsIf( 'rc_last_oldid', $this->fld_ids
);
210 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
211 $this->addFieldsIf( 'rc_user', $this->fld_user
);
212 $this->addFieldsIf( 'rc_user_text', $this->fld_user ||
$this->fld_userid
);
213 $this->addFieldsIf( 'rc_minor', $this->fld_flags
);
214 $this->addFieldsIf( 'rc_bot', $this->fld_flags
);
215 $this->addFieldsIf( 'rc_new', $this->fld_flags
);
216 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes
);
217 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes
);
218 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
219 $this->addFieldsIf( 'rc_logid', $this->fld_loginfo
);
220 $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo
);
221 $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo
);
222 $this->addFieldsIf( 'rc_params', $this->fld_loginfo
);
223 if ( $this->fld_redirect ||
isset( $show['redirect'] ) ||
isset( $show['!redirect'] ) ) {
224 $this->addTables( 'page' );
225 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
226 $this->addFields( 'page_is_redirect' );
230 if ( $this->fld_tags
) {
231 $this->addTables( 'tag_summary' );
232 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
233 $this->addFields( 'ts_tags' );
236 if ( !is_null( $params['tag'] ) ) {
237 $this->addTables( 'change_tag' );
238 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
239 $this->addWhereFld( 'ct_tag' , $params['tag'] );
240 global $wgOldChangeTagsIndex;
241 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
244 $this->token
= $params['token'];
245 $this->addOption( 'LIMIT', $params['limit'] +
1 );
246 $this->addOption( 'USE INDEX', $index );
249 /* Perform the actual query. */
250 $res = $this->select( __METHOD__
);
252 /* Iterate through the rows, adding data extracted from them to our query result. */
253 foreach ( $res as $row ) {
254 if ( ++
$count > $params['limit'] ) {
255 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
256 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
260 /* Extract the data from a single row. */
261 $vals = $this->extractRowInfo( $row );
263 /* Add that row's data to our final output. */
267 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
269 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
274 /* Format the result */
275 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
279 * Extracts from a single sql row the data needed to describe one recent change.
281 * @param $row The row from which to extract the data.
282 * @return An array mapping strings (descriptors) to their respective string values.
285 public function extractRowInfo( $row ) {
286 /* If page was moved somewhere, get the title of the move target. */
287 $movedToTitle = false;
288 if ( isset( $row->rc_moved_to_title
) && $row->rc_moved_to_title
!== '' )
290 $movedToTitle = Title
::makeTitle( $row->rc_moved_to_ns
, $row->rc_moved_to_title
);
293 /* Determine the title of the page that has been changed. */
294 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
296 /* Our output data. */
299 $type = intval( $row->rc_type
);
301 /* Determine what kind of change this was. */
304 $vals['type'] = 'edit';
307 $vals['type'] = 'new';
310 $vals['type'] = 'move';
313 $vals['type'] = 'log';
315 case RC_MOVE_OVER_REDIRECT
:
316 $vals['type'] = 'move over redirect';
319 $vals['type'] = $type;
322 /* Create a new entry in the result for the title. */
323 if ( $this->fld_title
) {
324 ApiQueryBase
::addTitleInfo( $vals, $title );
325 if ( $movedToTitle ) {
326 ApiQueryBase
::addTitleInfo( $vals, $movedToTitle, 'new_' );
330 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
331 if ( $this->fld_ids
) {
332 $vals['rcid'] = intval( $row->rc_id
);
333 $vals['pageid'] = intval( $row->rc_cur_id
);
334 $vals['revid'] = intval( $row->rc_this_oldid
);
335 $vals['old_revid'] = intval( $row->rc_last_oldid
);
338 /* Add user data and 'anon' flag, if use is anonymous. */
339 if ( $this->fld_user ||
$this->fld_userid
) {
341 if ( $this->fld_user
) {
342 $vals['user'] = $row->rc_user_text
;
345 if ( $this->fld_userid
) {
346 $vals['userid'] = $row->rc_user
;
349 if ( !$row->rc_user
) {
354 /* Add flags, such as new, minor, bot. */
355 if ( $this->fld_flags
) {
356 if ( $row->rc_bot
) {
359 if ( $row->rc_new
) {
362 if ( $row->rc_minor
) {
367 /* Add sizes of each revision. (Only available on 1.10+) */
368 if ( $this->fld_sizes
) {
369 $vals['oldlen'] = intval( $row->rc_old_len
);
370 $vals['newlen'] = intval( $row->rc_new_len
);
373 /* Add the timestamp. */
374 if ( $this->fld_timestamp
) {
375 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
378 /* Add edit summary / log summary. */
379 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
380 $vals['comment'] = $row->rc_comment
;
383 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
385 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment
, $title );
388 if ( $this->fld_redirect
) {
389 if ( $row->page_is_redirect
) {
390 $vals['redirect'] = '';
394 /* Add the patrolled flag */
395 if ( $this->fld_patrolled
&& $row->rc_patrolled
== 1 ) {
396 $vals['patrolled'] = '';
399 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
400 $vals['logid'] = intval( $row->rc_logid
);
401 $vals['logtype'] = $row->rc_log_type
;
402 $vals['logaction'] = $row->rc_log_action
;
403 ApiQueryLogEvents
::addLogParams(
405 $vals, $row->rc_params
,
406 $row->rc_log_type
, $row->rc_timestamp
410 if ( $this->fld_tags
) {
411 if ( $row->ts_tags
) {
412 $tags = explode( ',', $row->ts_tags
);
413 $this->getResult()->setIndexedTagName( $tags, 'tag' );
414 $vals['tags'] = $tags;
416 $vals['tags'] = array();
420 if ( !is_null( $this->token
) ) {
421 $tokenFunctions = $this->getTokenFunctions();
422 foreach ( $this->token
as $t ) {
423 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
424 $title, RecentChange
::newFromRow( $row ) );
425 if ( $val === false ) {
426 $this->setWarning( "Action '$t' is not allowed for the current user" );
428 $vals[$t . 'token'] = $val;
436 private function parseRCType( $type ) {
437 if ( is_array( $type ) ) {
439 foreach ( $type as $t ) {
440 $retval[] = $this->parseRCType( $t );
454 public function getCacheMode( $params ) {
455 if ( isset( $params['show'] ) ) {
456 foreach ( $params['show'] as $show ) {
457 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
462 if ( isset( $params['token'] ) ) {
465 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
466 // formatComment() calls wfMsg() among other things
467 return 'anon-public-user-private';
472 public function getAllowedParams() {
475 ApiBase
::PARAM_TYPE
=> 'timestamp'
478 ApiBase
::PARAM_TYPE
=> 'timestamp'
481 ApiBase
::PARAM_DFLT
=> 'older',
482 ApiBase
::PARAM_TYPE
=> array(
487 'namespace' => array(
488 ApiBase
::PARAM_ISMULTI
=> true,
489 ApiBase
::PARAM_TYPE
=> 'namespace'
492 ApiBase
::PARAM_TYPE
=> 'user'
494 'excludeuser' => array(
495 ApiBase
::PARAM_TYPE
=> 'user'
499 ApiBase
::PARAM_ISMULTI
=> true,
500 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
501 ApiBase
::PARAM_TYPE
=> array(
518 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
519 ApiBase
::PARAM_ISMULTI
=> true
522 ApiBase
::PARAM_ISMULTI
=> true,
523 ApiBase
::PARAM_TYPE
=> array(
537 ApiBase
::PARAM_DFLT
=> 10,
538 ApiBase
::PARAM_TYPE
=> 'limit',
539 ApiBase
::PARAM_MIN
=> 1,
540 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
541 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
544 ApiBase
::PARAM_ISMULTI
=> true,
545 ApiBase
::PARAM_TYPE
=> array(
554 public function getParamDescription() {
556 'start' => 'The timestamp to start enumerating from',
557 'end' => 'The timestamp to end enumerating',
558 'dir' => 'In which direction to enumerate',
559 'namespace' => 'Filter log entries to only this namespace(s)',
560 'user' => 'Only list changes by this user',
561 'excludeuser' => 'Don\'t list changes by this user',
563 'Include additional pieces of information',
564 ' user - Adds the user responsible for the edit and tags if they are an IP',
565 ' userid - Adds the user id responsible for the edit',
566 ' comment - Adds the comment for the edit',
567 ' parsedcomment - Adds the parsed comment for the edit',
568 ' flags - Adds flags for the edit',
569 ' timestamp - Adds timestamp of the edit',
570 ' title - Adds the page title of the edit',
571 ' ids - Adds the page id, recent changes id and the new and old revision id',
572 ' sizes - Adds the new and old page length in bytes',
573 ' redirect - Tags edit if page is a redirect',
574 ' patrolled - Tags edits have have been patrolled',
575 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
576 ' tags - Lists tags for the entry',
578 'token' => 'Which tokens to obtain for each change',
580 'Show only items that meet this criteria.',
581 "For example, to see only minor edits done by logged-in users, set {$this->getModulePrefix()}show=minor|!anon"
583 'type' => 'Which types of changes to show',
584 'limit' => 'How many total changes to return',
585 'tag' => 'Only list changes tagged with this tag',
589 public function getDescription() {
590 return 'Enumerate recent changes';
593 public function getPossibleErrors() {
594 return array_merge( parent
::getPossibleErrors(), array(
596 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
597 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
601 protected function getExamples() {
603 'api.php?action=query&list=recentchanges'
607 public function getVersion() {
608 return __CLASS__
. ': $Id$';