Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / api / ApiQueryRevisions.php
blob415288ef79d71cbbf44f8a16c666928620cf1c31
1 <?php
2 /**
5 * Created on Sep 7, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
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.
32 * @ingroup API
34 class ApiQueryRevisions extends ApiQueryBase {
36 private $diffto, $difftotext, $expandTemplates, $generateXML, $section,
37 $token, $parseContent, $contentFormat;
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'rv' );
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;
47 private $tokenFunctions;
49 protected function getTokenFunctions() {
50 // tokenname => function
51 // function prototype is func($pageid, $title, $rev)
52 // should return token or false
54 // Don't call the hooks twice
55 if ( isset( $this->tokenFunctions ) ) {
56 return $this->tokenFunctions;
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();
64 $this->tokenFunctions = array(
65 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
67 wfRunHooks( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions ) );
68 return $this->tokenFunctions;
71 /**
72 * @param $pageid
73 * @param $title Title
74 * @param $rev Revision
75 * @return bool|String
77 public static function getRollbackToken( $pageid, $title, $rev ) {
78 global $wgUser;
79 if ( !$wgUser->isAllowed( 'rollback' ) ) {
80 return false;
82 return $wgUser->getEditToken(
83 array( $title->getPrefixedText(), $rev->getUserText() ) );
86 public function execute() {
87 $params = $this->extractRequestParams( false );
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'] ) );
98 $pageSet = $this->getPageSet();
99 $pageCount = $pageSet->getGoodTitleCount();
100 $revCount = $pageSet->getRevisionCount();
102 // Optimization -- nothing to do
103 if ( $revCount === 0 && $pageCount === 0 ) {
104 return;
107 if ( $revCount > 0 && $enumRevMode ) {
108 $this->dieUsage( 'The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids' );
111 if ( $pageCount > 1 && $enumRevMode ) {
112 $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' );
115 if ( !is_null( $params['difftotext'] ) ) {
116 $this->difftotext = $params['difftotext'];
117 } elseif ( !is_null( $params['diffto'] ) ) {
118 if ( $params['diffto'] == 'cur' ) {
119 $params['diffto'] = 0;
121 if ( ( !ctype_digit( $params['diffto'] ) || $params['diffto'] < 0 )
122 && $params['diffto'] != 'prev' && $params['diffto'] != 'next' ) {
123 $this->dieUsage( 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"', 'diffto' );
125 // Check whether the revision exists and is readable,
126 // DifferenceEngine returns a rather ambiguous empty
127 // string if that's not the case
128 if ( $params['diffto'] != 0 ) {
129 $difftoRev = Revision::newFromID( $params['diffto'] );
130 if ( !$difftoRev ) {
131 $this->dieUsageMsg( array( 'nosuchrevid', $params['diffto'] ) );
133 if ( $difftoRev->isDeleted( Revision::DELETED_TEXT ) ) {
134 $this->setWarning( "Couldn't diff to r{$difftoRev->getID()}: content is hidden" );
135 $params['diffto'] = null;
138 $this->diffto = $params['diffto'];
141 $db = $this->getDB();
142 $this->addTables( 'page' );
143 $this->addFields( Revision::selectFields() );
144 $this->addWhere( 'page_id = rev_page' );
146 $prop = array_flip( $params['prop'] );
148 // Optional fields
149 $this->fld_ids = isset( $prop['ids'] );
150 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
151 $this->fld_flags = isset( $prop['flags'] );
152 $this->fld_timestamp = isset( $prop['timestamp'] );
153 $this->fld_comment = isset( $prop['comment'] );
154 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
155 $this->fld_size = isset( $prop['size'] );
156 $this->fld_sha1 = isset( $prop['sha1'] );
157 $this->fld_contentmodel = isset( $prop['contentmodel'] );
158 $this->fld_userid = isset( $prop['userid'] );
159 $this->fld_user = isset( $prop['user'] );
160 $this->token = $params['token'];
162 if ( !empty( $params['contentformat'] ) ) {
163 $this->contentFormat = $params['contentformat'];
166 // Possible indexes used
167 $index = array();
169 $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
170 $botMax = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
171 $limit = $params['limit'];
172 if ( $limit == 'max' ) {
173 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
174 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
177 if ( !is_null( $this->token ) || $pageCount > 0 ) {
178 $this->addFields( Revision::selectPageFields() );
181 if ( isset( $prop['tags'] ) ) {
182 $this->fld_tags = true;
183 $this->addTables( 'tag_summary' );
184 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
185 $this->addFields( 'ts_tags' );
188 if ( !is_null( $params['tag'] ) ) {
189 $this->addTables( 'change_tag' );
190 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
191 $this->addWhereFld( 'ct_tag', $params['tag'] );
192 $index['change_tag'] = 'change_tag_tag_id';
195 if ( isset( $prop['content'] ) || !is_null( $this->difftotext ) ) {
196 // For each page we will request, the user must have read rights for that page
197 $user = $this->getUser();
198 /** @var $title Title */
199 foreach ( $pageSet->getGoodTitles() as $title ) {
200 if ( !$title->userCan( 'read', $user ) ) {
201 $this->dieUsage(
202 'The current user is not allowed to read ' . $title->getPrefixedText(),
203 'accessdenied' );
207 $this->addTables( 'text' );
208 $this->addWhere( 'rev_text_id=old_id' );
209 $this->addFields( 'old_id' );
210 $this->addFields( Revision::selectTextFields() );
212 $this->fld_content = isset( $prop['content'] );
214 $this->expandTemplates = $params['expandtemplates'];
215 $this->generateXML = $params['generatexml'];
216 $this->parseContent = $params['parse'];
217 if ( $this->parseContent ) {
218 // Must manually initialize unset limit
219 if ( is_null( $limit ) ) {
220 $limit = 1;
222 // We are only going to parse 1 revision per request
223 $this->validateLimit( 'limit', $limit, 1, 1, 1 );
225 if ( isset( $params['section'] ) ) {
226 $this->section = $params['section'];
227 } else {
228 $this->section = false;
232 // add user name, if needed
233 if ( $this->fld_user ) {
234 $this->addTables( 'user' );
235 $this->addJoinConds( array( 'user' => Revision::userJoinCond() ) );
236 $this->addFields( Revision::selectUserFields() );
239 // Bug 24166 - API error when using rvprop=tags
240 $this->addTables( 'revision' );
242 if ( $enumRevMode ) {
243 // This is mostly to prevent parameter errors (and optimize SQL?)
244 if ( !is_null( $params['startid'] ) && !is_null( $params['start'] ) ) {
245 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
248 if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) ) {
249 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
252 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
253 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
256 // Continuing effectively uses startid. But we can't use rvstartid
257 // directly, because there is no way to tell the client to ''not''
258 // send rvstart if it sent it in the original query. So instead we
259 // send the continuation startid as rvcontinue, and ignore both
260 // rvstart and rvstartid when that is supplied.
261 if ( !is_null( $params['continue'] ) ) {
262 $params['startid'] = $params['continue'];
263 $params['start'] = null;
266 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
267 // the same result. This way users may request revisions starting at a given time,
268 // but to page through results use the rev_id returned after each page.
269 // Switching to rev_id removes the potential problem of having more than
270 // one row with the same timestamp for the same page.
271 // The order needs to be the same as start parameter to avoid SQL filesort.
272 if ( is_null( $params['startid'] ) && is_null( $params['endid'] ) ) {
273 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
274 $params['start'], $params['end'] );
275 } else {
276 $this->addWhereRange( 'rev_id', $params['dir'],
277 $params['startid'], $params['endid'] );
278 // One of start and end can be set
279 // If neither is set, this does nothing
280 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
281 $params['start'], $params['end'], false );
284 // must manually initialize unset limit
285 if ( is_null( $limit ) ) {
286 $limit = 10;
288 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
290 // There is only one ID, use it
291 $ids = array_keys( $pageSet->getGoodTitles() );
292 $this->addWhereFld( 'rev_page', reset( $ids ) );
294 if ( !is_null( $params['user'] ) ) {
295 $this->addWhereFld( 'rev_user_text', $params['user'] );
296 } elseif ( !is_null( $params['excludeuser'] ) ) {
297 $this->addWhere( 'rev_user_text != ' .
298 $db->addQuotes( $params['excludeuser'] ) );
300 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
301 // Paranoia: avoid brute force searches (bug 17342)
302 $this->addWhere( $db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
304 } elseif ( $revCount > 0 ) {
305 $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
306 $revs = $pageSet->getRevisionIDs();
307 if ( self::truncateArray( $revs, $max ) ) {
308 $this->setWarning( "Too many values supplied for parameter 'revids': the limit is $max" );
311 // Get all revision IDs
312 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
314 if ( !is_null( $params['continue'] ) ) {
315 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
317 $this->addOption( 'ORDER BY', 'rev_id' );
319 // assumption testing -- we should never get more then $revCount rows.
320 $limit = $revCount;
321 } elseif ( $pageCount > 0 ) {
322 $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
323 $titles = $pageSet->getGoodTitles();
324 if ( self::truncateArray( $titles, $max ) ) {
325 $this->setWarning( "Too many values supplied for parameter 'titles': the limit is $max" );
328 // When working in multi-page non-enumeration mode,
329 // limit to the latest revision only
330 $this->addWhere( 'page_id=rev_page' );
331 $this->addWhere( 'page_latest=rev_id' );
333 // Get all page IDs
334 $this->addWhereFld( 'page_id', array_keys( $titles ) );
335 // Every time someone relies on equality propagation, god kills a kitten :)
336 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
338 if ( !is_null( $params['continue'] ) ) {
339 $cont = explode( '|', $params['continue'] );
340 $this->dieContinueUsageIf( count( $cont ) != 2 );
341 $pageid = intval( $cont[0] );
342 $revid = intval( $cont[1] );
343 $this->addWhere(
344 "rev_page > $pageid OR " .
345 "(rev_page = $pageid AND " .
346 "rev_id >= $revid)"
349 $this->addOption( 'ORDER BY', array(
350 'rev_page',
351 'rev_id'
354 // assumption testing -- we should never get more then $pageCount rows.
355 $limit = $pageCount;
356 } else {
357 ApiBase::dieDebug( __METHOD__, 'param validation?' );
360 $this->addOption( 'LIMIT', $limit + 1 );
361 $this->addOption( 'USE INDEX', $index );
363 $count = 0;
364 $res = $this->select( __METHOD__ );
366 foreach ( $res as $row ) {
367 if ( ++ $count > $limit ) {
368 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
369 if ( !$enumRevMode ) {
370 ApiBase::dieDebug( __METHOD__, 'Got more rows then expected' ); // bug report
372 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
373 break;
376 $fit = $this->addPageSubItem( $row->rev_page, $this->extractRowInfo( $row ), 'rev' );
377 if ( !$fit ) {
378 if ( $enumRevMode ) {
379 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
380 } elseif ( $revCount > 0 ) {
381 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
382 } else {
383 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
384 '|' . intval( $row->rev_id ) );
386 break;
391 private function extractRowInfo( $row ) {
392 $revision = new Revision( $row );
393 $title = $revision->getTitle();
394 $vals = array();
396 if ( $this->fld_ids ) {
397 $vals['revid'] = intval( $revision->getId() );
398 // $vals['oldid'] = intval( $row->rev_text_id ); // todo: should this be exposed?
399 if ( !is_null( $revision->getParentId() ) ) {
400 $vals['parentid'] = intval( $revision->getParentId() );
404 if ( $this->fld_flags && $revision->isMinor() ) {
405 $vals['minor'] = '';
408 if ( $this->fld_user || $this->fld_userid ) {
409 if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
410 $vals['userhidden'] = '';
411 } else {
412 if ( $this->fld_user ) {
413 $vals['user'] = $revision->getUserText();
415 $userid = $revision->getUser();
416 if ( !$userid ) {
417 $vals['anon'] = '';
420 if ( $this->fld_userid ) {
421 $vals['userid'] = $userid;
426 if ( $this->fld_timestamp ) {
427 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
430 if ( $this->fld_size ) {
431 if ( !is_null( $revision->getSize() ) ) {
432 $vals['size'] = intval( $revision->getSize() );
433 } else {
434 $vals['size'] = 0;
438 if ( $this->fld_sha1 && !$revision->isDeleted( Revision::DELETED_TEXT ) ) {
439 if ( $revision->getSha1() != '' ) {
440 $vals['sha1'] = wfBaseConvert( $revision->getSha1(), 36, 16, 40 );
441 } else {
442 $vals['sha1'] = '';
444 } elseif ( $this->fld_sha1 ) {
445 $vals['sha1hidden'] = '';
448 if ( $this->fld_contentmodel ) {
449 $vals['contentmodel'] = $revision->getContentModel();
452 if ( $this->fld_comment || $this->fld_parsedcomment ) {
453 if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
454 $vals['commenthidden'] = '';
455 } else {
456 $comment = $revision->getComment();
458 if ( $this->fld_comment ) {
459 $vals['comment'] = $comment;
462 if ( $this->fld_parsedcomment ) {
463 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
468 if ( $this->fld_tags ) {
469 if ( $row->ts_tags ) {
470 $tags = explode( ',', $row->ts_tags );
471 $this->getResult()->setIndexedTagName( $tags, 'tag' );
472 $vals['tags'] = $tags;
473 } else {
474 $vals['tags'] = array();
478 if ( !is_null( $this->token ) ) {
479 $tokenFunctions = $this->getTokenFunctions();
480 foreach ( $this->token as $t ) {
481 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
482 if ( $val === false ) {
483 $this->setWarning( "Action '$t' is not allowed for the current user" );
484 } else {
485 $vals[$t . 'token'] = $val;
490 $content = null;
491 global $wgParser;
492 if ( $this->fld_content || !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) {
493 $content = $revision->getContent();
494 // Expand templates after getting section content because
495 // template-added sections don't count and Parser::preprocess()
496 // will have less input
497 if ( $content && $this->section !== false ) {
498 $content = $content->getSection( $this->section, false );
499 if ( !$content ) {
500 $this->dieUsage( "There is no section {$this->section} in r" . $revision->getId(), 'nosuchsection' );
504 if ( $this->fld_content && $content && !$revision->isDeleted( Revision::DELETED_TEXT ) ) {
505 $text = null;
507 if ( $this->generateXML ) {
508 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
509 $t = $content->getNativeData(); # note: don't set $text
511 $wgParser->startExternalParse( $title, ParserOptions::newFromContext( $this->getContext() ), OT_PREPROCESS );
512 $dom = $wgParser->preprocessToDom( $t );
513 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
514 $xml = $dom->saveXML();
515 } else {
516 $xml = $dom->__toString();
518 $vals['parsetree'] = $xml;
519 } else {
520 $this->setWarning( "Conversion to XML is supported for wikitext only, " .
521 $title->getPrefixedDBkey() .
522 " uses content model " . $content->getModel() );
526 if ( $this->expandTemplates && !$this->parseContent ) {
527 #XXX: implement template expansion for all content types in ContentHandler?
528 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
529 $text = $content->getNativeData();
531 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
532 } else {
533 $this->setWarning( "Template expansion is supported for wikitext only, " .
534 $title->getPrefixedDBkey() .
535 " uses content model " . $content->getModel() );
537 $text = false;
540 if ( $this->parseContent ) {
541 $po = $content->getParserOutput( $title, $revision->getId(), ParserOptions::newFromContext( $this->getContext() ) );
542 $text = $po->getText();
545 if ( $text === null ) {
546 $format = $this->contentFormat ? $this->contentFormat : $content->getDefaultFormat();
547 $model = $content->getModel();
549 if ( !$content->isSupportedFormat( $format ) ) {
550 $name = $title->getPrefixedDBkey();
552 $this->dieUsage( "The requested format {$this->contentFormat} is not supported " .
553 "for content model $model used by $name", 'badformat' );
556 $text = $content->serialize( $format );
558 // always include format and model.
559 // Format is needed to deserialize, model is needed to interpret.
560 $vals['contentformat'] = $format;
561 $vals['contentmodel'] = $model;
564 if ( $text !== false ) {
565 ApiResult::setContent( $vals, $text );
567 } elseif ( $this->fld_content ) {
568 if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
569 $vals['texthidden'] = '';
570 } else {
571 $vals['textmissing'] = '';
575 if ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) {
576 global $wgAPIMaxUncachedDiffs;
577 static $n = 0; // Number of uncached diffs we've had
579 if ( is_null( $content ) ) {
580 $vals['textmissing'] = '';
581 } elseif ( $n < $wgAPIMaxUncachedDiffs ) {
582 $vals['diff'] = array();
583 $context = new DerivativeContext( $this->getContext() );
584 $context->setTitle( $title );
585 $handler = $revision->getContentHandler();
587 if ( !is_null( $this->difftotext ) ) {
588 $model = $title->getContentModel();
590 if ( $this->contentFormat
591 && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat ) ) {
593 $name = $title->getPrefixedDBkey();
595 $this->dieUsage( "The requested format {$this->contentFormat} is not supported for " .
596 "content model $model used by $name", 'badformat' );
599 $difftocontent = ContentHandler::makeContent( $this->difftotext, $title, $model, $this->contentFormat );
601 $engine = $handler->createDifferenceEngine( $context );
602 $engine->setContent( $content, $difftocontent );
603 } else {
604 $engine = $handler->createDifferenceEngine( $context, $revision->getID(), $this->diffto );
605 $vals['diff']['from'] = $engine->getOldid();
606 $vals['diff']['to'] = $engine->getNewid();
608 $difftext = $engine->getDiffBody();
609 ApiResult::setContent( $vals['diff'], $difftext );
610 if ( !$engine->wasCacheHit() ) {
611 $n++;
613 } else {
614 $vals['diff']['notcached'] = '';
617 return $vals;
620 public function getCacheMode( $params ) {
621 if ( isset( $params['token'] ) ) {
622 return 'private';
624 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
625 // formatComment() calls wfMessage() among other things
626 return 'anon-public-user-private';
628 return 'public';
631 public function getAllowedParams() {
632 return array(
633 'prop' => array(
634 ApiBase::PARAM_ISMULTI => true,
635 ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
636 ApiBase::PARAM_TYPE => array(
637 'ids',
638 'flags',
639 'timestamp',
640 'user',
641 'userid',
642 'size',
643 'sha1',
644 'contentmodel',
645 'comment',
646 'parsedcomment',
647 'content',
648 'tags'
651 'limit' => array(
652 ApiBase::PARAM_TYPE => 'limit',
653 ApiBase::PARAM_MIN => 1,
654 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
655 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
657 'startid' => array(
658 ApiBase::PARAM_TYPE => 'integer'
660 'endid' => array(
661 ApiBase::PARAM_TYPE => 'integer'
663 'start' => array(
664 ApiBase::PARAM_TYPE => 'timestamp'
666 'end' => array(
667 ApiBase::PARAM_TYPE => 'timestamp'
669 'dir' => array(
670 ApiBase::PARAM_DFLT => 'older',
671 ApiBase::PARAM_TYPE => array(
672 'newer',
673 'older'
676 'user' => array(
677 ApiBase::PARAM_TYPE => 'user'
679 'excludeuser' => array(
680 ApiBase::PARAM_TYPE => 'user'
682 'tag' => null,
683 'expandtemplates' => false,
684 'generatexml' => false,
685 'parse' => false,
686 'section' => null,
687 'token' => array(
688 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
689 ApiBase::PARAM_ISMULTI => true
691 'continue' => null,
692 'diffto' => null,
693 'difftotext' => null,
694 'contentformat' => array(
695 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
696 ApiBase::PARAM_DFLT => null
701 public function getParamDescription() {
702 $p = $this->getModulePrefix();
703 return array(
704 'prop' => array(
705 'Which properties to get for each revision:',
706 ' ids - The ID of the revision',
707 ' flags - Revision flags (minor)',
708 ' timestamp - The timestamp of the revision',
709 ' user - User that made the revision',
710 ' userid - User id of revision creator',
711 ' size - Length (bytes) of the revision',
712 ' sha1 - SHA-1 (base 16) of the revision',
713 ' contentmodel - Content model id',
714 ' comment - Comment by the user for revision',
715 ' parsedcomment - Parsed comment by the user for the revision',
716 ' content - Text of the revision',
717 ' tags - Tags for the revision',
719 'limit' => 'Limit how many revisions will be returned (enum)',
720 'startid' => 'From which revision id to start enumeration (enum)',
721 'endid' => 'Stop revision enumeration on this revid (enum)',
722 'start' => 'From which revision timestamp to start enumeration (enum)',
723 'end' => 'Enumerate up to this timestamp (enum)',
724 'dir' => $this->getDirectionDescription( $p, ' (enum)' ),
725 'user' => 'Only include revisions made by user (enum)',
726 'excludeuser' => 'Exclude revisions made by user (enum)',
727 'expandtemplates' => "Expand templates in revision content (requires {$p}prop=content)",
728 'generatexml' => "Generate XML parse tree for revision content (requires {$p}prop=content)",
729 'parse' => array( "Parse revision content (requires {$p}prop=content).",
730 'For performance reasons if this option is used, rvlimit is enforced to 1.' ),
731 'section' => 'Only retrieve the content of this section number',
732 'token' => 'Which tokens to obtain for each revision',
733 'continue' => 'When more results are available, use this to continue',
734 'diffto' => array( 'Revision ID to diff each revision to.',
735 'Use "prev", "next" and "cur" for the previous, next and current revision respectively' ),
736 'difftotext' => array( 'Text to diff each revision to. Only diffs a limited number of revisions.',
737 "Overrides {$p}diffto. If {$p}section is set, only that section will be diffed against this text" ),
738 'tag' => 'Only list revisions tagged with this tag',
739 'contentformat' => 'Serialization format used for difftotext and expected for output of content',
743 public function getResultProperties() {
744 $props = array(
745 '' => array(),
746 'ids' => array(
747 'revid' => 'integer',
748 'parentid' => array(
749 ApiBase::PROP_TYPE => 'integer',
750 ApiBase::PROP_NULLABLE => true
753 'flags' => array(
754 'minor' => 'boolean'
756 'user' => array(
757 'userhidden' => 'boolean',
758 'user' => 'string',
759 'anon' => 'boolean'
761 'userid' => array(
762 'userhidden' => 'boolean',
763 'userid' => 'integer',
764 'anon' => 'boolean'
766 'timestamp' => array(
767 'timestamp' => 'timestamp'
769 'size' => array(
770 'size' => 'integer'
772 'sha1' => array(
773 'sha1' => 'string'
775 'comment' => array(
776 'commenthidden' => 'boolean',
777 'comment' => array(
778 ApiBase::PROP_TYPE => 'string',
779 ApiBase::PROP_NULLABLE => true
782 'parsedcomment' => array(
783 'commenthidden' => 'boolean',
784 'parsedcomment' => array(
785 ApiBase::PROP_TYPE => 'string',
786 ApiBase::PROP_NULLABLE => true
789 'content' => array(
790 '*' => array(
791 ApiBase::PROP_TYPE => 'string',
792 ApiBase::PROP_NULLABLE => true
794 'texthidden' => 'boolean',
795 'textmissing' => 'boolean',
797 'contentmodel' => array(
798 'contentmodel' => 'string'
802 self::addTokenProperties( $props, $this->getTokenFunctions() );
804 return $props;
807 public function getDescription() {
808 return array(
809 'Get revision information',
810 'May be used in several ways:',
811 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter',
812 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params',
813 ' 3) Get data about a set of revisions by setting their IDs with revids parameter',
814 'All parameters marked as (enum) may only be used with a single page (#2)'
818 public function getPossibleErrors() {
819 return array_merge( parent::getPossibleErrors(), array(
820 array( 'nosuchrevid', 'diffto' ),
821 array( 'code' => 'revids', 'info' => 'The revids= parameter may not be used with the list options '
822 . '(limit, startid, endid, dirNewer, start, end).' ),
823 array( 'code' => 'multpages', 'info' => 'titles, pageids or a generator was used to supply multiple pages, '
824 . ' but the limit, startid, endid, dirNewer, user, excludeuser, '
825 . 'start and end parameters may only be used on a single page.' ),
826 array( 'code' => 'diffto', 'info' => 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"' ),
827 array( 'code' => 'badparams', 'info' => 'start and startid cannot be used together' ),
828 array( 'code' => 'badparams', 'info' => 'end and endid cannot be used together' ),
829 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
830 array( 'code' => 'nosuchsection', 'info' => 'There is no section section in rID' ),
831 array( 'code' => 'badformat', 'info' => 'The requested serialization format can not be applied '
832 . ' to the page\'s content model' ),
833 ) );
836 public function getExamples() {
837 return array(
838 'Get data with content for the last revision of titles "API" and "Main Page"',
839 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
840 'Get last 5 revisions of the "Main Page"',
841 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
842 'Get first 5 revisions of the "Main Page"',
843 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
844 'Get first 5 revisions of the "Main Page" made after 2006-05-01',
845 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
846 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
847 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
848 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
849 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
853 public function getHelpUrls() {
854 return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';