3 * API for MediaWiki 1.8+
5 * Created on Sep 7, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query action to enumerate revisions of a given page, or show top revisions of multiple pages.
34 * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
35 * In the enumeration mode, ranges of revisions may be requested and filtered.
39 class ApiQueryRevisions
extends ApiQueryBase
{
41 public function __construct( $query, $moduleName ) {
42 parent
::__construct( $query, $moduleName, 'rv' );
45 private $fld_ids = false, $fld_flags = false, $fld_timestamp = false, $fld_size = false,
46 $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_content = false, $fld_tags = false;
48 protected function getTokenFunctions() {
49 // tokenname => function
50 // function prototype is func($pageid, $title, $rev)
51 // should return token or false
53 // Don't call the hooks twice
54 if ( isset( $this->tokenFunctions
) ) {
55 return $this->tokenFunctions
;
58 // If we're in JSON callback mode, no tokens can be obtained
59 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
63 $this->tokenFunctions
= array(
64 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
66 wfRunHooks( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions
) );
67 return $this->tokenFunctions
;
70 public static function getRollbackToken( $pageid, $title, $rev ) {
72 if ( !$wgUser->isAllowed( 'rollback' ) ) {
75 return $wgUser->editToken( array( $title->getPrefixedText(),
76 $rev->getUserText() ) );
79 public function execute() {
80 $params = $this->extractRequestParams( false );
82 // If any of those parameters are used, work in 'enumeration' mode.
83 // Enum mode can only be used when exactly one page is provided.
84 // Enumerating revisions on multiple pages make it extremely
85 // difficult to manage continuations and require additional SQL indexes
86 $enumRevMode = ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ||
87 !is_null( $params['limit'] ) ||
!is_null( $params['startid'] ) ||
88 !is_null( $params['endid'] ) ||
$params['dir'] === 'newer' ||
89 !is_null( $params['start'] ) ||
!is_null( $params['end'] ) );
92 $pageSet = $this->getPageSet();
93 $pageCount = $pageSet->getGoodTitleCount();
94 $revCount = $pageSet->getRevisionCount();
96 // Optimization -- nothing to do
97 if ( $revCount === 0 && $pageCount === 0 ) {
101 if ( $revCount > 0 && $enumRevMode ) {
102 $this->dieUsage( 'The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids' );
105 if ( $pageCount > 1 && $enumRevMode ) {
106 $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' );
109 $this->diffto
= $this->difftotext
= null;
110 if ( !is_null( $params['difftotext'] ) ) {
111 $this->difftotext
= $params['difftotext'];
112 } elseif ( !is_null( $params['diffto'] ) ) {
113 if ( $params['diffto'] == 'cur' ) {
114 $params['diffto'] = 0;
116 if ( ( !ctype_digit( $params['diffto'] ) ||
$params['diffto'] < 0 )
117 && $params['diffto'] != 'prev' && $params['diffto'] != 'next' )
119 $this->dieUsage( 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"', 'diffto' );
121 // Check whether the revision exists and is readable,
122 // DifferenceEngine returns a rather ambiguous empty
123 // string if that's not the case
124 if ( $params['diffto'] != 0 ) {
125 $difftoRev = Revision
::newFromID( $params['diffto'] );
127 $this->dieUsageMsg( array( 'nosuchrevid', $params['diffto'] ) );
129 if ( !$difftoRev->userCan( Revision
::DELETED_TEXT
) ) {
130 $this->setWarning( "Couldn't diff to r{$difftoRev->getID()}: content is hidden" );
131 $params['diffto'] = null;
134 $this->diffto
= $params['diffto'];
137 $db = $this->getDB();
138 $this->addTables( array( 'page', 'revision' ) );
139 $this->addFields( Revision
::selectFields() );
140 $this->addWhere( 'page_id = rev_page' );
142 $prop = array_flip( $params['prop'] );
145 $this->fld_ids
= isset ( $prop['ids'] );
146 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
147 $this->fld_flags
= isset ( $prop['flags'] );
148 $this->fld_timestamp
= isset ( $prop['timestamp'] );
149 $this->fld_comment
= isset ( $prop['comment'] );
150 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
151 $this->fld_size
= isset ( $prop['size'] );
152 $this->fld_user
= isset ( $prop['user'] );
153 $this->token
= $params['token'];
155 // Possible indexes used
158 if ( !is_null( $this->token
) ||
$pageCount > 0 ) {
159 $this->addFields( Revision
::selectPageFields() );
162 if ( isset( $prop['tags'] ) ) {
163 $this->fld_tags
= true;
164 $this->addTables( 'tag_summary' );
165 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
166 $this->addFields( 'ts_tags' );
169 if ( !is_null( $params['tag'] ) ) {
170 $this->addTables( 'change_tag' );
171 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
172 $this->addWhereFld( 'ct_tag' , $params['tag'] );
173 global $wgOldChangeTagsIndex;
174 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
177 if ( isset( $prop['content'] ) ||
!is_null( $this->difftotext
) ) {
178 // For each page we will request, the user must have read rights for that page
179 foreach ( $pageSet->getGoodTitles() as $title ) {
180 if ( !$title->userCanRead() ) {
182 'The current user is not allowed to read ' . $title->getPrefixedText(),
187 $this->addTables( 'text' );
188 $this->addWhere( 'rev_text_id=old_id' );
189 $this->addFields( 'old_id' );
190 $this->addFields( Revision
::selectTextFields() );
192 $this->fld_content
= isset( $prop['content'] );
194 $this->expandTemplates
= $params['expandtemplates'];
195 $this->generateXML
= $params['generatexml'];
196 if ( isset( $params['section'] ) ) {
197 $this->section
= $params['section'];
199 $this->section
= false;
203 $userMax = ( $this->fld_content ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
);
204 $botMax = ( $this->fld_content ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
);
205 $limit = $params['limit'];
206 if ( $limit == 'max' ) {
207 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
208 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
211 if ( $enumRevMode ) {
212 // This is mostly to prevent parameter errors (and optimize SQL?)
213 if ( !is_null( $params['startid'] ) && !is_null( $params['start'] ) ) {
214 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
217 if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) ) {
218 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
221 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
222 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
225 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
226 // the same result. This way users may request revisions starting at a given time,
227 // but to page through results use the rev_id returned after each page.
228 // Switching to rev_id removes the potential problem of having more than
229 // one row with the same timestamp for the same page.
230 // The order needs to be the same as start parameter to avoid SQL filesort.
231 if ( is_null( $params['startid'] ) && is_null( $params['endid'] ) ) {
232 $this->addWhereRange( 'rev_timestamp', $params['dir'],
233 $params['start'], $params['end'] );
235 $this->addWhereRange( 'rev_id', $params['dir'],
236 $params['startid'], $params['endid'] );
237 // One of start and end can be set
238 // If neither is set, this does nothing
239 $this->addWhereRange( 'rev_timestamp', $params['dir'],
240 $params['start'], $params['end'], false );
243 // must manually initialize unset limit
244 if ( is_null( $limit ) ) {
247 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
249 // There is only one ID, use it
250 $ids = array_keys( $pageSet->getGoodTitles() );
251 $this->addWhereFld( 'rev_page', reset( $ids ) );
253 if ( !is_null( $params['user'] ) ) {
254 $this->addWhereFld( 'rev_user_text', $params['user'] );
255 } elseif ( !is_null( $params['excludeuser'] ) ) {
256 $this->addWhere( 'rev_user_text != ' .
257 $db->addQuotes( $params['excludeuser'] ) );
259 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
260 // Paranoia: avoid brute force searches (bug 17342)
261 $this->addWhere( $db->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' );
263 } elseif ( $revCount > 0 ) {
264 $max = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
265 $revs = $pageSet->getRevisionIDs();
266 if ( self
::truncateArray( $revs, $max ) ) {
267 $this->setWarning( "Too many values supplied for parameter 'revids': the limit is $max" );
270 // Get all revision IDs
271 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
273 if ( !is_null( $params['continue'] ) ) {
274 $this->addWhere( "rev_id >= '" . intval( $params['continue'] ) . "'" );
276 $this->addOption( 'ORDER BY', 'rev_id' );
278 // assumption testing -- we should never get more then $revCount rows.
280 } elseif ( $pageCount > 0 ) {
281 $max = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
282 $titles = $pageSet->getGoodTitles();
283 if ( self
::truncateArray( $titles, $max ) ) {
284 $this->setWarning( "Too many values supplied for parameter 'titles': the limit is $max" );
287 // When working in multi-page non-enumeration mode,
288 // limit to the latest revision only
289 $this->addWhere( 'page_id=rev_page' );
290 $this->addWhere( 'page_latest=rev_id' );
293 $this->addWhereFld( 'page_id', array_keys( $titles ) );
294 // Every time someone relies on equality propagation, god kills a kitten :)
295 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
297 if ( !is_null( $params['continue'] ) ) {
298 $cont = explode( '|', $params['continue'] );
299 if ( count( $cont ) != 2 ) {
300 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
301 'value returned by the previous query', '_badcontinue' );
303 $pageid = intval( $cont[0] );
304 $revid = intval( $cont[1] );
306 "rev_page > '$pageid' OR " .
307 "(rev_page = '$pageid' AND " .
308 "rev_id >= '$revid')"
311 $this->addOption( 'ORDER BY', 'rev_page, rev_id' );
313 // assumption testing -- we should never get more then $pageCount rows.
316 ApiBase
::dieDebug( __METHOD__
, 'param validation?' );
319 $this->addOption( 'LIMIT', $limit +
1 );
320 $this->addOption( 'USE INDEX', $index );
323 $res = $this->select( __METHOD__
);
325 foreach ( $res as $row ) {
326 if ( ++
$count > $limit ) {
327 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
328 if ( !$enumRevMode ) {
329 ApiBase
::dieDebug( __METHOD__
, 'Got more rows then expected' ); // bug report
331 $this->setContinueEnumParameter( 'startid', intval( $row->rev_id
) );
335 $fit = $this->addPageSubItem( $row->rev_page
, $this->extractRowInfo( $row ), 'rev' );
337 if ( $enumRevMode ) {
338 $this->setContinueEnumParameter( 'startid', intval( $row->rev_id
) );
339 } elseif ( $revCount > 0 ) {
340 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id
) );
342 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page
) .
343 '|' . intval( $row->rev_id
) );
350 private function extractRowInfo( $row ) {
351 $revision = new Revision( $row );
352 $title = $revision->getTitle();
355 if ( $this->fld_ids
) {
356 $vals['revid'] = intval( $revision->getId() );
357 // $vals['oldid'] = intval( $row->rev_text_id ); // todo: should this be exposed?
358 if ( !is_null( $revision->getParentId() ) ) {
359 $vals['parentid'] = intval( $revision->getParentId() );
363 if ( $this->fld_flags
&& $revision->isMinor() ) {
367 if ( $this->fld_user
) {
368 if ( $revision->isDeleted( Revision
::DELETED_USER
) ) {
369 $vals['userhidden'] = '';
371 $vals['user'] = $revision->getUserText();
372 if ( !$revision->getUser() ) {
378 if ( $this->fld_timestamp
) {
379 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $revision->getTimestamp() );
382 if ( $this->fld_size
&& !is_null( $revision->getSize() ) ) {
383 $vals['size'] = intval( $revision->getSize() );
386 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
387 if ( $revision->isDeleted( Revision
::DELETED_COMMENT
) ) {
388 $vals['commenthidden'] = '';
390 $comment = $revision->getComment();
392 if ( $this->fld_comment
) {
393 $vals['comment'] = $comment;
396 if ( $this->fld_parsedcomment
) {
398 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $comment, $title );
403 if ( $this->fld_tags
) {
404 if ( $row->ts_tags
) {
405 $tags = explode( ',', $row->ts_tags
);
406 $this->getResult()->setIndexedTagName( $tags, 'tag' );
407 $vals['tags'] = $tags;
409 $vals['tags'] = array();
413 if ( !is_null( $this->token
) ) {
414 $tokenFunctions = $this->getTokenFunctions();
415 foreach ( $this->token
as $t ) {
416 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
417 if ( $val === false ) {
418 $this->setWarning( "Action '$t' is not allowed for the current user" );
420 $vals[$t . 'token'] = $val;
427 if ( $this->fld_content ||
!is_null( $this->difftotext
) ) {
428 $text = $revision->getText();
429 // Expand templates after getting section content because
430 // template-added sections don't count and Parser::preprocess()
431 // will have less input
432 if ( $this->section
!== false ) {
433 $text = $wgParser->getSection( $text, $this->section
, false );
434 if ( $text === false ) {
435 $this->dieUsage( "There is no section {$this->section} in r" . $revision->getId(), 'nosuchsection' );
439 if ( $this->fld_content
&& !$revision->isDeleted( Revision
::DELETED_TEXT
) ) {
440 if ( $this->generateXML
) {
441 $wgParser->startExternalParse( $title, new ParserOptions(), OT_PREPROCESS
);
442 $dom = $wgParser->preprocessToDom( $text );
443 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
444 $xml = $dom->saveXML();
446 $xml = $dom->__toString();
448 $vals['parsetree'] = $xml;
451 if ( $this->expandTemplates
) {
452 $text = $wgParser->preprocess( $text, $title, new ParserOptions() );
454 ApiResult
::setContent( $vals, $text );
455 } elseif ( $this->fld_content
) {
456 $vals['texthidden'] = '';
459 if ( !is_null( $this->diffto
) ||
!is_null( $this->difftotext
) ) {
460 global $wgAPIMaxUncachedDiffs;
461 static $n = 0; // Number of uncached diffs we've had
462 if ( $n < $wgAPIMaxUncachedDiffs ) {
463 $vals['diff'] = array();
464 if ( !is_null( $this->difftotext
) ) {
465 $engine = new DifferenceEngine( $title );
466 $engine->setText( $text, $this->difftotext
);
468 $engine = new DifferenceEngine( $title, $revision->getID(), $this->diffto
);
469 $vals['diff']['from'] = $engine->getOldid();
470 $vals['diff']['to'] = $engine->getNewid();
472 $difftext = $engine->getDiffBody();
473 ApiResult
::setContent( $vals['diff'], $difftext );
474 if ( !$engine->wasCacheHit() ) {
478 $vals['diff']['notcached'] = '';
484 public function getCacheMode( $params ) {
485 if ( isset( $params['token'] ) ) {
488 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
489 // formatComment() calls wfMsg() among other things
490 return 'anon-public-user-private';
495 public function getAllowedParams() {
498 ApiBase
::PARAM_ISMULTI
=> true,
499 ApiBase
::PARAM_DFLT
=> 'ids|timestamp|flags|comment|user',
500 ApiBase
::PARAM_TYPE
=> array(
513 ApiBase
::PARAM_TYPE
=> 'limit',
514 ApiBase
::PARAM_MIN
=> 1,
515 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
516 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
519 ApiBase
::PARAM_TYPE
=> 'integer'
522 ApiBase
::PARAM_TYPE
=> 'integer'
525 ApiBase
::PARAM_TYPE
=> 'timestamp'
528 ApiBase
::PARAM_TYPE
=> 'timestamp'
531 ApiBase
::PARAM_DFLT
=> 'older',
532 ApiBase
::PARAM_TYPE
=> array(
538 ApiBase
::PARAM_TYPE
=> 'user'
540 'excludeuser' => array(
541 ApiBase
::PARAM_TYPE
=> 'user'
544 'expandtemplates' => false,
545 'generatexml' => false,
548 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
549 ApiBase
::PARAM_ISMULTI
=> true
553 'difftotext' => null,
557 public function getParamDescription() {
558 $p = $this->getModulePrefix();
561 'Which properties to get for each revision:',
562 ' ids - The ID of the revision',
563 ' flags - Revision flags (minor)',
564 ' timestamp - The timestamp of the revision',
565 ' user - Gives user to make the revision',
566 ' size - Length of the revision',
567 ' comment - Comment by the user for revision',
568 ' parsedcomment - Parsed comment by the user for the revision',
569 ' content - Text of the revision',
570 ' tags - Tags for the revision',
572 'limit' => 'Limit how many revisions will be returned (enum)',
573 'startid' => 'From which revision id to start enumeration (enum)',
574 'endid' => 'Stop revision enumeration on this revid (enum)',
575 'start' => 'From which revision timestamp to start enumeration (enum)',
576 'end' => 'Enumerate up to this timestamp (enum)',
577 'dir' => 'Direction of enumeration - towards "newer" or "older" revisions (enum)',
578 'user' => 'Only include revisions made by user',
579 'excludeuser' => 'Exclude revisions made by user',
580 'expandtemplates' => 'Expand templates in revision content',
581 'generatexml' => 'Generate XML parse tree for revision content',
582 'section' => 'Only retrieve the content of this section number',
583 'token' => 'Which tokens to obtain for each revision',
584 'continue' => 'When more results are available, use this to continue',
585 'diffto' => array( 'Revision ID to diff each revision to.',
586 'Use "prev", "next" and "cur" for the previous, next and current revision respectively' ),
587 'difftotext' => array( 'Text to diff each revision to. Only diffs a limited number of revisions.',
588 "Overrides {$p}diffto. If {$p}section is set, only that section will be diffed against this text" ),
589 'tag' => 'Only list revisions tagged with this tag',
593 public function getDescription() {
595 'Get revision information',
596 'This module may be used in several ways:',
597 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter',
598 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params',
599 ' 3) Get data about a set of revisions by setting their IDs with revids parameter',
600 'All parameters marked as (enum) may only be used with a single page (#2)'
604 public function getPossibleErrors() {
605 return array_merge( parent
::getPossibleErrors(), array(
606 array( 'nosuchrevid', 'diffto' ),
607 array( 'code' => 'revids', 'info' => 'The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).' ),
608 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.' ),
609 array( 'code' => 'diffto', 'info' => 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"' ),
610 array( 'code' => 'badparams', 'info' => 'start and startid cannot be used together' ),
611 array( 'code' => 'badparams', 'info' => 'end and endid cannot be used together' ),
612 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
613 array( 'code' => 'nosuchsection', 'info' => 'There is no section section in rID' ),
617 protected function getExamples() {
619 'Get data with content for the last revision of titles "API" and "Main Page":',
620 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
621 'Get last 5 revisions of the "Main Page":',
622 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
623 'Get first 5 revisions of the "Main Page":',
624 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
625 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
626 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
627 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
628 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
629 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
630 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
634 public function getVersion() {
635 return __CLASS__
. ': $Id$';