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