Clarify comment on $wgRateLimits.
[lhc/web/wiklou.git] / includes / api / ApiQueryRecentChanges.php
1 <?php
2
3 /*
4 * Created on Oct 19, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * A query action to enumerate the recent changes that were done to the wiki.
33 * Various filters are supported.
34 *
35 * @addtogroup API
36 */
37 class ApiQueryRecentChanges extends ApiQueryBase {
38
39 public function __construct($query, $moduleName) {
40 parent :: __construct($query, $moduleName, 'rc');
41 }
42
43 private $fld_comment = false, $fld_user = false, $fld_flags = false,
44 $fld_timestamp = false, $fld_title = false, $fld_ids = false,
45 $fld_sizes = false;
46
47 /**
48 * Generates and outputs the result of this query based upon the provided parameters.
49 */
50 public function execute() {
51 /* Initialize vars */
52 $limit = $prop = $namespace = $show = $type = $dir = $start = $end = null;
53
54 /* Get the parameters of the request. */
55 extract($this->extractRequestParams());
56
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'
61 */
62 $this->addTables('recentchanges');
63 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
64 $this->addWhereFld('rc_namespace', $namespace);
65 $this->addWhereFld('rc_deleted', 0);
66 if(!is_null($type))
67 $this->addWhereFld('rc_type', $this->parseRCType($type));
68
69 if (!is_null($show)) {
70 $show = array_flip($show);
71
72 /* Check for conflicting parameters. */
73 if ((isset ($show['minor']) && isset ($show['!minor']))
74 || (isset ($show['bot']) && isset ($show['!bot']))
75 || (isset ($show['anon']) && isset ($show['!anon']))) {
76
77 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
78 }
79
80 /* Add additional conditions to query depending upon parameters. */
81 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
82 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
83 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
84 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
85 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
86 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
87 }
88
89 /* Add the fields we're concerned with to out query. */
90 $this->addFields(array (
91 'rc_timestamp',
92 'rc_namespace',
93 'rc_title',
94 'rc_type',
95 'rc_moved_to_ns',
96 'rc_moved_to_title'
97 ));
98
99 /* Determine what properties we need to display. */
100 if (!is_null($prop)) {
101 $prop = array_flip($prop);
102
103 /* Set up internal members based upon params. */
104 $this->fld_comment = isset ($prop['comment']);
105 $this->fld_user = isset ($prop['user']);
106 $this->fld_flags = isset ($prop['flags']);
107 $this->fld_timestamp = isset ($prop['timestamp']);
108 $this->fld_title = isset ($prop['title']);
109 $this->fld_ids = isset ($prop['ids']);
110 $this->fld_sizes = isset ($prop['sizes']);
111
112 /* Add fields to our query if they are specified as a needed parameter. */
113 $this->addFieldsIf('rc_id', $this->fld_ids);
114 $this->addFieldsIf('rc_cur_id', $this->fld_ids);
115 $this->addFieldsIf('rc_this_oldid', $this->fld_ids);
116 $this->addFieldsIf('rc_last_oldid', $this->fld_ids);
117 $this->addFieldsIf('rc_comment', $this->fld_comment);
118 $this->addFieldsIf('rc_user', $this->fld_user);
119 $this->addFieldsIf('rc_user_text', $this->fld_user);
120 $this->addFieldsIf('rc_minor', $this->fld_flags);
121 $this->addFieldsIf('rc_bot', $this->fld_flags);
122 $this->addFieldsIf('rc_new', $this->fld_flags);
123 $this->addFieldsIf('rc_old_len', $this->fld_sizes);
124 $this->addFieldsIf('rc_new_len', $this->fld_sizes);
125 }
126
127 /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
128 * generate a "continue" parameter, to allow paging. */
129 $this->addOption('LIMIT', $limit +1);
130
131 /* Specify the index to use in the query as rc_timestamp, instead of rc_revid (default). */
132 $this->addOption('USE INDEX', 'rc_timestamp');
133
134 $data = array ();
135 $count = 0;
136
137 /* Perform the actual query. */
138 $db = $this->getDB();
139 $res = $this->select(__METHOD__);
140
141 /* Iterate through the rows, adding data extracted from them to our query result. */
142 while ($row = $db->fetchObject($res)) {
143 if (++ $count > $limit) {
144 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
145 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
146 break;
147 }
148
149 /* Extract the data from a single row. */
150 $vals = $this->extractRowInfo($row);
151
152 /* Add that row's data to our final output. */
153 if($vals)
154 $data[] = $vals;
155 }
156
157 $db->freeResult($res);
158
159 /* Format the result */
160 $result = $this->getResult();
161 $result->setIndexedTagName($data, 'rc');
162 $result->addValue('query', $this->getModuleName(), $data);
163 }
164
165 /**
166 * Extracts from a single sql row the data needed to describe one recent change.
167 *
168 * @param $row The row from which to extract the data.
169 * @return An array mapping strings (descriptors) to their respective string values.
170 * @access private
171 */
172 private function extractRowInfo($row) {
173 /* If page was moved somewhere, get the title of the move target. */
174 $movedToTitle = false;
175 if (!empty($row->rc_moved_to_title))
176 $movedToTitle = Title :: makeTitle($row->rc_moved_to_ns, $row->rc_moved_to_title);
177
178 /* Determine the title of the page that has been changed. */
179 $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
180
181 /* Our output data. */
182 $vals = array ();
183
184 $type = intval ( $row->rc_type );
185
186 /* Determine what kind of change this was. */
187 switch ( $type ) {
188 case RC_EDIT: $vals['type'] = 'edit'; break;
189 case RC_NEW: $vals['type'] = 'new'; break;
190 case RC_MOVE: $vals['type'] = 'move'; break;
191 case RC_LOG: $vals['type'] = 'log'; break;
192 case RC_MOVE_OVER_REDIRECT: $vals['type'] = 'move over redirect'; break;
193 default: $vals['type'] = $type;
194 }
195
196 /* Create a new entry in the result for the title. */
197 if ($this->fld_title) {
198 ApiQueryBase :: addTitleInfo($vals, $title);
199 if ($movedToTitle)
200 ApiQueryBase :: addTitleInfo($vals, $movedToTitle, "new_");
201 }
202
203 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
204 if ($this->fld_ids) {
205 $vals['rcid'] = intval($row->rc_id);
206 $vals['pageid'] = intval($row->rc_cur_id);
207 $vals['revid'] = intval($row->rc_this_oldid);
208 $vals['old_revid'] = intval( $row->rc_last_oldid );
209 }
210
211 /* Add user data and 'anon' flag, if use is anonymous. */
212 if ($this->fld_user) {
213 $vals['user'] = $row->rc_user_text;
214 if(!$row->rc_user)
215 $vals['anon'] = '';
216 }
217
218 /* Add flags, such as new, minor, bot. */
219 if ($this->fld_flags) {
220 if ($row->rc_bot)
221 $vals['bot'] = '';
222 if ($row->rc_new)
223 $vals['new'] = '';
224 if ($row->rc_minor)
225 $vals['minor'] = '';
226 }
227
228 /* Add sizes of each revision. (Only available on 1.10+) */
229 if ($this->fld_sizes) {
230 $vals['oldlen'] = intval($row->rc_old_len);
231 $vals['newlen'] = intval($row->rc_new_len);
232 }
233
234 /* Add the timestamp. */
235 if ($this->fld_timestamp)
236 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
237
238 /* Add edit summary / log summary. */
239 if ($this->fld_comment && !empty ($row->rc_comment)) {
240 $vals['comment'] = $row->rc_comment;
241 }
242
243 return $vals;
244 }
245
246 private function parseRCType($type)
247 {
248 if(is_array($type))
249 {
250 $retval = array();
251 foreach($type as $t)
252 $retval[] = $this->parseRCType($t);
253 return $retval;
254 }
255 switch($type)
256 {
257 case 'edit': return RC_EDIT;
258 case 'new': return RC_NEW;
259 case 'log': return RC_LOG;
260 }
261 }
262
263 protected function getAllowedParams() {
264 return array (
265 'start' => array (
266 ApiBase :: PARAM_TYPE => 'timestamp'
267 ),
268 'end' => array (
269 ApiBase :: PARAM_TYPE => 'timestamp'
270 ),
271 'dir' => array (
272 ApiBase :: PARAM_DFLT => 'older',
273 ApiBase :: PARAM_TYPE => array (
274 'newer',
275 'older'
276 )
277 ),
278 'namespace' => array (
279 ApiBase :: PARAM_ISMULTI => true,
280 ApiBase :: PARAM_TYPE => 'namespace'
281 ),
282 'prop' => array (
283 ApiBase :: PARAM_ISMULTI => true,
284 ApiBase :: PARAM_DFLT => 'title|timestamp|ids',
285 ApiBase :: PARAM_TYPE => array (
286 'user',
287 'comment',
288 'flags',
289 'timestamp',
290 'title',
291 'ids',
292 'sizes'
293 )
294 ),
295 'show' => array (
296 ApiBase :: PARAM_ISMULTI => true,
297 ApiBase :: PARAM_TYPE => array (
298 'minor',
299 '!minor',
300 'bot',
301 '!bot',
302 'anon',
303 '!anon'
304 )
305 ),
306 'limit' => array (
307 ApiBase :: PARAM_DFLT => 10,
308 ApiBase :: PARAM_TYPE => 'limit',
309 ApiBase :: PARAM_MIN => 1,
310 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
311 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
312 ),
313 'type' => array (
314 ApiBase :: PARAM_ISMULTI => true,
315 ApiBase :: PARAM_TYPE => array (
316 'edit',
317 'new',
318 'log'
319 )
320 )
321 );
322 }
323
324 protected function getParamDescription() {
325 return array (
326 'start' => 'The timestamp to start enumerating from.',
327 'end' => 'The timestamp to end enumerating.',
328 'dir' => 'In which direction to enumerate.',
329 'namespace' => 'Filter log entries to only this namespace(s)',
330 'prop' => 'Include additional pieces of information',
331 'show' => array (
332 'Show only items that meet this criteria.',
333 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
334 ),
335 'type' => 'Which types of changes to show.',
336 'limit' => 'How many total pages to return.'
337 );
338 }
339
340 protected function getDescription() {
341 return 'Enumerate recent changes';
342 }
343
344 protected function getExamples() {
345 return array (
346 'api.php?action=query&list=recentchanges'
347 );
348 }
349
350 public function getVersion() {
351 return __CLASS__ . ': $Id$';
352 }
353 }
354