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';
345 $vals['type'] = 'external';
347 case RC_MOVE_OVER_REDIRECT
:
348 $vals['type'] = 'move over redirect';
351 $vals['type'] = $type;
354 /* Create a new entry in the result for the title. */
355 if ( $this->fld_title
) {
356 ApiQueryBase
::addTitleInfo( $vals, $title );
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_type
== 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 $logEntry = DatabaseLogEntry
::newFromRow( (array)$row );
432 ApiQueryLogEvents
::addLogParams(
435 $logEntry->getParameters(),
436 $logEntry->getType(),
437 $logEntry->getSubtype(),
438 $logEntry->getTimestamp()
442 if ( $this->fld_tags
) {
443 if ( $row->ts_tags
) {
444 $tags = explode( ',', $row->ts_tags
);
445 $this->getResult()->setIndexedTagName( $tags, 'tag' );
446 $vals['tags'] = $tags;
448 $vals['tags'] = array();
452 if ( !is_null( $this->token
) ) {
453 $tokenFunctions = $this->getTokenFunctions();
454 foreach ( $this->token
as $t ) {
455 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
456 $title, RecentChange
::newFromRow( $row ) );
457 if ( $val === false ) {
458 $this->setWarning( "Action '$t' is not allowed for the current user" );
460 $vals[$t . 'token'] = $val;
468 private function parseRCType( $type ) {
469 if ( is_array( $type ) ) {
471 foreach ( $type as $t ) {
472 $retval[] = $this->parseRCType( $t );
488 public function getCacheMode( $params ) {
489 if ( isset( $params['show'] ) ) {
490 foreach ( $params['show'] as $show ) {
491 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
496 if ( isset( $params['token'] ) ) {
499 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
500 // formatComment() calls wfMessage() among other things
501 return 'anon-public-user-private';
506 public function getAllowedParams() {
509 ApiBase
::PARAM_TYPE
=> 'timestamp'
512 ApiBase
::PARAM_TYPE
=> 'timestamp'
515 ApiBase
::PARAM_DFLT
=> 'older',
516 ApiBase
::PARAM_TYPE
=> array(
521 'namespace' => array(
522 ApiBase
::PARAM_ISMULTI
=> true,
523 ApiBase
::PARAM_TYPE
=> 'namespace'
526 ApiBase
::PARAM_TYPE
=> 'user'
528 'excludeuser' => array(
529 ApiBase
::PARAM_TYPE
=> 'user'
533 ApiBase
::PARAM_ISMULTI
=> true,
534 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
535 ApiBase
::PARAM_TYPE
=> array(
552 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
553 ApiBase
::PARAM_ISMULTI
=> true
556 ApiBase
::PARAM_ISMULTI
=> true,
557 ApiBase
::PARAM_TYPE
=> array(
571 ApiBase
::PARAM_DFLT
=> 10,
572 ApiBase
::PARAM_TYPE
=> 'limit',
573 ApiBase
::PARAM_MIN
=> 1,
574 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
575 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
578 ApiBase
::PARAM_ISMULTI
=> true,
579 ApiBase
::PARAM_TYPE
=> array(
590 public function getParamDescription() {
591 $p = $this->getModulePrefix();
593 'start' => 'The timestamp to start enumerating from',
594 'end' => 'The timestamp to end enumerating',
595 'dir' => $this->getDirectionDescription( $p ),
596 'namespace' => 'Filter log entries to only this namespace(s)',
597 'user' => 'Only list changes by this user',
598 'excludeuser' => 'Don\'t list changes by this user',
600 'Include additional pieces of information',
601 ' user - Adds the user responsible for the edit and tags if they are an IP',
602 ' userid - Adds the user id responsible for the edit',
603 ' comment - Adds the comment for the edit',
604 ' parsedcomment - Adds the parsed comment for the edit',
605 ' flags - Adds flags for the edit',
606 ' timestamp - Adds timestamp of the edit',
607 ' title - Adds the page title of the edit',
608 ' ids - Adds the page ID, recent changes ID and the new and old revision ID',
609 ' sizes - Adds the new and old page length in bytes',
610 ' redirect - Tags edit if page is a redirect',
611 ' patrolled - Tags edits that have been patrolled',
612 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
613 ' tags - Lists tags for the entry',
615 'token' => 'Which tokens to obtain for each change',
617 'Show only items that meet this criteria.',
618 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
620 'type' => 'Which types of changes to show',
621 'limit' => 'How many total changes to return',
622 'tag' => 'Only list changes tagged with this tag',
623 'toponly' => 'Only list changes which are the latest revision',
627 public function getResultProperties() {
632 ApiBase
::PROP_TYPE
=> array(
645 ApiBase
::PROP_TYPE
=> 'namespace',
646 ApiBase
::PROP_NULLABLE
=> true
648 'new_title' => array(
649 ApiBase
::PROP_TYPE
=> 'string',
650 ApiBase
::PROP_NULLABLE
=> true
655 'pageid' => 'integer',
656 'revid' => 'integer',
657 'old_revid' => 'integer'
664 'userid' => 'integer',
673 'oldlen' => 'integer',
674 'newlen' => 'integer'
676 'timestamp' => array(
677 'timestamp' => 'timestamp'
681 ApiBase
::PROP_TYPE
=> 'string',
682 ApiBase
::PROP_NULLABLE
=> true
685 'parsedcomment' => array(
686 'parsedcomment' => array(
687 ApiBase
::PROP_TYPE
=> 'string',
688 ApiBase
::PROP_NULLABLE
=> true
692 'redirect' => 'boolean'
694 'patrolled' => array(
695 'patrolled' => 'boolean'
699 ApiBase
::PROP_TYPE
=> 'integer',
700 ApiBase
::PROP_NULLABLE
=> true
703 ApiBase
::PROP_TYPE
=> $wgLogTypes,
704 ApiBase
::PROP_NULLABLE
=> true
706 'logaction' => array(
707 ApiBase
::PROP_TYPE
=> 'string',
708 ApiBase
::PROP_NULLABLE
=> true
713 self
::addTokenProperties( $props, $this->getTokenFunctions() );
718 public function getDescription() {
719 return 'Enumerate recent changes';
722 public function getPossibleErrors() {
723 return array_merge( parent
::getPossibleErrors(), array(
725 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
726 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
730 public function getExamples() {
732 'api.php?action=query&list=recentchanges'
736 public function getHelpUrls() {
737 return 'https://www.mediawiki.org/wiki/API:Recentchanges';