Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiQueryRevisions.php
blob48f604664fbb59760a78072a5e51178aeef55dad
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 [];
62 $this->tokenFunctions = [
63 'rollback' => [ 'ApiQueryRevisions', 'getRollbackToken' ]
65 Hooks::run( 'APIQueryRevisionsTokens', [ &$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( 'rollback' );
86 protected function run( ApiPageSet $resultPageSet = null ) {
87 $params = $this->extractRequestParams( false );
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 = ( $params['user'] !== null || $params['excludeuser'] !== null ||
94 $params['limit'] !== null || $params['startid'] !== null ||
95 $params['endid'] !== null || $params['dir'] === 'newer' ||
96 $params['start'] !== null || $params['end'] !== null );
98 $pageSet = $this->getPageSet();
99 $pageCount = $pageSet->getGoodTitleCount();
100 $revCount = $pageSet->getRevisionCount();
102 // Optimization -- nothing to do
103 if ( $revCount === 0 && $pageCount === 0 ) {
104 // Nothing to do
105 return;
107 if ( $revCount > 0 && count( $pageSet->getLiveRevisionIDs() ) === 0 ) {
108 // We're in revisions mode but all given revisions are deleted
109 return;
112 if ( $revCount > 0 && $enumRevMode ) {
113 $this->dieWithError(
114 [ 'apierror-revisions-nolist', $this->getModulePrefix() ], 'invalidparammix'
118 if ( $pageCount > 1 && $enumRevMode ) {
119 $this->dieWithError(
120 [ 'apierror-revisions-singlepage', $this->getModulePrefix() ], 'invalidparammix'
124 // In non-enum mode, rvlimit can't be directly used. Use the maximum
125 // allowed value.
126 if ( !$enumRevMode ) {
127 $this->setParsedLimit = false;
128 $params['limit'] = 'max';
131 $db = $this->getDB();
132 $this->addTables( [ 'revision', 'page' ] );
133 $this->addJoinConds(
134 [ 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ] ]
137 if ( $resultPageSet === null ) {
138 $this->parseParameters( $params );
139 $this->token = $params['token'];
140 $this->addFields( Revision::selectFields() );
141 if ( $this->token !== null || $pageCount > 0 ) {
142 $this->addFields( Revision::selectPageFields() );
144 } else {
145 $this->limit = $this->getParameter( 'limit' ) ?: 10;
146 $this->addFields( [ 'rev_id', 'rev_timestamp', 'rev_page' ] );
149 if ( $this->fld_tags ) {
150 $this->addTables( 'tag_summary' );
151 $this->addJoinConds(
152 [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ]
154 $this->addFields( 'ts_tags' );
157 if ( $params['tag'] !== null ) {
158 $this->addTables( 'change_tag' );
159 $this->addJoinConds(
160 [ 'change_tag' => [ 'INNER JOIN', [ 'rev_id=ct_rev_id' ] ] ]
162 $this->addWhereFld( 'ct_tag', $params['tag'] );
165 if ( $this->fetchContent ) {
166 // For each page we will request, the user must have read rights for that page
167 $user = $this->getUser();
168 $status = Status::newGood();
169 /** @var $title Title */
170 foreach ( $pageSet->getGoodTitles() as $title ) {
171 if ( !$title->userCan( 'read', $user ) ) {
172 $status->fatal( ApiMessage::create(
173 [ 'apierror-cannotviewtitle', wfEscapeWikiText( $title->getPrefixedText() ) ],
174 'accessdenied'
175 ) );
178 if ( !$status->isGood() ) {
179 $this->dieStatus( $status );
182 $this->addTables( 'text' );
183 $this->addJoinConds(
184 [ 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ] ]
186 $this->addFields( 'old_id' );
187 $this->addFields( Revision::selectTextFields() );
190 // add user name, if needed
191 if ( $this->fld_user ) {
192 $this->addTables( 'user' );
193 $this->addJoinConds( [ 'user' => Revision::userJoinCond() ] );
194 $this->addFields( Revision::selectUserFields() );
197 if ( $enumRevMode ) {
198 // Indexes targeted:
199 // page_timestamp if we don't have rvuser
200 // page_user_timestamp if we have a logged-in rvuser
201 // page_timestamp or usertext_timestamp if we have an IP rvuser
203 // This is mostly to prevent parameter errors (and optimize SQL?)
204 $this->requireMaxOneParameter( $params, 'startid', 'start' );
205 $this->requireMaxOneParameter( $params, 'endid', 'end' );
206 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
208 if ( $params['continue'] !== null ) {
209 $cont = explode( '|', $params['continue'] );
210 $this->dieContinueUsageIf( count( $cont ) != 2 );
211 $op = ( $params['dir'] === 'newer' ? '>' : '<' );
212 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
213 $continueId = (int)$cont[1];
214 $this->dieContinueUsageIf( $continueId != $cont[1] );
215 $this->addWhere( "rev_timestamp $op $continueTimestamp OR " .
216 "(rev_timestamp = $continueTimestamp AND " .
217 "rev_id $op= $continueId)"
221 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
222 $params['start'], $params['end'] );
223 $this->addWhereRange( 'rev_id', $params['dir'],
224 $params['startid'], $params['endid'] );
226 // There is only one ID, use it
227 $ids = array_keys( $pageSet->getGoodTitles() );
228 $this->addWhereFld( 'rev_page', reset( $ids ) );
230 if ( $params['user'] !== null ) {
231 $user = User::newFromName( $params['user'] );
232 if ( $user && $user->getId() > 0 ) {
233 $this->addWhereFld( 'rev_user', $user->getId() );
234 } else {
235 $this->addWhereFld( 'rev_user_text', $params['user'] );
237 } elseif ( $params['excludeuser'] !== null ) {
238 $user = User::newFromName( $params['excludeuser'] );
239 if ( $user && $user->getId() > 0 ) {
240 $this->addWhere( 'rev_user != ' . $user->getId() );
241 } else {
242 $this->addWhere( 'rev_user_text != ' .
243 $db->addQuotes( $params['excludeuser'] ) );
246 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
247 // Paranoia: avoid brute force searches (bug 17342)
248 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
249 $bitmask = Revision::DELETED_USER;
250 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
251 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
252 } else {
253 $bitmask = 0;
255 if ( $bitmask ) {
256 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
259 } elseif ( $revCount > 0 ) {
260 // Always targets the PRIMARY index
262 $revs = $pageSet->getLiveRevisionIDs();
264 // Get all revision IDs
265 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
267 if ( $params['continue'] !== null ) {
268 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
270 $this->addOption( 'ORDER BY', 'rev_id' );
271 } elseif ( $pageCount > 0 ) {
272 // Always targets the rev_page_id index
274 $titles = $pageSet->getGoodTitles();
276 // When working in multi-page non-enumeration mode,
277 // limit to the latest revision only
278 $this->addWhere( 'page_latest=rev_id' );
280 // Get all page IDs
281 $this->addWhereFld( 'page_id', array_keys( $titles ) );
282 // Every time someone relies on equality propagation, god kills a kitten :)
283 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
285 if ( $params['continue'] !== null ) {
286 $cont = explode( '|', $params['continue'] );
287 $this->dieContinueUsageIf( count( $cont ) != 2 );
288 $pageid = intval( $cont[0] );
289 $revid = intval( $cont[1] );
290 $this->addWhere(
291 "rev_page > $pageid OR " .
292 "(rev_page = $pageid AND " .
293 "rev_id >= $revid)"
296 $this->addOption( 'ORDER BY', [
297 'rev_page',
298 'rev_id'
299 ] );
300 } else {
301 ApiBase::dieDebug( __METHOD__, 'param validation?' );
304 $this->addOption( 'LIMIT', $this->limit + 1 );
306 $count = 0;
307 $generated = [];
308 $hookData = [];
309 $res = $this->select( __METHOD__, [], $hookData );
311 foreach ( $res as $row ) {
312 if ( ++$count > $this->limit ) {
313 // We've reached the one extra which shows that there are
314 // additional pages to be had. Stop here...
315 if ( $enumRevMode ) {
316 $this->setContinueEnumParameter( 'continue',
317 $row->rev_timestamp . '|' . intval( $row->rev_id ) );
318 } elseif ( $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 ) );
324 break;
327 if ( $resultPageSet !== null ) {
328 $generated[] = $row->rev_id;
329 } else {
330 $revision = new Revision( $row );
331 $rev = $this->extractRevisionInfo( $revision, $row );
333 if ( $this->token !== null ) {
334 $title = $revision->getTitle();
335 $tokenFunctions = $this->getTokenFunctions();
336 foreach ( $this->token as $t ) {
337 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
338 if ( $val === false ) {
339 $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
340 } else {
341 $rev[$t . 'token'] = $val;
346 $fit = $this->processRow( $row, $rev, $hookData ) &&
347 $this->addPageSubItem( $row->rev_page, $rev, 'rev' );
348 if ( !$fit ) {
349 if ( $enumRevMode ) {
350 $this->setContinueEnumParameter( 'continue',
351 $row->rev_timestamp . '|' . intval( $row->rev_id ) );
352 } elseif ( $revCount > 0 ) {
353 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
354 } else {
355 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
356 '|' . intval( $row->rev_id ) );
358 break;
363 if ( $resultPageSet !== null ) {
364 $resultPageSet->populateFromRevisionIDs( $generated );
368 public function getCacheMode( $params ) {
369 if ( isset( $params['token'] ) ) {
370 return 'private';
372 return parent::getCacheMode( $params );
375 public function getAllowedParams() {
376 $ret = parent::getAllowedParams() + [
377 'startid' => [
378 ApiBase::PARAM_TYPE => 'integer',
379 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
381 'endid' => [
382 ApiBase::PARAM_TYPE => 'integer',
383 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
385 'start' => [
386 ApiBase::PARAM_TYPE => 'timestamp',
387 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
389 'end' => [
390 ApiBase::PARAM_TYPE => 'timestamp',
391 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
393 'dir' => [
394 ApiBase::PARAM_DFLT => 'older',
395 ApiBase::PARAM_TYPE => [
396 'newer',
397 'older'
399 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
400 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
402 'user' => [
403 ApiBase::PARAM_TYPE => 'user',
404 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
406 'excludeuser' => [
407 ApiBase::PARAM_TYPE => 'user',
408 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
410 'tag' => null,
411 'token' => [
412 ApiBase::PARAM_DEPRECATED => true,
413 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
414 ApiBase::PARAM_ISMULTI => true
416 'continue' => [
417 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
421 $ret['limit'][ApiBase::PARAM_HELP_MSG_INFO] = [ [ 'singlepageonly' ] ];
423 return $ret;
426 protected function getExamplesMessages() {
427 return [
428 'action=query&prop=revisions&titles=API|Main%20Page&' .
429 'rvprop=timestamp|user|comment|content'
430 => 'apihelp-query+revisions-example-content',
431 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
432 'rvprop=timestamp|user|comment'
433 => 'apihelp-query+revisions-example-last5',
434 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
435 'rvprop=timestamp|user|comment&rvdir=newer'
436 => 'apihelp-query+revisions-example-first5',
437 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
438 'rvprop=timestamp|user|comment&rvdir=newer&rvstart=2006-05-01T00:00:00Z'
439 => 'apihelp-query+revisions-example-first5-after',
440 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
441 'rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1'
442 => 'apihelp-query+revisions-example-first5-not-localhost',
443 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
444 'rvprop=timestamp|user|comment&rvuser=MediaWiki%20default'
445 => 'apihelp-query+revisions-example-first5-user',
449 public function getHelpUrls() {
450 return 'https://www.mediawiki.org/wiki/API:Revisions';