7d62f33f48fc524078f9e36bd31491bb0af360c1
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,
47 $fld_tags = false, $token = array();
49 private $tokenFunctions;
52 * Get an array mapping token names to their handler functions.
53 * The prototype for a token function is func($pageid, $title, $rc)
54 * it should return a token or false (permission denied)
55 * @return array array(tokenname => function)
57 protected function getTokenFunctions() {
58 // Don't call the hooks twice
59 if ( isset( $this->tokenFunctions
) ) {
60 return $this->tokenFunctions
;
63 // If we're in JSON callback mode, no tokens can be obtained
64 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
68 $this->tokenFunctions
= array(
69 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
71 wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions
) );
72 return $this->tokenFunctions
;
78 * @param $rc RecentChange
81 public static function getPatrolToken( $pageid, $title, $rc ) {
83 if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() ||
84 $rc->getAttribute( 'rc_type' ) != RC_NEW
) )
89 // The patrol token is always the same, let's exploit that
90 static $cachedPatrolToken = null;
91 if ( is_null( $cachedPatrolToken ) ) {
92 $cachedPatrolToken = $wgUser->getEditToken( 'patrol' );
95 return $cachedPatrolToken;
99 * Sets internal state to include the desired properties in the output.
100 * @param $prop Array associative array of properties, only keys are used here
102 public function initProperties( $prop ) {
103 $this->fld_comment
= isset( $prop['comment'] );
104 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
105 $this->fld_user
= isset( $prop['user'] );
106 $this->fld_userid
= isset( $prop['userid'] );
107 $this->fld_flags
= isset( $prop['flags'] );
108 $this->fld_timestamp
= isset( $prop['timestamp'] );
109 $this->fld_title
= isset( $prop['title'] );
110 $this->fld_ids
= isset( $prop['ids'] );
111 $this->fld_sizes
= isset( $prop['sizes'] );
112 $this->fld_redirect
= isset( $prop['redirect'] );
113 $this->fld_patrolled
= isset( $prop['patrolled'] );
114 $this->fld_loginfo
= isset( $prop['loginfo'] );
115 $this->fld_tags
= isset( $prop['tags'] );
118 public function execute() {
122 public function executeGenerator( $resultPageSet ) {
123 $this->run( $resultPageSet );
127 * Generates and outputs the result of this query based upon the provided parameters.
129 * @param $resultPageSet ApiPageSet
131 public function run( $resultPageSet = null ) {
132 $user = $this->getUser();
133 /* Get the parameters of the request. */
134 $params = $this->extractRequestParams();
136 /* Build our basic query. Namely, something along the lines of:
137 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
138 * AND rc_timestamp < $end AND rc_namespace = $namespace
139 * AND rc_deleted = '0'
141 $this->addTables( 'recentchanges' );
142 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
143 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
144 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
145 $this->addWhereFld( 'rc_deleted', 0 );
147 if ( !is_null( $params['type'] ) ) {
148 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
151 if ( !is_null( $params['show'] ) ) {
152 $show = array_flip( $params['show'] );
154 /* Check for conflicting parameters. */
155 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
156 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
157 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
158 ||
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
159 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
161 $this->dieUsageMsg( 'show' );
165 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
166 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
167 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
171 /* Add additional conditions to query depending upon parameters. */
172 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
173 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
174 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
175 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
176 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
177 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
178 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
179 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
180 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
182 // Don't throw log entries out the window here
183 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) );
186 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
187 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
190 if ( !is_null( $params['user'] ) ) {
191 $this->addWhereFld( 'rc_user_text', $params['user'] );
192 $index['recentchanges'] = 'rc_user_text';
195 if ( !is_null( $params['excludeuser'] ) ) {
196 // We don't use the rc_user_text index here because
197 // * it would require us to sort by rc_user_text before rc_timestamp
198 // * the != condition doesn't throw out too many rows anyway
199 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
202 /* Add the fields we're concerned with to our query. */
203 $this->addFields( array(
214 $showRedirects = false;
215 /* Determine what properties we need to display. */
216 if ( !is_null( $params['prop'] ) ) {
217 $prop = array_flip( $params['prop'] );
219 /* Set up internal members based upon params. */
220 $this->initProperties( $prop );
222 if ( $this->fld_patrolled
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
223 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
226 /* Add fields to our query if they are specified as a needed parameter. */
227 $this->addFieldsIf( array( 'rc_id', 'rc_this_oldid', 'rc_last_oldid' ), $this->fld_ids
);
228 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
229 $this->addFieldsIf( 'rc_user', $this->fld_user
);
230 $this->addFieldsIf( 'rc_user_text', $this->fld_user ||
$this->fld_userid
);
231 $this->addFieldsIf( array( 'rc_minor', 'rc_new', 'rc_bot' ) , $this->fld_flags
);
232 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
233 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
234 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo
);
235 $showRedirects = $this->fld_redirect ||
isset( $show['redirect'] ) ||
isset( $show['!redirect'] );
238 if ( $this->fld_tags
) {
239 $this->addTables( 'tag_summary' );
240 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
241 $this->addFields( 'ts_tags' );
244 if ( $params['toponly'] ||
$showRedirects ) {
245 $this->addTables( 'page' );
246 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
247 $this->addFields( 'page_is_redirect' );
249 if ( $params['toponly'] ) {
250 $this->addWhere( 'rc_this_oldid = page_latest' );
254 if ( !is_null( $params['tag'] ) ) {
255 $this->addTables( 'change_tag' );
256 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
257 $this->addWhereFld( 'ct_tag' , $params['tag'] );
258 global $wgOldChangeTagsIndex;
259 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
262 $this->token
= $params['token'];
263 $this->addOption( 'LIMIT', $params['limit'] +
1 );
264 $this->addOption( 'USE INDEX', $index );
267 /* Perform the actual query. */
268 $res = $this->select( __METHOD__
);
272 $result = $this->getResult();
274 /* Iterate through the rows, adding data extracted from them to our query result. */
275 foreach ( $res as $row ) {
276 if ( ++
$count > $params['limit'] ) {
277 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
278 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
282 if ( is_null( $resultPageSet ) ) {
283 /* Extract the data from a single row. */
284 $vals = $this->extractRowInfo( $row );
286 /* Add that row's data to our final output. */
290 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
292 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
296 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
300 if ( is_null( $resultPageSet ) ) {
301 /* Format the result */
302 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
304 $resultPageSet->populateFromTitles( $titles );
309 * Extracts from a single sql row the data needed to describe one recent change.
311 * @param $row The row from which to extract the data.
312 * @return An array mapping strings (descriptors) to their respective string values.
315 public function extractRowInfo( $row ) {
316 /* If page was moved somewhere, get the title of the move target. */
317 $movedToTitle = false;
318 if ( isset( $row->rc_moved_to_title
) && $row->rc_moved_to_title
!== '' ) {
319 $movedToTitle = Title
::makeTitle( $row->rc_moved_to_ns
, $row->rc_moved_to_title
);
322 /* Determine the title of the page that has been changed. */
323 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
325 /* Our output data. */
328 $type = intval( $row->rc_type
);
330 /* Determine what kind of change this was. */
333 $vals['type'] = 'edit';
336 $vals['type'] = 'new';
339 $vals['type'] = 'move';
342 $vals['type'] = 'log';
344 case RC_MOVE_OVER_REDIRECT
:
345 $vals['type'] = 'move over redirect';
348 $vals['type'] = $type;
351 /* Create a new entry in the result for the title. */
352 if ( $this->fld_title
) {
353 ApiQueryBase
::addTitleInfo( $vals, $title );
354 if ( $movedToTitle ) {
355 ApiQueryBase
::addTitleInfo( $vals, $movedToTitle, 'new_' );
359 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
360 if ( $this->fld_ids
) {
361 $vals['rcid'] = intval( $row->rc_id
);
362 $vals['pageid'] = intval( $row->rc_cur_id
);
363 $vals['revid'] = intval( $row->rc_this_oldid
);
364 $vals['old_revid'] = intval( $row->rc_last_oldid
);
367 /* Add user data and 'anon' flag, if use is anonymous. */
368 if ( $this->fld_user ||
$this->fld_userid
) {
370 if ( $this->fld_user
) {
371 $vals['user'] = $row->rc_user_text
;
374 if ( $this->fld_userid
) {
375 $vals['userid'] = $row->rc_user
;
378 if ( !$row->rc_user
) {
383 /* Add flags, such as new, minor, bot. */
384 if ( $this->fld_flags
) {
385 if ( $row->rc_bot
) {
388 if ( $row->rc_new
) {
391 if ( $row->rc_minor
) {
396 /* Add sizes of each revision. (Only available on 1.10+) */
397 if ( $this->fld_sizes
) {
398 $vals['oldlen'] = intval( $row->rc_old_len
);
399 $vals['newlen'] = intval( $row->rc_new_len
);
402 /* Add the timestamp. */
403 if ( $this->fld_timestamp
) {
404 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
407 /* Add edit summary / log summary. */
408 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
409 $vals['comment'] = $row->rc_comment
;
412 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
413 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
416 if ( $this->fld_redirect
) {
417 if ( $row->page_is_redirect
) {
418 $vals['redirect'] = '';
422 /* Add the patrolled flag */
423 if ( $this->fld_patrolled
&& $row->rc_patrolled
== 1 ) {
424 $vals['patrolled'] = '';
427 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
428 $vals['logid'] = intval( $row->rc_logid
);
429 $vals['logtype'] = $row->rc_log_type
;
430 $vals['logaction'] = $row->rc_log_action
;
431 ApiQueryLogEvents
::addLogParams(
441 if ( $this->fld_tags
) {
442 if ( $row->ts_tags
) {
443 $tags = explode( ',', $row->ts_tags
);
444 $this->getResult()->setIndexedTagName( $tags, 'tag' );
445 $vals['tags'] = $tags;
447 $vals['tags'] = array();
451 if ( !is_null( $this->token
) ) {
452 $tokenFunctions = $this->getTokenFunctions();
453 foreach ( $this->token
as $t ) {
454 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
455 $title, RecentChange
::newFromRow( $row ) );
456 if ( $val === false ) {
457 $this->setWarning( "Action '$t' is not allowed for the current user" );
459 $vals[$t . 'token'] = $val;
467 private function parseRCType( $type ) {
468 if ( is_array( $type ) ) {
470 foreach ( $type as $t ) {
471 $retval[] = $this->parseRCType( $t );
485 public function getCacheMode( $params ) {
486 if ( isset( $params['show'] ) ) {
487 foreach ( $params['show'] as $show ) {
488 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
493 if ( isset( $params['token'] ) ) {
496 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
497 // formatComment() calls wfMsg() among other things
498 return 'anon-public-user-private';
503 public function getAllowedParams() {
506 ApiBase
::PARAM_TYPE
=> 'timestamp'
509 ApiBase
::PARAM_TYPE
=> 'timestamp'
512 ApiBase
::PARAM_DFLT
=> 'older',
513 ApiBase
::PARAM_TYPE
=> array(
518 'namespace' => array(
519 ApiBase
::PARAM_ISMULTI
=> true,
520 ApiBase
::PARAM_TYPE
=> 'namespace'
523 ApiBase
::PARAM_TYPE
=> 'user'
525 'excludeuser' => array(
526 ApiBase
::PARAM_TYPE
=> 'user'
530 ApiBase
::PARAM_ISMULTI
=> true,
531 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
532 ApiBase
::PARAM_TYPE
=> array(
549 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
550 ApiBase
::PARAM_ISMULTI
=> true
553 ApiBase
::PARAM_ISMULTI
=> true,
554 ApiBase
::PARAM_TYPE
=> array(
568 ApiBase
::PARAM_DFLT
=> 10,
569 ApiBase
::PARAM_TYPE
=> 'limit',
570 ApiBase
::PARAM_MIN
=> 1,
571 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
572 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
575 ApiBase
::PARAM_ISMULTI
=> true,
576 ApiBase
::PARAM_TYPE
=> array(
586 public function getParamDescription() {
587 $p = $this->getModulePrefix();
589 'start' => 'The timestamp to start enumerating from',
590 'end' => 'The timestamp to end enumerating',
591 'dir' => $this->getDirectionDescription( $p ),
592 'namespace' => 'Filter log entries to only this namespace(s)',
593 'user' => 'Only list changes by this user',
594 'excludeuser' => 'Don\'t list changes by this user',
596 'Include additional pieces of information',
597 ' user - Adds the user responsible for the edit and tags if they are an IP',
598 ' userid - Adds the user id responsible for the edit',
599 ' comment - Adds the comment for the edit',
600 ' parsedcomment - Adds the parsed comment for the edit',
601 ' flags - Adds flags for the edit',
602 ' timestamp - Adds timestamp of the edit',
603 ' title - Adds the page title of the edit',
604 ' ids - Adds the page ID, recent changes ID and the new and old revision ID',
605 ' sizes - Adds the new and old page length in bytes',
606 ' redirect - Tags edit if page is a redirect',
607 ' patrolled - Tags edits that have been patrolled',
608 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
609 ' tags - Lists tags for the entry',
611 'token' => 'Which tokens to obtain for each change',
613 'Show only items that meet this criteria.',
614 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
616 'type' => 'Which types of changes to show',
617 'limit' => 'How many total changes to return',
618 'tag' => 'Only list changes tagged with this tag',
619 'toponly' => 'Only list changes which are the latest revision',
623 public function getDescription() {
624 return 'Enumerate recent changes';
627 public function getPossibleErrors() {
628 return array_merge( parent
::getPossibleErrors(), array(
630 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
631 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
635 public function getExamples() {
637 'api.php?action=query&list=recentchanges'
641 public function getHelpUrls() {
642 return 'http://www.mediawiki.org/wiki/API:Recentchanges';
645 public function getVersion() {
646 return __CLASS__
. ': $Id$';