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'
62 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
63 $this->addWhereFld('rc_namespace', $namespace);
64 $this->addWhereFld('rc_deleted', 0);
68 foreach($titles as $t)
70 $obj = Title
::newFromText($t);
72 if($obj->getNamespace() < 0)
74 // LinkBatch refuses these, but we need them anyway
75 if(!array_key_exists($obj->getNamespace(), $lb->data
))
76 $lb->data
[$obj->getNamespace()] = array();
77 $lb->data
[$obj->getNamespace()][$obj->getDbKey()] = 1;
80 $where = $lb->constructSet('rc', $this->getDb());
82 $this->addWhere($where);
86 $this->addWhereFld('rc_type', $this->parseRCType($type));
88 if (!is_null($show)) {
89 $show = array_flip($show);
91 /* Check for conflicting parameters. */
92 if ((isset ($show['minor']) && isset ($show['!minor']))
93 ||
(isset ($show['bot']) && isset ($show['!bot']))
94 ||
(isset ($show['anon']) && isset ($show['!anon']))
95 ||
(isset ($show['redirect']) && isset ($show['!redirect']))) {
97 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
100 /* Add additional conditions to query depending upon parameters. */
101 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
102 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
103 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
104 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
105 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
106 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
107 $this->addWhereIf('page_is_redirect = 1', isset ($show['redirect']));
108 $this->addWhereIf('page_is_redirect = 0', isset ($show['!redirect']));
111 /* Add the fields we're concerned with to out query. */
112 $this->addFields(array (
121 /* Determine what properties we need to display. */
122 if (!is_null($prop)) {
123 $prop = array_flip($prop);
125 /* Set up internal members based upon params. */
126 $this->fld_comment
= isset ($prop['comment']);
127 $this->fld_user
= isset ($prop['user']);
128 $this->fld_flags
= isset ($prop['flags']);
129 $this->fld_timestamp
= isset ($prop['timestamp']);
130 $this->fld_title
= isset ($prop['title']);
131 $this->fld_ids
= isset ($prop['ids']);
132 $this->fld_sizes
= isset ($prop['sizes']);
133 $this->fld_redirect
= isset($prop['redirect']);
134 $this->fld_patrolled
= isset($prop['patrolled']);
137 if($this->fld_patrolled
&& !$wgUser->isAllowed('patrol'))
138 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
140 /* Add fields to our query if they are specified as a needed parameter. */
141 $this->addFieldsIf('rc_id', $this->fld_ids
);
142 $this->addFieldsIf('rc_cur_id', $this->fld_ids
);
143 $this->addFieldsIf('rc_this_oldid', $this->fld_ids
);
144 $this->addFieldsIf('rc_last_oldid', $this->fld_ids
);
145 $this->addFieldsIf('rc_comment', $this->fld_comment
);
146 $this->addFieldsIf('rc_user', $this->fld_user
);
147 $this->addFieldsIf('rc_user_text', $this->fld_user
);
148 $this->addFieldsIf('rc_minor', $this->fld_flags
);
149 $this->addFieldsIf('rc_bot', $this->fld_flags
);
150 $this->addFieldsIf('rc_new', $this->fld_flags
);
151 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
152 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
153 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled
);
154 if($this->fld_redirect ||
isset($show['redirect']) ||
isset($show['!redirect']))
156 $this->addTables('page');
157 $this->addWhere('page_namespace=rc_namespace');
158 $this->addWhere('page_title=rc_title');
159 $this->addFields('page_is_redirect');
162 $this->addTables('recentchanges');
163 /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
164 * generate a "continue" parameter, to allow paging. */
165 $this->addOption('LIMIT', $limit +
1);
167 /* Specify the index to use in the query as rc_timestamp, instead of rc_revid (default). */
168 $this->addOption('USE INDEX', 'rc_timestamp');
173 /* Perform the actual query. */
174 $db = $this->getDB();
175 $res = $this->select(__METHOD__
);
177 /* Iterate through the rows, adding data extracted from them to our query result. */
178 while ($row = $db->fetchObject($res)) {
179 if (++
$count > $limit) {
180 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
181 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
185 /* Extract the data from a single row. */
186 $vals = $this->extractRowInfo($row);
188 /* Add that row's data to our final output. */
193 $db->freeResult($res);
195 /* Format the result */
196 $result = $this->getResult();
197 $result->setIndexedTagName($data, 'rc');
198 $result->addValue('query', $this->getModuleName(), $data);
202 * Extracts from a single sql row the data needed to describe one recent change.
204 * @param $row The row from which to extract the data.
205 * @return An array mapping strings (descriptors) to their respective string values.
208 private function extractRowInfo($row) {
209 /* If page was moved somewhere, get the title of the move target. */
210 $movedToTitle = false;
211 if (!empty($row->rc_moved_to_title
))
212 $movedToTitle = Title
:: makeTitle($row->rc_moved_to_ns
, $row->rc_moved_to_title
);
214 /* Determine the title of the page that has been changed. */
215 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
217 /* Our output data. */
220 $type = intval ( $row->rc_type
);
222 /* Determine what kind of change this was. */
224 case RC_EDIT
: $vals['type'] = 'edit'; break;
225 case RC_NEW
: $vals['type'] = 'new'; break;
226 case RC_MOVE
: $vals['type'] = 'move'; break;
227 case RC_LOG
: $vals['type'] = 'log'; break;
228 case RC_MOVE_OVER_REDIRECT
: $vals['type'] = 'move over redirect'; break;
229 default: $vals['type'] = $type;
232 /* Create a new entry in the result for the title. */
233 if ($this->fld_title
) {
234 ApiQueryBase
:: addTitleInfo($vals, $title);
236 ApiQueryBase
:: addTitleInfo($vals, $movedToTitle, "new_");
239 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
240 if ($this->fld_ids
) {
241 $vals['rcid'] = intval($row->rc_id
);
242 $vals['pageid'] = intval($row->rc_cur_id
);
243 $vals['revid'] = intval($row->rc_this_oldid
);
244 $vals['old_revid'] = intval( $row->rc_last_oldid
);
247 /* Add user data and 'anon' flag, if use is anonymous. */
248 if ($this->fld_user
) {
249 $vals['user'] = $row->rc_user_text
;
254 /* Add flags, such as new, minor, bot. */
255 if ($this->fld_flags
) {
264 /* Add sizes of each revision. (Only available on 1.10+) */
265 if ($this->fld_sizes
) {
266 $vals['oldlen'] = intval($row->rc_old_len
);
267 $vals['newlen'] = intval($row->rc_new_len
);
270 /* Add the timestamp. */
271 if ($this->fld_timestamp
)
272 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
274 /* Add edit summary / log summary. */
275 if ($this->fld_comment
&& !empty ($row->rc_comment
)) {
276 $vals['comment'] = $row->rc_comment
;
279 if ($this->fld_redirect
)
280 if($row->page_is_redirect
)
281 $vals['redirect'] = '';
283 /* Add the patrolled flag */
284 if ($this->fld_patrolled
&& $row->rc_patrolled
== 1)
285 $vals['patrolled'] = '';
290 private function parseRCType($type)
296 $retval[] = $this->parseRCType($t);
301 case 'edit': return RC_EDIT
;
302 case 'new': return RC_NEW
;
303 case 'log': return RC_LOG
;
307 public function getAllowedParams() {
310 ApiBase
:: PARAM_TYPE
=> 'timestamp'
313 ApiBase
:: PARAM_TYPE
=> 'timestamp'
316 ApiBase
:: PARAM_DFLT
=> 'older',
317 ApiBase
:: PARAM_TYPE
=> array (
322 'namespace' => array (
323 ApiBase
:: PARAM_ISMULTI
=> true,
324 ApiBase
:: PARAM_TYPE
=> 'namespace'
327 ApiBase
:: PARAM_ISMULTI
=> true
330 ApiBase
:: PARAM_ISMULTI
=> true,
331 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
332 ApiBase
:: PARAM_TYPE
=> array (
345 ApiBase
:: PARAM_ISMULTI
=> true,
346 ApiBase
:: PARAM_TYPE
=> array (
358 ApiBase
:: PARAM_DFLT
=> 10,
359 ApiBase
:: PARAM_TYPE
=> 'limit',
360 ApiBase
:: PARAM_MIN
=> 1,
361 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
362 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
365 ApiBase
:: PARAM_ISMULTI
=> true,
366 ApiBase
:: PARAM_TYPE
=> array (
375 public function getParamDescription() {
377 'start' => 'The timestamp to start enumerating from.',
378 'end' => 'The timestamp to end enumerating.',
379 'dir' => 'In which direction to enumerate.',
380 'namespace' => 'Filter log entries to only this namespace(s)',
381 'titles' => 'Filter log entries to only these page titles',
382 'prop' => 'Include additional pieces of information',
384 'Show only items that meet this criteria.',
385 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
387 'type' => 'Which types of changes to show.',
388 'limit' => 'How many total pages to return.'
392 public function getDescription() {
393 return 'Enumerate recent changes';
396 protected function getExamples() {
398 'api.php?action=query&list=recentchanges'
402 public function getVersion() {
403 return __CLASS__
. ': $Id$';