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