Merge branch 'Wikidata' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 7, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * A query action to enumerate revisions of a given page, or show top revisions of multiple pages.
29 * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
30 * In the enumeration mode, ranges of revisions may be requested and filtered.
31 *
32 * @ingroup API
33 */
34 class ApiQueryRevisions extends ApiQueryBase {
35
36 private $diffto, $difftotext, $expandTemplates, $generateXML, $section,
37 $token, $parseContent, $contentFormat;
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'rv' );
41 }
42
43 private $fld_ids = false, $fld_flags = false, $fld_timestamp = false, $fld_size = false, $fld_sha1 = false,
44 $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
45 $fld_content = false, $fld_tags = false, $fld_contentmodel = false;
46
47 private $tokenFunctions;
48
49 protected function getTokenFunctions() {
50 // tokenname => function
51 // function prototype is func($pageid, $title, $rev)
52 // should return token or false
53
54 // Don't call the hooks twice
55 if ( isset( $this->tokenFunctions ) ) {
56 return $this->tokenFunctions;
57 }
58
59 // If we're in JSON callback mode, no tokens can be obtained
60 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
61 return array();
62 }
63
64 $this->tokenFunctions = array(
65 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
66 );
67 wfRunHooks( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions ) );
68 return $this->tokenFunctions;
69 }
70
71 /**
72 * @param $pageid
73 * @param $title Title
74 * @param $rev Revision
75 * @return bool|String
76 */
77 public static function getRollbackToken( $pageid, $title, $rev ) {
78 global $wgUser;
79 if ( !$wgUser->isAllowed( 'rollback' ) ) {
80 return false;
81 }
82 return $wgUser->getEditToken(
83 array( $title->getPrefixedText(), $rev->getUserText() ) );
84 }
85
86 public function execute() {
87 $params = $this->extractRequestParams( false );
88
89 // If any of those parameters are used, work in 'enumeration' mode.
90 // Enum mode can only be used when exactly one page is provided.
91 // Enumerating revisions on multiple pages make it extremely
92 // difficult to manage continuations and require additional SQL indexes
93 $enumRevMode = ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ||
94 !is_null( $params['limit'] ) || !is_null( $params['startid'] ) ||
95 !is_null( $params['endid'] ) || $params['dir'] === 'newer' ||
96 !is_null( $params['start'] ) || !is_null( $params['end'] ) );
97
98
99 $pageSet = $this->getPageSet();
100 $pageCount = $pageSet->getGoodTitleCount();
101 $revCount = $pageSet->getRevisionCount();
102
103 // Optimization -- nothing to do
104 if ( $revCount === 0 && $pageCount === 0 ) {
105 return;
106 }
107
108 if ( $revCount > 0 && $enumRevMode ) {
109 $this->dieUsage( 'The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids' );
110 }
111
112 if ( $pageCount > 1 && $enumRevMode ) {
113 $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' );
114 }
115
116 if ( !is_null( $params['difftotext'] ) ) {
117 $this->difftotext = $params['difftotext'];
118 } elseif ( !is_null( $params['diffto'] ) ) {
119 if ( $params['diffto'] == 'cur' ) {
120 $params['diffto'] = 0;
121 }
122 if ( ( !ctype_digit( $params['diffto'] ) || $params['diffto'] < 0 )
123 && $params['diffto'] != 'prev' && $params['diffto'] != 'next' ) {
124 $this->dieUsage( 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"', 'diffto' );
125 }
126 // Check whether the revision exists and is readable,
127 // DifferenceEngine returns a rather ambiguous empty
128 // string if that's not the case
129 if ( $params['diffto'] != 0 ) {
130 $difftoRev = Revision::newFromID( $params['diffto'] );
131 if ( !$difftoRev ) {
132 $this->dieUsageMsg( array( 'nosuchrevid', $params['diffto'] ) );
133 }
134 if ( $difftoRev->isDeleted( Revision::DELETED_TEXT ) ) {
135 $this->setWarning( "Couldn't diff to r{$difftoRev->getID()}: content is hidden" );
136 $params['diffto'] = null;
137 }
138 }
139 $this->diffto = $params['diffto'];
140 }
141
142 $db = $this->getDB();
143 $this->addTables( 'page' );
144 $this->addFields( Revision::selectFields() );
145 $this->addWhere( 'page_id = rev_page' );
146
147 $prop = array_flip( $params['prop'] );
148
149 // Optional fields
150 $this->fld_ids = isset ( $prop['ids'] );
151 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
152 $this->fld_flags = isset ( $prop['flags'] );
153 $this->fld_timestamp = isset ( $prop['timestamp'] );
154 $this->fld_comment = isset ( $prop['comment'] );
155 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
156 $this->fld_size = isset ( $prop['size'] );
157 $this->fld_sha1 = isset ( $prop['sha1'] );
158 $this->fld_contentmodel = isset ( $prop['contentmodel'] );
159 $this->fld_userid = isset( $prop['userid'] );
160 $this->fld_user = isset ( $prop['user'] );
161 $this->token = $params['token'];
162
163 if ( !empty( $params['contentformat'] ) ) {
164 $n = ContentHandler::getContentFormatID( $params['contentformat'] );
165
166 if ( is_int( $n ) ) {
167 $this->contentFormat = $n;
168 } else {
169 $this->dieUsage( "Unknown format " . $params['contentformat'], 'badformat' );
170 }
171 }
172
173 // Possible indexes used
174 $index = array();
175
176 $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
177 $botMax = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
178 $limit = $params['limit'];
179 if ( $limit == 'max' ) {
180 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
181 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
182 }
183
184 if ( !is_null( $this->token ) || $pageCount > 0 ) {
185 $this->addFields( Revision::selectPageFields() );
186 }
187
188 if ( isset( $prop['tags'] ) ) {
189 $this->fld_tags = true;
190 $this->addTables( 'tag_summary' );
191 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
192 $this->addFields( 'ts_tags' );
193 }
194
195 if ( !is_null( $params['tag'] ) ) {
196 $this->addTables( 'change_tag' );
197 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
198 $this->addWhereFld( 'ct_tag' , $params['tag'] );
199 global $wgOldChangeTagsIndex;
200 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
201 }
202
203 if ( isset( $prop['content'] ) || !is_null( $this->difftotext ) ) {
204 // For each page we will request, the user must have read rights for that page
205 foreach ( $pageSet->getGoodTitles() as $title ) {
206 if ( !$title->userCan( 'read' ) ) {
207 $this->dieUsage(
208 'The current user is not allowed to read ' . $title->getPrefixedText(),
209 'accessdenied' );
210 }
211 }
212
213 $this->addTables( 'text' );
214 $this->addWhere( 'rev_text_id=old_id' );
215 $this->addFields( 'old_id' );
216 $this->addFields( Revision::selectTextFields() );
217
218 $this->fld_content = isset( $prop['content'] );
219
220 $this->expandTemplates = $params['expandtemplates'];
221 $this->generateXML = $params['generatexml'];
222 $this->parseContent = $params['parse'];
223 if ( $this->parseContent ) {
224 // Must manually initialize unset limit
225 if ( is_null( $limit ) ) {
226 $limit = 1;
227 }
228 // We are only going to parse 1 revision per request
229 $this->validateLimit( 'limit', $limit, 1, 1, 1 );
230 }
231 if ( isset( $params['section'] ) ) {
232 $this->section = $params['section'];
233 } else {
234 $this->section = false;
235 }
236 }
237
238 // add user name, if needed
239 if ( $this->fld_user ) {
240 $this->addTables( 'user' );
241 $this->addJoinConds( array( 'user' => Revision::userJoinCond() ) );
242 $this->addFields( Revision::selectUserFields() );
243 }
244
245 // Bug 24166 - API error when using rvprop=tags
246 $this->addTables( 'revision' );
247
248 if ( $enumRevMode ) {
249 // This is mostly to prevent parameter errors (and optimize SQL?)
250 if ( !is_null( $params['startid'] ) && !is_null( $params['start'] ) ) {
251 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
252 }
253
254 if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) ) {
255 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
256 }
257
258 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
259 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
260 }
261
262 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
263 // the same result. This way users may request revisions starting at a given time,
264 // but to page through results use the rev_id returned after each page.
265 // Switching to rev_id removes the potential problem of having more than
266 // one row with the same timestamp for the same page.
267 // The order needs to be the same as start parameter to avoid SQL filesort.
268 if ( is_null( $params['startid'] ) && is_null( $params['endid'] ) ) {
269 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
270 $params['start'], $params['end'] );
271 } else {
272 $this->addWhereRange( 'rev_id', $params['dir'],
273 $params['startid'], $params['endid'] );
274 // One of start and end can be set
275 // If neither is set, this does nothing
276 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
277 $params['start'], $params['end'], false );
278 }
279
280 // must manually initialize unset limit
281 if ( is_null( $limit ) ) {
282 $limit = 10;
283 }
284 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
285
286 // There is only one ID, use it
287 $ids = array_keys( $pageSet->getGoodTitles() );
288 $this->addWhereFld( 'rev_page', reset( $ids ) );
289
290 if ( !is_null( $params['user'] ) ) {
291 $this->addWhereFld( 'rev_user_text', $params['user'] );
292 } elseif ( !is_null( $params['excludeuser'] ) ) {
293 $this->addWhere( 'rev_user_text != ' .
294 $db->addQuotes( $params['excludeuser'] ) );
295 }
296 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
297 // Paranoia: avoid brute force searches (bug 17342)
298 $this->addWhere( $db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
299 }
300 } elseif ( $revCount > 0 ) {
301 $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
302 $revs = $pageSet->getRevisionIDs();
303 if ( self::truncateArray( $revs, $max ) ) {
304 $this->setWarning( "Too many values supplied for parameter 'revids': the limit is $max" );
305 }
306
307 // Get all revision IDs
308 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
309
310 if ( !is_null( $params['continue'] ) ) {
311 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
312 }
313 $this->addOption( 'ORDER BY', 'rev_id' );
314
315 // assumption testing -- we should never get more then $revCount rows.
316 $limit = $revCount;
317 } elseif ( $pageCount > 0 ) {
318 $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
319 $titles = $pageSet->getGoodTitles();
320 if ( self::truncateArray( $titles, $max ) ) {
321 $this->setWarning( "Too many values supplied for parameter 'titles': the limit is $max" );
322 }
323
324 // When working in multi-page non-enumeration mode,
325 // limit to the latest revision only
326 $this->addWhere( 'page_id=rev_page' );
327 $this->addWhere( 'page_latest=rev_id' );
328
329 // Get all page IDs
330 $this->addWhereFld( 'page_id', array_keys( $titles ) );
331 // Every time someone relies on equality propagation, god kills a kitten :)
332 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
333
334 if ( !is_null( $params['continue'] ) ) {
335 $cont = explode( '|', $params['continue'] );
336 if ( count( $cont ) != 2 ) {
337 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
338 'value returned by the previous query', '_badcontinue' );
339 }
340 $pageid = intval( $cont[0] );
341 $revid = intval( $cont[1] );
342 $this->addWhere(
343 "rev_page > $pageid OR " .
344 "(rev_page = $pageid AND " .
345 "rev_id >= $revid)"
346 );
347 }
348 $this->addOption( 'ORDER BY', array(
349 'rev_page',
350 'rev_id'
351 ));
352
353 // assumption testing -- we should never get more then $pageCount rows.
354 $limit = $pageCount;
355 } else {
356 ApiBase::dieDebug( __METHOD__, 'param validation?' );
357 }
358
359 $this->addOption( 'LIMIT', $limit + 1 );
360 $this->addOption( 'USE INDEX', $index );
361
362 $count = 0;
363 $res = $this->select( __METHOD__ );
364
365 foreach ( $res as $row ) {
366 if ( ++ $count > $limit ) {
367 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
368 if ( !$enumRevMode ) {
369 ApiBase::dieDebug( __METHOD__, 'Got more rows then expected' ); // bug report
370 }
371 $this->setContinueEnumParameter( 'startid', intval( $row->rev_id ) );
372 break;
373 }
374
375 $fit = $this->addPageSubItem( $row->rev_page, $this->extractRowInfo( $row ), 'rev' );
376 if ( !$fit ) {
377 if ( $enumRevMode ) {
378 $this->setContinueEnumParameter( 'startid', intval( $row->rev_id ) );
379 } elseif ( $revCount > 0 ) {
380 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
381 } else {
382 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
383 '|' . intval( $row->rev_id ) );
384 }
385 break;
386 }
387 }
388 }
389
390 private function extractRowInfo( $row ) {
391 $revision = new Revision( $row );
392 $title = $revision->getTitle();
393 $vals = array();
394
395 if ( $this->fld_ids ) {
396 $vals['revid'] = intval( $revision->getId() );
397 // $vals['oldid'] = intval( $row->rev_text_id ); // todo: should this be exposed?
398 if ( !is_null( $revision->getParentId() ) ) {
399 $vals['parentid'] = intval( $revision->getParentId() );
400 }
401 }
402
403 if ( $this->fld_flags && $revision->isMinor() ) {
404 $vals['minor'] = '';
405 }
406
407 if ( $this->fld_user || $this->fld_userid ) {
408 if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
409 $vals['userhidden'] = '';
410 } else {
411 if ( $this->fld_user ) {
412 $vals['user'] = $revision->getUserText();
413 }
414 $userid = $revision->getUser();
415 if ( !$userid ) {
416 $vals['anon'] = '';
417 }
418
419 if ( $this->fld_userid ) {
420 $vals['userid'] = $userid;
421 }
422 }
423 }
424
425 if ( $this->fld_timestamp ) {
426 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
427 }
428
429 if ( $this->fld_size ) {
430 if ( !is_null( $revision->getSize() ) ) {
431 $vals['size'] = intval( $revision->getSize() );
432 } else {
433 $vals['size'] = 0;
434 }
435 }
436
437 if ( $this->fld_sha1 ) {
438 if ( $revision->getSha1() != '' ) {
439 $vals['sha1'] = wfBaseConvert( $revision->getSha1(), 36, 16, 40 );
440 } else {
441 $vals['sha1'] = '';
442 }
443 }
444
445 if ( $this->fld_contentmodel ) {
446 $vals['contentmodel'] = $revision->getContentModel();
447 }
448
449 if ( $this->fld_comment || $this->fld_parsedcomment ) {
450 if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
451 $vals['commenthidden'] = '';
452 } else {
453 $comment = $revision->getComment();
454
455 if ( $this->fld_comment ) {
456 $vals['comment'] = $comment;
457 }
458
459 if ( $this->fld_parsedcomment ) {
460 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
461 }
462 }
463 }
464
465 if ( $this->fld_tags ) {
466 if ( $row->ts_tags ) {
467 $tags = explode( ',', $row->ts_tags );
468 $this->getResult()->setIndexedTagName( $tags, 'tag' );
469 $vals['tags'] = $tags;
470 } else {
471 $vals['tags'] = array();
472 }
473 }
474
475 if ( !is_null( $this->token ) ) {
476 $tokenFunctions = $this->getTokenFunctions();
477 foreach ( $this->token as $t ) {
478 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
479 if ( $val === false ) {
480 $this->setWarning( "Action '$t' is not allowed for the current user" );
481 } else {
482 $vals[$t . 'token'] = $val;
483 }
484 }
485 }
486
487 $content = null;
488 global $wgParser;
489 if ( $this->fld_content || !is_null( $this->difftotext ) ) {
490 $content = $revision->getContent();
491 // Expand templates after getting section content because
492 // template-added sections don't count and Parser::preprocess()
493 // will have less input
494 if ( $this->section !== false ) {
495 $content = $content->getSection( $this->section, false );
496 if ( !$content ) {
497 $this->dieUsage( "There is no section {$this->section} in r" . $revision->getId(), 'nosuchsection' );
498 }
499 }
500 }
501 if ( $this->fld_content && !$revision->isDeleted( Revision::DELETED_TEXT ) ) {
502 $text = null;
503
504 if ( $this->generateXML ) {
505 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
506 $t = $content->getNativeData(); # note: don't set $text
507
508 $wgParser->startExternalParse( $title, ParserOptions::newFromContext( $this->getContext() ), OT_PREPROCESS );
509 $dom = $wgParser->preprocessToDom( $t );
510 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
511 $xml = $dom->saveXML();
512 } else {
513 $xml = $dom->__toString();
514 }
515 $vals['parsetree'] = $xml;
516 } else {
517 $this->setWarning( "Conversion to XML is supported for wikitext only, " .
518 $title->getPrefixedDBkey() .
519 " uses content model #" . $content->getModel() .
520 " (" . ContentHandler::getContentModelName( $content->getModel() ). ")" );
521 }
522 }
523
524 if ( $this->expandTemplates && !$this->parseContent ) {
525 #XXX: implement template expansion for all content types in ContentHandler?
526 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
527 $text = $content->getNativeData();
528
529 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
530 } else {
531 $this->setWarning( "Template expansion is supported for wikitext only, " .
532 $title->getPrefixedDBkey() .
533 " uses content model #" . $content->getModel() .
534 " (" . ContentHandler::getContentModelName( $content->getModel() ). ")" );
535
536 $text = false;
537 }
538 }
539 if ( $this->parseContent ) {
540 $po = $content->getParserOutput( $title, ParserOptions::newFromContext( $this->getContext() ) );
541 $text = $po->getText();
542 }
543
544 if ( $text === null ) {
545 $format = $this->contentFormat ? $this->contentFormat : $content->getDefaultFormat();
546
547 if ( !$content->isSupportedFormat( $format ) ) {
548 $model = $content->getModel();
549 $formatName = ContentHandler::getContentFormatMimeType( $format );
550 $modelName = ContentHandler::getContentModelName( $model );
551 $name = $title->getPrefixedDBkey();
552
553 $this->dieUsage( "The requested format #{$this->contentFormat} ($formatName) is not supported for content model #$model ($modelName) used by $name", 'badformat' );
554 }
555
556 $text = $content->serialize( $format );
557 $vals['textformat'] = ContentHandler::getContentFormatMimeType( $format );
558 }
559
560 if ( $text !== false ) {
561 ApiResult::setContent( $vals, $text );
562 }
563 } elseif ( $this->fld_content ) {
564 $vals['texthidden'] = '';
565 }
566
567 if ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) {
568 global $wgAPIMaxUncachedDiffs;
569 static $n = 0; // Number of uncached diffs we've had
570 if ( $n < $wgAPIMaxUncachedDiffs ) {
571 $vals['diff'] = array();
572 $context = new DerivativeContext( $this->getContext() );
573 $context->setTitle( $title );
574 $handler = ContentHandler::getForTitle( $title );
575
576 if ( !is_null( $this->difftotext ) ) {
577 $model = $title->getContentModel();
578
579 if ( $this->contentFormat && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat ) ) {
580 $formatName = ContentHandler::getContentFormatMimeType( $this->contentFormat );
581 $modelName = ContentHandler::getContentModelName( $model );
582 $name = $title->getPrefixedDBkey();
583
584 $this->dieUsage( "The requested format #{$this->contentFormat} ($formatName) is not supported for content model #$model ($modelName) used by $name", 'badformat' );
585 }
586
587 $difftocontent = ContentHandler::makeContent( $this->difftotext, $title, $model, $this->contentFormat );
588
589 $engine = $handler->createDifferenceEngine( $context );
590 $engine->setContent( $content, $difftocontent );
591 } else {
592 $engine = $handler->createDifferenceEngine( $context, $revision->getID(), $this->diffto );
593 $vals['diff']['from'] = $engine->getOldid();
594 $vals['diff']['to'] = $engine->getNewid();
595 }
596 $difftext = $engine->getDiffBody();
597 ApiResult::setContent( $vals['diff'], $difftext );
598 if ( !$engine->wasCacheHit() ) {
599 $n++;
600 }
601 } else {
602 $vals['diff']['notcached'] = '';
603 }
604 }
605 return $vals;
606 }
607
608 public function getCacheMode( $params ) {
609 if ( isset( $params['token'] ) ) {
610 return 'private';
611 }
612 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
613 // formatComment() calls wfMsg() among other things
614 return 'anon-public-user-private';
615 }
616 return 'public';
617 }
618
619 public function getAllowedParams() {
620 return array(
621 'prop' => array(
622 ApiBase::PARAM_ISMULTI => true,
623 ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
624 ApiBase::PARAM_TYPE => array(
625 'ids',
626 'flags',
627 'timestamp',
628 'user',
629 'userid',
630 'size',
631 'sha1',
632 'contentmodel',
633 'comment',
634 'parsedcomment',
635 'content',
636 'tags'
637 )
638 ),
639 'limit' => array(
640 ApiBase::PARAM_TYPE => 'limit',
641 ApiBase::PARAM_MIN => 1,
642 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
643 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
644 ),
645 'startid' => array(
646 ApiBase::PARAM_TYPE => 'integer'
647 ),
648 'endid' => array(
649 ApiBase::PARAM_TYPE => 'integer'
650 ),
651 'start' => array(
652 ApiBase::PARAM_TYPE => 'timestamp'
653 ),
654 'end' => array(
655 ApiBase::PARAM_TYPE => 'timestamp'
656 ),
657 'dir' => array(
658 ApiBase::PARAM_DFLT => 'older',
659 ApiBase::PARAM_TYPE => array(
660 'newer',
661 'older'
662 )
663 ),
664 'user' => array(
665 ApiBase::PARAM_TYPE => 'user'
666 ),
667 'excludeuser' => array(
668 ApiBase::PARAM_TYPE => 'user'
669 ),
670 'tag' => null,
671 'expandtemplates' => false,
672 'generatexml' => false,
673 'parse' => false,
674 'section' => null,
675 'token' => array(
676 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
677 ApiBase::PARAM_ISMULTI => true
678 ),
679 'continue' => null,
680 'diffto' => null,
681 'difftotext' => null,
682 'contentformat' => array(
683 ApiBase::PARAM_TYPE => array_values( $GLOBALS[ 'wgContentFormatMimeTypes' ] ),
684 ApiBase::PARAM_DFLT => null
685 ),
686 );
687 }
688
689 public function getParamDescription() {
690 $p = $this->getModulePrefix();
691 return array(
692 'prop' => array(
693 'Which properties to get for each revision:',
694 ' ids - The ID of the revision',
695 ' flags - Revision flags (minor)',
696 ' timestamp - The timestamp of the revision',
697 ' user - User that made the revision',
698 ' userid - User id of revision creator',
699 ' size - Length (bytes) of the revision',
700 ' sha1 - SHA-1 (base 16) of the revision',
701 ' contentmodel - Content model id',
702 ' comment - Comment by the user for revision',
703 ' parsedcomment - Parsed comment by the user for the revision',
704 ' content - Text of the revision',
705 ' tags - Tags for the revision',
706 ),
707 'limit' => 'Limit how many revisions will be returned (enum)',
708 'startid' => 'From which revision id to start enumeration (enum)',
709 'endid' => 'Stop revision enumeration on this revid (enum)',
710 'start' => 'From which revision timestamp to start enumeration (enum)',
711 'end' => 'Enumerate up to this timestamp (enum)',
712 'dir' => $this->getDirectionDescription( $p, ' (enum)' ),
713 'user' => 'Only include revisions made by user (enum)',
714 'excludeuser' => 'Exclude revisions made by user (enum)',
715 'expandtemplates' => 'Expand templates in revision content',
716 'generatexml' => 'Generate XML parse tree for revision content',
717 'parse' => 'Parse revision content. For performance reasons if this option is used, rvlimit is enforced to 1.',
718 'section' => 'Only retrieve the content of this section number',
719 'token' => 'Which tokens to obtain for each revision',
720 'continue' => 'When more results are available, use this to continue',
721 'diffto' => array( 'Revision ID to diff each revision to.',
722 'Use "prev", "next" and "cur" for the previous, next and current revision respectively' ),
723 'difftotext' => array( 'Text to diff each revision to. Only diffs a limited number of revisions.',
724 "Overrides {$p}diffto. If {$p}section is set, only that section will be diffed against this text" ),
725 'tag' => 'Only list revisions tagged with this tag',
726 'contentformat' => 'Serialization format used for difftotext and expected for output of content',
727 );
728 }
729
730 public function getDescription() {
731 return array(
732 'Get revision information',
733 'May be used in several ways:',
734 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter',
735 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params',
736 ' 3) Get data about a set of revisions by setting their IDs with revids parameter',
737 'All parameters marked as (enum) may only be used with a single page (#2)'
738 );
739 }
740
741 public function getPossibleErrors() {
742 return array_merge( parent::getPossibleErrors(), array(
743 array( 'nosuchrevid', 'diffto' ),
744 array( 'code' => 'revids', 'info' => 'The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).' ),
745 array( 'code' => 'multpages', 'info' => '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.' ),
746 array( 'code' => 'diffto', 'info' => 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"' ),
747 array( 'code' => 'badparams', 'info' => 'start and startid cannot be used together' ),
748 array( 'code' => 'badparams', 'info' => 'end and endid cannot be used together' ),
749 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
750 array( 'code' => 'nosuchsection', 'info' => 'There is no section section in rID' ),
751 array( 'code' => 'badformat', 'info' => 'The requested serialization format can not be applied to the page\'s content model' ),
752 ) );
753 }
754
755 public function getExamples() {
756 return array(
757 'Get data with content for the last revision of titles "API" and "Main Page"',
758 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
759 'Get last 5 revisions of the "Main Page"',
760 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
761 'Get first 5 revisions of the "Main Page"',
762 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
763 'Get first 5 revisions of the "Main Page" made after 2006-05-01',
764 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
765 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
766 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
767 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
768 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
769 );
770 }
771
772 public function getHelpUrls() {
773 return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';
774 }
775
776 public function getVersion() {
777 return __CLASS__ . ': $Id$';
778 }
779 }