Fix regression from r36678: we can't use $this->dieUsageMsg() in a static method...
[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 * @ingroup 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 = $titles = $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 $db = $this->getDB();
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);
68 if(!empty($titles))
69 {
70 $lb = new LinkBatch;
71 foreach($titles as $t)
72 {
73 $obj = Title::newFromText($t);
74 $lb->addObj($obj);
75 if($obj->getNamespace() < 0)
76 {
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;
81 }
82 }
83 $where = $lb->constructSet('rc', $this->getDb());
84 if($where != '')
85 $this->addWhere($where);
86 }
87
88 if(!is_null($type))
89 $this->addWhereFld('rc_type', $this->parseRCType($type));
90
91 if (!is_null($show)) {
92 $show = array_flip($show);
93
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']))) {
100
101 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
102 }
103
104 // Check permissions
105 global $wgUser;
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');
108
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']));
121 }
122
123 /* Add the fields we're concerned with to out query. */
124 $this->addFields(array (
125 'rc_timestamp',
126 'rc_namespace',
127 'rc_title',
128 'rc_type',
129 'rc_moved_to_ns',
130 'rc_moved_to_title'
131 ));
132
133 /* Determine what properties we need to display. */
134 if (!is_null($prop)) {
135 $prop = array_flip($prop);
136
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']);
147
148 global $wgUser;
149 if($this->fld_patrolled && !$wgUser->isAllowed('patrol'))
150 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
151
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']))
167 {
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');
171 }
172 }
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);
176
177 $data = array ();
178 $count = 0;
179
180 /* Perform the actual query. */
181 $db = $this->getDB();
182 $res = $this->select(__METHOD__);
183
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));
189 break;
190 }
191
192 /* Extract the data from a single row. */
193 $vals = $this->extractRowInfo($row);
194
195 /* Add that row's data to our final output. */
196 if($vals)
197 $data[] = $vals;
198 }
199
200 $db->freeResult($res);
201
202 /* Format the result */
203 $result = $this->getResult();
204 $result->setIndexedTagName($data, 'rc');
205 $result->addValue('query', $this->getModuleName(), $data);
206 }
207
208 /**
209 * Extracts from a single sql row the data needed to describe one recent change.
210 *
211 * @param $row The row from which to extract the data.
212 * @return An array mapping strings (descriptors) to their respective string values.
213 * @access private
214 */
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);
220
221 /* Determine the title of the page that has been changed. */
222 $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
223
224 /* Our output data. */
225 $vals = array ();
226
227 $type = intval ( $row->rc_type );
228
229 /* Determine what kind of change this was. */
230 switch ( $type ) {
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;
237 }
238
239 /* Create a new entry in the result for the title. */
240 if ($this->fld_title) {
241 ApiQueryBase :: addTitleInfo($vals, $title);
242 if ($movedToTitle)
243 ApiQueryBase :: addTitleInfo($vals, $movedToTitle, "new_");
244 }
245
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 );
252 }
253
254 /* Add user data and 'anon' flag, if use is anonymous. */
255 if ($this->fld_user) {
256 $vals['user'] = $row->rc_user_text;
257 if(!$row->rc_user)
258 $vals['anon'] = '';
259 }
260
261 /* Add flags, such as new, minor, bot. */
262 if ($this->fld_flags) {
263 if ($row->rc_bot)
264 $vals['bot'] = '';
265 if ($row->rc_new)
266 $vals['new'] = '';
267 if ($row->rc_minor)
268 $vals['minor'] = '';
269 }
270
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);
275 }
276
277 /* Add the timestamp. */
278 if ($this->fld_timestamp)
279 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
280
281 /* Add edit summary / log summary. */
282 if ($this->fld_comment && !empty ($row->rc_comment)) {
283 $vals['comment'] = $row->rc_comment;
284 }
285
286 if ($this->fld_redirect)
287 if($row->page_is_redirect)
288 $vals['redirect'] = '';
289
290 /* Add the patrolled flag */
291 if ($this->fld_patrolled && $row->rc_patrolled == 1)
292 $vals['patrolled'] = '';
293
294 return $vals;
295 }
296
297 private function parseRCType($type)
298 {
299 if(is_array($type))
300 {
301 $retval = array();
302 foreach($type as $t)
303 $retval[] = $this->parseRCType($t);
304 return $retval;
305 }
306 switch($type)
307 {
308 case 'edit': return RC_EDIT;
309 case 'new': return RC_NEW;
310 case 'log': return RC_LOG;
311 }
312 }
313
314 public function getAllowedParams() {
315 return array (
316 'start' => array (
317 ApiBase :: PARAM_TYPE => 'timestamp'
318 ),
319 'end' => array (
320 ApiBase :: PARAM_TYPE => 'timestamp'
321 ),
322 'dir' => array (
323 ApiBase :: PARAM_DFLT => 'older',
324 ApiBase :: PARAM_TYPE => array (
325 'newer',
326 'older'
327 )
328 ),
329 'namespace' => array (
330 ApiBase :: PARAM_ISMULTI => true,
331 ApiBase :: PARAM_TYPE => 'namespace'
332 ),
333 'titles' => array(
334 ApiBase :: PARAM_ISMULTI => true
335 ),
336 'prop' => array (
337 ApiBase :: PARAM_ISMULTI => true,
338 ApiBase :: PARAM_DFLT => 'title|timestamp|ids',
339 ApiBase :: PARAM_TYPE => array (
340 'user',
341 'comment',
342 'flags',
343 'timestamp',
344 'title',
345 'ids',
346 'sizes',
347 'redirect',
348 'patrolled'
349 )
350 ),
351 'show' => array (
352 ApiBase :: PARAM_ISMULTI => true,
353 ApiBase :: PARAM_TYPE => array (
354 'minor',
355 '!minor',
356 'bot',
357 '!bot',
358 'anon',
359 '!anon',
360 'redirect',
361 '!redirect',
362 'patrolled',
363 '!patrolled'
364 )
365 ),
366 'limit' => 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
372 ),
373 'type' => array (
374 ApiBase :: PARAM_ISMULTI => true,
375 ApiBase :: PARAM_TYPE => array (
376 'edit',
377 'new',
378 'log'
379 )
380 )
381 );
382 }
383
384 public function getParamDescription() {
385 return array (
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',
392 'show' => array (
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'
395 ),
396 'type' => 'Which types of changes to show.',
397 'limit' => 'How many total pages to return.'
398 );
399 }
400
401 public function getDescription() {
402 return 'Enumerate recent changes';
403 }
404
405 protected function getExamples() {
406 return array (
407 'api.php?action=query&list=recentchanges'
408 );
409 }
410
411 public function getVersion() {
412 return __CLASS__ . ': $Id$';
413 }
414 }