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
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(
220 $showRedirects = false;
221 /* Determine what properties we need to display. */
222 if ( !is_null( $params['prop'] ) ) {
223 $prop = array_flip( $params['prop'] );
225 /* Set up internal members based upon params. */
226 $this->initProperties( $prop );
228 if ( $this->fld_patrolled
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
229 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
232 /* Add fields to our query if they are specified as a needed parameter. */
233 $this->addFieldsIf( array( 'rc_id', 'rc_this_oldid', 'rc_last_oldid' ), $this->fld_ids
);
234 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
235 $this->addFieldsIf( 'rc_user', $this->fld_user
);
236 $this->addFieldsIf( 'rc_user_text', $this->fld_user ||
$this->fld_userid
);
237 $this->addFieldsIf( array( 'rc_minor', 'rc_type', 'rc_bot' ) , $this->fld_flags
);
238 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
239 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
240 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo
);
241 $showRedirects = $this->fld_redirect ||
isset( $show['redirect'] ) ||
isset( $show['!redirect'] );
244 if ( $this->fld_tags
) {
245 $this->addTables( 'tag_summary' );
246 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
247 $this->addFields( 'ts_tags' );
250 if ( $params['toponly'] ||
$showRedirects ) {
251 $this->addTables( 'page' );
252 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
253 $this->addFields( 'page_is_redirect' );
255 if ( $params['toponly'] ) {
256 $this->addWhere( 'rc_this_oldid = page_latest' );
260 if ( !is_null( $params['tag'] ) ) {
261 $this->addTables( 'change_tag' );
262 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
263 $this->addWhereFld( 'ct_tag' , $params['tag'] );
264 global $wgOldChangeTagsIndex;
265 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
268 $this->token
= $params['token'];
269 $this->addOption( 'LIMIT', $params['limit'] +
1 );
270 $this->addOption( 'USE INDEX', $index );
273 /* Perform the actual query. */
274 $res = $this->select( __METHOD__
);
278 $result = $this->getResult();
280 /* Iterate through the rows, adding data extracted from them to our query result. */
281 foreach ( $res as $row ) {
282 if ( ++
$count > $params['limit'] ) {
283 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
284 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
288 if ( is_null( $resultPageSet ) ) {
289 /* Extract the data from a single row. */
290 $vals = $this->extractRowInfo( $row );
292 /* Add that row's data to our final output. */
296 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
298 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
302 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
306 if ( is_null( $resultPageSet ) ) {
307 /* Format the result */
308 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
310 $resultPageSet->populateFromTitles( $titles );
315 * Extracts from a single sql row the data needed to describe one recent change.
317 * @param $row The row from which to extract the data.
318 * @return array An array mapping strings (descriptors) to their respective string values.
321 public function extractRowInfo( $row ) {
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 );
356 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
357 if ( $this->fld_ids
) {
358 $vals['rcid'] = intval( $row->rc_id
);
359 $vals['pageid'] = intval( $row->rc_cur_id
);
360 $vals['revid'] = intval( $row->rc_this_oldid
);
361 $vals['old_revid'] = intval( $row->rc_last_oldid
);
364 /* Add user data and 'anon' flag, if use is anonymous. */
365 if ( $this->fld_user ||
$this->fld_userid
) {
367 if ( $this->fld_user
) {
368 $vals['user'] = $row->rc_user_text
;
371 if ( $this->fld_userid
) {
372 $vals['userid'] = $row->rc_user
;
375 if ( !$row->rc_user
) {
380 /* Add flags, such as new, minor, bot. */
381 if ( $this->fld_flags
) {
382 if ( $row->rc_bot
) {
385 if ( $row->rc_type
== RC_NEW
) {
388 if ( $row->rc_minor
) {
393 /* Add sizes of each revision. (Only available on 1.10+) */
394 if ( $this->fld_sizes
) {
395 $vals['oldlen'] = intval( $row->rc_old_len
);
396 $vals['newlen'] = intval( $row->rc_new_len
);
399 /* Add the timestamp. */
400 if ( $this->fld_timestamp
) {
401 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
404 /* Add edit summary / log summary. */
405 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
406 $vals['comment'] = $row->rc_comment
;
409 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
410 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
413 if ( $this->fld_redirect
) {
414 if ( $row->page_is_redirect
) {
415 $vals['redirect'] = '';
419 /* Add the patrolled flag */
420 if ( $this->fld_patrolled
&& $row->rc_patrolled
== 1 ) {
421 $vals['patrolled'] = '';
424 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
425 $vals['logid'] = intval( $row->rc_logid
);
426 $vals['logtype'] = $row->rc_log_type
;
427 $vals['logaction'] = $row->rc_log_action
;
428 $logEntry = DatabaseLogEntry
::newFromRow( (array)$row );
429 ApiQueryLogEvents
::addLogParams(
432 $logEntry->getParameters(),
433 $logEntry->getType(),
434 $logEntry->getSubtype(),
435 $logEntry->getTimestamp()
439 if ( $this->fld_tags
) {
440 if ( $row->ts_tags
) {
441 $tags = explode( ',', $row->ts_tags
);
442 $this->getResult()->setIndexedTagName( $tags, 'tag' );
443 $vals['tags'] = $tags;
445 $vals['tags'] = array();
449 if ( !is_null( $this->token
) ) {
450 $tokenFunctions = $this->getTokenFunctions();
451 foreach ( $this->token
as $t ) {
452 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
453 $title, RecentChange
::newFromRow( $row ) );
454 if ( $val === false ) {
455 $this->setWarning( "Action '$t' is not allowed for the current user" );
457 $vals[$t . 'token'] = $val;
465 private function parseRCType( $type ) {
466 if ( is_array( $type ) ) {
468 foreach ( $type as $t ) {
469 $retval[] = $this->parseRCType( $t );
483 public function getCacheMode( $params ) {
484 if ( isset( $params['show'] ) ) {
485 foreach ( $params['show'] as $show ) {
486 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
491 if ( isset( $params['token'] ) ) {
494 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
495 // formatComment() calls wfMessage() among other things
496 return 'anon-public-user-private';
501 public function getAllowedParams() {
504 ApiBase
::PARAM_TYPE
=> 'timestamp'
507 ApiBase
::PARAM_TYPE
=> 'timestamp'
510 ApiBase
::PARAM_DFLT
=> 'older',
511 ApiBase
::PARAM_TYPE
=> array(
516 'namespace' => array(
517 ApiBase
::PARAM_ISMULTI
=> true,
518 ApiBase
::PARAM_TYPE
=> 'namespace'
521 ApiBase
::PARAM_TYPE
=> 'user'
523 'excludeuser' => array(
524 ApiBase
::PARAM_TYPE
=> 'user'
528 ApiBase
::PARAM_ISMULTI
=> true,
529 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
530 ApiBase
::PARAM_TYPE
=> array(
547 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
548 ApiBase
::PARAM_ISMULTI
=> true
551 ApiBase
::PARAM_ISMULTI
=> true,
552 ApiBase
::PARAM_TYPE
=> array(
566 ApiBase
::PARAM_DFLT
=> 10,
567 ApiBase
::PARAM_TYPE
=> 'limit',
568 ApiBase
::PARAM_MIN
=> 1,
569 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
570 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
573 ApiBase
::PARAM_ISMULTI
=> true,
574 ApiBase
::PARAM_TYPE
=> array(
584 public function getParamDescription() {
585 $p = $this->getModulePrefix();
587 'start' => 'The timestamp to start enumerating from',
588 'end' => 'The timestamp to end enumerating',
589 'dir' => $this->getDirectionDescription( $p ),
590 'namespace' => 'Filter log entries to only this namespace(s)',
591 'user' => 'Only list changes by this user',
592 'excludeuser' => 'Don\'t list changes by this user',
594 'Include additional pieces of information',
595 ' user - Adds the user responsible for the edit and tags if they are an IP',
596 ' userid - Adds the user id responsible for the edit',
597 ' comment - Adds the comment for the edit',
598 ' parsedcomment - Adds the parsed comment for the edit',
599 ' flags - Adds flags for the edit',
600 ' timestamp - Adds timestamp of the edit',
601 ' title - Adds the page title of the edit',
602 ' ids - Adds the page ID, recent changes ID and the new and old revision ID',
603 ' sizes - Adds the new and old page length in bytes',
604 ' redirect - Tags edit if page is a redirect',
605 ' patrolled - Tags edits that have been patrolled',
606 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
607 ' tags - Lists tags for the entry',
609 'token' => 'Which tokens to obtain for each change',
611 'Show only items that meet this criteria.',
612 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
614 'type' => 'Which types of changes to show',
615 'limit' => 'How many total changes to return',
616 'tag' => 'Only list changes tagged with this tag',
617 'toponly' => 'Only list changes which are the latest revision',
621 public function getResultProperties() {
626 ApiBase
::PROP_TYPE
=> array(
639 ApiBase
::PROP_TYPE
=> 'namespace',
640 ApiBase
::PROP_NULLABLE
=> true
642 'new_title' => array(
643 ApiBase
::PROP_TYPE
=> 'string',
644 ApiBase
::PROP_NULLABLE
=> true
649 'pageid' => 'integer',
650 'revid' => 'integer',
651 'old_revid' => 'integer'
658 'userid' => 'integer',
667 'oldlen' => 'integer',
668 'newlen' => 'integer'
670 'timestamp' => array(
671 'timestamp' => 'timestamp'
675 ApiBase
::PROP_TYPE
=> 'string',
676 ApiBase
::PROP_NULLABLE
=> true
679 'parsedcomment' => array(
680 'parsedcomment' => array(
681 ApiBase
::PROP_TYPE
=> 'string',
682 ApiBase
::PROP_NULLABLE
=> true
686 'redirect' => 'boolean'
688 'patrolled' => array(
689 'patrolled' => 'boolean'
693 ApiBase
::PROP_TYPE
=> 'integer',
694 ApiBase
::PROP_NULLABLE
=> true
697 ApiBase
::PROP_TYPE
=> $wgLogTypes,
698 ApiBase
::PROP_NULLABLE
=> true
700 'logaction' => array(
701 ApiBase
::PROP_TYPE
=> 'string',
702 ApiBase
::PROP_NULLABLE
=> true
707 self
::addTokenProperties( $props, $this->getTokenFunctions() );
712 public function getDescription() {
713 return 'Enumerate recent changes';
716 public function getPossibleErrors() {
717 return array_merge( parent
::getPossibleErrors(), array(
719 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
720 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
724 public function getExamples() {
726 'api.php?action=query&list=recentchanges'
730 public function getHelpUrls() {
731 return 'https://www.mediawiki.org/wiki/API:Recentchanges';
734 public function getVersion() {
735 return __CLASS__
. ': $Id$';