wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / api / ApiQueryRevisions.php
blob0282fc593c8b38cd719f5dc25edb5fcd1e0fe3a3
1 <?php
2 /**
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
24 * @file
27 /**
28 * A query action to enumerate revisions of a given page, or show top revisions
29 * of multiple pages. Various pieces of information may be shown - flags,
30 * comments, and the actual wiki markup of the rev. In the enumeration mode,
31 * ranges of revisions may be requested and filtered.
33 * @ingroup API
35 class ApiQueryRevisions extends ApiQueryRevisionsBase {
37 private $token = null;
39 public function __construct( ApiQuery $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'rv' );
43 private $tokenFunctions;
45 /** @deprecated since 1.24 */
46 protected function getTokenFunctions() {
47 // tokenname => function
48 // function prototype is func($pageid, $title, $rev)
49 // should return token or false
51 // Don't call the hooks twice
52 if ( isset( $this->tokenFunctions ) ) {
53 return $this->tokenFunctions;
56 // If we're in a mode that breaks the same-origin policy, no tokens can
57 // be obtained
58 if ( $this->lacksSameOriginSecurity() ) {
59 return array();
62 $this->tokenFunctions = array(
63 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
65 Hooks::run( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions ) );
67 return $this->tokenFunctions;
70 /**
71 * @deprecated since 1.24
72 * @param int $pageid
73 * @param Title $title
74 * @param Revision $rev
75 * @return bool|string
77 public static function getRollbackToken( $pageid, $title, $rev ) {
78 global $wgUser;
79 if ( !$wgUser->isAllowed( 'rollback' ) ) {
80 return false;
83 return $wgUser->getEditToken(
84 array( $title->getPrefixedText(), $rev->getUserText() ) );
87 protected function run( ApiPageSet $resultPageSet = null ) {
88 $params = $this->extractRequestParams( false );
90 // If any of those parameters are used, work in 'enumeration' mode.
91 // Enum mode can only be used when exactly one page is provided.
92 // Enumerating revisions on multiple pages make it extremely
93 // difficult to manage continuations and require additional SQL indexes
94 $enumRevMode = ( $params['user'] !== null || $params['excludeuser'] !== null ||
95 $params['limit'] !== null || $params['startid'] !== null ||
96 $params['endid'] !== null || $params['dir'] === 'newer' ||
97 $params['start'] !== null || $params['end'] !== null );
99 $pageSet = $this->getPageSet();
100 $pageCount = $pageSet->getGoodTitleCount();
101 $revCount = $pageSet->getRevisionCount();
103 // Optimization -- nothing to do
104 if ( $revCount === 0 && $pageCount === 0 ) {
105 // Nothing to do
106 return;
108 if ( $revCount > 0 && count( $pageSet->getLiveRevisionIDs() ) === 0 ) {
109 // We're in revisions mode but all given revisions are deleted
110 return;
113 if ( $revCount > 0 && $enumRevMode ) {
114 $this->dieUsage(
115 'The revids= parameter may not be used with the list options ' .
116 '(limit, startid, endid, dirNewer, start, end).',
117 'revids'
121 if ( $pageCount > 1 && $enumRevMode ) {
122 $this->dieUsage(
123 'titles, pageids or a generator was used to supply multiple pages, ' .
124 'but the limit, startid, endid, dirNewer, user, excludeuser, start ' .
125 'and end parameters may only be used on a single page.',
126 'multpages'
130 // In non-enum mode, rvlimit can't be directly used. Use the maximum
131 // allowed value.
132 if ( !$enumRevMode ) {
133 $this->setParsedLimit = false;
134 $params['limit'] = 'max';
137 $db = $this->getDB();
138 $this->addTables( array( 'revision', 'page' ) );
139 $this->addJoinConds(
140 array( 'page' => array( 'INNER JOIN', array( 'page_id = rev_page' ) ) )
143 if ( $resultPageSet === null ) {
144 $this->parseParameters( $params );
145 $this->token = $params['token'];
146 $this->addFields( Revision::selectFields() );
147 if ( $this->token !== null || $pageCount > 0 ) {
148 $this->addFields( Revision::selectPageFields() );
150 } else {
151 $this->limit = $this->getParameter( 'limit' ) ?: 10;
152 $this->addFields( array( 'rev_id', 'rev_timestamp', 'rev_page' ) );
155 if ( $this->fld_tags ) {
156 $this->addTables( 'tag_summary' );
157 $this->addJoinConds(
158 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
160 $this->addFields( 'ts_tags' );
163 if ( $params['tag'] !== null ) {
164 $this->addTables( 'change_tag' );
165 $this->addJoinConds(
166 array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
168 $this->addWhereFld( 'ct_tag', $params['tag'] );
171 if ( $this->fetchContent ) {
172 // For each page we will request, the user must have read rights for that page
173 $user = $this->getUser();
174 /** @var $title Title */
175 foreach ( $pageSet->getGoodTitles() as $title ) {
176 if ( !$title->userCan( 'read', $user ) ) {
177 $this->dieUsage(
178 'The current user is not allowed to read ' . $title->getPrefixedText(),
179 'accessdenied' );
183 $this->addTables( 'text' );
184 $this->addJoinConds(
185 array( 'text' => array( 'INNER JOIN', array( 'rev_text_id=old_id' ) ) )
187 $this->addFields( 'old_id' );
188 $this->addFields( Revision::selectTextFields() );
191 // add user name, if needed
192 if ( $this->fld_user ) {
193 $this->addTables( 'user' );
194 $this->addJoinConds( array( 'user' => Revision::userJoinCond() ) );
195 $this->addFields( Revision::selectUserFields() );
198 if ( $enumRevMode ) {
199 // Indexes targeted:
200 // page_timestamp if we don't have rvuser
201 // page_user_timestamp if we have a logged-in rvuser
202 // page_timestamp or usertext_timestamp if we have an IP rvuser
204 // This is mostly to prevent parameter errors (and optimize SQL?)
205 if ( $params['startid'] !== null && $params['start'] !== null ) {
206 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
209 if ( $params['endid'] !== null && $params['end'] !== null ) {
210 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
213 if ( $params['user'] !== null && $params['excludeuser'] !== null ) {
214 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
217 if ( $params['continue'] !== null ) {
218 $cont = explode( '|', $params['continue'] );
219 $this->dieContinueUsageIf( count( $cont ) != 2 );
220 $op = ( $params['dir'] === 'newer' ? '>' : '<' );
221 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
222 $continueId = (int)$cont[1];
223 $this->dieContinueUsageIf( $continueId != $cont[1] );
224 $this->addWhere( "rev_timestamp $op $continueTimestamp OR " .
225 "(rev_timestamp = $continueTimestamp AND " .
226 "rev_id $op= $continueId)"
230 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
231 $params['start'], $params['end'] );
232 $this->addWhereRange( 'rev_id', $params['dir'],
233 $params['startid'], $params['endid'] );
235 // There is only one ID, use it
236 $ids = array_keys( $pageSet->getGoodTitles() );
237 $this->addWhereFld( 'rev_page', reset( $ids ) );
239 if ( $params['user'] !== null ) {
240 $user = User::newFromName( $params['user'] );
241 if ( $user && $user->getId() > 0 ) {
242 $this->addWhereFld( 'rev_user', $user->getId() );
243 } else {
244 $this->addWhereFld( 'rev_user_text', $params['user'] );
246 } elseif ( $params['excludeuser'] !== null ) {
247 $user = User::newFromName( $params['excludeuser'] );
248 if ( $user && $user->getId() > 0 ) {
249 $this->addWhere( 'rev_user != ' . $user->getId() );
250 } else {
251 $this->addWhere( 'rev_user_text != ' .
252 $db->addQuotes( $params['excludeuser'] ) );
255 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
256 // Paranoia: avoid brute force searches (bug 17342)
257 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
258 $bitmask = Revision::DELETED_USER;
259 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
260 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
261 } else {
262 $bitmask = 0;
264 if ( $bitmask ) {
265 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
268 } elseif ( $revCount > 0 ) {
269 // Always targets the PRIMARY index
271 $revs = $pageSet->getLiveRevisionIDs();
273 // Get all revision IDs
274 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
276 if ( $params['continue'] !== null ) {
277 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
279 $this->addOption( 'ORDER BY', 'rev_id' );
280 } elseif ( $pageCount > 0 ) {
281 // Always targets the rev_page_id index
283 $titles = $pageSet->getGoodTitles();
285 // When working in multi-page non-enumeration mode,
286 // limit to the latest revision only
287 $this->addWhere( 'page_latest=rev_id' );
289 // Get all page IDs
290 $this->addWhereFld( 'page_id', array_keys( $titles ) );
291 // Every time someone relies on equality propagation, god kills a kitten :)
292 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
294 if ( $params['continue'] !== null ) {
295 $cont = explode( '|', $params['continue'] );
296 $this->dieContinueUsageIf( count( $cont ) != 2 );
297 $pageid = intval( $cont[0] );
298 $revid = intval( $cont[1] );
299 $this->addWhere(
300 "rev_page > $pageid OR " .
301 "(rev_page = $pageid AND " .
302 "rev_id >= $revid)"
305 $this->addOption( 'ORDER BY', array(
306 'rev_page',
307 'rev_id'
308 ) );
309 } else {
310 ApiBase::dieDebug( __METHOD__, 'param validation?' );
313 $this->addOption( 'LIMIT', $this->limit + 1 );
315 $count = 0;
316 $generated = array();
317 $res = $this->select( __METHOD__ );
319 foreach ( $res as $row ) {
320 if ( ++$count > $this->limit ) {
321 // We've reached the one extra which shows that there are
322 // additional pages to be had. Stop here...
323 if ( $enumRevMode ) {
324 $this->setContinueEnumParameter( 'continue',
325 $row->rev_timestamp . '|' . intval( $row->rev_id ) );
326 } elseif ( $revCount > 0 ) {
327 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
328 } else {
329 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
330 '|' . intval( $row->rev_id ) );
332 break;
335 if ( $resultPageSet !== null ) {
336 $generated[] = $row->rev_id;
337 } else {
338 $revision = new Revision( $row );
339 $rev = $this->extractRevisionInfo( $revision, $row );
341 if ( $this->token !== null ) {
342 $title = $revision->getTitle();
343 $tokenFunctions = $this->getTokenFunctions();
344 foreach ( $this->token as $t ) {
345 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
346 if ( $val === false ) {
347 $this->setWarning( "Action '$t' is not allowed for the current user" );
348 } else {
349 $rev[$t . 'token'] = $val;
354 $fit = $this->addPageSubItem( $row->rev_page, $rev, 'rev' );
355 if ( !$fit ) {
356 if ( $enumRevMode ) {
357 $this->setContinueEnumParameter( 'continue',
358 $row->rev_timestamp . '|' . intval( $row->rev_id ) );
359 } elseif ( $revCount > 0 ) {
360 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
361 } else {
362 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
363 '|' . intval( $row->rev_id ) );
365 break;
370 if ( $resultPageSet !== null ) {
371 $resultPageSet->populateFromRevisionIDs( $generated );
375 public function getCacheMode( $params ) {
376 if ( isset( $params['token'] ) ) {
377 return 'private';
379 return parent::getCacheMode( $params );
382 public function getAllowedParams() {
383 $ret = parent::getAllowedParams() + array(
384 'startid' => array(
385 ApiBase::PARAM_TYPE => 'integer',
386 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
388 'endid' => array(
389 ApiBase::PARAM_TYPE => 'integer',
390 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
392 'start' => array(
393 ApiBase::PARAM_TYPE => 'timestamp',
394 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
396 'end' => array(
397 ApiBase::PARAM_TYPE => 'timestamp',
398 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
400 'dir' => array(
401 ApiBase::PARAM_DFLT => 'older',
402 ApiBase::PARAM_TYPE => array(
403 'newer',
404 'older'
406 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
407 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
409 'user' => array(
410 ApiBase::PARAM_TYPE => 'user',
411 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
413 'excludeuser' => array(
414 ApiBase::PARAM_TYPE => 'user',
415 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
417 'tag' => null,
418 'token' => array(
419 ApiBase::PARAM_DEPRECATED => true,
420 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
421 ApiBase::PARAM_ISMULTI => true
423 'continue' => array(
424 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
428 $ret['limit'][ApiBase::PARAM_HELP_MSG_INFO] = array( array( 'singlepageonly' ) );
430 return $ret;
433 protected function getExamplesMessages() {
434 return array(
435 'action=query&prop=revisions&titles=API|Main%20Page&' .
436 'rvprop=timestamp|user|comment|content'
437 => 'apihelp-query+revisions-example-content',
438 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
439 'rvprop=timestamp|user|comment'
440 => 'apihelp-query+revisions-example-last5',
441 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
442 'rvprop=timestamp|user|comment&rvdir=newer'
443 => 'apihelp-query+revisions-example-first5',
444 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
445 'rvprop=timestamp|user|comment&rvdir=newer&rvstart=2006-05-01T00:00:00Z'
446 => 'apihelp-query+revisions-example-first5-after',
447 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
448 'rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1'
449 => 'apihelp-query+revisions-example-first5-not-localhost',
450 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
451 'rvprop=timestamp|user|comment&rvuser=MediaWiki%20default'
452 => 'apihelp-query+revisions-example-first5-user',
456 public function getHelpUrls() {
457 return 'https://www.mediawiki.org/wiki/API:Revisions';