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