(bug 13618) Added rcprop=redirect and rcshow=redirect to list=recentchanges
[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 = $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 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
63 $this->addWhereFld('rc_namespace', $namespace);
64 $this->addWhereFld('rc_deleted', 0);
65 if(!empty($titles))
66 {
67 $lb = new LinkBatch;
68 foreach($titles as $t)
69 {
70 $obj = Title::newFromText($t);
71 $lb->addObj($obj);
72 if($obj->getNamespace() < 0)
73 {
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;
78 }
79 }
80 $where = $lb->constructSet('rc', $this->getDb());
81 if($where != '')
82 $this->addWhere($where);
83 }
84
85 if(!is_null($type))
86 $this->addWhereFld('rc_type', $this->parseRCType($type));
87
88 if (!is_null($show)) {
89 $show = array_flip($show);
90
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']))) {
96
97 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
98 }
99
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']));
109 }
110
111 /* Add the fields we're concerned with to out query. */
112 $this->addFields(array (
113 'rc_timestamp',
114 'rc_namespace',
115 'rc_title',
116 'rc_type',
117 'rc_moved_to_ns',
118 'rc_moved_to_title'
119 ));
120
121 /* Determine what properties we need to display. */
122 if (!is_null($prop)) {
123 $prop = array_flip($prop);
124
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']);
135
136 global $wgUser;
137 if($this->fld_patrolled && !$wgUser->isAllowed('patrol'))
138 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
139
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']))
155 {
156 $this->addTables('page');
157 $this->addWhere('page_namespace=rc_namespace');
158 $this->addWhere('page_title=rc_title');
159 $this->addFields('page_is_redirect');
160 }
161 }
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);
166
167 /* Specify the index to use in the query as rc_timestamp, instead of rc_revid (default). */
168 $this->addOption('USE INDEX', 'rc_timestamp');
169
170 $data = array ();
171 $count = 0;
172
173 /* Perform the actual query. */
174 $db = $this->getDB();
175 $res = $this->select(__METHOD__);
176
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));
182 break;
183 }
184
185 /* Extract the data from a single row. */
186 $vals = $this->extractRowInfo($row);
187
188 /* Add that row's data to our final output. */
189 if($vals)
190 $data[] = $vals;
191 }
192
193 $db->freeResult($res);
194
195 /* Format the result */
196 $result = $this->getResult();
197 $result->setIndexedTagName($data, 'rc');
198 $result->addValue('query', $this->getModuleName(), $data);
199 }
200
201 /**
202 * Extracts from a single sql row the data needed to describe one recent change.
203 *
204 * @param $row The row from which to extract the data.
205 * @return An array mapping strings (descriptors) to their respective string values.
206 * @access private
207 */
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);
213
214 /* Determine the title of the page that has been changed. */
215 $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
216
217 /* Our output data. */
218 $vals = array ();
219
220 $type = intval ( $row->rc_type );
221
222 /* Determine what kind of change this was. */
223 switch ( $type ) {
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;
230 }
231
232 /* Create a new entry in the result for the title. */
233 if ($this->fld_title) {
234 ApiQueryBase :: addTitleInfo($vals, $title);
235 if ($movedToTitle)
236 ApiQueryBase :: addTitleInfo($vals, $movedToTitle, "new_");
237 }
238
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 );
245 }
246
247 /* Add user data and 'anon' flag, if use is anonymous. */
248 if ($this->fld_user) {
249 $vals['user'] = $row->rc_user_text;
250 if(!$row->rc_user)
251 $vals['anon'] = '';
252 }
253
254 /* Add flags, such as new, minor, bot. */
255 if ($this->fld_flags) {
256 if ($row->rc_bot)
257 $vals['bot'] = '';
258 if ($row->rc_new)
259 $vals['new'] = '';
260 if ($row->rc_minor)
261 $vals['minor'] = '';
262 }
263
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);
268 }
269
270 /* Add the timestamp. */
271 if ($this->fld_timestamp)
272 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
273
274 /* Add edit summary / log summary. */
275 if ($this->fld_comment && !empty ($row->rc_comment)) {
276 $vals['comment'] = $row->rc_comment;
277 }
278
279 if ($this->fld_redirect)
280 if($row->page_is_redirect)
281 $vals['redirect'] = '';
282
283 /* Add the patrolled flag */
284 if ($this->fld_patrolled && $row->rc_patrolled == 1)
285 $vals['patrolled'] = '';
286
287 return $vals;
288 }
289
290 private function parseRCType($type)
291 {
292 if(is_array($type))
293 {
294 $retval = array();
295 foreach($type as $t)
296 $retval[] = $this->parseRCType($t);
297 return $retval;
298 }
299 switch($type)
300 {
301 case 'edit': return RC_EDIT;
302 case 'new': return RC_NEW;
303 case 'log': return RC_LOG;
304 }
305 }
306
307 public function getAllowedParams() {
308 return array (
309 'start' => array (
310 ApiBase :: PARAM_TYPE => 'timestamp'
311 ),
312 'end' => array (
313 ApiBase :: PARAM_TYPE => 'timestamp'
314 ),
315 'dir' => array (
316 ApiBase :: PARAM_DFLT => 'older',
317 ApiBase :: PARAM_TYPE => array (
318 'newer',
319 'older'
320 )
321 ),
322 'namespace' => array (
323 ApiBase :: PARAM_ISMULTI => true,
324 ApiBase :: PARAM_TYPE => 'namespace'
325 ),
326 'titles' => array(
327 ApiBase :: PARAM_ISMULTI => true
328 ),
329 'prop' => array (
330 ApiBase :: PARAM_ISMULTI => true,
331 ApiBase :: PARAM_DFLT => 'title|timestamp|ids',
332 ApiBase :: PARAM_TYPE => array (
333 'user',
334 'comment',
335 'flags',
336 'timestamp',
337 'title',
338 'ids',
339 'sizes',
340 'redirect',
341 'patrolled'
342 )
343 ),
344 'show' => array (
345 ApiBase :: PARAM_ISMULTI => true,
346 ApiBase :: PARAM_TYPE => array (
347 'minor',
348 '!minor',
349 'bot',
350 '!bot',
351 'anon',
352 '!anon',
353 'redirect',
354 '!redirect'
355 )
356 ),
357 'limit' => 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
363 ),
364 'type' => array (
365 ApiBase :: PARAM_ISMULTI => true,
366 ApiBase :: PARAM_TYPE => array (
367 'edit',
368 'new',
369 'log'
370 )
371 )
372 );
373 }
374
375 public function getParamDescription() {
376 return array (
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',
383 'show' => array (
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'
386 ),
387 'type' => 'Which types of changes to show.',
388 'limit' => 'How many total pages to return.'
389 );
390 }
391
392 public function getDescription() {
393 return 'Enumerate recent changes';
394 }
395
396 protected function getExamples() {
397 return array (
398 'api.php?action=query&list=recentchanges'
399 );
400 }
401
402 public function getVersion() {
403 return __CLASS__ . ': $Id$';
404 }
405 }
406