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 $rc = $db->tableName('recentchanges');
64 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
65 $this->addWhereFld('rc_namespace', $namespace);
66 $this->addWhereFld('rc_deleted', 0);
70 foreach($titles as $t)
72 $obj = Title
::newFromText($t);
74 if($obj->getNamespace() < 0)
76 // LinkBatch refuses these, but we need them anyway
77 if(!array_key_exists($obj->getNamespace(), $lb->data
))
78 $lb->data
[$obj->getNamespace()] = array();
79 $lb->data
[$obj->getNamespace()][$obj->getDbKey()] = 1;
82 $where = $lb->constructSet('rc', $this->getDb());
84 $this->addWhere($where);
88 $this->addWhereFld('rc_type', $this->parseRCType($type));
90 if (!is_null($show)) {
91 $show = array_flip($show);
93 /* Check for conflicting parameters. */
94 if ((isset ($show['minor']) && isset ($show['!minor']))
95 ||
(isset ($show['bot']) && isset ($show['!bot']))
96 ||
(isset ($show['anon']) && isset ($show['!anon']))
97 ||
(isset ($show['redirect']) && isset ($show['!redirect']))
98 ||
(isset ($show['patrolled']) && isset ($show['!patrolled']))) {
100 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
105 if((isset($show['patrolled']) ||
isset($show['!patrolled'])) && !$wgUser->isAllowed('patrol'))
106 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
108 /* Add additional conditions to query depending upon parameters. */
109 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
110 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
111 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
112 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
113 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
114 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
115 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
116 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
117 $this->addWhereIf('page_is_redirect = 1', isset ($show['redirect']));
118 // Don't throw log entries out the window here
119 $this->addWhereIf('page_is_redirect = 0 OR page_is_redirect IS NULL', isset ($show['!redirect']));
122 /* Add the fields we're concerned with to out query. */
123 $this->addFields(array (
132 /* Determine what properties we need to display. */
133 if (!is_null($prop)) {
134 $prop = array_flip($prop);
136 /* Set up internal members based upon params. */
137 $this->fld_comment
= isset ($prop['comment']);
138 $this->fld_user
= isset ($prop['user']);
139 $this->fld_flags
= isset ($prop['flags']);
140 $this->fld_timestamp
= isset ($prop['timestamp']);
141 $this->fld_title
= isset ($prop['title']);
142 $this->fld_ids
= isset ($prop['ids']);
143 $this->fld_sizes
= isset ($prop['sizes']);
144 $this->fld_redirect
= isset($prop['redirect']);
145 $this->fld_patrolled
= isset($prop['patrolled']);
148 if($this->fld_patrolled
&& !$wgUser->isAllowed('patrol'))
149 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
151 /* Add fields to our query if they are specified as a needed parameter. */
152 $this->addFieldsIf('rc_id', $this->fld_ids
);
153 $this->addFieldsIf('rc_cur_id', $this->fld_ids
);
154 $this->addFieldsIf('rc_this_oldid', $this->fld_ids
);
155 $this->addFieldsIf('rc_last_oldid', $this->fld_ids
);
156 $this->addFieldsIf('rc_comment', $this->fld_comment
);
157 $this->addFieldsIf('rc_user', $this->fld_user
);
158 $this->addFieldsIf('rc_user_text', $this->fld_user
);
159 $this->addFieldsIf('rc_minor', $this->fld_flags
);
160 $this->addFieldsIf('rc_bot', $this->fld_flags
);
161 $this->addFieldsIf('rc_new', $this->fld_flags
);
162 $this->addFieldsIf('rc_old_len', $this->fld_sizes
);
163 $this->addFieldsIf('rc_new_len', $this->fld_sizes
);
164 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled
);
165 if($this->fld_redirect ||
isset($show['redirect']) ||
isset($show['!redirect']))
167 $page = $db->tableName('page');
168 $tables = "$page RIGHT JOIN $rc FORCE INDEX(rc_timestamp) ON page_namespace=rc_namespace AND page_title=rc_title";
169 $this->addFields('page_is_redirect');
173 $tables = "$rc FORCE INDEX(rc_timestamp)";
174 $this->addTables($tables);
175 /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
176 * generate a "continue" parameter, to allow paging. */
177 $this->addOption('LIMIT', $limit +
1);
182 /* Perform the actual query. */
183 $db = $this->getDB();
184 $res = $this->select(__METHOD__
);
186 /* Iterate through the rows, adding data extracted from them to our query result. */
187 while ($row = $db->fetchObject($res)) {
188 if (++
$count > $limit) {
189 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
190 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
));
194 /* Extract the data from a single row. */
195 $vals = $this->extractRowInfo($row);
197 /* Add that row's data to our final output. */
202 $db->freeResult($res);
204 /* Format the result */
205 $result = $this->getResult();
206 $result->setIndexedTagName($data, 'rc');
207 $result->addValue('query', $this->getModuleName(), $data);
211 * Extracts from a single sql row the data needed to describe one recent change.
213 * @param $row The row from which to extract the data.
214 * @return An array mapping strings (descriptors) to their respective string values.
217 private function extractRowInfo($row) {
218 /* If page was moved somewhere, get the title of the move target. */
219 $movedToTitle = false;
220 if (!empty($row->rc_moved_to_title
))
221 $movedToTitle = Title
:: makeTitle($row->rc_moved_to_ns
, $row->rc_moved_to_title
);
223 /* Determine the title of the page that has been changed. */
224 $title = Title
:: makeTitle($row->rc_namespace
, $row->rc_title
);
226 /* Our output data. */
229 $type = intval ( $row->rc_type
);
231 /* Determine what kind of change this was. */
233 case RC_EDIT
: $vals['type'] = 'edit'; break;
234 case RC_NEW
: $vals['type'] = 'new'; break;
235 case RC_MOVE
: $vals['type'] = 'move'; break;
236 case RC_LOG
: $vals['type'] = 'log'; break;
237 case RC_MOVE_OVER_REDIRECT
: $vals['type'] = 'move over redirect'; break;
238 default: $vals['type'] = $type;
241 /* Create a new entry in the result for the title. */
242 if ($this->fld_title
) {
243 ApiQueryBase
:: addTitleInfo($vals, $title);
245 ApiQueryBase
:: addTitleInfo($vals, $movedToTitle, "new_");
248 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
249 if ($this->fld_ids
) {
250 $vals['rcid'] = intval($row->rc_id
);
251 $vals['pageid'] = intval($row->rc_cur_id
);
252 $vals['revid'] = intval($row->rc_this_oldid
);
253 $vals['old_revid'] = intval( $row->rc_last_oldid
);
256 /* Add user data and 'anon' flag, if use is anonymous. */
257 if ($this->fld_user
) {
258 $vals['user'] = $row->rc_user_text
;
263 /* Add flags, such as new, minor, bot. */
264 if ($this->fld_flags
) {
273 /* Add sizes of each revision. (Only available on 1.10+) */
274 if ($this->fld_sizes
) {
275 $vals['oldlen'] = intval($row->rc_old_len
);
276 $vals['newlen'] = intval($row->rc_new_len
);
279 /* Add the timestamp. */
280 if ($this->fld_timestamp
)
281 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rc_timestamp
);
283 /* Add edit summary / log summary. */
284 if ($this->fld_comment
&& !empty ($row->rc_comment
)) {
285 $vals['comment'] = $row->rc_comment
;
288 if ($this->fld_redirect
)
289 if($row->page_is_redirect
)
290 $vals['redirect'] = '';
292 /* Add the patrolled flag */
293 if ($this->fld_patrolled
&& $row->rc_patrolled
== 1)
294 $vals['patrolled'] = '';
299 private function parseRCType($type)
305 $retval[] = $this->parseRCType($t);
310 case 'edit': return RC_EDIT
;
311 case 'new': return RC_NEW
;
312 case 'log': return RC_LOG
;
316 public function getAllowedParams() {
319 ApiBase
:: PARAM_TYPE
=> 'timestamp'
322 ApiBase
:: PARAM_TYPE
=> 'timestamp'
325 ApiBase
:: PARAM_DFLT
=> 'older',
326 ApiBase
:: PARAM_TYPE
=> array (
331 'namespace' => array (
332 ApiBase
:: PARAM_ISMULTI
=> true,
333 ApiBase
:: PARAM_TYPE
=> 'namespace'
336 ApiBase
:: PARAM_ISMULTI
=> true
339 ApiBase
:: PARAM_ISMULTI
=> true,
340 ApiBase
:: PARAM_DFLT
=> 'title|timestamp|ids',
341 ApiBase
:: PARAM_TYPE
=> array (
354 ApiBase
:: PARAM_ISMULTI
=> true,
355 ApiBase
:: PARAM_TYPE
=> array (
369 ApiBase
:: PARAM_DFLT
=> 10,
370 ApiBase
:: PARAM_TYPE
=> 'limit',
371 ApiBase
:: PARAM_MIN
=> 1,
372 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
373 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
376 ApiBase
:: PARAM_ISMULTI
=> true,
377 ApiBase
:: PARAM_TYPE
=> array (
386 public function getParamDescription() {
388 'start' => 'The timestamp to start enumerating from.',
389 'end' => 'The timestamp to end enumerating.',
390 'dir' => 'In which direction to enumerate.',
391 'namespace' => 'Filter log entries to only this namespace(s)',
392 'titles' => 'Filter log entries to only these page titles',
393 'prop' => 'Include additional pieces of information',
395 'Show only items that meet this criteria.',
396 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
398 'type' => 'Which types of changes to show.',
399 'limit' => 'How many total pages to return.'
403 public function getDescription() {
404 return 'Enumerate recent changes';
407 protected function getExamples() {
409 'api.php?action=query&list=recentchanges'
413 public function getVersion() {
414 return __CLASS__
. ': $Id$';