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