4 * Created on Oct 19, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * A query action to enumerate the recent changes that were done to the wiki.
33 * Various filters are supported.
37 class ApiQueryRecentChanges
extends ApiQueryBase
{
39 public function __construct($query, $moduleName) {
40 parent
:: __construct($query, $moduleName, 'rc');
43 private $fld_comment = false, $fld_user = false, $fld_flags = false,
44 $fld_timestamp = false, $fld_title = false, $fld_ids = false,
47 protected function getTokenFunctions() {
48 // tokenname => function
49 // function prototype is func($pageid, $title, $rev)
50 // should return token or false
52 // Don't call the hooks twice
53 if(isset($this->tokenFunctions
))
54 return $this->tokenFunctions
;
56 // If we're in JSON callback mode, no tokens can be obtained
57 if(!is_null($this->getMain()->getRequest()->getVal('callback')))
60 $this->tokenFunctions
= array(
61 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
63 wfRunHooks('APIQueryRecentChangesTokens', array(&$this->tokenFunctions
));
64 return $this->tokenFunctions
;
67 public static function getPatrolToken($pageid, $title, $rc)
70 if(!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
73 // The patrol token is always the same, let's exploit that
74 static $cachedPatrolToken = null;
75 if(!is_null($cachedPatrolToken))
76 return $cachedPatrolToken;
78 $cachedPatrolToken = $wgUser->editToken();
79 return $cachedPatrolToken;
83 * Generates and outputs the result of this query based upon the provided parameters.
85 public function execute() {
86 /* Get the parameters of the request. */
87 $params = $this->extractRequestParams();
89 /* Build our basic query. Namely, something along the lines of:
90 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
91 * AND rc_timestamp < $end AND rc_namespace = $namespace
92 * AND rc_deleted = '0'
95 $this->addTables('recentchanges');
96 $this->addOption('USE INDEX', array('recentchanges' => 'rc_timestamp'));
97 $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
98 $this->addWhereFld('rc_namespace', $params['namespace']);
99 $this->addWhereFld('rc_deleted', 0);
101 if(!is_null($params['type']))
102 $this->addWhereFld('rc_type', $this->parseRCType($params['type']));
104 if (!is_null($params['show'])) {
105 $show = array_flip($params['show']);
107 /* Check for conflicting parameters. */
108 if ((isset ($show['minor']) && isset ($show['!minor']))
109 ||
(isset ($show['bot']) && isset ($show['!bot']))
110 ||
(isset ($show['anon']) && isset ($show['!anon']))
111 ||
(isset ($show['redirect']) && isset ($show['!redirect']))
112 ||
(isset ($show['patrolled']) && isset ($show['!patrolled']))) {
114 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
119 if((isset($show['patrolled']) ||
isset($show['!patrolled'])) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
120 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
122 /* Add additional conditions to query depending upon parameters. */
123 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
124 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
125 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
126 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
127 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
128 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
129 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
130 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
131 $this->addWhereIf('page_is_redirect = 1', isset ($show['redirect']));
132 // Don't throw log entries out the window here
133 $this->addWhereIf('page_is_redirect = 0 OR page_is_redirect IS NULL', isset ($show['!redirect']));
136 /* Add the fields we're concerned with to out query. */
137 $this->addFields(array (
147 /* Determine what properties we need to display. */
148 if (!is_null($params['prop'])) {
149 $prop = array_flip($params['prop']);
151 /* Set up internal members based upon params. */
152 $this->fld_comment
= isset ($prop['comment']);
153 $this->fld_user
= isset ($prop['user']);
154 $this->fld_flags
= isset ($prop['flags']);
155 $this->fld_timestamp
= isset ($prop['timestamp']);
156 $this->fld_title
= isset ($prop['title']);
157 $this->fld_ids
= isset ($prop['ids']);
158 $this->fld_sizes
= isset ($prop['sizes']);
159 $this->fld_redirect
= isset($prop['redirect']);
160 $this->fld_patrolled
= isset($prop['patrolled']);
161 $this->fld_loginfo
= isset($prop['loginfo']);
164 if($this->fld_patrolled
&& !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
165 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
167 /* Add fields to our query if they are specified as a needed parameter. */
168 $this->addFieldsIf('rc_id', $this->fld_ids
);
169 $this->addFieldsIf('rc_this_oldid', $this->fld_ids
);
170 $this->addFieldsIf('rc_last_oldid', $this->fld_ids
);
171 $this->addFieldsIf('rc_comment', $this->fld_comment
);
172 $this->addFieldsIf('rc_user', $this->fld_user
);
173 $this->addFieldsIf('rc_user_text', $this->fld_user
);
174 $this->addFieldsIf('rc_minor', $this->fld_flags
);
175 $this->addFieldsIf('rc_bot', $this->fld_flags
);
176 $this->addFieldsIf('rc_new', $this->fld_flags
);
177 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
178 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
179 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled
);
180 $this->addFieldsIf('rc_logid', $this->fld_loginfo
);
181 $this->addFieldsIf('rc_log_type', $this->fld_loginfo
);
182 $this->addFieldsIf('rc_log_action', $this->fld_loginfo
);
183 $this->addFieldsIf('rc_params', $this->fld_loginfo
);
184 if($this->fld_redirect ||
isset($show['redirect']) ||
isset($show['!redirect']))
186 $this->addTables('page');
187 $this->addJoinConds(array('page' => array('LEFT JOIN', array('rc_namespace=page_namespace', 'rc_title=page_title'))));
188 $this->addFields('page_is_redirect');
191 $this->token
= $params['token'];
192 $this->addOption('LIMIT', $params['limit'] +
1);
195 /* Perform the actual query. */
196 $db = $this->getDB();
197 $res = $this->select(__METHOD__
);
199 /* Iterate through the rows, adding data extracted from them to our query result. */
200 while ($row = $db->fetchObject($res)) {
201 if (++
$count > $params['limit']) {
202 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
203 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
207 /* Extract the data from a single row. */
208 $vals = $this->extractRowInfo($row);
210 /* Add that row's data to our final output. */
213 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
216 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
221 $db->freeResult($res);
223 /* Format the result */
224 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'rc');
228 * Extracts from a single sql row the data needed to describe one recent change.
230 * @param $row The row from which to extract the data.
231 * @return An array mapping strings (descriptors) to their respective string values.
234 private function extractRowInfo($row) {
235 /* If page was moved somewhere, get the title of the move target. */
236 $movedToTitle = false;
237 if (isset($row->rc_moved_to_title
) && $row->rc_moved_to_title
!== '')
238 $movedToTitle = Title
:: makeTitle($row->rc_moved_to_ns
, $row->rc_moved_to_title
);
240 /* Determine the title of the page that has been changed. */
241 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
243 /* Our output data. */
246 $type = intval ( $row->rc_type
);
248 /* Determine what kind of change this was. */
250 case RC_EDIT
: $vals['type'] = 'edit'; break;
251 case RC_NEW
: $vals['type'] = 'new'; break;
252 case RC_MOVE
: $vals['type'] = 'move'; break;
253 case RC_LOG
: $vals['type'] = 'log'; break;
254 case RC_MOVE_OVER_REDIRECT
: $vals['type'] = 'move over redirect'; break;
255 default: $vals['type'] = $type;
258 /* Create a new entry in the result for the title. */
259 if ($this->fld_title
) {
260 ApiQueryBase
:: addTitleInfo($vals, $title);
262 ApiQueryBase
:: addTitleInfo($vals, $movedToTitle, "new_");
265 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
266 if ($this->fld_ids
) {
267 $vals['rcid'] = intval($row->rc_id
);
268 $vals['pageid'] = intval($row->rc_cur_id
);
269 $vals['revid'] = intval($row->rc_this_oldid
);
270 $vals['old_revid'] = intval( $row->rc_last_oldid
);
273 /* Add user data and 'anon' flag, if use is anonymous. */
274 if ($this->fld_user
) {
275 $vals['user'] = $row->rc_user_text
;
280 /* Add flags, such as new, minor, bot. */
281 if ($this->fld_flags
) {
290 /* Add sizes of each revision. (Only available on 1.10+) */
291 if ($this->fld_sizes
) {
292 $vals['oldlen'] = intval($row->rc_old_len
);
293 $vals['newlen'] = intval($row->rc_new_len
);
296 /* Add the timestamp. */
297 if ($this->fld_timestamp
)
298 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
300 /* Add edit summary / log summary. */
301 if ($this->fld_comment
&& isset($row->rc_comment
)) {
302 $vals['comment'] = $row->rc_comment
;
305 if ($this->fld_redirect
)
306 if($row->page_is_redirect
)
307 $vals['redirect'] = '';
309 /* Add the patrolled flag */
310 if ($this->fld_patrolled
&& $row->rc_patrolled
== 1)
311 $vals['patrolled'] = '';
313 if ($this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
314 $vals['logid'] = $row->rc_logid
;
315 $vals['logtype'] = $row->rc_log_type
;
316 $vals['logaction'] = $row->rc_log_action
;
317 ApiQueryLogEvents
::addLogParams($this->getResult(),
318 $vals, $row->rc_params
,
319 $row->rc_log_type
, $row->rc_timestamp
);
322 if(!is_null($this->token
))
324 $tokenFunctions = $this->getTokenFunctions();
325 foreach($this->token
as $t)
327 $val = call_user_func($tokenFunctions[$t], $row->rc_cur_id
,
328 $title, RecentChange
::newFromRow($row));
330 $this->setWarning("Action '$t' is not allowed for the current user");
332 $vals[$t . 'token'] = $val;
339 private function parseRCType($type)
345 $retval[] = $this->parseRCType($t);
350 case 'edit': return RC_EDIT
;
351 case 'new': return RC_NEW
;
352 case 'log': return RC_LOG
;
356 public function getAllowedParams() {
359 ApiBase
:: PARAM_TYPE
=> 'timestamp'
362 ApiBase
:: PARAM_TYPE
=> 'timestamp'
365 ApiBase
:: PARAM_DFLT
=> 'older',
366 ApiBase
:: PARAM_TYPE
=> array (
371 'namespace' => array (
372 ApiBase
:: PARAM_ISMULTI
=> true,
373 ApiBase
:: PARAM_TYPE
=> 'namespace'
376 ApiBase
:: PARAM_ISMULTI
=> true,
377 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
378 ApiBase
:: PARAM_TYPE
=> array (
392 ApiBase
:: PARAM_TYPE
=> array_keys($this->getTokenFunctions()),
393 ApiBase
:: PARAM_ISMULTI
=> true
396 ApiBase
:: PARAM_ISMULTI
=> true,
397 ApiBase
:: PARAM_TYPE
=> array (
411 ApiBase
:: PARAM_DFLT
=> 10,
412 ApiBase
:: PARAM_TYPE
=> 'limit',
413 ApiBase
:: PARAM_MIN
=> 1,
414 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
415 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
418 ApiBase
:: PARAM_ISMULTI
=> true,
419 ApiBase
:: PARAM_TYPE
=> array (
428 public function getParamDescription() {
430 'start' => 'The timestamp to start enumerating from.',
431 'end' => 'The timestamp to end enumerating.',
432 'dir' => 'In which direction to enumerate.',
433 'namespace' => 'Filter log entries to only this namespace(s)',
434 'prop' => 'Include additional pieces of information',
435 'token' => 'Which tokens to obtain for each change',
437 'Show only items that meet this criteria.',
438 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
440 'type' => 'Which types of changes to show.',
441 'limit' => 'How many total changes to return.'
445 public function getDescription() {
446 return 'Enumerate recent changes';
449 protected function getExamples() {
451 'api.php?action=query&list=recentchanges'
455 public function getVersion() {
456 return __CLASS__
. ': $Id$';