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
76 public static function getPatrolToken( $pageid, $title, $rc ) {
78 if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() ||
79 $rc->getAttribute( 'rc_type' ) != RC_NEW
) )
84 // The patrol token is always the same, let's exploit that
85 static $cachedPatrolToken = null;
86 if ( is_null( $cachedPatrolToken ) ) {
87 $cachedPatrolToken = $wgUser->getEditToken( 'patrol' );
90 return $cachedPatrolToken;
94 * Sets internal state to include the desired properties in the output.
95 * @param $prop Array associative array of properties, only keys are used here
97 public function initProperties( $prop ) {
98 $this->fld_comment
= isset( $prop['comment'] );
99 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
100 $this->fld_user
= isset( $prop['user'] );
101 $this->fld_userid
= isset( $prop['userid'] );
102 $this->fld_flags
= isset( $prop['flags'] );
103 $this->fld_timestamp
= isset( $prop['timestamp'] );
104 $this->fld_title
= isset( $prop['title'] );
105 $this->fld_ids
= isset( $prop['ids'] );
106 $this->fld_sizes
= isset( $prop['sizes'] );
107 $this->fld_redirect
= isset( $prop['redirect'] );
108 $this->fld_patrolled
= isset( $prop['patrolled'] );
109 $this->fld_loginfo
= isset( $prop['loginfo'] );
110 $this->fld_tags
= isset( $prop['tags'] );
113 public function execute() {
117 public function executeGenerator( $resultPageSet ) {
118 $this->run( $resultPageSet );
122 * Generates and outputs the result of this query based upon the provided parameters.
124 * @param $resultPageSet ApiPageSet
126 public function run( $resultPageSet = null ) {
127 $user = $this->getUser();
128 /* Get the parameters of the request. */
129 $params = $this->extractRequestParams();
131 /* Build our basic query. Namely, something along the lines of:
132 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
133 * AND rc_timestamp < $end AND rc_namespace = $namespace
134 * AND rc_deleted = '0'
136 $this->addTables( 'recentchanges' );
137 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
138 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
139 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
140 $this->addWhereFld( 'rc_deleted', 0 );
142 if ( !is_null( $params['type'] ) ) {
143 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
146 if ( !is_null( $params['show'] ) ) {
147 $show = array_flip( $params['show'] );
149 /* Check for conflicting parameters. */
150 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
151 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
152 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
153 ||
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
154 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
156 $this->dieUsageMsg( 'show' );
160 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
161 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
162 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
166 /* Add additional conditions to query depending upon parameters. */
167 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
168 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
169 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
170 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
171 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
172 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
173 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
174 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
175 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
177 // Don't throw log entries out the window here
178 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) );
181 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
182 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
185 if ( !is_null( $params['user'] ) ) {
186 $this->addWhereFld( 'rc_user_text', $params['user'] );
187 $index['recentchanges'] = 'rc_user_text';
190 if ( !is_null( $params['excludeuser'] ) ) {
191 // We don't use the rc_user_text index here because
192 // * it would require us to sort by rc_user_text before rc_timestamp
193 // * the != condition doesn't throw out too many rows anyway
194 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
197 /* Add the fields we're concerned with to our query. */
198 $this->addFields( array(
209 $showRedirects = false;
210 /* Determine what properties we need to display. */
211 if ( !is_null( $params['prop'] ) ) {
212 $prop = array_flip( $params['prop'] );
214 /* Set up internal members based upon params. */
215 $this->initProperties( $prop );
217 if ( $this->fld_patrolled
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
218 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
221 /* Add fields to our query if they are specified as a needed parameter. */
222 $this->addFieldsIf( array( 'rc_id', 'rc_this_oldid', 'rc_last_oldid' ), $this->fld_ids
);
223 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
224 $this->addFieldsIf( 'rc_user', $this->fld_user
);
225 $this->addFieldsIf( 'rc_user_text', $this->fld_user ||
$this->fld_userid
);
226 $this->addFieldsIf( array( 'rc_minor', 'rc_new', 'rc_bot' ) , $this->fld_flags
);
227 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
228 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
229 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo
);
230 $showRedirects = $this->fld_redirect ||
isset( $show['redirect'] ) ||
isset( $show['!redirect'] );
233 if ( $this->fld_tags
) {
234 $this->addTables( 'tag_summary' );
235 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
236 $this->addFields( 'ts_tags' );
239 if ( $params['toponly'] ||
$showRedirects ) {
240 $this->addTables( 'page' );
241 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
242 $this->addFields( 'page_is_redirect' );
244 if ( $params['toponly'] ) {
245 $this->addWhere( 'rc_this_oldid = page_latest' );
249 if ( !is_null( $params['tag'] ) ) {
250 $this->addTables( 'change_tag' );
251 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
252 $this->addWhereFld( 'ct_tag' , $params['tag'] );
253 global $wgOldChangeTagsIndex;
254 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
257 $this->token
= $params['token'];
258 $this->addOption( 'LIMIT', $params['limit'] +
1 );
259 $this->addOption( 'USE INDEX', $index );
262 /* Perform the actual query. */
263 $res = $this->select( __METHOD__
);
267 $result = $this->getResult();
269 /* Iterate through the rows, adding data extracted from them to our query result. */
270 foreach ( $res as $row ) {
271 if ( ++
$count > $params['limit'] ) {
272 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
273 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
277 if ( is_null( $resultPageSet ) ) {
278 /* Extract the data from a single row. */
279 $vals = $this->extractRowInfo( $row );
281 /* Add that row's data to our final output. */
285 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
287 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
) );
291 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
295 if ( is_null( $resultPageSet ) ) {
296 /* Format the result */
297 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
299 $resultPageSet->populateFromTitles( $titles );
304 * Extracts from a single sql row the data needed to describe one recent change.
306 * @param $row The row from which to extract the data.
307 * @return array An array mapping strings (descriptors) to their respective string values.
310 public function extractRowInfo( $row ) {
311 /* If page was moved somewhere, get the title of the move target. */
312 $movedToTitle = false;
313 if ( isset( $row->rc_moved_to_title
) && $row->rc_moved_to_title
!== '' ) {
314 $movedToTitle = Title
::makeTitle( $row->rc_moved_to_ns
, $row->rc_moved_to_title
);
317 /* Determine the title of the page that has been changed. */
318 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
320 /* Our output data. */
323 $type = intval( $row->rc_type
);
325 /* Determine what kind of change this was. */
328 $vals['type'] = 'edit';
331 $vals['type'] = 'new';
334 $vals['type'] = 'move';
337 $vals['type'] = 'log';
339 case RC_MOVE_OVER_REDIRECT
:
340 $vals['type'] = 'move over redirect';
343 $vals['type'] = $type;
346 /* Create a new entry in the result for the title. */
347 if ( $this->fld_title
) {
348 ApiQueryBase
::addTitleInfo( $vals, $title );
349 if ( $movedToTitle ) {
350 ApiQueryBase
::addTitleInfo( $vals, $movedToTitle, 'new_' );
354 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
355 if ( $this->fld_ids
) {
356 $vals['rcid'] = intval( $row->rc_id
);
357 $vals['pageid'] = intval( $row->rc_cur_id
);
358 $vals['revid'] = intval( $row->rc_this_oldid
);
359 $vals['old_revid'] = intval( $row->rc_last_oldid
);
362 /* Add user data and 'anon' flag, if use is anonymous. */
363 if ( $this->fld_user ||
$this->fld_userid
) {
365 if ( $this->fld_user
) {
366 $vals['user'] = $row->rc_user_text
;
369 if ( $this->fld_userid
) {
370 $vals['userid'] = $row->rc_user
;
373 if ( !$row->rc_user
) {
378 /* Add flags, such as new, minor, bot. */
379 if ( $this->fld_flags
) {
380 if ( $row->rc_bot
) {
383 if ( $row->rc_new
) {
386 if ( $row->rc_minor
) {
391 /* Add sizes of each revision. (Only available on 1.10+) */
392 if ( $this->fld_sizes
) {
393 $vals['oldlen'] = intval( $row->rc_old_len
);
394 $vals['newlen'] = intval( $row->rc_new_len
);
397 /* Add the timestamp. */
398 if ( $this->fld_timestamp
) {
399 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
402 /* Add edit summary / log summary. */
403 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
404 $vals['comment'] = $row->rc_comment
;
407 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
408 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
411 if ( $this->fld_redirect
) {
412 if ( $row->page_is_redirect
) {
413 $vals['redirect'] = '';
417 /* Add the patrolled flag */
418 if ( $this->fld_patrolled
&& $row->rc_patrolled
== 1 ) {
419 $vals['patrolled'] = '';
422 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
423 $vals['logid'] = intval( $row->rc_logid
);
424 $vals['logtype'] = $row->rc_log_type
;
425 $vals['logaction'] = $row->rc_log_action
;
426 $logEntry = DatabaseLogEntry
::newFromRow( (array)$row );
427 ApiQueryLogEvents
::addLogParams(
430 $logEntry->getParameters(),
431 $logEntry->getType(),
432 $logEntry->getSubtype(),
433 $logEntry->getTimestamp()
437 if ( $this->fld_tags
) {
438 if ( $row->ts_tags
) {
439 $tags = explode( ',', $row->ts_tags
);
440 $this->getResult()->setIndexedTagName( $tags, 'tag' );
441 $vals['tags'] = $tags;
443 $vals['tags'] = array();
447 if ( !is_null( $this->token
) ) {
448 $tokenFunctions = $this->getTokenFunctions();
449 foreach ( $this->token
as $t ) {
450 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
451 $title, RecentChange
::newFromRow( $row ) );
452 if ( $val === false ) {
453 $this->setWarning( "Action '$t' is not allowed for the current user" );
455 $vals[$t . 'token'] = $val;
463 private function parseRCType( $type ) {
464 if ( is_array( $type ) ) {
466 foreach ( $type as $t ) {
467 $retval[] = $this->parseRCType( $t );
481 public function getCacheMode( $params ) {
482 if ( isset( $params['show'] ) ) {
483 foreach ( $params['show'] as $show ) {
484 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
489 if ( isset( $params['token'] ) ) {
492 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
493 // formatComment() calls wfMsg() among other things
494 return 'anon-public-user-private';
499 public function getAllowedParams() {
502 ApiBase
::PARAM_TYPE
=> 'timestamp'
505 ApiBase
::PARAM_TYPE
=> 'timestamp'
508 ApiBase
::PARAM_DFLT
=> 'older',
509 ApiBase
::PARAM_TYPE
=> array(
514 'namespace' => array(
515 ApiBase
::PARAM_ISMULTI
=> true,
516 ApiBase
::PARAM_TYPE
=> 'namespace'
519 ApiBase
::PARAM_TYPE
=> 'user'
521 'excludeuser' => array(
522 ApiBase
::PARAM_TYPE
=> 'user'
526 ApiBase
::PARAM_ISMULTI
=> true,
527 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
528 ApiBase
::PARAM_TYPE
=> array(
545 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
546 ApiBase
::PARAM_ISMULTI
=> true
549 ApiBase
::PARAM_ISMULTI
=> true,
550 ApiBase
::PARAM_TYPE
=> array(
564 ApiBase
::PARAM_DFLT
=> 10,
565 ApiBase
::PARAM_TYPE
=> 'limit',
566 ApiBase
::PARAM_MIN
=> 1,
567 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
568 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
571 ApiBase
::PARAM_ISMULTI
=> true,
572 ApiBase
::PARAM_TYPE
=> array(
582 public function getParamDescription() {
583 $p = $this->getModulePrefix();
585 'start' => 'The timestamp to start enumerating from',
586 'end' => 'The timestamp to end enumerating',
587 'dir' => $this->getDirectionDescription( $p ),
588 'namespace' => 'Filter log entries to only this namespace(s)',
589 'user' => 'Only list changes by this user',
590 'excludeuser' => 'Don\'t list changes by this user',
592 'Include additional pieces of information',
593 ' user - Adds the user responsible for the edit and tags if they are an IP',
594 ' userid - Adds the user id responsible for the edit',
595 ' comment - Adds the comment for the edit',
596 ' parsedcomment - Adds the parsed comment for the edit',
597 ' flags - Adds flags for the edit',
598 ' timestamp - Adds timestamp of the edit',
599 ' title - Adds the page title of the edit',
600 ' ids - Adds the page ID, recent changes ID and the new and old revision ID',
601 ' sizes - Adds the new and old page length in bytes',
602 ' redirect - Tags edit if page is a redirect',
603 ' patrolled - Tags edits that have been patrolled',
604 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
605 ' tags - Lists tags for the entry',
607 'token' => 'Which tokens to obtain for each change',
609 'Show only items that meet this criteria.',
610 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
612 'type' => 'Which types of changes to show',
613 'limit' => 'How many total changes to return',
614 'tag' => 'Only list changes tagged with this tag',
615 'toponly' => 'Only list changes which are the latest revision',
619 public function getDescription() {
620 return 'Enumerate recent changes';
623 public function getPossibleErrors() {
624 return array_merge( parent
::getPossibleErrors(), array(
626 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
627 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
631 public function getExamples() {
633 'api.php?action=query&list=recentchanges'
637 public function getHelpUrls() {
638 return 'https://www.mediawiki.org/wiki/API:Recentchanges';
641 public function getVersion() {
642 return __CLASS__
. ': $Id$';