merged master
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 7, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * A query action to enumerate revisions of a given page, or show top revisions of multiple pages.
29 * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
30 * In the enumeration mode, ranges of revisions may be requested and filtered.
31 *
32 * @ingroup API
33 */
34 class ApiQueryRevisions extends ApiQueryBase {
35
36 private $diffto, $difftotext, $expandTemplates, $generateXML, $section,
37 $token, $parseContent, $contentFormat;
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'rv' );
41 }
42
43 private $fld_ids = false, $fld_flags = false, $fld_timestamp = false, $fld_size = false, $fld_sha1 = false,
44 $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
45 $fld_content = false, $fld_tags = false, $fld_contentmodel = false;
46
47 private $tokenFunctions;
48
49 protected function getTokenFunctions() {
50 // tokenname => function
51 // function prototype is func($pageid, $title, $rev)
52 // should return token or false
53
54 // Don't call the hooks twice
55 if ( isset( $this->tokenFunctions ) ) {
56 return $this->tokenFunctions;
57 }
58
59 // If we're in JSON callback mode, no tokens can be obtained
60 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
61 return array();
62 }
63
64 $this->tokenFunctions = array(
65 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
66 );
67 wfRunHooks( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions ) );
68 return $this->tokenFunctions;
69 }
70
71 /**
72 * @param $pageid
73 * @param $title Title
74 * @param $rev Revision
75 * @return bool|String
76 */
77 public static function getRollbackToken( $pageid, $title, $rev ) {
78 global $wgUser;
79 if ( !$wgUser->isAllowed( 'rollback' ) ) {
80 return false;
81 }
82 return $wgUser->getEditToken(
83 array( $title->getPrefixedText(), $rev->getUserText() ) );
84 }
85
86 public function execute() {
87 $params = $this->extractRequestParams( false );
88
89 // If any of those parameters are used, work in 'enumeration' mode.
90 // Enum mode can only be used when exactly one page is provided.
91 // Enumerating revisions on multiple pages make it extremely
92 // difficult to manage continuations and require additional SQL indexes
93 $enumRevMode = ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ||
94 !is_null( $params['limit'] ) || !is_null( $params['startid'] ) ||
95 !is_null( $params['endid'] ) || $params['dir'] === 'newer' ||
96 !is_null( $params['start'] ) || !is_null( $params['end'] ) );
97
98
99 $pageSet = $this->getPageSet();
100 $pageCount = $pageSet->getGoodTitleCount();
101 $revCount = $pageSet->getRevisionCount();
102
103 // Optimization -- nothing to do
104 if ( $revCount === 0 && $pageCount === 0 ) {
105 return;
106 }
107
108 if ( $revCount > 0 && $enumRevMode ) {
109 $this->dieUsage( 'The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids' );
110 }
111
112 if ( $pageCount > 1 && $enumRevMode ) {
113 $this->dieUsage( 'titles, pageids or a generator was used to supply multiple pages, but the limit, startid, endid, dirNewer, user, excludeuser, start and end parameters may only be used on a single page.', 'multpages' );
114 }
115
116 if ( !is_null( $params['difftotext'] ) ) {
117 $this->difftotext = $params['difftotext'];
118 } elseif ( !is_null( $params['diffto'] ) ) {
119 if ( $params['diffto'] == 'cur' ) {
120 $params['diffto'] = 0;
121 }
122 if ( ( !ctype_digit( $params['diffto'] ) || $params['diffto'] < 0 )
123 && $params['diffto'] != 'prev' && $params['diffto'] != 'next' ) {
124 $this->dieUsage( 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"', 'diffto' );
125 }
126 // Check whether the revision exists and is readable,
127 // DifferenceEngine returns a rather ambiguous empty
128 // string if that's not the case
129 if ( $params['diffto'] != 0 ) {
130 $difftoRev = Revision::newFromID( $params['diffto'] );
131 if ( !$difftoRev ) {
132 $this->dieUsageMsg( array( 'nosuchrevid', $params['diffto'] ) );
133 }
134 if ( $difftoRev->isDeleted( Revision::DELETED_TEXT ) ) {
135 $this->setWarning( "Couldn't diff to r{$difftoRev->getID()}: content is hidden" );
136 $params['diffto'] = null;
137 }
138 }
139 $this->diffto = $params['diffto'];
140 }
141
142 $db = $this->getDB();
143 $this->addTables( 'page' );
144 $this->addFields( Revision::selectFields() );
145 $this->addWhere( 'page_id = rev_page' );
146
147 $prop = array_flip( $params['prop'] );
148
149 // Optional fields
150 $this->fld_ids = isset ( $prop['ids'] );
151 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
152 $this->fld_flags = isset ( $prop['flags'] );
153 $this->fld_timestamp = isset ( $prop['timestamp'] );
154 $this->fld_comment = isset ( $prop['comment'] );
155 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
156 $this->fld_size = isset ( $prop['size'] );
157 $this->fld_sha1 = isset ( $prop['sha1'] );
158 $this->fld_contentmodel = isset ( $prop['contentmodel'] );
159 $this->fld_userid = isset( $prop['userid'] );
160 $this->fld_user = isset ( $prop['user'] );
161 $this->token = $params['token'];
162
163 if ( !empty( $params['contentformat'] ) ) {
164 $this->contentFormat = $params['contentformat'];
165 }
166
167 // Possible indexes used
168 $index = array();
169
170 $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
171 $botMax = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
172 $limit = $params['limit'];
173 if ( $limit == 'max' ) {
174 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
175 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
176 }
177
178 if ( !is_null( $this->token ) || $pageCount > 0 ) {
179 $this->addFields( Revision::selectPageFields() );
180 }
181
182 if ( isset( $prop['tags'] ) ) {
183 $this->fld_tags = true;
184 $this->addTables( 'tag_summary' );
185 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
186 $this->addFields( 'ts_tags' );
187 }
188
189 if ( !is_null( $params['tag'] ) ) {
190 $this->addTables( 'change_tag' );
191 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
192 $this->addWhereFld( 'ct_tag' , $params['tag'] );
193 global $wgOldChangeTagsIndex;
194 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
195 }
196
197 if ( isset( $prop['content'] ) || !is_null( $this->difftotext ) ) {
198 // For each page we will request, the user must have read rights for that page
199 foreach ( $pageSet->getGoodTitles() as $title ) {
200 if ( !$title->userCan( 'read' ) ) {
201 $this->dieUsage(
202 'The current user is not allowed to read ' . $title->getPrefixedText(),
203 'accessdenied' );
204 }
205 }
206
207 $this->addTables( 'text' );
208 $this->addWhere( 'rev_text_id=old_id' );
209 $this->addFields( 'old_id' );
210 $this->addFields( Revision::selectTextFields() );
211
212 $this->fld_content = isset( $prop['content'] );
213
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;
221 }
222 // We are only going to parse 1 revision per request
223 $this->validateLimit( 'limit', $limit, 1, 1, 1 );
224 }
225 if ( isset( $params['section'] ) ) {
226 $this->section = $params['section'];
227 } else {
228 $this->section = false;
229 }
230 }
231
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() );
237 }
238
239 // Bug 24166 - API error when using rvprop=tags
240 $this->addTables( 'revision' );
241
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' );
246 }
247
248 if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) ) {
249 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
250 }
251
252 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
253 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
254 }
255
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 unset( $params['start'] );
264 }
265
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 );
282 }
283
284 // must manually initialize unset limit
285 if ( is_null( $limit ) ) {
286 $limit = 10;
287 }
288 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
289
290 // There is only one ID, use it
291 $ids = array_keys( $pageSet->getGoodTitles() );
292 $this->addWhereFld( 'rev_page', reset( $ids ) );
293
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'] ) );
299 }
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' );
303 }
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" );
309 }
310
311 // Get all revision IDs
312 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
313
314 if ( !is_null( $params['continue'] ) ) {
315 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
316 }
317 $this->addOption( 'ORDER BY', 'rev_id' );
318
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" );
326 }
327
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' );
332
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 ) );
337
338 if ( !is_null( $params['continue'] ) ) {
339 $cont = explode( '|', $params['continue'] );
340 if ( count( $cont ) != 2 ) {
341 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
342 'value returned by the previous query', '_badcontinue' );
343 }
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 ) {
442 if ( $revision->getSha1() != '' ) {
443 $vals['sha1'] = wfBaseConvert( $revision->getSha1(), 36, 16, 40 );
444 } else {
445 $vals['sha1'] = '';
446 }
447 }
448
449 if ( $this->fld_contentmodel ) {
450 $vals['contentmodel'] = $revision->getContentModel();
451 }
452
453 if ( $this->fld_comment || $this->fld_parsedcomment ) {
454 if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
455 $vals['commenthidden'] = '';
456 } else {
457 $comment = $revision->getComment();
458
459 if ( $this->fld_comment ) {
460 $vals['comment'] = $comment;
461 }
462
463 if ( $this->fld_parsedcomment ) {
464 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
465 }
466 }
467 }
468
469 if ( $this->fld_tags ) {
470 if ( $row->ts_tags ) {
471 $tags = explode( ',', $row->ts_tags );
472 $this->getResult()->setIndexedTagName( $tags, 'tag' );
473 $vals['tags'] = $tags;
474 } else {
475 $vals['tags'] = array();
476 }
477 }
478
479 if ( !is_null( $this->token ) ) {
480 $tokenFunctions = $this->getTokenFunctions();
481 foreach ( $this->token as $t ) {
482 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
483 if ( $val === false ) {
484 $this->setWarning( "Action '$t' is not allowed for the current user" );
485 } else {
486 $vals[$t . 'token'] = $val;
487 }
488 }
489 }
490
491 $content = null;
492 global $wgParser;
493 if ( $this->fld_content || !is_null( $this->difftotext ) ) {
494 $content = $revision->getContent();
495 // Expand templates after getting section content because
496 // template-added sections don't count and Parser::preprocess()
497 // will have less input
498 if ( $this->section !== false ) {
499 $content = $content->getSection( $this->section, false );
500 if ( !$content ) {
501 $this->dieUsage( "There is no section {$this->section} in r" . $revision->getId(), 'nosuchsection' );
502 }
503 }
504 }
505 if ( $this->fld_content && !$revision->isDeleted( Revision::DELETED_TEXT ) ) {
506 $text = null;
507
508 if ( $this->generateXML ) {
509 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
510 $t = $content->getNativeData(); # note: don't set $text
511
512 $wgParser->startExternalParse( $title, ParserOptions::newFromContext( $this->getContext() ), OT_PREPROCESS );
513 $dom = $wgParser->preprocessToDom( $t );
514 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
515 $xml = $dom->saveXML();
516 } else {
517 $xml = $dom->__toString();
518 }
519 $vals['parsetree'] = $xml;
520 } else {
521 $this->setWarning( "Conversion to XML is supported for wikitext only, " .
522 $title->getPrefixedDBkey() .
523 " uses content model " . $content->getModel() . ")" );
524 }
525 }
526
527 if ( $this->expandTemplates && !$this->parseContent ) {
528 #XXX: implement template expansion for all content types in ContentHandler?
529 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
530 $text = $content->getNativeData();
531
532 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
533 } else {
534 $this->setWarning( "Template expansion is supported for wikitext only, " .
535 $title->getPrefixedDBkey() .
536 " uses content model " . $content->getModel() . ")" );
537
538 $text = false;
539 }
540 }
541 if ( $this->parseContent ) {
542 $po = $content->getParserOutput( $title, ParserOptions::newFromContext( $this->getContext() ) );
543 $text = $po->getText();
544 }
545
546 if ( $text === null ) {
547 $format = $this->contentFormat ? $this->contentFormat : $content->getDefaultFormat();
548
549 if ( !$content->isSupportedFormat( $format ) ) {
550 $model = $content->getModel();
551 $name = $title->getPrefixedDBkey();
552
553 $this->dieUsage( "The requested format {$this->contentFormat} is not supported ".
554 "for content model $model used by $name", 'badformat' );
555 }
556
557 $text = $content->serialize( $format );
558 $vals['contentformat'] = $format;
559 }
560
561 if ( $text !== false ) {
562 ApiResult::setContent( $vals, $text );
563 }
564 } elseif ( $this->fld_content ) {
565 $vals['texthidden'] = '';
566 }
567
568 if ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) {
569 global $wgAPIMaxUncachedDiffs;
570 static $n = 0; // Number of uncached diffs we've had
571 if ( $n < $wgAPIMaxUncachedDiffs ) {
572 $vals['diff'] = array();
573 $context = new DerivativeContext( $this->getContext() );
574 $context->setTitle( $title );
575 $handler = $revision->getContentHandler();
576
577 if ( !is_null( $this->difftotext ) ) {
578 $model = $title->getContentModel();
579
580 if ( $this->contentFormat
581 && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat ) ) {
582
583 $name = $title->getPrefixedDBkey();
584
585 $this->dieUsage( "The requested format {$this->contentFormat} is not supported for ".
586 "content model $model used by $name", 'badformat' );
587 }
588
589 $difftocontent = ContentHandler::makeContent( $this->difftotext, $title, $model, $this->contentFormat );
590
591 $engine = $handler->createDifferenceEngine( $context );
592 $engine->setContent( $content, $difftocontent );
593 } else {
594 $engine = $handler->createDifferenceEngine( $context, $revision->getID(), $this->diffto );
595 $vals['diff']['from'] = $engine->getOldid();
596 $vals['diff']['to'] = $engine->getNewid();
597 }
598 $difftext = $engine->getDiffBody();
599 ApiResult::setContent( $vals['diff'], $difftext );
600 if ( !$engine->wasCacheHit() ) {
601 $n++;
602 }
603 } else {
604 $vals['diff']['notcached'] = '';
605 }
606 }
607 return $vals;
608 }
609
610 public function getCacheMode( $params ) {
611 if ( isset( $params['token'] ) ) {
612 return 'private';
613 }
614 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
615 // formatComment() calls wfMessage() among other things
616 return 'anon-public-user-private';
617 }
618 return 'public';
619 }
620
621 public function getAllowedParams() {
622 return array(
623 'prop' => array(
624 ApiBase::PARAM_ISMULTI => true,
625 ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
626 ApiBase::PARAM_TYPE => array(
627 'ids',
628 'flags',
629 'timestamp',
630 'user',
631 'userid',
632 'size',
633 'sha1',
634 'contentmodel',
635 'comment',
636 'parsedcomment',
637 'content',
638 'tags'
639 )
640 ),
641 'limit' => array(
642 ApiBase::PARAM_TYPE => 'limit',
643 ApiBase::PARAM_MIN => 1,
644 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
645 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
646 ),
647 'startid' => array(
648 ApiBase::PARAM_TYPE => 'integer'
649 ),
650 'endid' => array(
651 ApiBase::PARAM_TYPE => 'integer'
652 ),
653 'start' => array(
654 ApiBase::PARAM_TYPE => 'timestamp'
655 ),
656 'end' => array(
657 ApiBase::PARAM_TYPE => 'timestamp'
658 ),
659 'dir' => array(
660 ApiBase::PARAM_DFLT => 'older',
661 ApiBase::PARAM_TYPE => array(
662 'newer',
663 'older'
664 )
665 ),
666 'user' => array(
667 ApiBase::PARAM_TYPE => 'user'
668 ),
669 'excludeuser' => array(
670 ApiBase::PARAM_TYPE => 'user'
671 ),
672 'tag' => null,
673 'expandtemplates' => false,
674 'generatexml' => false,
675 'parse' => false,
676 'section' => null,
677 'token' => array(
678 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
679 ApiBase::PARAM_ISMULTI => true
680 ),
681 'continue' => null,
682 'diffto' => null,
683 'difftotext' => null,
684 'contentformat' => array(
685 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
686 ApiBase::PARAM_DFLT => null
687 ),
688 );
689 }
690
691 public function getParamDescription() {
692 $p = $this->getModulePrefix();
693 return array(
694 'prop' => array(
695 'Which properties to get for each revision:',
696 ' ids - The ID of the revision',
697 ' flags - Revision flags (minor)',
698 ' timestamp - The timestamp of the revision',
699 ' user - User that made the revision',
700 ' userid - User id of revision creator',
701 ' size - Length (bytes) of the revision',
702 ' sha1 - SHA-1 (base 16) of the revision',
703 ' contentmodel - Content model id',
704 ' comment - Comment by the user for revision',
705 ' parsedcomment - Parsed comment by the user for the revision',
706 ' content - Text of the revision',
707 ' tags - Tags for the revision',
708 ),
709 'limit' => 'Limit how many revisions will be returned (enum)',
710 'startid' => 'From which revision id to start enumeration (enum)',
711 'endid' => 'Stop revision enumeration on this revid (enum)',
712 'start' => 'From which revision timestamp to start enumeration (enum)',
713 'end' => 'Enumerate up to this timestamp (enum)',
714 'dir' => $this->getDirectionDescription( $p, ' (enum)' ),
715 'user' => 'Only include revisions made by user (enum)',
716 'excludeuser' => 'Exclude revisions made by user (enum)',
717 'expandtemplates' => 'Expand templates in revision content',
718 'generatexml' => 'Generate XML parse tree for revision content',
719 'parse' => 'Parse revision content. For performance reasons if this option is used, rvlimit is enforced to 1.',
720 'section' => 'Only retrieve the content of this section number',
721 'token' => 'Which tokens to obtain for each revision',
722 'continue' => 'When more results are available, use this to continue',
723 'diffto' => array( 'Revision ID to diff each revision to.',
724 'Use "prev", "next" and "cur" for the previous, next and current revision respectively' ),
725 'difftotext' => array( 'Text to diff each revision to. Only diffs a limited number of revisions.',
726 "Overrides {$p}diffto. If {$p}section is set, only that section will be diffed against this text" ),
727 'tag' => 'Only list revisions tagged with this tag',
728 'contentformat' => 'Serialization format used for difftotext and expected for output of content',
729 );
730 }
731
732 public function getResultProperties() {
733 $props = array(
734 '' => array(),
735 'ids' => array(
736 'revid' => 'integer',
737 'parentid' => array(
738 ApiBase::PROP_TYPE => 'integer',
739 ApiBase::PROP_NULLABLE => true
740 )
741 ),
742 'flags' => array(
743 'minor' => 'boolean'
744 ),
745 'user' => array(
746 'userhidden' => 'boolean',
747 'user' => 'string',
748 'anon' => 'boolean'
749 ),
750 'userid' => array(
751 'userhidden' => 'boolean',
752 'userid' => 'integer',
753 'anon' => 'boolean'
754 ),
755 'timestamp' => array(
756 'timestamp' => 'timestamp'
757 ),
758 'size' => array(
759 'size' => 'integer'
760 ),
761 'sha1' => array(
762 'sha1' => 'string'
763 ),
764 'comment' => array(
765 'commenthidden' => 'boolean',
766 'comment' => array(
767 ApiBase::PROP_TYPE => 'string',
768 ApiBase::PROP_NULLABLE => true
769 )
770 ),
771 'parsedcomment' => array(
772 'commenthidden' => 'boolean',
773 'parsedcomment' => array(
774 ApiBase::PROP_TYPE => 'string',
775 ApiBase::PROP_NULLABLE => true
776 )
777 ),
778 'content' => array(
779 '*' => array(
780 ApiBase::PROP_TYPE => 'string',
781 ApiBase::PROP_NULLABLE => true
782 ),
783 'texthidden' => 'boolean'
784 )
785 );
786
787 self::addTokenProperties( $props, $this->getTokenFunctions() );
788
789 return $props;
790 }
791
792 public function getDescription() {
793 return array(
794 'Get revision information',
795 'May be used in several ways:',
796 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter',
797 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params',
798 ' 3) Get data about a set of revisions by setting their IDs with revids parameter',
799 'All parameters marked as (enum) may only be used with a single page (#2)'
800 );
801 }
802
803 public function getPossibleErrors() {
804 return array_merge( parent::getPossibleErrors(), array(
805 array( 'nosuchrevid', 'diffto' ),
806 array( 'code' => 'revids', 'info' => 'The revids= parameter may not be used with the list options '
807 . '(limit, startid, endid, dirNewer, start, end).' ),
808 array( 'code' => 'multpages', 'info' => 'titles, pageids or a generator was used to supply multiple pages, '
809 . ' but the limit, startid, endid, dirNewer, user, excludeuser, '
810 . 'start and end parameters may only be used on a single page.' ),
811 array( 'code' => 'diffto', 'info' => 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"' ),
812 array( 'code' => 'badparams', 'info' => 'start and startid cannot be used together' ),
813 array( 'code' => 'badparams', 'info' => 'end and endid cannot be used together' ),
814 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
815 array( 'code' => 'nosuchsection', 'info' => 'There is no section section in rID' ),
816 array( 'code' => 'badformat', 'info' => 'The requested serialization format can not be applied '
817 . ' to the page\'s content model' ),
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&rvprop=timestamp|user|comment|content',
825 'Get last 5 revisions of the "Main Page"',
826 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
827 'Get first 5 revisions of the "Main Page"',
828 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
829 'Get first 5 revisions of the "Main Page" made after 2006-05-01',
830 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
831 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
832 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
833 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
834 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
835 );
836 }
837
838 public function getHelpUrls() {
839 return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';
840 }
841
842 public function getVersion() {
843 return __CLASS__ . ': $Id$';
844 }
845 }