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