API: Ensure it checks *only* the fast limit or the slow limit, not both. On fast...
[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 = 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 extremelly
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
89 if (isset ($prop['user'])) {
90 $this->addFields('rev_user');
91 $this->addFields('rev_user_text');
92 $this->fld_user = true;
93 }
94 if (isset ($prop['content'])) {
95
96 // For each page we will request, the user must have read rights for that page
97 foreach ($pageSet->getGoodTitles() as $title) {
98 if( !$title->userCanRead() )
99 $this->dieUsage(
100 'The current user is not allowed to read ' . $title->getPrefixedText(),
101 'accessdenied');
102 }
103
104 $this->addTables('text');
105 $this->addWhere('rev_text_id=old_id');
106 $this->addFields('old_id');
107 $this->addFields('old_text');
108 $this->addFields('old_flags');
109
110 $this->fld_content = true;
111
112 $this->expandTemplates = $expandtemplates;
113 }
114
115 $userMax = ($this->fld_content ? 50 : 500);
116 $botMax = ($this->fld_content ? 200 : 10000);
117
118 if ($enumRevMode) {
119
120 // This is mostly to prevent parameter errors (and optimize sql?)
121 if (!is_null($startid) && !is_null($start))
122 $this->dieUsage('start and startid cannot be used together', 'badparams');
123
124 if (!is_null($endid) && !is_null($end))
125 $this->dieUsage('end and endid cannot be used together', 'badparams');
126
127 if(!is_null($user) && !is_null( $excludeuser))
128 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
129
130 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
131 // the same result. This way users may request revisions starting at a given time,
132 // but to page through results use the rev_id returned after each page.
133 // Switching to rev_id removes the potential problem of having more than
134 // one row with the same timestamp for the same page.
135 // The order needs to be the same as start parameter to avoid SQL filesort.
136
137 if (is_null($startid) && is_null($endid))
138 $this->addWhereRange('rev_timestamp', $dir, $start, $end);
139 else
140 $this->addWhereRange('rev_id', $dir, $startid, $endid);
141
142 // must manually initialize unset limit
143 if (is_null($limit))
144 $limit = 10;
145 $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
146
147 // There is only one ID, use it
148 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
149
150 if(!is_null($user)) {
151 $this->addWhereFld('rev_user_text', $user);
152 } elseif (!is_null( $excludeuser)) {
153 $this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
154 }
155 }
156 elseif ($revCount > 0) {
157 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
158
159 // Get all revision IDs
160 $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
161
162 // assumption testing -- we should never get more then $revCount rows.
163 $limit = $revCount;
164 }
165 elseif ($pageCount > 0) {
166 // When working in multi-page non-enumeration mode,
167 // limit to the latest revision only
168 $this->addTables('page');
169 $this->addWhere('page_id=rev_page');
170 $this->addWhere('page_latest=rev_id');
171 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
172
173 // Get all page IDs
174 $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
175
176 // assumption testing -- we should never get more then $pageCount rows.
177 $limit = $pageCount;
178 } else
179 ApiBase :: dieDebug(__METHOD__, 'param validation?');
180
181 $this->addOption('LIMIT', $limit +1);
182
183 $data = array ();
184 $count = 0;
185 $res = $this->select(__METHOD__);
186
187 $db = $this->getDB();
188 while ($row = $db->fetchObject($res)) {
189
190 if (++ $count > $limit) {
191 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
192 if (!$enumRevMode)
193 ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
194 $this->setContinueEnumParameter('startid', intval($row->rev_id));
195 break;
196 }
197
198 $this->getResult()->addValue(
199 array (
200 'query',
201 'pages',
202 intval($row->rev_page),
203 'revisions'),
204 null,
205 $this->extractRowInfo($row));
206 }
207 $db->freeResult($res);
208
209 // Ensure that all revisions are shown as '<rev>' elements
210 $result = $this->getResult();
211 if ($result->getIsRawMode()) {
212 $data =& $result->getData();
213 foreach ($data['query']['pages'] as & $page) {
214 if (is_array($page) && array_key_exists('revisions', $page)) {
215 $result->setIndexedTagName($page['revisions'], 'rev');
216 }
217 }
218 }
219 }
220
221 private function extractRowInfo($row) {
222
223 $vals = array ();
224
225 if ($this->fld_ids) {
226 $vals['revid'] = intval($row->rev_id);
227 // $vals['oldid'] = intval($row->rev_text_id); // todo: should this be exposed?
228 }
229
230 if ($this->fld_flags && $row->rev_minor_edit)
231 $vals['minor'] = '';
232
233 if ($this->fld_user) {
234 $vals['user'] = $row->rev_user_text;
235 if (!$row->rev_user)
236 $vals['anon'] = '';
237 }
238
239 if ($this->fld_timestamp) {
240 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
241 }
242
243 if ($this->fld_size && !is_null($row->rev_len)) {
244 $vals['size'] = intval($row->rev_len);
245 }
246
247 if ($this->fld_comment && !empty ($row->rev_comment)) {
248 $vals['comment'] = $row->rev_comment;
249 }
250
251 if ($this->fld_content) {
252 $text = Revision :: getRevisionText($row);
253 if ($this->expandTemplates) {
254 global $wgParser;
255 $text = $wgParser->preprocess( $text, Title::newFromID($row->rev_page), new ParserOptions() );
256 }
257 ApiResult :: setContent($vals, $text);
258 }
259
260 return $vals;
261 }
262
263 protected function getAllowedParams() {
264 return array (
265 'prop' => array (
266 ApiBase :: PARAM_ISMULTI => true,
267 ApiBase :: PARAM_DFLT => 'ids|timestamp|flags|comment|user',
268 ApiBase :: PARAM_TYPE => array (
269 'ids',
270 'flags',
271 'timestamp',
272 'user',
273 'size',
274 'comment',
275 'content',
276 )
277 ),
278 'limit' => array (
279 ApiBase :: PARAM_TYPE => 'limit',
280 ApiBase :: PARAM_MIN => 1,
281 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
282 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
283 ),
284 'startid' => array (
285 ApiBase :: PARAM_TYPE => 'integer'
286 ),
287 'endid' => array (
288 ApiBase :: PARAM_TYPE => 'integer'
289 ),
290 'start' => array (
291 ApiBase :: PARAM_TYPE => 'timestamp'
292 ),
293 'end' => array (
294 ApiBase :: PARAM_TYPE => 'timestamp'
295 ),
296 'dir' => array (
297 ApiBase :: PARAM_DFLT => 'older',
298 ApiBase :: PARAM_TYPE => array (
299 'newer',
300 'older'
301 )
302 ),
303 'user' => array(
304 ApiBase :: PARAM_TYPE => 'user'
305 ),
306 'excludeuser' => array(
307 ApiBase :: PARAM_TYPE => 'user'
308 ),
309
310 'expandtemplates' => false,
311 );
312 }
313
314 protected function getParamDescription() {
315 return array (
316 'prop' => 'Which properties to get for each revision.',
317 'limit' => 'limit how many revisions will be returned (enum)',
318 'startid' => 'from which revision id to start enumeration (enum)',
319 'endid' => 'stop revision enumeration on this revid (enum)',
320 'start' => 'from which revision timestamp to start enumeration (enum)',
321 'end' => 'enumerate up to this timestamp (enum)',
322 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)',
323 'user' => 'only include revisions made by user',
324 'excludeuser' => 'exclude revisions made by user',
325 'expandtemplates' => 'expand templates in revision content'
326 );
327 }
328
329 protected function getDescription() {
330 return array (
331 'Get revision information.',
332 'This module may be used in several ways:',
333 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
334 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
335 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
336 'All parameters marked as (enum) may only be used with a single page (#2).'
337 );
338 }
339
340 protected function getExamples() {
341 return array (
342 'Get data with content for the last revision of titles "API" and "Main Page":',
343 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
344 'Get last 5 revisions of the "Main Page":',
345 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
346 'Get first 5 revisions of the "Main Page":',
347 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
348 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
349 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
350 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
351 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
352 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
353 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
354 );
355 }
356
357 public function getVersion() {
358 return __CLASS__ . ': $Id$';
359 }
360 }
361