* Fix rvdiffformat=array
[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 = $diffto = $difftoprev = $diffformat = 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) | !$difftoprev);
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, end and difftoprev 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($diffto || $difftoprev)
90 switch($diffformat)
91 {
92 case 'traditional':
93 $this->formatter = new DiffFormatter;
94 break;
95 case 'unified':
96 $this->formatter = new UnifiedDiffFormatter;
97 break;
98 case 'array':
99 $this->formatter = new ArrayDiffFormatter;
100 }
101 if($diffto)
102 {
103 global $wgContLang;
104 $difftoRev = Revision::newFromID($diffto);
105 if(!($difftoRev instanceof Revision))
106 $this->dieUsage("There is no revision with ID $diffto", 'nosuchrev');
107 $this->diffOldText = $difftoRev->revText();
108 if($this->diffOldText == '') // deleted revision
109 $this->dieUsage("There is no revision with ID $diffto", 'nosuchrev'); // fake non-existence
110 $this->diffOldText = explode("\n", $wgContLang->segmentForDiff($this->diffOldText));
111 $this->diffto = $diffto;
112 }
113 else
114 $this->diffto = false;
115 if($difftoprev)
116 {
117 $this->revCache = array();
118 $this->difftoprev = true;
119 }
120 else
121 $this->difftoprev = false;
122
123 if (isset ($prop['user'])) {
124 $this->addFields('rev_user');
125 $this->addFields('rev_user_text');
126 $this->fld_user = true;
127 }
128 if (isset ($prop['content']) || !is_null($diffto) || $difftoprev) {
129
130 // For each page we will request, the user must have read rights for that page
131 foreach ($pageSet->getGoodTitles() as $title) {
132 if( !$title->userCanRead() )
133 $this->dieUsage(
134 'The current user is not allowed to read ' . $title->getPrefixedText(),
135 'accessdenied');
136 }
137
138 $this->addTables('text');
139 $this->addWhere('rev_text_id=old_id');
140 $this->addFields('old_id');
141 $this->addFields('old_text');
142 $this->addFields('old_flags');
143
144 $this->fld_content = isset($prop['content']);
145
146 $this->expandTemplates = $expandtemplates;
147 }
148
149 $userMax = ($this->fld_content || $diffto || $difftoprev ? 50 : 500);
150 $botMax = ($this->fld_content || $diffto || $difftoprev ? 200 : 10000);
151
152 if ($enumRevMode) {
153
154 // This is mostly to prevent parameter errors (and optimize SQL?)
155 if (!is_null($startid) && !is_null($start))
156 $this->dieUsage('start and startid cannot be used together', 'badparams');
157
158 if (!is_null($endid) && !is_null($end))
159 $this->dieUsage('end and endid cannot be used together', 'badparams');
160
161 if(!is_null($user) && !is_null( $excludeuser))
162 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
163
164 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
165 // the same result. This way users may request revisions starting at a given time,
166 // but to page through results use the rev_id returned after each page.
167 // Switching to rev_id removes the potential problem of having more than
168 // one row with the same timestamp for the same page.
169 // The order needs to be the same as start parameter to avoid SQL filesort.
170
171 if (is_null($startid) && is_null($endid))
172 $this->addWhereRange('rev_timestamp', $dir, $start, $end);
173 else
174 $this->addWhereRange('rev_id', $dir, $startid, $endid);
175
176 // must manually initialize unset limit
177 if (is_null($limit))
178 $limit = 10;
179 $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
180
181 // There is only one ID, use it
182 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
183
184 if(!is_null($user)) {
185 $this->addWhereFld('rev_user_text', $user);
186 } elseif (!is_null( $excludeuser)) {
187 $this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
188 }
189 }
190 elseif ($revCount > 0) {
191 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
192
193 // Get all revision IDs
194 $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
195
196 // assumption testing -- we should never get more then $revCount rows.
197 $limit = $revCount;
198 }
199 elseif ($pageCount > 0) {
200 // When working in multi-page non-enumeration mode,
201 // limit to the latest revision only
202 $this->addTables('page');
203 $this->addWhere('page_id=rev_page');
204 $this->addWhere('page_latest=rev_id');
205 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
206
207 // Get all page IDs
208 $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
209
210 // assumption testing -- we should never get more then $pageCount rows.
211 $limit = $pageCount;
212 } else
213 ApiBase :: dieDebug(__METHOD__, 'param validation?');
214
215 $this->addOption('LIMIT', $limit +1);
216
217 $data = array ();
218 $count = 0;
219 $res = $this->select(__METHOD__);
220
221 $db = $this->getDB();
222 while ($row = $db->fetchObject($res)) {
223
224 if (++ $count > $limit) {
225 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
226 if (!$enumRevMode)
227 ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
228 $this->setContinueEnumParameter('startid', intval($row->rev_id));
229 break;
230 }
231
232 $this->getResult()->addValue(
233 array (
234 'query',
235 'pages',
236 intval($row->rev_page),
237 'revisions'),
238 null,
239 $this->extractRowInfo($row));
240 }
241 $db->freeResult($res);
242
243 if($this->difftoprev)
244 {
245 global $wgContLang;
246 ksort($this->revCache, SORT_NUMERIC);
247 $previousRevID = null;
248 $oldText = null;
249 $data =& $this->getResult()->getData();
250 $pageID = current(array_keys($pageSet->getGoodTitles()));
251 $this->diffArr = array();
252 foreach($this->revCache as $revid => $revtext)
253 {
254 $r = array();
255 if(is_null($previousRevID))
256 {
257 // First run
258 $previousRevID = $revid;
259 $oldText = explode("\n", $wgContLang->segmentForDiff($revtext));
260 continue;
261 }
262 $newText = explode("\n", $wgContLang->segmentForDiff($revtext));
263 $diff = new Diff($oldText, $newText);
264 $r['from'] = $previousRevID;
265 $formatted = $this->formatter->format($diff);
266 if(!is_array($formatted))
267 ApiResult::setContent($r, $wgContLang->unsegmentForDiff($this->formatter->format($diff)));
268 else
269 {
270 $r['differences'] = $formatted;
271 $this->getResult()->setIndexedTagName($r['differences'], 'diff');
272 }
273 $this->diffArr[$revid] = $r;
274
275 $previousRevID = $revid;
276 $oldText = $newText;
277 }
278
279 if ( $this->diffArr ) {
280 # Populate the query result with the contents of $this->diffArr.
281 $knownrevs = array_keys($this->diffArr);
282 $i = count($knownrevs) - 1;
283 foreach($data['query']['pages'][$pageID]['revisions'] as &$rev) {
284 if ( $i >= 0 && isset ( $this->diffArr[$knownrevs[$i]] ) )
285 $rev['difftoprev'] = $this->diffArr[$knownrevs[$i]];
286 $i --;
287 }
288 }
289 }
290
291 // Ensure that all revisions are shown as '<rev>' elements
292 $result = $this->getResult();
293 if ($result->getIsRawMode()) {
294 $data =& $result->getData();
295 foreach ($data['query']['pages'] as & $page) {
296 if (is_array($page) && array_key_exists('revisions', $page)) {
297 $result->setIndexedTagName($page['revisions'], 'rev');
298 }
299 }
300 }
301 }
302
303 private function extractRowInfo($row) {
304
305 $vals = array ();
306
307 if ($this->fld_ids) {
308 $vals['revid'] = intval($row->rev_id);
309 // $vals['oldid'] = intval($row->rev_text_id); // todo: should this be exposed?
310 }
311
312 if ($this->fld_flags && $row->rev_minor_edit)
313 $vals['minor'] = '';
314
315 if ($this->fld_user) {
316 $vals['user'] = $row->rev_user_text;
317 if (!$row->rev_user)
318 $vals['anon'] = '';
319 }
320
321 if ($this->fld_timestamp) {
322 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
323 }
324
325 if ($this->fld_size && !is_null($row->rev_len)) {
326 $vals['size'] = intval($row->rev_len);
327 }
328
329 if ($this->fld_comment && !empty ($row->rev_comment)) {
330 $vals['comment'] = $row->rev_comment;
331 }
332
333 if ($this->fld_content || $this->diffto || $this->difftoprev)
334 $text = Revision :: getRevisionText($row);
335 if ($this->fld_content) {
336 if ($this->expandTemplates) {
337 global $wgParser;
338 $text = $wgParser->preprocess( $text, Title::newFromID($row->rev_page), new ParserOptions() );
339 }
340 ApiResult :: setContent($vals, $text);
341 }
342
343 if($this->diffto)
344 {
345 global $wgContLang;
346 $newText = explode("\n", $wgContLang->segmentForDiff($text));
347 $diff = new Diff($this->diffOldText, $newText);
348 $vals['diffto']['from'] = $this->diffto;
349 $arraydiff = $this->formatter instanceof ArrayDiffFormatter;
350 if( $arraydiff ) {
351 $changes = $wgContLang->unsegmentForDiff($this->formatter->format($diff));
352 $this->getResult()->setIndexedTagName( $changes, 'change' );
353 $vals['diffto'] = $changes;
354 } else {
355 ApiResult::setContent($vals['diffto'], $wgContLang->unsegmentForDiff($this->formatter->format($diff)));
356 }
357 }
358 if($this->difftoprev)
359 // Cache the revision's content for later use
360 $this->revCache[$row->rev_id] = $text;
361
362 return $vals;
363 }
364
365 protected function getAllowedParams() {
366 return array (
367 'prop' => array (
368 ApiBase :: PARAM_ISMULTI => true,
369 ApiBase :: PARAM_DFLT => 'ids|timestamp|flags|comment|user',
370 ApiBase :: PARAM_TYPE => array (
371 'ids',
372 'flags',
373 'timestamp',
374 'user',
375 'size',
376 'comment',
377 'content',
378 )
379 ),
380 'limit' => array (
381 ApiBase :: PARAM_TYPE => 'limit',
382 ApiBase :: PARAM_MIN => 1,
383 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
384 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
385 ),
386 'startid' => array (
387 ApiBase :: PARAM_TYPE => 'integer'
388 ),
389 'endid' => array (
390 ApiBase :: PARAM_TYPE => 'integer'
391 ),
392 'start' => array (
393 ApiBase :: PARAM_TYPE => 'timestamp'
394 ),
395 'end' => array (
396 ApiBase :: PARAM_TYPE => 'timestamp'
397 ),
398 'dir' => array (
399 ApiBase :: PARAM_DFLT => 'older',
400 ApiBase :: PARAM_TYPE => array (
401 'newer',
402 'older'
403 )
404 ),
405 'user' => array(
406 ApiBase :: PARAM_TYPE => 'user'
407 ),
408 'excludeuser' => array(
409 ApiBase :: PARAM_TYPE => 'user'
410 ),
411
412 'expandtemplates' => false,
413 'diffto' => array(
414 ApiBase :: PARAM_TYPE => 'integer'
415 ),
416 'difftoprev' => false,
417 'diffformat' => array(
418 ApiBase :: PARAM_TYPE => array(
419 'traditional',
420 'unified',
421 'array',
422 ),
423 ApiBase ::PARAM_DFLT => 'unified'
424 )
425 );
426 }
427
428 protected function getParamDescription() {
429 return array (
430 'prop' => 'Which properties to get for each revision.',
431 'limit' => 'limit how many revisions will be returned (enum)',
432 'startid' => 'from which revision id to start enumeration (enum)',
433 'endid' => 'stop revision enumeration on this revid (enum)',
434 'start' => 'from which revision timestamp to start enumeration (enum)',
435 'end' => 'enumerate up to this timestamp (enum)',
436 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)',
437 'user' => 'only include revisions made by user',
438 'excludeuser' => 'exclude revisions made by user',
439 'expandtemplates' => 'expand templates in revision content',
440 'diffto' => 'Revision number to compare all revisions with',
441 'difftoprev' => 'Diff each revision to the previous one (enum)',
442 'diffformat' => 'Format to use for diffs',
443 );
444 }
445
446 protected function getDescription() {
447 return array (
448 'Get revision information.',
449 'This module may be used in several ways:',
450 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
451 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
452 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
453 'All parameters marked as (enum) may only be used with a single page (#2).'
454 );
455 }
456
457 protected function getExamples() {
458 return array (
459 'Get data with content for the last revision of titles "API" and "Main Page":',
460 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
461 'Get last 5 revisions of the "Main Page":',
462 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
463 'Get first 5 revisions of the "Main Page":',
464 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
465 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
466 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
467 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
468 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
469 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
470 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
471 );
472 }
473
474 public function getVersion() {
475 return __CLASS__ . ': $Id$';
476 }
477 }
478