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