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