API: Standardize limits. Use ApiBase constants to avoid similar breakage and inconsis...
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
1 <?php
2
3 /*
4 * Created on Sep 7, 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 revisions of a given page, or show top revisions of multiple pages.
33 * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
34 * In the enumeration mode, ranges of revisions may be requested and filtered.
35 *
36 * @addtogroup API
37 */
38 class ApiQueryRevisions extends ApiQueryBase {
39
40 public function __construct($query, $moduleName) {
41 parent :: __construct($query, $moduleName, 'rv');
42 }
43
44 private $fld_ids = false, $fld_flags = false, $fld_timestamp = false, $fld_size = false,
45 $fld_comment = false, $fld_user = false, $fld_content = false;
46
47 public function execute() {
48 $limit = $startid = $endid = $start = $end = $dir = $prop = $user = $excludeuser = $token = null;
49 extract($this->extractRequestParams());
50
51 // If any of those parameters are used, work in 'enumeration' mode.
52 // Enum mode can only be used when exactly one page is provided.
53 // Enumerating revisions on multiple pages make it extremely
54 // difficult to manage continuations and require additional SQL indexes
55 $enumRevMode = (!is_null($user) || !is_null($excludeuser) || !is_null($limit) || !is_null($startid) || !is_null($endid) || $dir === 'newer' || !is_null($start) || !is_null($end));
56
57
58 $pageSet = $this->getPageSet();
59 $pageCount = $pageSet->getGoodTitleCount();
60 $revCount = $pageSet->getRevisionCount();
61
62 // Optimization -- nothing to do
63 if ($revCount === 0 && $pageCount === 0)
64 return;
65
66 if ($revCount > 0 && $enumRevMode)
67 $this->dieUsage('The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids');
68
69 if ($pageCount > 1 && $enumRevMode)
70 $this->dieUsage('titles, pageids or a generator was used to supply multiple pages, but the limit, startid, endid, dirNewer, user, excludeuser, start and end parameters may only be used on a single page.', 'multpages');
71
72 $this->addTables('revision');
73 $this->addWhere('rev_deleted=0');
74
75 $prop = array_flip($prop);
76
77 // These field are needed regardless of the client requesting them
78 $this->addFields('rev_id');
79 $this->addFields('rev_page');
80
81 // Optional fields
82 $this->fld_ids = isset ($prop['ids']);
83 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
84 $this->fld_flags = $this->addFieldsIf('rev_minor_edit', isset ($prop['flags']));
85 $this->fld_timestamp = $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp']));
86 $this->fld_comment = $this->addFieldsIf('rev_comment', isset ($prop['comment']));
87 $this->fld_size = $this->addFieldsIf('rev_len', isset ($prop['size']));
88 if(!is_null($token))
89 {
90 $this->tok_rollback = $this->getTokenFlag($token, 'rollback');
91 }
92
93 if (isset ($prop['user'])) {
94 $this->addFields('rev_user');
95 $this->addFields('rev_user_text');
96 $this->fld_user = true;
97 }
98 else if($this->tok_rollback)
99 $this->addFields('rev_user_text');
100
101 if (isset ($prop['content'])) {
102
103 // For each page we will request, the user must have read rights for that page
104 foreach ($pageSet->getGoodTitles() as $title) {
105 if( !$title->userCanRead() )
106 $this->dieUsage(
107 'The current user is not allowed to read ' . $title->getPrefixedText(),
108 'accessdenied');
109 }
110
111 $this->addTables('text');
112 $this->addWhere('rev_text_id=old_id');
113 $this->addFields('old_id');
114 $this->addFields('old_text');
115 $this->addFields('old_flags');
116
117 $this->fld_content = true;
118
119 $this->expandTemplates = $expandtemplates;
120 }
121
122 $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
123 $botMax = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
124
125 if ($enumRevMode) {
126
127 // This is mostly to prevent parameter errors (and optimize SQL?)
128 if (!is_null($startid) && !is_null($start))
129 $this->dieUsage('start and startid cannot be used together', 'badparams');
130
131 if (!is_null($endid) && !is_null($end))
132 $this->dieUsage('end and endid cannot be used together', 'badparams');
133
134 if(!is_null($user) && !is_null( $excludeuser))
135 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
136
137 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
138 // the same result. This way users may request revisions starting at a given time,
139 // but to page through results use the rev_id returned after each page.
140 // Switching to rev_id removes the potential problem of having more than
141 // one row with the same timestamp for the same page.
142 // The order needs to be the same as start parameter to avoid SQL filesort.
143
144 if (is_null($startid) && is_null($endid))
145 $this->addWhereRange('rev_timestamp', $dir, $start, $end);
146 else
147 $this->addWhereRange('rev_id', $dir, $startid, $endid);
148
149 // must manually initialize unset limit
150 if (is_null($limit))
151 $limit = 10;
152 $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
153
154 // There is only one ID, use it
155 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
156
157 if(!is_null($user)) {
158 $this->addWhereFld('rev_user_text', $user);
159 } elseif (!is_null( $excludeuser)) {
160 $this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
161 }
162 }
163 elseif ($revCount > 0) {
164 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
165
166 // Get all revision IDs
167 $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
168
169 // assumption testing -- we should never get more then $revCount rows.
170 $limit = $revCount;
171 }
172 elseif ($pageCount > 0) {
173 // When working in multi-page non-enumeration mode,
174 // limit to the latest revision only
175 $this->addTables('page');
176 $this->addWhere('page_id=rev_page');
177 $this->addWhere('page_latest=rev_id');
178 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
179
180 // Get all page IDs
181 $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
182
183 // assumption testing -- we should never get more then $pageCount rows.
184 $limit = $pageCount;
185 } else
186 ApiBase :: dieDebug(__METHOD__, 'param validation?');
187
188 $this->addOption('LIMIT', $limit +1);
189
190 $data = array ();
191 $count = 0;
192 $res = $this->select(__METHOD__);
193
194 $db = $this->getDB();
195 while ($row = $db->fetchObject($res)) {
196
197 if (++ $count > $limit) {
198 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
199 if (!$enumRevMode)
200 ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
201 $this->setContinueEnumParameter('startid', intval($row->rev_id));
202 break;
203 }
204
205 $this->getResult()->addValue(
206 array (
207 'query',
208 'pages',
209 intval($row->rev_page),
210 'revisions'),
211 null,
212 $this->extractRowInfo($row));
213 }
214 $db->freeResult($res);
215
216 // Ensure that all revisions are shown as '<rev>' elements
217 $result = $this->getResult();
218 if ($result->getIsRawMode()) {
219 $data =& $result->getData();
220 foreach ($data['query']['pages'] as & $page) {
221 if (is_array($page) && array_key_exists('revisions', $page)) {
222 $result->setIndexedTagName($page['revisions'], 'rev');
223 }
224 }
225 }
226 }
227
228 private function extractRowInfo($row) {
229
230 $vals = array ();
231
232 if ($this->fld_ids) {
233 $vals['revid'] = intval($row->rev_id);
234 // $vals['oldid'] = intval($row->rev_text_id); // todo: should this be exposed?
235 }
236
237 if ($this->fld_flags && $row->rev_minor_edit)
238 $vals['minor'] = '';
239
240 if ($this->fld_user) {
241 $vals['user'] = $row->rev_user_text;
242 if (!$row->rev_user)
243 $vals['anon'] = '';
244 }
245
246 if ($this->fld_timestamp) {
247 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
248 }
249
250 if ($this->fld_size && !is_null($row->rev_len)) {
251 $vals['size'] = intval($row->rev_len);
252 }
253
254 if ($this->fld_comment && !empty ($row->rev_comment)) {
255 $vals['comment'] = $row->rev_comment;
256 }
257
258 if($this->tok_rollback || ($this->fld_content && $this->expandTemplates))
259 $title = Title::newFromID($row->rev_page);
260
261 if($this->tok_rollback) {
262 global $wgUser;
263 $vals['rollbacktoken'] = $wgUser->editToken(array($title->getPrefixedText(), $row->rev_user_text));
264 }
265
266
267 if ($this->fld_content) {
268 $text = Revision :: getRevisionText($row);
269 if ($this->expandTemplates) {
270 global $wgParser;
271 $text = $wgParser->preprocess( $text, $title, new ParserOptions() );
272 }
273 ApiResult :: setContent($vals, $text);
274 }
275 return $vals;
276 }
277
278 protected function getAllowedParams() {
279 return array (
280 'prop' => array (
281 ApiBase :: PARAM_ISMULTI => true,
282 ApiBase :: PARAM_DFLT => 'ids|timestamp|flags|comment|user',
283 ApiBase :: PARAM_TYPE => array (
284 'ids',
285 'flags',
286 'timestamp',
287 'user',
288 'size',
289 'comment',
290 'content',
291 )
292 ),
293 'limit' => array (
294 ApiBase :: PARAM_TYPE => 'limit',
295 ApiBase :: PARAM_MIN => 1,
296 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
297 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
298 ),
299 'startid' => array (
300 ApiBase :: PARAM_TYPE => 'integer'
301 ),
302 'endid' => array (
303 ApiBase :: PARAM_TYPE => 'integer'
304 ),
305 'start' => array (
306 ApiBase :: PARAM_TYPE => 'timestamp'
307 ),
308 'end' => array (
309 ApiBase :: PARAM_TYPE => 'timestamp'
310 ),
311 'dir' => array (
312 ApiBase :: PARAM_DFLT => 'older',
313 ApiBase :: PARAM_TYPE => array (
314 'newer',
315 'older'
316 )
317 ),
318 'user' => array(
319 ApiBase :: PARAM_TYPE => 'user'
320 ),
321 'excludeuser' => array(
322 ApiBase :: PARAM_TYPE => 'user'
323 ),
324
325 'expandtemplates' => false,
326 'token' => array(
327 ApiBase :: PARAM_TYPE => array(
328 'rollback'
329 ),
330 ApiBase :: PARAM_ISMULTI => true
331 ),
332 );
333 }
334
335 protected function getParamDescription() {
336 return array (
337 'prop' => 'Which properties to get for each revision.',
338 'limit' => 'limit how many revisions will be returned (enum)',
339 'startid' => 'from which revision id to start enumeration (enum)',
340 'endid' => 'stop revision enumeration on this revid (enum)',
341 'start' => 'from which revision timestamp to start enumeration (enum)',
342 'end' => 'enumerate up to this timestamp (enum)',
343 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)',
344 'user' => 'only include revisions made by user',
345 'excludeuser' => 'exclude revisions made by user',
346 'expandtemplates' => 'expand templates in revision content',
347 'token' => 'Which tokens to obtain for each revision',
348 );
349 }
350
351 protected function getDescription() {
352 return array (
353 'Get revision information.',
354 'This module may be used in several ways:',
355 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
356 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
357 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
358 'All parameters marked as (enum) may only be used with a single page (#2).'
359 );
360 }
361
362 protected function getExamples() {
363 return array (
364 'Get data with content for the last revision of titles "API" and "Main Page":',
365 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
366 'Get last 5 revisions of the "Main Page":',
367 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
368 'Get first 5 revisions of the "Main Page":',
369 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
370 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
371 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
372 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
373 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
374 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
375 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
376 );
377 }
378
379 public function getVersion() {
380 return __CLASS__ . ': $Id$';
381 }
382 }
383