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,
48 * Generates and outputs the result of this query based upon the provided parameters.
50 public function execute() {
52 $limit = $prop = $namespace = $titles = $show = $type = $dir = $start = $end = null;
54 /* Get the parameters of the request. */
55 extract($this->extractRequestParams());
57 /* Build our basic query. Namely, something along the lines of:
58 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
59 * AND rc_timestamp < $end AND rc_namespace = $namespace
60 * AND rc_deleted = '0'
63 $this->addTables('recentchanges');
64 $this->addOption('USE INDEX', array('recentchanges' => 'rc_timestamp'));
65 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
66 $this->addWhereFld('rc_namespace', $namespace);
67 $this->addWhereFld('rc_deleted', 0);
71 foreach($titles as $t)
73 $obj = Title
::newFromText($t);
75 if($obj->getNamespace() < 0)
77 // LinkBatch refuses these, but we need them anyway
78 if(!array_key_exists($obj->getNamespace(), $lb->data
))
79 $lb->data
[$obj->getNamespace()] = array();
80 $lb->data
[$obj->getNamespace()][$obj->getDbKey()] = 1;
83 $where = $lb->constructSet('rc', $this->getDb());
85 $this->addWhere($where);
89 $this->addWhereFld('rc_type', $this->parseRCType($type));
91 if (!is_null($show)) {
92 $show = array_flip($show);
94 /* Check for conflicting parameters. */
95 if ((isset ($show['minor']) && isset ($show['!minor']))
96 ||
(isset ($show['bot']) && isset ($show['!bot']))
97 ||
(isset ($show['anon']) && isset ($show['!anon']))
98 ||
(isset ($show['redirect']) && isset ($show['!redirect']))
99 ||
(isset ($show['patrolled']) && isset ($show['!patrolled']))) {
101 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
106 if((isset($show['patrolled']) ||
isset($show['!patrolled'])) && !$wgUser->isAllowed('patrol'))
107 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
109 /* Add additional conditions to query depending upon parameters. */
110 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
111 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
112 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
113 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
114 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
115 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
116 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
117 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
118 $this->addWhereIf('page_is_redirect = 1', isset ($show['redirect']));
119 // Don't throw log entries out the window here
120 $this->addWhereIf('page_is_redirect = 0 OR page_is_redirect IS NULL', isset ($show['!redirect']));
123 /* Add the fields we're concerned with to out query. */
124 $this->addFields(array (
133 /* Determine what properties we need to display. */
134 if (!is_null($prop)) {
135 $prop = array_flip($prop);
137 /* Set up internal members based upon params. */
138 $this->fld_comment
= isset ($prop['comment']);
139 $this->fld_user
= isset ($prop['user']);
140 $this->fld_flags
= isset ($prop['flags']);
141 $this->fld_timestamp
= isset ($prop['timestamp']);
142 $this->fld_title
= isset ($prop['title']);
143 $this->fld_ids
= isset ($prop['ids']);
144 $this->fld_sizes
= isset ($prop['sizes']);
145 $this->fld_redirect
= isset($prop['redirect']);
146 $this->fld_patrolled
= isset($prop['patrolled']);
149 if($this->fld_patrolled
&& !$wgUser->isAllowed('patrol'))
150 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
152 /* Add fields to our query if they are specified as a needed parameter. */
153 $this->addFieldsIf('rc_id', $this->fld_ids
);
154 $this->addFieldsIf('rc_cur_id', $this->fld_ids
);
155 $this->addFieldsIf('rc_this_oldid', $this->fld_ids
);
156 $this->addFieldsIf('rc_last_oldid', $this->fld_ids
);
157 $this->addFieldsIf('rc_comment', $this->fld_comment
);
158 $this->addFieldsIf('rc_user', $this->fld_user
);
159 $this->addFieldsIf('rc_user_text', $this->fld_user
);
160 $this->addFieldsIf('rc_minor', $this->fld_flags
);
161 $this->addFieldsIf('rc_bot', $this->fld_flags
);
162 $this->addFieldsIf('rc_new', $this->fld_flags
);
163 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
164 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
165 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled
);
166 if($this->fld_redirect ||
isset($show['redirect']) ||
isset($show['!redirect']))
168 $this->addTables('page');
169 $this->addJoinConds(array('page' => array('LEFT JOIN', array('rc_namespace=page_namespace', 'rc_title=page_title'))));
170 $this->addFields('page_is_redirect');
173 /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
174 * generate a "continue" parameter, to allow paging. */
175 $this->addOption('LIMIT', $limit +
1);
180 /* Perform the actual query. */
181 $db = $this->getDB();
182 $res = $this->select(__METHOD__
);
184 /* Iterate through the rows, adding data extracted from them to our query result. */
185 while ($row = $db->fetchObject($res)) {
186 if (++
$count > $limit) {
187 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
188 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
192 /* Extract the data from a single row. */
193 $vals = $this->extractRowInfo($row);
195 /* Add that row's data to our final output. */
200 $db->freeResult($res);
202 /* Format the result */
203 $result = $this->getResult();
204 $result->setIndexedTagName($data, 'rc');
205 $result->addValue('query', $this->getModuleName(), $data);
209 * Extracts from a single sql row the data needed to describe one recent change.
211 * @param $row The row from which to extract the data.
212 * @return An array mapping strings (descriptors) to their respective string values.
215 private function extractRowInfo($row) {
216 /* If page was moved somewhere, get the title of the move target. */
217 $movedToTitle = false;
218 if (!empty($row->rc_moved_to_title
))
219 $movedToTitle = Title
:: makeTitle($row->rc_moved_to_ns
, $row->rc_moved_to_title
);
221 /* Determine the title of the page that has been changed. */
222 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
224 /* Our output data. */
227 $type = intval ( $row->rc_type
);
229 /* Determine what kind of change this was. */
231 case RC_EDIT
: $vals['type'] = 'edit'; break;
232 case RC_NEW
: $vals['type'] = 'new'; break;
233 case RC_MOVE
: $vals['type'] = 'move'; break;
234 case RC_LOG
: $vals['type'] = 'log'; break;
235 case RC_MOVE_OVER_REDIRECT
: $vals['type'] = 'move over redirect'; break;
236 default: $vals['type'] = $type;
239 /* Create a new entry in the result for the title. */
240 if ($this->fld_title
) {
241 ApiQueryBase
:: addTitleInfo($vals, $title);
243 ApiQueryBase
:: addTitleInfo($vals, $movedToTitle, "new_");
246 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
247 if ($this->fld_ids
) {
248 $vals['rcid'] = intval($row->rc_id
);
249 $vals['pageid'] = intval($row->rc_cur_id
);
250 $vals['revid'] = intval($row->rc_this_oldid
);
251 $vals['old_revid'] = intval( $row->rc_last_oldid
);
254 /* Add user data and 'anon' flag, if use is anonymous. */
255 if ($this->fld_user
) {
256 $vals['user'] = $row->rc_user_text
;
261 /* Add flags, such as new, minor, bot. */
262 if ($this->fld_flags
) {
271 /* Add sizes of each revision. (Only available on 1.10+) */
272 if ($this->fld_sizes
) {
273 $vals['oldlen'] = intval($row->rc_old_len
);
274 $vals['newlen'] = intval($row->rc_new_len
);
277 /* Add the timestamp. */
278 if ($this->fld_timestamp
)
279 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
281 /* Add edit summary / log summary. */
282 if ($this->fld_comment
&& !empty ($row->rc_comment
)) {
283 $vals['comment'] = $row->rc_comment
;
286 if ($this->fld_redirect
)
287 if($row->page_is_redirect
)
288 $vals['redirect'] = '';
290 /* Add the patrolled flag */
291 if ($this->fld_patrolled
&& $row->rc_patrolled
== 1)
292 $vals['patrolled'] = '';
297 private function parseRCType($type)
303 $retval[] = $this->parseRCType($t);
308 case 'edit': return RC_EDIT
;
309 case 'new': return RC_NEW
;
310 case 'log': return RC_LOG
;
314 public function getAllowedParams() {
317 ApiBase
:: PARAM_TYPE
=> 'timestamp'
320 ApiBase
:: PARAM_TYPE
=> 'timestamp'
323 ApiBase
:: PARAM_DFLT
=> 'older',
324 ApiBase
:: PARAM_TYPE
=> array (
329 'namespace' => array (
330 ApiBase
:: PARAM_ISMULTI
=> true,
331 ApiBase
:: PARAM_TYPE
=> 'namespace'
334 ApiBase
:: PARAM_ISMULTI
=> true
337 ApiBase
:: PARAM_ISMULTI
=> true,
338 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
339 ApiBase
:: PARAM_TYPE
=> array (
352 ApiBase
:: PARAM_ISMULTI
=> true,
353 ApiBase
:: PARAM_TYPE
=> array (
367 ApiBase
:: PARAM_DFLT
=> 10,
368 ApiBase
:: PARAM_TYPE
=> 'limit',
369 ApiBase
:: PARAM_MIN
=> 1,
370 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
371 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
374 ApiBase
:: PARAM_ISMULTI
=> true,
375 ApiBase
:: PARAM_TYPE
=> array (
384 public function getParamDescription() {
386 'start' => 'The timestamp to start enumerating from.',
387 'end' => 'The timestamp to end enumerating.',
388 'dir' => 'In which direction to enumerate.',
389 'namespace' => 'Filter log entries to only this namespace(s)',
390 'titles' => 'Filter log entries to only these page titles',
391 'prop' => 'Include additional pieces of information',
393 'Show only items that meet this criteria.',
394 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
396 'type' => 'Which types of changes to show.',
397 'limit' => 'How many total pages to return.'
401 public function getDescription() {
402 return 'Enumerate recent changes';
405 protected function getExamples() {
407 'api.php?action=query&list=recentchanges'
411 public function getVersion() {
412 return __CLASS__
. ': $Id$';