* remove end of line whitespace
[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 $db = $this->getDB();
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);
67 if(!empty($titles))
68 {
69 $lb = new LinkBatch;
70 foreach($titles as $t)
71 {
72 $obj = Title::newFromText($t);
73 $lb->addObj($obj);
74 if($obj->getNamespace() < 0)
75 {
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;
80 }
81 }
82 $where = $lb->constructSet('rc', $this->getDb());
83 if($where != '')
84 $this->addWhere($where);
85 }
86
87 if(!is_null($type))
88 $this->addWhereFld('rc_type', $this->parseRCType($type));
89
90 if (!is_null($show)) {
91 $show = array_flip($show);
92
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
99 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
100 }
101
102 /* Add additional conditions to query depending upon parameters. */
103 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
104 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
105 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
106 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
107 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
108 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
109 $this->addWhereIf('page_is_redirect = 1', isset ($show['redirect']));
110 // Don't throw log entries out the window here
111 $this->addWhereIf('page_is_redirect = 0 OR page_is_redirect IS NULL', isset ($show['!redirect']));
112 }
113
114 /* Add the fields we're concerned with to out query. */
115 $this->addFields(array (
116 'rc_timestamp',
117 'rc_namespace',
118 'rc_title',
119 'rc_type',
120 'rc_moved_to_ns',
121 'rc_moved_to_title'
122 ));
123
124 /* Determine what properties we need to display. */
125 if (!is_null($prop)) {
126 $prop = array_flip($prop);
127
128 /* Set up internal members based upon params. */
129 $this->fld_comment = isset ($prop['comment']);
130 $this->fld_user = isset ($prop['user']);
131 $this->fld_flags = isset ($prop['flags']);
132 $this->fld_timestamp = isset ($prop['timestamp']);
133 $this->fld_title = isset ($prop['title']);
134 $this->fld_ids = isset ($prop['ids']);
135 $this->fld_sizes = isset ($prop['sizes']);
136 $this->fld_redirect = isset($prop['redirect']);
137 $this->fld_patrolled = isset($prop['patrolled']);
138
139 global $wgUser;
140 if($this->fld_patrolled && !$wgUser->isAllowed('patrol'))
141 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
142
143 /* Add fields to our query if they are specified as a needed parameter. */
144 $this->addFieldsIf('rc_id', $this->fld_ids);
145 $this->addFieldsIf('rc_cur_id', $this->fld_ids);
146 $this->addFieldsIf('rc_this_oldid', $this->fld_ids);
147 $this->addFieldsIf('rc_last_oldid', $this->fld_ids);
148 $this->addFieldsIf('rc_comment', $this->fld_comment);
149 $this->addFieldsIf('rc_user', $this->fld_user);
150 $this->addFieldsIf('rc_user_text', $this->fld_user);
151 $this->addFieldsIf('rc_minor', $this->fld_flags);
152 $this->addFieldsIf('rc_bot', $this->fld_flags);
153 $this->addFieldsIf('rc_new', $this->fld_flags);
154 $this->addFieldsIf('rc_old_len', $this->fld_sizes);
155 $this->addFieldsIf('rc_new_len', $this->fld_sizes);
156 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled);
157 if($this->fld_redirect || isset($show['redirect']) || isset($show['!redirect']))
158 {
159 $page = $db->tableName('page');
160 $tables = "$page RIGHT JOIN $rc FORCE INDEX(rc_timestamp) ON page_namespace=rc_namespace AND page_title=rc_title";
161 $this->addFields('page_is_redirect');
162 }
163 }
164 if(!isset($tables))
165 $tables = "$rc FORCE INDEX(rc_timestamp)";
166 $this->addTables($tables);
167 /* Specify the limit for our query. It's $limit+1 because we (possibly) need to
168 * generate a "continue" parameter, to allow paging. */
169 $this->addOption('LIMIT', $limit +1);
170
171 $data = array ();
172 $count = 0;
173
174 /* Perform the actual query. */
175 $db = $this->getDB();
176 $res = $this->select(__METHOD__);
177
178 /* Iterate through the rows, adding data extracted from them to our query result. */
179 while ($row = $db->fetchObject($res)) {
180 if (++ $count > $limit) {
181 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
182 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
183 break;
184 }
185
186 /* Extract the data from a single row. */
187 $vals = $this->extractRowInfo($row);
188
189 /* Add that row's data to our final output. */
190 if($vals)
191 $data[] = $vals;
192 }
193
194 $db->freeResult($res);
195
196 /* Format the result */
197 $result = $this->getResult();
198 $result->setIndexedTagName($data, 'rc');
199 $result->addValue('query', $this->getModuleName(), $data);
200 }
201
202 /**
203 * Extracts from a single sql row the data needed to describe one recent change.
204 *
205 * @param $row The row from which to extract the data.
206 * @return An array mapping strings (descriptors) to their respective string values.
207 * @access private
208 */
209 private function extractRowInfo($row) {
210 /* If page was moved somewhere, get the title of the move target. */
211 $movedToTitle = false;
212 if (!empty($row->rc_moved_to_title))
213 $movedToTitle = Title :: makeTitle($row->rc_moved_to_ns, $row->rc_moved_to_title);
214
215 /* Determine the title of the page that has been changed. */
216 $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
217
218 /* Our output data. */
219 $vals = array ();
220
221 $type = intval ( $row->rc_type );
222
223 /* Determine what kind of change this was. */
224 switch ( $type ) {
225 case RC_EDIT: $vals['type'] = 'edit'; break;
226 case RC_NEW: $vals['type'] = 'new'; break;
227 case RC_MOVE: $vals['type'] = 'move'; break;
228 case RC_LOG: $vals['type'] = 'log'; break;
229 case RC_MOVE_OVER_REDIRECT: $vals['type'] = 'move over redirect'; break;
230 default: $vals['type'] = $type;
231 }
232
233 /* Create a new entry in the result for the title. */
234 if ($this->fld_title) {
235 ApiQueryBase :: addTitleInfo($vals, $title);
236 if ($movedToTitle)
237 ApiQueryBase :: addTitleInfo($vals, $movedToTitle, "new_");
238 }
239
240 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
241 if ($this->fld_ids) {
242 $vals['rcid'] = intval($row->rc_id);
243 $vals['pageid'] = intval($row->rc_cur_id);
244 $vals['revid'] = intval($row->rc_this_oldid);
245 $vals['old_revid'] = intval( $row->rc_last_oldid );
246 }
247
248 /* Add user data and 'anon' flag, if use is anonymous. */
249 if ($this->fld_user) {
250 $vals['user'] = $row->rc_user_text;
251 if(!$row->rc_user)
252 $vals['anon'] = '';
253 }
254
255 /* Add flags, such as new, minor, bot. */
256 if ($this->fld_flags) {
257 if ($row->rc_bot)
258 $vals['bot'] = '';
259 if ($row->rc_new)
260 $vals['new'] = '';
261 if ($row->rc_minor)
262 $vals['minor'] = '';
263 }
264
265 /* Add sizes of each revision. (Only available on 1.10+) */
266 if ($this->fld_sizes) {
267 $vals['oldlen'] = intval($row->rc_old_len);
268 $vals['newlen'] = intval($row->rc_new_len);
269 }
270
271 /* Add the timestamp. */
272 if ($this->fld_timestamp)
273 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
274
275 /* Add edit summary / log summary. */
276 if ($this->fld_comment && !empty ($row->rc_comment)) {
277 $vals['comment'] = $row->rc_comment;
278 }
279
280 if ($this->fld_redirect)
281 if($row->page_is_redirect)
282 $vals['redirect'] = '';
283
284 /* Add the patrolled flag */
285 if ($this->fld_patrolled && $row->rc_patrolled == 1)
286 $vals['patrolled'] = '';
287
288 return $vals;
289 }
290
291 private function parseRCType($type)
292 {
293 if(is_array($type))
294 {
295 $retval = array();
296 foreach($type as $t)
297 $retval[] = $this->parseRCType($t);
298 return $retval;
299 }
300 switch($type)
301 {
302 case 'edit': return RC_EDIT;
303 case 'new': return RC_NEW;
304 case 'log': return RC_LOG;
305 }
306 }
307
308 public function getAllowedParams() {
309 return array (
310 'start' => array (
311 ApiBase :: PARAM_TYPE => 'timestamp'
312 ),
313 'end' => array (
314 ApiBase :: PARAM_TYPE => 'timestamp'
315 ),
316 'dir' => array (
317 ApiBase :: PARAM_DFLT => 'older',
318 ApiBase :: PARAM_TYPE => array (
319 'newer',
320 'older'
321 )
322 ),
323 'namespace' => array (
324 ApiBase :: PARAM_ISMULTI => true,
325 ApiBase :: PARAM_TYPE => 'namespace'
326 ),
327 'titles' => array(
328 ApiBase :: PARAM_ISMULTI => true
329 ),
330 'prop' => array (
331 ApiBase :: PARAM_ISMULTI => true,
332 ApiBase :: PARAM_DFLT => 'title|timestamp|ids',
333 ApiBase :: PARAM_TYPE => array (
334 'user',
335 'comment',
336 'flags',
337 'timestamp',
338 'title',
339 'ids',
340 'sizes',
341 'redirect',
342 'patrolled'
343 )
344 ),
345 'show' => array (
346 ApiBase :: PARAM_ISMULTI => true,
347 ApiBase :: PARAM_TYPE => array (
348 'minor',
349 '!minor',
350 'bot',
351 '!bot',
352 'anon',
353 '!anon',
354 'redirect',
355 '!redirect'
356 )
357 ),
358 'limit' => array (
359 ApiBase :: PARAM_DFLT => 10,
360 ApiBase :: PARAM_TYPE => 'limit',
361 ApiBase :: PARAM_MIN => 1,
362 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
363 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
364 ),
365 'type' => array (
366 ApiBase :: PARAM_ISMULTI => true,
367 ApiBase :: PARAM_TYPE => array (
368 'edit',
369 'new',
370 'log'
371 )
372 )
373 );
374 }
375
376 public function getParamDescription() {
377 return array (
378 'start' => 'The timestamp to start enumerating from.',
379 'end' => 'The timestamp to end enumerating.',
380 'dir' => 'In which direction to enumerate.',
381 'namespace' => 'Filter log entries to only this namespace(s)',
382 'titles' => 'Filter log entries to only these page titles',
383 'prop' => 'Include additional pieces of information',
384 'show' => array (
385 'Show only items that meet this criteria.',
386 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
387 ),
388 'type' => 'Which types of changes to show.',
389 'limit' => 'How many total pages to return.'
390 );
391 }
392
393 public function getDescription() {
394 return 'Enumerate recent changes';
395 }
396
397 protected function getExamples() {
398 return array (
399 'api.php?action=query&list=recentchanges'
400 );
401 }
402
403 public function getVersion() {
404 return __CLASS__ . ': $Id$';
405 }
406 }