API: Fix up r46825:
[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 protected function getTokenFunctions() {
48 // tokenname => function
49 // function prototype is func($pageid, $title, $rev)
50 // should return token or false
51
52 // Don't call the hooks twice
53 if(isset($this->tokenFunctions))
54 return $this->tokenFunctions;
55
56 // If we're in JSON callback mode, no tokens can be obtained
57 if(!is_null($this->getMain()->getRequest()->getVal('callback')))
58 return array();
59
60 $this->tokenFunctions = array(
61 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
62 );
63 wfRunHooks('APIQueryRecentChangesTokens', array(&$this->tokenFunctions));
64 return $this->tokenFunctions;
65 }
66
67 public static function getPatrolToken($pageid, $title, $rc)
68 {
69 global $wgUser;
70 if(!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
71 return false;
72
73 // The patrol token is always the same, let's exploit that
74 static $cachedPatrolToken = null;
75 if(!is_null($cachedPatrolToken))
76 return $cachedPatrolToken;
77
78 $cachedPatrolToken = $wgUser->editToken();
79 return $cachedPatrolToken;
80 }
81
82 /**
83 * Generates and outputs the result of this query based upon the provided parameters.
84 */
85 public function execute() {
86 /* Get the parameters of the request. */
87 $params = $this->extractRequestParams();
88
89 /* Build our basic query. Namely, something along the lines of:
90 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
91 * AND rc_timestamp < $end AND rc_namespace = $namespace
92 * AND rc_deleted = '0'
93 */
94 $db = $this->getDB();
95 $this->addTables('recentchanges');
96 $this->addOption('USE INDEX', array('recentchanges' => 'rc_timestamp'));
97 $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
98 $this->addWhereFld('rc_namespace', $params['namespace']);
99 $this->addWhereFld('rc_deleted', 0);
100
101 if(!is_null($params['type']))
102 $this->addWhereFld('rc_type', $this->parseRCType($params['type']));
103
104 if (!is_null($params['show'])) {
105 $show = array_flip($params['show']);
106
107 /* Check for conflicting parameters. */
108 if ((isset ($show['minor']) && isset ($show['!minor']))
109 || (isset ($show['bot']) && isset ($show['!bot']))
110 || (isset ($show['anon']) && isset ($show['!anon']))
111 || (isset ($show['redirect']) && isset ($show['!redirect']))
112 || (isset ($show['patrolled']) && isset ($show['!patrolled']))) {
113
114 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
115 }
116
117 // Check permissions
118 global $wgUser;
119 if((isset($show['patrolled']) || isset($show['!patrolled'])) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
120 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
121
122 /* Add additional conditions to query depending upon parameters. */
123 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
124 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
125 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
126 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
127 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
128 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
129 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
130 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
131 $this->addWhereIf('page_is_redirect = 1', isset ($show['redirect']));
132 // Don't throw log entries out the window here
133 $this->addWhereIf('page_is_redirect = 0 OR page_is_redirect IS NULL', isset ($show['!redirect']));
134 }
135
136 /* Add the fields we're concerned with to out query. */
137 $this->addFields(array (
138 'rc_timestamp',
139 'rc_namespace',
140 'rc_title',
141 'rc_cur_id',
142 'rc_type',
143 'rc_moved_to_ns',
144 'rc_moved_to_title'
145 ));
146
147 /* Determine what properties we need to display. */
148 if (!is_null($params['prop'])) {
149 $prop = array_flip($params['prop']);
150
151 /* Set up internal members based upon params. */
152 $this->fld_comment = isset ($prop['comment']);
153 $this->fld_user = isset ($prop['user']);
154 $this->fld_flags = isset ($prop['flags']);
155 $this->fld_timestamp = isset ($prop['timestamp']);
156 $this->fld_title = isset ($prop['title']);
157 $this->fld_ids = isset ($prop['ids']);
158 $this->fld_sizes = isset ($prop['sizes']);
159 $this->fld_redirect = isset($prop['redirect']);
160 $this->fld_patrolled = isset($prop['patrolled']);
161 $this->fld_loginfo = isset($prop['loginfo']);
162
163 global $wgUser;
164 if($this->fld_patrolled && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
165 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
166
167 /* Add fields to our query if they are specified as a needed parameter. */
168 $this->addFieldsIf('rc_id', $this->fld_ids);
169 $this->addFieldsIf('rc_this_oldid', $this->fld_ids);
170 $this->addFieldsIf('rc_last_oldid', $this->fld_ids);
171 $this->addFieldsIf('rc_comment', $this->fld_comment);
172 $this->addFieldsIf('rc_user', $this->fld_user);
173 $this->addFieldsIf('rc_user_text', $this->fld_user);
174 $this->addFieldsIf('rc_minor', $this->fld_flags);
175 $this->addFieldsIf('rc_bot', $this->fld_flags);
176 $this->addFieldsIf('rc_new', $this->fld_flags);
177 $this->addFieldsIf('rc_old_len', $this->fld_sizes);
178 $this->addFieldsIf('rc_new_len', $this->fld_sizes);
179 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled);
180 $this->addFieldsIf('rc_logid', $this->fld_loginfo);
181 $this->addFieldsIf('rc_log_type', $this->fld_loginfo);
182 $this->addFieldsIf('rc_log_action', $this->fld_loginfo);
183 $this->addFieldsIf('rc_params', $this->fld_loginfo);
184 if($this->fld_redirect || isset($show['redirect']) || isset($show['!redirect']))
185 {
186 $this->addTables('page');
187 $this->addJoinConds(array('page' => array('LEFT JOIN', array('rc_namespace=page_namespace', 'rc_title=page_title'))));
188 $this->addFields('page_is_redirect');
189 }
190 }
191 $this->token = $params['token'];
192 $this->addOption('LIMIT', $params['limit'] +1);
193
194 $count = 0;
195 /* Perform the actual query. */
196 $db = $this->getDB();
197 $res = $this->select(__METHOD__);
198
199 /* Iterate through the rows, adding data extracted from them to our query result. */
200 while ($row = $db->fetchObject($res)) {
201 if (++ $count > $params['limit']) {
202 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
203 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
204 break;
205 }
206
207 /* Extract the data from a single row. */
208 $vals = $this->extractRowInfo($row);
209
210 /* Add that row's data to our final output. */
211 if(!$vals)
212 continue;
213 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
214 if(!$fit)
215 {
216 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
217 break;
218 }
219 }
220
221 $db->freeResult($res);
222
223 /* Format the result */
224 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'rc');
225 }
226
227 /**
228 * Extracts from a single sql row the data needed to describe one recent change.
229 *
230 * @param $row The row from which to extract the data.
231 * @return An array mapping strings (descriptors) to their respective string values.
232 * @access private
233 */
234 private function extractRowInfo($row) {
235 /* If page was moved somewhere, get the title of the move target. */
236 $movedToTitle = false;
237 if (isset($row->rc_moved_to_title) && $row->rc_moved_to_title !== '')
238 $movedToTitle = Title :: makeTitle($row->rc_moved_to_ns, $row->rc_moved_to_title);
239
240 /* Determine the title of the page that has been changed. */
241 $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
242
243 /* Our output data. */
244 $vals = array ();
245
246 $type = intval ( $row->rc_type );
247
248 /* Determine what kind of change this was. */
249 switch ( $type ) {
250 case RC_EDIT: $vals['type'] = 'edit'; break;
251 case RC_NEW: $vals['type'] = 'new'; break;
252 case RC_MOVE: $vals['type'] = 'move'; break;
253 case RC_LOG: $vals['type'] = 'log'; break;
254 case RC_MOVE_OVER_REDIRECT: $vals['type'] = 'move over redirect'; break;
255 default: $vals['type'] = $type;
256 }
257
258 /* Create a new entry in the result for the title. */
259 if ($this->fld_title) {
260 ApiQueryBase :: addTitleInfo($vals, $title);
261 if ($movedToTitle)
262 ApiQueryBase :: addTitleInfo($vals, $movedToTitle, "new_");
263 }
264
265 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
266 if ($this->fld_ids) {
267 $vals['rcid'] = intval($row->rc_id);
268 $vals['pageid'] = intval($row->rc_cur_id);
269 $vals['revid'] = intval($row->rc_this_oldid);
270 $vals['old_revid'] = intval( $row->rc_last_oldid );
271 }
272
273 /* Add user data and 'anon' flag, if use is anonymous. */
274 if ($this->fld_user) {
275 $vals['user'] = $row->rc_user_text;
276 if(!$row->rc_user)
277 $vals['anon'] = '';
278 }
279
280 /* Add flags, such as new, minor, bot. */
281 if ($this->fld_flags) {
282 if ($row->rc_bot)
283 $vals['bot'] = '';
284 if ($row->rc_new)
285 $vals['new'] = '';
286 if ($row->rc_minor)
287 $vals['minor'] = '';
288 }
289
290 /* Add sizes of each revision. (Only available on 1.10+) */
291 if ($this->fld_sizes) {
292 $vals['oldlen'] = intval($row->rc_old_len);
293 $vals['newlen'] = intval($row->rc_new_len);
294 }
295
296 /* Add the timestamp. */
297 if ($this->fld_timestamp)
298 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
299
300 /* Add edit summary / log summary. */
301 if ($this->fld_comment && isset($row->rc_comment)) {
302 $vals['comment'] = $row->rc_comment;
303 }
304
305 if ($this->fld_redirect)
306 if($row->page_is_redirect)
307 $vals['redirect'] = '';
308
309 /* Add the patrolled flag */
310 if ($this->fld_patrolled && $row->rc_patrolled == 1)
311 $vals['patrolled'] = '';
312
313 if ($this->fld_loginfo && $row->rc_type == RC_LOG) {
314 $vals['logid'] = $row->rc_logid;
315 $vals['logtype'] = $row->rc_log_type;
316 $vals['logaction'] = $row->rc_log_action;
317 ApiQueryLogEvents::addLogParams($this->getResult(),
318 $vals, $row->rc_params,
319 $row->rc_log_type, $row->rc_timestamp);
320 }
321
322 if(!is_null($this->token))
323 {
324 $tokenFunctions = $this->getTokenFunctions();
325 foreach($this->token as $t)
326 {
327 $val = call_user_func($tokenFunctions[$t], $row->rc_cur_id,
328 $title, RecentChange::newFromRow($row));
329 if($val === false)
330 $this->setWarning("Action '$t' is not allowed for the current user");
331 else
332 $vals[$t . 'token'] = $val;
333 }
334 }
335
336 return $vals;
337 }
338
339 private function parseRCType($type)
340 {
341 if(is_array($type))
342 {
343 $retval = array();
344 foreach($type as $t)
345 $retval[] = $this->parseRCType($t);
346 return $retval;
347 }
348 switch($type)
349 {
350 case 'edit': return RC_EDIT;
351 case 'new': return RC_NEW;
352 case 'log': return RC_LOG;
353 }
354 }
355
356 public function getAllowedParams() {
357 return array (
358 'start' => array (
359 ApiBase :: PARAM_TYPE => 'timestamp'
360 ),
361 'end' => array (
362 ApiBase :: PARAM_TYPE => 'timestamp'
363 ),
364 'dir' => array (
365 ApiBase :: PARAM_DFLT => 'older',
366 ApiBase :: PARAM_TYPE => array (
367 'newer',
368 'older'
369 )
370 ),
371 'namespace' => array (
372 ApiBase :: PARAM_ISMULTI => true,
373 ApiBase :: PARAM_TYPE => 'namespace'
374 ),
375 'prop' => array (
376 ApiBase :: PARAM_ISMULTI => true,
377 ApiBase :: PARAM_DFLT => 'title|timestamp|ids',
378 ApiBase :: PARAM_TYPE => array (
379 'user',
380 'comment',
381 'flags',
382 'timestamp',
383 'title',
384 'ids',
385 'sizes',
386 'redirect',
387 'patrolled',
388 'loginfo',
389 )
390 ),
391 'token' => array(
392 ApiBase :: PARAM_TYPE => array_keys($this->getTokenFunctions()),
393 ApiBase :: PARAM_ISMULTI => true
394 ),
395 'show' => array (
396 ApiBase :: PARAM_ISMULTI => true,
397 ApiBase :: PARAM_TYPE => array (
398 'minor',
399 '!minor',
400 'bot',
401 '!bot',
402 'anon',
403 '!anon',
404 'redirect',
405 '!redirect',
406 'patrolled',
407 '!patrolled'
408 )
409 ),
410 'limit' => array (
411 ApiBase :: PARAM_DFLT => 10,
412 ApiBase :: PARAM_TYPE => 'limit',
413 ApiBase :: PARAM_MIN => 1,
414 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
415 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
416 ),
417 'type' => array (
418 ApiBase :: PARAM_ISMULTI => true,
419 ApiBase :: PARAM_TYPE => array (
420 'edit',
421 'new',
422 'log'
423 )
424 )
425 );
426 }
427
428 public function getParamDescription() {
429 return array (
430 'start' => 'The timestamp to start enumerating from.',
431 'end' => 'The timestamp to end enumerating.',
432 'dir' => 'In which direction to enumerate.',
433 'namespace' => 'Filter log entries to only this namespace(s)',
434 'prop' => 'Include additional pieces of information',
435 'token' => 'Which tokens to obtain for each change',
436 'show' => array (
437 'Show only items that meet this criteria.',
438 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
439 ),
440 'type' => 'Which types of changes to show.',
441 'limit' => 'How many total changes to return.'
442 );
443 }
444
445 public function getDescription() {
446 return 'Enumerate recent changes';
447 }
448
449 protected function getExamples() {
450 return array (
451 'api.php?action=query&list=recentchanges'
452 );
453 }
454
455 public function getVersion() {
456 return __CLASS__ . ': $Id$';
457 }
458 }