4 * Created on Sep 7, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
32 * A query action to enumerate revisions of a given page, or show top revisions of multiple pages.
33 * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
34 * In the enumeration mode, ranges of revisions may be requested and filtered.
38 class ApiQueryRevisions
extends ApiQueryBase
{
40 public function __construct( $query, $moduleName ) {
41 parent
:: __construct( $query, $moduleName, 'rv' );
44 private $fld_ids = false, $fld_flags = false, $fld_timestamp = false, $fld_size = false,
45 $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_content = false, $fld_tags = false;
47 protected function getTokenFunctions() {
48 // tokenname => function
49 // function prototype is func($pageid, $title, $rev)
50 // should return token or false
52 // Don't call the hooks twice
53 if ( isset( $this->tokenFunctions
) )
54 return $this->tokenFunctions
;
56 // If we're in JSON callback mode, no tokens can be obtained
57 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) )
60 $this->tokenFunctions
= array(
61 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
63 wfRunHooks( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions
) );
64 return $this->tokenFunctions
;
67 public static function getRollbackToken( $pageid, $title, $rev )
70 if ( !$wgUser->isAllowed( 'rollback' ) )
72 return $wgUser->editToken( array( $title->getPrefixedText(),
73 $rev->getUserText() ) );
76 public function execute() {
77 $params = $this->extractRequestParams( false );
79 // If any of those parameters are used, work in 'enumeration' mode.
80 // Enum mode can only be used when exactly one page is provided.
81 // Enumerating revisions on multiple pages make it extremely
82 // difficult to manage continuations and require additional SQL indexes
83 $enumRevMode = ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ||
84 !is_null( $params['limit'] ) ||
!is_null( $params['startid'] ) ||
85 !is_null( $params['endid'] ) ||
$params['dir'] === 'newer' ||
86 !is_null( $params['start'] ) ||
!is_null( $params['end'] ) );
89 $pageSet = $this->getPageSet();
90 $pageCount = $pageSet->getGoodTitleCount();
91 $revCount = $pageSet->getRevisionCount();
93 // Optimization -- nothing to do
94 if ( $revCount === 0 && $pageCount === 0 )
97 if ( $revCount > 0 && $enumRevMode )
98 $this->dieUsage( 'The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids' );
100 if ( $pageCount > 1 && $enumRevMode )
101 $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' );
103 $this->diffto
= $this->difftotext
= null;
104 if ( !is_null( $params['difftotext'] ) ) {
105 $this->difftotext
= $params['difftotext'];
106 } else if ( !is_null( $params['diffto'] ) ) {
107 if ( $params['diffto'] == 'cur' )
108 $params['diffto'] = 0;
109 if ( ( !ctype_digit( $params['diffto'] ) ||
$params['diffto'] < 0 )
110 && $params['diffto'] != 'prev' && $params['diffto'] != 'next' )
111 $this->dieUsage( 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"', 'diffto' );
112 // Check whether the revision exists and is readable,
113 // DifferenceEngine returns a rather ambiguous empty
114 // string if that's not the case
115 if ( $params['diffto'] != 0 ) {
116 $difftoRev = Revision
::newFromID( $params['diffto'] );
118 $this->dieUsageMsg( array( 'nosuchrevid', $params['diffto'] ) );
119 if ( !$difftoRev->userCan( Revision
::DELETED_TEXT
) ) {
120 $this->setWarning( "Couldn't diff to r{$difftoRev->getID()}: content is hidden" );
121 $params['diffto'] = null;
124 $this->diffto
= $params['diffto'];
127 $db = $this->getDB();
128 $this->addTables( array( 'page', 'revision' ) );
129 $this->addFields( Revision
::selectFields() );
130 $this->addWhere( 'page_id = rev_page' );
132 $prop = array_flip( $params['prop'] );
135 $this->fld_ids
= isset ( $prop['ids'] );
136 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // should this be exposed?
137 $this->fld_flags
= isset ( $prop['flags'] );
138 $this->fld_timestamp
= isset ( $prop['timestamp'] );
139 $this->fld_comment
= isset ( $prop['comment'] );
140 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
141 $this->fld_size
= isset ( $prop['size'] );
142 $this->fld_user
= isset ( $prop['user'] );
143 $this->token
= $params['token'];
145 // Possible indexes used
148 if ( !is_null( $this->token
) ||
$pageCount > 0 ) {
149 $this->addFields( Revision
::selectPageFields() );
152 if ( isset ( $prop['tags'] ) ) {
153 $this->fld_tags
= true;
154 $this->addTables( 'tag_summary' );
155 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
156 $this->addFields( 'ts_tags' );
159 if ( !is_null( $params['tag'] ) ) {
160 $this->addTables( 'change_tag' );
161 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
162 $this->addWhereFld( 'ct_tag' , $params['tag'] );
163 global $wgOldChangeTagsIndex;
164 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
167 if ( isset( $prop['content'] ) ||
!is_null( $this->difftotext
) ) {
169 // For each page we will request, the user must have read rights for that page
170 foreach ( $pageSet->getGoodTitles() as $title ) {
171 if ( !$title->userCanRead() )
173 'The current user is not allowed to read ' . $title->getPrefixedText(),
177 $this->addTables( 'text' );
178 $this->addWhere( 'rev_text_id=old_id' );
179 $this->addFields( 'old_id' );
180 $this->addFields( Revision
::selectTextFields() );
182 $this->fld_content
= isset( $prop['content'] );
184 $this->expandTemplates
= $params['expandtemplates'];
185 $this->generateXML
= $params['generatexml'];
186 if ( isset( $params['section'] ) )
187 $this->section
= $params['section'];
189 $this->section
= false;
192 $userMax = ( $this->fld_content ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
);
193 $botMax = ( $this->fld_content ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
);
194 $limit = $params['limit'];
195 if ( $limit == 'max' ) {
196 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
197 $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
200 if ( $enumRevMode ) {
202 // This is mostly to prevent parameter errors (and optimize SQL?)
203 if ( !is_null( $params['startid'] ) && !is_null( $params['start'] ) )
204 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
206 if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) )
207 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
209 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) )
210 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
212 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
213 // the same result. This way users may request revisions starting at a given time,
214 // but to page through results use the rev_id returned after each page.
215 // Switching to rev_id removes the potential problem of having more than
216 // one row with the same timestamp for the same page.
217 // The order needs to be the same as start parameter to avoid SQL filesort.
219 if ( is_null( $params['startid'] ) && is_null( $params['endid'] ) )
220 $this->addWhereRange( 'rev_timestamp', $params['dir'],
221 $params['start'], $params['end'] );
223 $this->addWhereRange( 'rev_id', $params['dir'],
224 $params['startid'], $params['endid'] );
225 // One of start and end can be set
226 // If neither is set, this does nothing
227 $this->addWhereRange( 'rev_timestamp', $params['dir'],
228 $params['start'], $params['end'], false );
231 // must manually initialize unset limit
232 if ( is_null( $limit ) )
234 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
236 // There is only one ID, use it
237 $ids = array_keys( $pageSet->getGoodTitles() );
238 $this->addWhereFld( 'rev_page', reset( $ids ) );
240 if ( !is_null( $params['user'] ) ) {
241 $this->addWhereFld( 'rev_user_text', $params['user'] );
242 } elseif ( !is_null( $params['excludeuser'] ) ) {
243 $this->addWhere( 'rev_user_text != ' .
244 $db->addQuotes( $params['excludeuser'] ) );
246 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
247 // Paranoia: avoid brute force searches (bug 17342)
248 $this->addWhere( $db->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' );
251 elseif ( $revCount > 0 ) {
252 $max = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
253 $revs = $pageSet->getRevisionIDs();
254 if ( self
::truncateArray( $revs, $max ) )
255 $this->setWarning( "Too many values supplied for parameter 'revids': the limit is $max" );
257 // Get all revision IDs
258 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
260 if ( !is_null( $params['continue'] ) )
261 $this->addWhere( "rev_id >= '" . intval( $params['continue'] ) . "'" );
262 $this->addOption( 'ORDER BY', 'rev_id' );
264 // assumption testing -- we should never get more then $revCount rows.
267 elseif ( $pageCount > 0 ) {
268 $max = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
269 $titles = $pageSet->getGoodTitles();
270 if ( self
::truncateArray( $titles, $max ) )
271 $this->setWarning( "Too many values supplied for parameter 'titles': the limit is $max" );
273 // When working in multi-page non-enumeration mode,
274 // limit to the latest revision only
275 $this->addWhere( 'page_id=rev_page' );
276 $this->addWhere( 'page_latest=rev_id' );
279 $this->addWhereFld( 'page_id', array_keys( $titles ) );
280 // Every time someone relies on equality propagation, god kills a kitten :)
281 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
283 if ( !is_null( $params['continue'] ) )
285 $cont = explode( '|', $params['continue'] );
286 if ( count( $cont ) != 2 )
287 $this->dieUsage( "Invalid continue param. You should pass the original " .
288 "value returned by the previous query", "_badcontinue" );
289 $pageid = intval( $cont[0] );
290 $revid = intval( $cont[1] );
291 $this->addWhere( "rev_page > '$pageid' OR " .
292 "(rev_page = '$pageid' AND " .
293 "rev_id >= '$revid')" );
295 $this->addOption( 'ORDER BY', 'rev_page, rev_id' );
297 // assumption testing -- we should never get more then $pageCount rows.
300 ApiBase
:: dieDebug( __METHOD__
, 'param validation?' );
302 $this->addOption( 'LIMIT', $limit +
1 );
303 $this->addOption( 'USE INDEX', $index );
307 $res = $this->select( __METHOD__
);
309 while ( $row = $db->fetchObject( $res ) ) {
311 if ( ++
$count > $limit ) {
312 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
314 ApiBase
:: dieDebug( __METHOD__
, 'Got more rows then expected' ); // bug report
315 $this->setContinueEnumParameter( 'startid', intval( $row->rev_id
) );
320 $fit = $this->addPageSubItem( $row->rev_page
, $this->extractRowInfo( $row ), 'rev' );
324 $this->setContinueEnumParameter( 'startid', intval( $row->rev_id
) );
325 else if ( $revCount > 0 )
326 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id
) );
328 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page
) .
329 '|' . intval( $row->rev_id
) );
333 $db->freeResult( $res );
336 private function extractRowInfo( $row ) {
337 $revision = new Revision( $row );
338 $title = $revision->getTitle();
341 if ( $this->fld_ids
) {
342 $vals['revid'] = intval( $revision->getId() );
343 // $vals['oldid'] = intval($row->rev_text_id); // todo: should this be exposed?
344 if ( !is_null( $revision->getParentId() ) )
345 $vals['parentid'] = intval( $revision->getParentId() );
348 if ( $this->fld_flags
&& $revision->isMinor() )
351 if ( $this->fld_user
) {
352 if ( $revision->isDeleted( Revision
::DELETED_USER
) ) {
353 $vals['userhidden'] = '';
355 $vals['user'] = $revision->getUserText();
356 if ( !$revision->getUser() )
361 if ( $this->fld_timestamp
) {
362 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $revision->getTimestamp() );
365 if ( $this->fld_size
&& !is_null( $revision->getSize() ) ) {
366 $vals['size'] = intval( $revision->getSize() );
369 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
370 if ( $revision->isDeleted( Revision
::DELETED_COMMENT
) ) {
371 $vals['commenthidden'] = '';
373 $comment = $revision->getComment();
374 if ( strval( $comment ) !== '' )
376 if ( $this->fld_comment
)
377 $vals['comment'] = $comment;
379 if ( $this->fld_parsedcomment
) {
381 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $comment, $title );
387 if ( $this->fld_tags
) {
388 if ( $row->ts_tags
) {
389 $tags = explode( ',', $row->ts_tags
);
390 $this->getResult()->setIndexedTagName( $tags, 'tag' );
391 $vals['tags'] = $tags;
393 $vals['tags'] = array();
397 if ( !is_null( $this->token
) )
399 $tokenFunctions = $this->getTokenFunctions();
400 foreach ( $this->token
as $t )
402 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
403 if ( $val === false )
404 $this->setWarning( "Action '$t' is not allowed for the current user" );
406 $vals[$t . 'token'] = $val;
411 if ( $this->fld_content ||
!is_null( $this->difftotext
) ) {
413 $text = $revision->getText();
414 // Expand templates after getting section content because
415 // template-added sections don't count and Parser::preprocess()
416 // will have less input
417 if ( $this->section
!== false ) {
418 $text = $wgParser->getSection( $text, $this->section
, false );
419 if ( $text === false )
420 $this->dieUsage( "There is no section {$this->section} in r" . $revision->getId(), 'nosuchsection' );
423 if ( $this->fld_content
&& !$revision->isDeleted( Revision
::DELETED_TEXT
) ) {
424 if ( $this->generateXML
) {
425 $wgParser->startExternalParse( $title, new ParserOptions(), OT_PREPROCESS
);
426 $dom = $wgParser->preprocessToDom( $text );
427 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
428 $xml = $dom->saveXML();
430 $xml = $dom->__toString();
432 $vals['parsetree'] = $xml;
435 if ( $this->expandTemplates
) {
436 $text = $wgParser->preprocess( $text, $title, new ParserOptions() );
438 ApiResult
:: setContent( $vals, $text );
439 } else if ( $this->fld_content
) {
440 $vals['texthidden'] = '';
443 if ( !is_null( $this->diffto
) ||
!is_null( $this->difftotext
) ) {
444 global $wgAPIMaxUncachedDiffs;
445 static $n = 0; // Number of uncached diffs we've had
446 if ( $n < $wgAPIMaxUncachedDiffs ) {
447 $vals['diff'] = array();
448 if ( !is_null( $this->difftotext
) ) {
449 $engine = new DifferenceEngine( $title );
450 $engine->setText( $text, $this->difftotext
);
452 $engine = new DifferenceEngine( $title, $revision->getID(), $this->diffto
);
453 $vals['diff']['from'] = $engine->getOldid();
454 $vals['diff']['to'] = $engine->getNewid();
456 $difftext = $engine->getDiffBody();
457 ApiResult
::setContent( $vals['diff'], $difftext );
458 if ( !$engine->wasCacheHit() )
461 $vals['diff']['notcached'] = '';
467 public function getAllowedParams() {
470 ApiBase
:: PARAM_ISMULTI
=> true,
471 ApiBase
:: PARAM_DFLT
=> 'ids|timestamp|flags|comment|user',
472 ApiBase
:: PARAM_TYPE
=> array (
485 ApiBase
:: PARAM_TYPE
=> 'limit',
486 ApiBase
:: PARAM_MIN
=> 1,
487 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
488 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
491 ApiBase
:: PARAM_TYPE
=> 'integer'
494 ApiBase
:: PARAM_TYPE
=> 'integer'
497 ApiBase
:: PARAM_TYPE
=> 'timestamp'
500 ApiBase
:: PARAM_TYPE
=> 'timestamp'
503 ApiBase
:: PARAM_DFLT
=> 'older',
504 ApiBase
:: PARAM_TYPE
=> array (
510 ApiBase
:: PARAM_TYPE
=> 'user'
512 'excludeuser' => array(
513 ApiBase
:: PARAM_TYPE
=> 'user'
516 'expandtemplates' => false,
517 'generatexml' => false,
520 ApiBase
:: PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
521 ApiBase
:: PARAM_ISMULTI
=> true
525 'difftotext' => null,
529 public function getParamDescription() {
531 'prop' => 'Which properties to get for each revision.',
532 'limit' => 'Limit how many revisions will be returned (enum)',
533 'startid' => 'From which revision id to start enumeration (enum)',
534 'endid' => 'Stop revision enumeration on this revid (enum)',
535 'start' => 'From which revision timestamp to start enumeration (enum)',
536 'end' => 'Enumerate up to this timestamp (enum)',
537 'dir' => 'Direction of enumeration - towards "newer" or "older" revisions (enum)',
538 'user' => 'Only include revisions made by user',
539 'excludeuser' => 'Exclude revisions made by user',
540 'expandtemplates' => 'Expand templates in revision content',
541 'generatexml' => 'Generate XML parse tree for revision content',
542 'section' => 'Only retrieve the content of this section',
543 'token' => 'Which tokens to obtain for each revision',
544 'continue' => 'When more results are available, use this to continue',
545 'diffto' => array( 'Revision ID to diff each revision to.',
546 'Use "prev", "next" and "cur" for the previous, next and current revision respectively.' ),
547 'difftotext' => array( 'Text to diff each revision to. Only diffs a limited number of revisions.',
548 'Overrides diffto. If rvsection is set, only that section will be diffed against this text.' ),
549 'tag' => 'Only list revisions tagged with this tag',
553 public function getDescription() {
555 'Get revision information.',
556 'This module may be used in several ways:',
557 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
558 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
559 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
560 'All parameters marked as (enum) may only be used with a single page (#2).'
564 public function getPossibleErrors() {
565 return array_merge( parent
::getPossibleErrors(), array(
566 array( 'nosuchrevid', 'diffto' ),
567 array( 'code' => 'revids', 'info' => 'The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).' ),
568 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.' ),
569 array( 'code' => 'diffto', 'info' => 'rvdiffto must be set to a non-negative number, "prev", "next" or "cur"' ),
570 array( 'code' => 'badparams', 'info' => 'start and startid cannot be used together' ),
571 array( 'code' => 'badparams', 'info' => 'end and endid cannot be used together' ),
572 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
573 array( 'code' => 'nosuchsection', 'info' => 'There is no section section in rID' ),
577 protected function getExamples() {
579 'Get data with content for the last revision of titles "API" and "Main Page":',
580 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
581 'Get last 5 revisions of the "Main Page":',
582 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
583 'Get first 5 revisions of the "Main Page":',
584 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
585 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
586 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
587 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
588 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
589 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
590 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
594 public function getVersion() {
595 return __CLASS__
. ': $Id$';