ParamDescriptions for ApiQueryRevisions were inconsistently starting with upper and...
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
1 <?php
2
3 /*
4 * Created on Sep 7, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
29 }
30
31 /**
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.
35 *
36 * @ingroup API
37 */
38 class ApiQueryRevisions extends ApiQueryBase {
39
40 public function __construct( $query, $moduleName ) {
41 parent :: __construct( $query, $moduleName, 'rv' );
42 }
43
44 private $fld_ids = false, $fld_flags = false, $fld_timestamp = false, $fld_size = false,
45 $fld_comment = false, $fld_user = false, $fld_content = false, $fld_tags = false;
46
47 protected function getTokenFunctions() {
48 // tokenname => function
49 // function prototype is func($pageid, $title, $rev)
50 // should return token or false
51
52 // Don't call the hooks twice
53 if ( isset( $this->tokenFunctions ) )
54 return $this->tokenFunctions;
55
56 // If we're in JSON callback mode, no tokens can be obtained
57 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) )
58 return array();
59
60 $this->tokenFunctions = array(
61 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
62 );
63 wfRunHooks( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions ) );
64 return $this->tokenFunctions;
65 }
66
67 public static function getRollbackToken( $pageid, $title, $rev )
68 {
69 global $wgUser;
70 if ( !$wgUser->isAllowed( 'rollback' ) )
71 return false;
72 return $wgUser->editToken( array( $title->getPrefixedText(),
73 $rev->getUserText() ) );
74 }
75
76 public function execute() {
77 $params = $this->extractRequestParams( false );
78
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'] ) );
87
88
89 $pageSet = $this->getPageSet();
90 $pageCount = $pageSet->getGoodTitleCount();
91 $revCount = $pageSet->getRevisionCount();
92
93 // Optimization -- nothing to do
94 if ( $revCount === 0 && $pageCount === 0 )
95 return;
96
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' );
99
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' );
102
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'] );
117 if ( !$difftoRev )
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;
122 }
123 }
124 $this->diffto = $params['diffto'];
125 }
126
127 $db = $this->getDB();
128 $this->addTables( array( 'page', 'revision' ) );
129 $this->addFields( Revision::selectFields() );
130 $this->addWhere( 'page_id = rev_page' );
131
132 $prop = array_flip( $params['prop'] );
133
134 // Optional fields
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_size = isset ( $prop['size'] );
141 $this->fld_user = isset ( $prop['user'] );
142 $this->token = $params['token'];
143
144 if ( !is_null( $this->token ) || $pageCount > 0 ) {
145 $this->addFields( Revision::selectPageFields() );
146 }
147
148 if ( isset ( $prop['tags'] ) ) {
149 $this->fld_tags = true;
150 $this->addTables( 'tag_summary' );
151 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
152 $this->addFields( 'ts_tags' );
153 }
154
155 if ( !is_null( $params['tag'] ) ) {
156 $this->addTables( 'change_tag' );
157 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
158 $this->addWhereFld( 'ct_tag' , $params['tag'] );
159 }
160
161 if ( isset( $prop['content'] ) || !is_null( $this->difftotext ) ) {
162
163 // For each page we will request, the user must have read rights for that page
164 foreach ( $pageSet->getGoodTitles() as $title ) {
165 if ( !$title->userCanRead() )
166 $this->dieUsage(
167 'The current user is not allowed to read ' . $title->getPrefixedText(),
168 'accessdenied' );
169 }
170
171 $this->addTables( 'text' );
172 $this->addWhere( 'rev_text_id=old_id' );
173 $this->addFields( 'old_id' );
174 $this->addFields( Revision::selectTextFields() );
175
176 $this->fld_content = isset( $prop['content'] );
177
178 $this->expandTemplates = $params['expandtemplates'];
179 $this->generateXML = $params['generatexml'];
180 if ( isset( $params['section'] ) )
181 $this->section = $params['section'];
182 else
183 $this->section = false;
184 }
185
186 $userMax = ( $this->fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
187 $botMax = ( $this->fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
188 $limit = $params['limit'];
189 if ( $limit == 'max' ) {
190 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
191 $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
192 }
193
194 if ( $enumRevMode ) {
195
196 // This is mostly to prevent parameter errors (and optimize SQL?)
197 if ( !is_null( $params['startid'] ) && !is_null( $params['start'] ) )
198 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
199
200 if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) )
201 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
202
203 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) )
204 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
205
206 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
207 // the same result. This way users may request revisions starting at a given time,
208 // but to page through results use the rev_id returned after each page.
209 // Switching to rev_id removes the potential problem of having more than
210 // one row with the same timestamp for the same page.
211 // The order needs to be the same as start parameter to avoid SQL filesort.
212
213 if ( is_null( $params['startid'] ) && is_null( $params['endid'] ) )
214 $this->addWhereRange( 'rev_timestamp', $params['dir'],
215 $params['start'], $params['end'] );
216 else {
217 $this->addWhereRange( 'rev_id', $params['dir'],
218 $params['startid'], $params['endid'] );
219 // One of start and end can be set
220 // If neither is set, this does nothing
221 $this->addWhereRange( 'rev_timestamp', $params['dir'],
222 $params['start'], $params['end'], false );
223 }
224
225 // must manually initialize unset limit
226 if ( is_null( $limit ) )
227 $limit = 10;
228 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
229
230 // There is only one ID, use it
231 $ids = array_keys( $pageSet->getGoodTitles() );
232 $this->addWhereFld( 'rev_page', reset( $ids ) );
233
234 if ( !is_null( $params['user'] ) ) {
235 $this->addWhereFld( 'rev_user_text', $params['user'] );
236 } elseif ( !is_null( $params['excludeuser'] ) ) {
237 $this->addWhere( 'rev_user_text != ' .
238 $db->addQuotes( $params['excludeuser'] ) );
239 }
240 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
241 // Paranoia: avoid brute force searches (bug 17342)
242 $this->addWhere( $db->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
243 }
244 }
245 elseif ( $revCount > 0 ) {
246 $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
247 $revs = $pageSet->getRevisionIDs();
248 if ( self::truncateArray( $revs, $max ) )
249 $this->setWarning( "Too many values supplied for parameter 'revids': the limit is $max" );
250
251 // Get all revision IDs
252 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
253
254 if ( !is_null( $params['continue'] ) )
255 $this->addWhere( "rev_id >= '" . intval( $params['continue'] ) . "'" );
256 $this->addOption( 'ORDER BY', 'rev_id' );
257
258 // assumption testing -- we should never get more then $revCount rows.
259 $limit = $revCount;
260 }
261 elseif ( $pageCount > 0 ) {
262 $max = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
263 $titles = $pageSet->getGoodTitles();
264 if ( self::truncateArray( $titles, $max ) )
265 $this->setWarning( "Too many values supplied for parameter 'titles': the limit is $max" );
266
267 // When working in multi-page non-enumeration mode,
268 // limit to the latest revision only
269 $this->addWhere( 'page_id=rev_page' );
270 $this->addWhere( 'page_latest=rev_id' );
271
272 // Get all page IDs
273 $this->addWhereFld( 'page_id', array_keys( $titles ) );
274 // Every time someone relies on equality propagation, god kills a kitten :)
275 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
276
277 if ( !is_null( $params['continue'] ) )
278 {
279 $cont = explode( '|', $params['continue'] );
280 if ( count( $cont ) != 2 )
281 $this->dieUsage( "Invalid continue param. You should pass the original " .
282 "value returned by the previous query", "_badcontinue" );
283 $pageid = intval( $cont[0] );
284 $revid = intval( $cont[1] );
285 $this->addWhere( "rev_page > '$pageid' OR " .
286 "(rev_page = '$pageid' AND " .
287 "rev_id >= '$revid')" );
288 }
289 $this->addOption( 'ORDER BY', 'rev_page, rev_id' );
290
291 // assumption testing -- we should never get more then $pageCount rows.
292 $limit = $pageCount;
293 } else
294 ApiBase :: dieDebug( __METHOD__, 'param validation?' );
295
296 $this->addOption( 'LIMIT', $limit + 1 );
297
298 $data = array ();
299 $count = 0;
300 $res = $this->select( __METHOD__ );
301
302 while ( $row = $db->fetchObject( $res ) ) {
303
304 if ( ++ $count > $limit ) {
305 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
306 if ( !$enumRevMode )
307 ApiBase :: dieDebug( __METHOD__, 'Got more rows then expected' ); // bug report
308 $this->setContinueEnumParameter( 'startid', intval( $row->rev_id ) );
309 break;
310 }
311
312 //
313 $fit = $this->addPageSubItem( $row->rev_page, $this->extractRowInfo( $row ), 'rev' );
314 if ( !$fit )
315 {
316 if ( $enumRevMode )
317 $this->setContinueEnumParameter( 'startid', intval( $row->rev_id ) );
318 else if ( $revCount > 0 )
319 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
320 else
321 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
322 '|' . intval( $row->rev_id ) );
323 break;
324 }
325 }
326 $db->freeResult( $res );
327 }
328
329 private function extractRowInfo( $row ) {
330 $revision = new Revision( $row );
331 $title = $revision->getTitle();
332 $vals = array ();
333
334 if ( $this->fld_ids ) {
335 $vals['revid'] = intval( $revision->getId() );
336 // $vals['oldid'] = intval($row->rev_text_id); // todo: should this be exposed?
337 if ( !is_null( $revision->getParentId() ) )
338 $vals['parentid'] = intval( $revision->getParentId() );
339 }
340
341 if ( $this->fld_flags && $revision->isMinor() )
342 $vals['minor'] = '';
343
344 if ( $this->fld_user ) {
345 if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
346 $vals['userhidden'] = '';
347 } else {
348 $vals['user'] = $revision->getUserText();
349 if ( !$revision->getUser() )
350 $vals['anon'] = '';
351 }
352 }
353
354 if ( $this->fld_timestamp ) {
355 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
356 }
357
358 if ( $this->fld_size && !is_null( $revision->getSize() ) ) {
359 $vals['size'] = intval( $revision->getSize() );
360 }
361
362 if ( $this->fld_comment ) {
363 if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
364 $vals['commenthidden'] = '';
365 } else {
366 $comment = $revision->getComment();
367 if ( strval( $comment ) !== '' )
368 $vals['comment'] = $comment;
369 }
370 }
371
372 if ( $this->fld_tags ) {
373 if ( $row->ts_tags ) {
374 $tags = explode( ',', $row->ts_tags );
375 $this->getResult()->setIndexedTagName( $tags, 'tag' );
376 $vals['tags'] = $tags;
377 } else {
378 $vals['tags'] = array();
379 }
380 }
381
382 if ( !is_null( $this->token ) )
383 {
384 $tokenFunctions = $this->getTokenFunctions();
385 foreach ( $this->token as $t )
386 {
387 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
388 if ( $val === false )
389 $this->setWarning( "Action '$t' is not allowed for the current user" );
390 else
391 $vals[$t . 'token'] = $val;
392 }
393 }
394
395 $text = null;
396 if ( $this->fld_content || !is_null( $this->difftotext ) ) {
397 global $wgParser;
398 $text = $revision->getText();
399 // Expand templates after getting section content because
400 // template-added sections don't count and Parser::preprocess()
401 // will have less input
402 if ( $this->section !== false ) {
403 $text = $wgParser->getSection( $text, $this->section, false );
404 if ( $text === false )
405 $this->dieUsage( "There is no section {$this->section} in r" . $revision->getId(), 'nosuchsection' );
406 }
407 }
408 if ( $this->fld_content && !$revision->isDeleted( Revision::DELETED_TEXT ) ) {
409 if ( $this->generateXML ) {
410 $wgParser->startExternalParse( $title, new ParserOptions(), OT_PREPROCESS );
411 $dom = $wgParser->preprocessToDom( $text );
412 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
413 $xml = $dom->saveXML();
414 } else {
415 $xml = $dom->__toString();
416 }
417 $vals['parsetree'] = $xml;
418
419 }
420 if ( $this->expandTemplates ) {
421 $text = $wgParser->preprocess( $text, $title, new ParserOptions() );
422 }
423 ApiResult :: setContent( $vals, $text );
424 } else if ( $this->fld_content ) {
425 $vals['texthidden'] = '';
426 }
427
428 if ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) {
429 global $wgAPIMaxUncachedDiffs;
430 static $n = 0; // Number of uncached diffs we've had
431 if ( $n < $wgAPIMaxUncachedDiffs ) {
432 $vals['diff'] = array();
433 if ( !is_null( $this->difftotext ) ) {
434 $engine = new DifferenceEngine( $title );
435 $engine->setText( $text, $this->difftotext );
436 } else {
437 $engine = new DifferenceEngine( $title, $revision->getID(), $this->diffto );
438 $vals['diff']['from'] = $engine->getOldid();
439 $vals['diff']['to'] = $engine->getNewid();
440 }
441 $difftext = $engine->getDiffBody();
442 ApiResult::setContent( $vals['diff'], $difftext );
443 if ( !$engine->wasCacheHit() )
444 $n++;
445 } else {
446 $vals['diff']['notcached'] = '';
447 }
448 }
449 return $vals;
450 }
451
452 public function getAllowedParams() {
453 return array (
454 'prop' => array (
455 ApiBase :: PARAM_ISMULTI => true,
456 ApiBase :: PARAM_DFLT => 'ids|timestamp|flags|comment|user',
457 ApiBase :: PARAM_TYPE => array (
458 'ids',
459 'flags',
460 'timestamp',
461 'user',
462 'size',
463 'comment',
464 'content',
465 'tags'
466 )
467 ),
468 'limit' => array (
469 ApiBase :: PARAM_TYPE => 'limit',
470 ApiBase :: PARAM_MIN => 1,
471 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
472 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
473 ),
474 'startid' => array (
475 ApiBase :: PARAM_TYPE => 'integer'
476 ),
477 'endid' => array (
478 ApiBase :: PARAM_TYPE => 'integer'
479 ),
480 'start' => array (
481 ApiBase :: PARAM_TYPE => 'timestamp'
482 ),
483 'end' => array (
484 ApiBase :: PARAM_TYPE => 'timestamp'
485 ),
486 'dir' => array (
487 ApiBase :: PARAM_DFLT => 'older',
488 ApiBase :: PARAM_TYPE => array (
489 'newer',
490 'older'
491 )
492 ),
493 'user' => array(
494 ApiBase :: PARAM_TYPE => 'user'
495 ),
496 'excludeuser' => array(
497 ApiBase :: PARAM_TYPE => 'user'
498 ),
499 'tag' => null,
500 'expandtemplates' => false,
501 'generatexml' => false,
502 'section' => null,
503 'token' => array(
504 ApiBase :: PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
505 ApiBase :: PARAM_ISMULTI => true
506 ),
507 'continue' => null,
508 'diffto' => null,
509 'difftotext' => null,
510 );
511 }
512
513 public function getParamDescription() {
514 return array (
515 'prop' => 'Which properties to get for each revision.',
516 'limit' => 'Limit how many revisions will be returned (enum)',
517 'startid' => 'From which revision id to start enumeration (enum)',
518 'endid' => 'Stop revision enumeration on this revid (enum)',
519 'start' => 'From which revision timestamp to start enumeration (enum)',
520 'end' => 'Enumerate up to this timestamp (enum)',
521 'dir' => 'Direction of enumeration - towards "newer" or "older" revisions (enum)',
522 'user' => 'Only include revisions made by user',
523 'excludeuser' => 'Exclude revisions made by user',
524 'expandtemplates' => 'Expand templates in revision content',
525 'generatexml' => 'Generate XML parse tree for revision content',
526 'section' => 'Only retrieve the content of this section',
527 'token' => 'Which tokens to obtain for each revision',
528 'continue' => 'When more results are available, use this to continue',
529 'diffto' => array( 'Revision ID to diff each revision to.',
530 'Use "prev", "next" and "cur" for the previous, next and current revision respectively.' ),
531 'difftotext' => array( 'Text to diff each revision to. Only diffs a limited number of revisions.',
532 'Overrides diffto. If rvsection is set, only that section will be diffed against this text.' ),
533 'tag' => 'Only list revisions tagged with this tag',
534 );
535 }
536
537 public function getDescription() {
538 return array (
539 'Get revision information.',
540 'This module may be used in several ways:',
541 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
542 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
543 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
544 'All parameters marked as (enum) may only be used with a single page (#2).'
545 );
546 }
547
548 protected function getExamples() {
549 return array (
550 'Get data with content for the last revision of titles "API" and "Main Page":',
551 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
552 'Get last 5 revisions of the "Main Page":',
553 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
554 'Get first 5 revisions of the "Main Page":',
555 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
556 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
557 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
558 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
559 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
560 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
561 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
562 );
563 }
564
565 public function getVersion() {
566 return __CLASS__ . ': $Id$';
567 }
568 }