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