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