Merge "Restore Signupstart and Signupend messages for account creation"
[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( $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 $pageid
76 * @param $title Title
77 * @param $rev Revision
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->isDeleted( Revision::DELETED_TEXT ) ) {
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->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 $this->addWhere( $db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
320 }
321 } elseif ( $revCount > 0 ) {
322 $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
323 $revs = $pageSet->getRevisionIDs();
324 if ( self::truncateArray( $revs, $max ) ) {
325 $this->setWarning( "Too many values supplied for parameter 'revids': the limit is $max" );
326 }
327
328 // Get all revision IDs
329 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
330
331 if ( !is_null( $params['continue'] ) ) {
332 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
333 }
334 $this->addOption( 'ORDER BY', 'rev_id' );
335
336 // assumption testing -- we should never get more then $revCount rows.
337 $limit = $revCount;
338 } elseif ( $pageCount > 0 ) {
339 $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
340 $titles = $pageSet->getGoodTitles();
341 if ( self::truncateArray( $titles, $max ) ) {
342 $this->setWarning( "Too many values supplied for parameter 'titles': the limit is $max" );
343 }
344
345 // When working in multi-page non-enumeration mode,
346 // limit to the latest revision only
347 $this->addWhere( 'page_id=rev_page' );
348 $this->addWhere( 'page_latest=rev_id' );
349
350 // Get all page IDs
351 $this->addWhereFld( 'page_id', array_keys( $titles ) );
352 // Every time someone relies on equality propagation, god kills a kitten :)
353 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
354
355 if ( !is_null( $params['continue'] ) ) {
356 $cont = explode( '|', $params['continue'] );
357 $this->dieContinueUsageIf( count( $cont ) != 2 );
358 $pageid = intval( $cont[0] );
359 $revid = intval( $cont[1] );
360 $this->addWhere(
361 "rev_page > $pageid OR " .
362 "(rev_page = $pageid AND " .
363 "rev_id >= $revid)"
364 );
365 }
366 $this->addOption( 'ORDER BY', array(
367 'rev_page',
368 'rev_id'
369 ) );
370
371 // assumption testing -- we should never get more then $pageCount rows.
372 $limit = $pageCount;
373 } else {
374 ApiBase::dieDebug( __METHOD__, 'param validation?' );
375 }
376
377 $this->addOption( 'LIMIT', $limit + 1 );
378
379 $count = 0;
380 $res = $this->select( __METHOD__ );
381
382 foreach ( $res as $row ) {
383 if ( ++$count > $limit ) {
384 // We've reached the one extra which shows that there are
385 // additional pages to be had. Stop here...
386 if ( !$enumRevMode ) {
387 ApiBase::dieDebug( __METHOD__, 'Got more rows then expected' ); // bug report
388 }
389 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
390 break;
391 }
392
393 $fit = $this->addPageSubItem( $row->rev_page, $this->extractRowInfo( $row ), 'rev' );
394 if ( !$fit ) {
395 if ( $enumRevMode ) {
396 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
397 } elseif ( $revCount > 0 ) {
398 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
399 } else {
400 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
401 '|' . intval( $row->rev_id ) );
402 }
403 break;
404 }
405 }
406 }
407
408 private function extractRowInfo( $row ) {
409 $revision = new Revision( $row );
410 $title = $revision->getTitle();
411 $vals = array();
412
413 if ( $this->fld_ids ) {
414 $vals['revid'] = intval( $revision->getId() );
415 // $vals['oldid'] = intval( $row->rev_text_id ); // todo: should this be exposed?
416 if ( !is_null( $revision->getParentId() ) ) {
417 $vals['parentid'] = intval( $revision->getParentId() );
418 }
419 }
420
421 if ( $this->fld_flags && $revision->isMinor() ) {
422 $vals['minor'] = '';
423 }
424
425 if ( $this->fld_user || $this->fld_userid ) {
426 if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
427 $vals['userhidden'] = '';
428 } else {
429 if ( $this->fld_user ) {
430 $vals['user'] = $revision->getUserText();
431 }
432 $userid = $revision->getUser();
433 if ( !$userid ) {
434 $vals['anon'] = '';
435 }
436
437 if ( $this->fld_userid ) {
438 $vals['userid'] = $userid;
439 }
440 }
441 }
442
443 if ( $this->fld_timestamp ) {
444 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
445 }
446
447 if ( $this->fld_size ) {
448 if ( !is_null( $revision->getSize() ) ) {
449 $vals['size'] = intval( $revision->getSize() );
450 } else {
451 $vals['size'] = 0;
452 }
453 }
454
455 if ( $this->fld_sha1 && !$revision->isDeleted( Revision::DELETED_TEXT ) ) {
456 if ( $revision->getSha1() != '' ) {
457 $vals['sha1'] = wfBaseConvert( $revision->getSha1(), 36, 16, 40 );
458 } else {
459 $vals['sha1'] = '';
460 }
461 } elseif ( $this->fld_sha1 ) {
462 $vals['sha1hidden'] = '';
463 }
464
465 if ( $this->fld_contentmodel ) {
466 $vals['contentmodel'] = $revision->getContentModel();
467 }
468
469 if ( $this->fld_comment || $this->fld_parsedcomment ) {
470 if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
471 $vals['commenthidden'] = '';
472 } else {
473 $comment = $revision->getComment();
474
475 if ( $this->fld_comment ) {
476 $vals['comment'] = $comment;
477 }
478
479 if ( $this->fld_parsedcomment ) {
480 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
481 }
482 }
483 }
484
485 if ( $this->fld_tags ) {
486 if ( $row->ts_tags ) {
487 $tags = explode( ',', $row->ts_tags );
488 $this->getResult()->setIndexedTagName( $tags, 'tag' );
489 $vals['tags'] = $tags;
490 } else {
491 $vals['tags'] = array();
492 }
493 }
494
495 if ( !is_null( $this->token ) ) {
496 $tokenFunctions = $this->getTokenFunctions();
497 foreach ( $this->token as $t ) {
498 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
499 if ( $val === false ) {
500 $this->setWarning( "Action '$t' is not allowed for the current user" );
501 } else {
502 $vals[$t . 'token'] = $val;
503 }
504 }
505 }
506
507 $content = null;
508 global $wgParser;
509 if ( $this->fld_content || !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) {
510 $content = $revision->getContent();
511 // Expand templates after getting section content because
512 // template-added sections don't count and Parser::preprocess()
513 // will have less input
514 if ( $content && $this->section !== false ) {
515 $content = $content->getSection( $this->section, false );
516 if ( !$content ) {
517 $this->dieUsage(
518 "There is no section {$this->section} in r" . $revision->getId(),
519 'nosuchsection'
520 );
521 }
522 }
523 }
524 if ( $this->fld_content && $content && !$revision->isDeleted( Revision::DELETED_TEXT ) ) {
525 $text = null;
526
527 if ( $this->generateXML ) {
528 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
529 $t = $content->getNativeData(); # note: don't set $text
530
531 $wgParser->startExternalParse(
532 $title,
533 ParserOptions::newFromContext( $this->getContext() ),
534 OT_PREPROCESS
535 );
536 $dom = $wgParser->preprocessToDom( $t );
537 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
538 $xml = $dom->saveXML();
539 } else {
540 $xml = $dom->__toString();
541 }
542 $vals['parsetree'] = $xml;
543 } else {
544 $this->setWarning( "Conversion to XML is supported for wikitext only, " .
545 $title->getPrefixedDBkey() .
546 " uses content model " . $content->getModel() );
547 }
548 }
549
550 if ( $this->expandTemplates && !$this->parseContent ) {
551 #XXX: implement template expansion for all content types in ContentHandler?
552 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
553 $text = $content->getNativeData();
554
555 $text = $wgParser->preprocess(
556 $text,
557 $title,
558 ParserOptions::newFromContext( $this->getContext() )
559 );
560 } else {
561 $this->setWarning( "Template expansion is supported for wikitext only, " .
562 $title->getPrefixedDBkey() .
563 " uses content model " . $content->getModel() );
564
565 $text = false;
566 }
567 }
568 if ( $this->parseContent ) {
569 $po = $content->getParserOutput(
570 $title,
571 $revision->getId(),
572 ParserOptions::newFromContext( $this->getContext() )
573 );
574 $text = $po->getText();
575 }
576
577 if ( $text === null ) {
578 $format = $this->contentFormat ? $this->contentFormat : $content->getDefaultFormat();
579 $model = $content->getModel();
580
581 if ( !$content->isSupportedFormat( $format ) ) {
582 $name = $title->getPrefixedDBkey();
583
584 $this->dieUsage( "The requested format {$this->contentFormat} is not supported " .
585 "for content model $model used by $name", 'badformat' );
586 }
587
588 $text = $content->serialize( $format );
589
590 // always include format and model.
591 // Format is needed to deserialize, model is needed to interpret.
592 $vals['contentformat'] = $format;
593 $vals['contentmodel'] = $model;
594 }
595
596 if ( $text !== false ) {
597 ApiResult::setContent( $vals, $text );
598 }
599 } elseif ( $this->fld_content ) {
600 if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
601 $vals['texthidden'] = '';
602 } else {
603 $vals['textmissing'] = '';
604 }
605 }
606
607 if ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) {
608 global $wgAPIMaxUncachedDiffs;
609 static $n = 0; // Number of uncached diffs we've had
610
611 if ( is_null( $content ) ) {
612 $vals['textmissing'] = '';
613 } elseif ( $n < $wgAPIMaxUncachedDiffs ) {
614 $vals['diff'] = array();
615 $context = new DerivativeContext( $this->getContext() );
616 $context->setTitle( $title );
617 $handler = $revision->getContentHandler();
618
619 if ( !is_null( $this->difftotext ) ) {
620 $model = $title->getContentModel();
621
622 if ( $this->contentFormat
623 && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat )
624 ) {
625
626 $name = $title->getPrefixedDBkey();
627
628 $this->dieUsage( "The requested format {$this->contentFormat} is not supported for " .
629 "content model $model used by $name", 'badformat' );
630 }
631
632 $difftocontent = ContentHandler::makeContent(
633 $this->difftotext,
634 $title,
635 $model,
636 $this->contentFormat
637 );
638
639 $engine = $handler->createDifferenceEngine( $context );
640 $engine->setContent( $content, $difftocontent );
641 } else {
642 $engine = $handler->createDifferenceEngine( $context, $revision->getID(), $this->diffto );
643 $vals['diff']['from'] = $engine->getOldid();
644 $vals['diff']['to'] = $engine->getNewid();
645 }
646 $difftext = $engine->getDiffBody();
647 ApiResult::setContent( $vals['diff'], $difftext );
648 if ( !$engine->wasCacheHit() ) {
649 $n++;
650 }
651 } else {
652 $vals['diff']['notcached'] = '';
653 }
654 }
655
656 return $vals;
657 }
658
659 public function getCacheMode( $params ) {
660 if ( isset( $params['token'] ) ) {
661 return 'private';
662 }
663 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
664 // formatComment() calls wfMessage() among other things
665 return 'anon-public-user-private';
666 }
667
668 return 'public';
669 }
670
671 public function getAllowedParams() {
672 return array(
673 'prop' => array(
674 ApiBase::PARAM_ISMULTI => true,
675 ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
676 ApiBase::PARAM_TYPE => array(
677 'ids',
678 'flags',
679 'timestamp',
680 'user',
681 'userid',
682 'size',
683 'sha1',
684 'contentmodel',
685 'comment',
686 'parsedcomment',
687 'content',
688 'tags'
689 )
690 ),
691 'limit' => array(
692 ApiBase::PARAM_TYPE => 'limit',
693 ApiBase::PARAM_MIN => 1,
694 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
695 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
696 ),
697 'startid' => array(
698 ApiBase::PARAM_TYPE => 'integer'
699 ),
700 'endid' => array(
701 ApiBase::PARAM_TYPE => 'integer'
702 ),
703 'start' => array(
704 ApiBase::PARAM_TYPE => 'timestamp'
705 ),
706 'end' => array(
707 ApiBase::PARAM_TYPE => 'timestamp'
708 ),
709 'dir' => array(
710 ApiBase::PARAM_DFLT => 'older',
711 ApiBase::PARAM_TYPE => array(
712 'newer',
713 'older'
714 )
715 ),
716 'user' => array(
717 ApiBase::PARAM_TYPE => 'user'
718 ),
719 'excludeuser' => array(
720 ApiBase::PARAM_TYPE => 'user'
721 ),
722 'tag' => null,
723 'expandtemplates' => false,
724 'generatexml' => false,
725 'parse' => false,
726 'section' => null,
727 'token' => array(
728 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
729 ApiBase::PARAM_ISMULTI => true
730 ),
731 'continue' => null,
732 'diffto' => null,
733 'difftotext' => null,
734 'contentformat' => array(
735 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
736 ApiBase::PARAM_DFLT => null
737 ),
738 );
739 }
740
741 public function getParamDescription() {
742 $p = $this->getModulePrefix();
743
744 return array(
745 'prop' => array(
746 'Which properties to get for each revision:',
747 ' ids - The ID of the revision',
748 ' flags - Revision flags (minor)',
749 ' timestamp - The timestamp of the revision',
750 ' user - User that made the revision',
751 ' userid - User id of revision creator',
752 ' size - Length (bytes) of the revision',
753 ' sha1 - SHA-1 (base 16) of the revision',
754 ' contentmodel - Content model id',
755 ' comment - Comment by the user for revision',
756 ' parsedcomment - Parsed comment by the user for the revision',
757 ' content - Text of the revision',
758 ' tags - Tags for the revision',
759 ),
760 'limit' => 'Limit how many revisions will be returned (enum)',
761 'startid' => 'From which revision id to start enumeration (enum)',
762 'endid' => 'Stop revision enumeration on this revid (enum)',
763 'start' => 'From which revision timestamp to start enumeration (enum)',
764 'end' => 'Enumerate up to this timestamp (enum)',
765 'dir' => $this->getDirectionDescription( $p, ' (enum)' ),
766 'user' => 'Only include revisions made by user (enum)',
767 'excludeuser' => 'Exclude revisions made by user (enum)',
768 'expandtemplates' => "Expand templates in revision content (requires {$p}prop=content)",
769 'generatexml' => "Generate XML parse tree for revision content (requires {$p}prop=content)",
770 'parse' => array( "Parse revision content (requires {$p}prop=content).",
771 'For performance reasons if this option is used, rvlimit is enforced to 1.' ),
772 'section' => 'Only retrieve the content of this section number',
773 'token' => 'Which tokens to obtain for each revision',
774 'continue' => 'When more results are available, use this to continue',
775 'diffto' => array( 'Revision ID to diff each revision to.',
776 'Use "prev", "next" and "cur" for the previous, next and current revision respectively' ),
777 'difftotext' => array(
778 'Text to diff each revision to. Only diffs a limited number of revisions.',
779 "Overrides {$p}diffto. If {$p}section is set, only that section will be",
780 'diffed against this text',
781 ),
782 'tag' => 'Only list revisions tagged with this tag',
783 'contentformat' => 'Serialization format used for difftotext and expected for output of content',
784 );
785 }
786
787 public function getResultProperties() {
788 $props = array(
789 '' => array(),
790 'ids' => array(
791 'revid' => 'integer',
792 'parentid' => array(
793 ApiBase::PROP_TYPE => 'integer',
794 ApiBase::PROP_NULLABLE => true
795 )
796 ),
797 'flags' => array(
798 'minor' => 'boolean'
799 ),
800 'user' => array(
801 'userhidden' => 'boolean',
802 'user' => 'string',
803 'anon' => 'boolean'
804 ),
805 'userid' => array(
806 'userhidden' => 'boolean',
807 'userid' => 'integer',
808 'anon' => 'boolean'
809 ),
810 'timestamp' => array(
811 'timestamp' => 'timestamp'
812 ),
813 'size' => array(
814 'size' => 'integer'
815 ),
816 'sha1' => array(
817 'sha1' => 'string'
818 ),
819 'comment' => array(
820 'commenthidden' => 'boolean',
821 'comment' => array(
822 ApiBase::PROP_TYPE => 'string',
823 ApiBase::PROP_NULLABLE => true
824 )
825 ),
826 'parsedcomment' => array(
827 'commenthidden' => 'boolean',
828 'parsedcomment' => array(
829 ApiBase::PROP_TYPE => 'string',
830 ApiBase::PROP_NULLABLE => true
831 )
832 ),
833 'content' => array(
834 '*' => array(
835 ApiBase::PROP_TYPE => 'string',
836 ApiBase::PROP_NULLABLE => true
837 ),
838 'texthidden' => 'boolean',
839 'textmissing' => 'boolean',
840 ),
841 'contentmodel' => array(
842 'contentmodel' => 'string'
843 ),
844 );
845
846 self::addTokenProperties( $props, $this->getTokenFunctions() );
847
848 return $props;
849 }
850
851 public function getDescription() {
852 return array(
853 'Get revision information',
854 'May be used in several ways:',
855 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter',
856 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params',
857 ' 3) Get data about a set of revisions by setting their IDs with revids parameter',
858 'All parameters marked as (enum) may only be used with a single page (#2)'
859 );
860 }
861
862 public function getPossibleErrors() {
863 return array_merge( parent::getPossibleErrors(), array(
864 array( 'nosuchrevid', 'diffto' ),
865 array(
866 'code' => 'revids',
867 'info' => 'The revids= parameter may not be used with the list options '
868 . '(limit, startid, endid, dirNewer, start, end).'
869 ),
870 array(
871 'code' => 'multpages',
872 'info' => 'titles, pageids or a generator was used to supply multiple pages, '
873 . ' but the limit, startid, endid, dirNewer, user, excludeuser, '
874 . 'start and end parameters may only be used on a single page.'
875 ),
876 array(
877 'code' => 'diffto',
878 'info' => 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"'
879 ),
880 array( 'code' => 'badparams', 'info' => 'start and startid cannot be used together' ),
881 array( 'code' => 'badparams', 'info' => 'end and endid cannot be used together' ),
882 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
883 array( 'code' => 'nosuchsection', 'info' => 'There is no section section in rID' ),
884 array( 'code' => 'badformat', 'info' => 'The requested serialization format can not be applied '
885 . ' to the page\'s content model' ),
886 ) );
887 }
888
889 public function getExamples() {
890 return array(
891 'Get data with content for the last revision of titles "API" and "Main Page"',
892 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&' .
893 'rvprop=timestamp|user|comment|content',
894 'Get last 5 revisions of the "Main Page"',
895 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
896 'rvprop=timestamp|user|comment',
897 'Get first 5 revisions of the "Main Page"',
898 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
899 'rvprop=timestamp|user|comment&rvdir=newer',
900 'Get first 5 revisions of the "Main Page" made after 2006-05-01',
901 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
902 'rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
903 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
904 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
905 'rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
906 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
907 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
908 'rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
909 );
910 }
911
912 public function getHelpUrls() {
913 return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';
914 }
915 }