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
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.
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
58 if ( $this->lacksSameOriginSecurity() ) {
62 $this->tokenFunctions
= [
63 'rollback' => [ 'ApiQueryRevisions', 'getRollbackToken' ]
65 Hooks
::run( 'APIQueryRevisionsTokens', [ &$this->tokenFunctions
] );
67 return $this->tokenFunctions
;
71 * @deprecated since 1.24
74 * @param Revision $rev
77 public static function getRollbackToken( $pageid, $title, $rev ) {
79 if ( !$wgUser->isAllowed( 'rollback' ) ) {
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 ) {
107 if ( $revCount > 0 && count( $pageSet->getLiveRevisionIDs() ) === 0 ) {
108 // We're in revisions mode but all given revisions are deleted
112 if ( $revCount > 0 && $enumRevMode ) {
114 'The revids= parameter may not be used with the list options ' .
115 '(limit, startid, endid, dirNewer, start, end).',
120 if ( $pageCount > 1 && $enumRevMode ) {
122 'titles, pageids or a generator was used to supply multiple pages, ' .
123 'but the limit, startid, endid, dirNewer, user, excludeuser, start ' .
124 'and end parameters may only be used on a single page.',
129 // In non-enum mode, rvlimit can't be directly used. Use the maximum
131 if ( !$enumRevMode ) {
132 $this->setParsedLimit
= false;
133 $params['limit'] = 'max';
136 $db = $this->getDB();
137 $this->addTables( [ 'revision', 'page' ] );
139 [ 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ] ]
142 if ( $resultPageSet === null ) {
143 $this->parseParameters( $params );
144 $this->token
= $params['token'];
145 $this->addFields( Revision
::selectFields() );
146 if ( $this->token
!== null ||
$pageCount > 0 ) {
147 $this->addFields( Revision
::selectPageFields() );
150 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
151 $this->addFields( [ 'rev_id', 'rev_timestamp', 'rev_page' ] );
154 if ( $this->fld_tags
) {
155 $this->addTables( 'tag_summary' );
157 [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ]
159 $this->addFields( 'ts_tags' );
162 if ( $params['tag'] !== null ) {
163 $this->addTables( 'change_tag' );
165 [ 'change_tag' => [ 'INNER JOIN', [ 'rev_id=ct_rev_id' ] ] ]
167 $this->addWhereFld( 'ct_tag', $params['tag'] );
170 if ( $this->fetchContent
) {
171 // For each page we will request, the user must have read rights for that page
172 $user = $this->getUser();
173 /** @var $title Title */
174 foreach ( $pageSet->getGoodTitles() as $title ) {
175 if ( !$title->userCan( 'read', $user ) ) {
177 'The current user is not allowed to read ' . $title->getPrefixedText(),
182 $this->addTables( 'text' );
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 ) {
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 if ( $params['startid'] !== null && $params['start'] !== null ) {
205 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
208 if ( $params['endid'] !== null && $params['end'] !== null ) {
209 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
212 if ( $params['user'] !== null && $params['excludeuser'] !== null ) {
213 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
216 if ( $params['continue'] !== null ) {
217 $cont = explode( '|', $params['continue'] );
218 $this->dieContinueUsageIf( count( $cont ) != 2 );
219 $op = ( $params['dir'] === 'newer' ?
'>' : '<' );
220 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
221 $continueId = (int)$cont[1];
222 $this->dieContinueUsageIf( $continueId != $cont[1] );
223 $this->addWhere( "rev_timestamp $op $continueTimestamp OR " .
224 "(rev_timestamp = $continueTimestamp AND " .
225 "rev_id $op= $continueId)"
229 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
230 $params['start'], $params['end'] );
231 $this->addWhereRange( 'rev_id', $params['dir'],
232 $params['startid'], $params['endid'] );
234 // There is only one ID, use it
235 $ids = array_keys( $pageSet->getGoodTitles() );
236 $this->addWhereFld( 'rev_page', reset( $ids ) );
238 if ( $params['user'] !== null ) {
239 $user = User
::newFromName( $params['user'] );
240 if ( $user && $user->getId() > 0 ) {
241 $this->addWhereFld( 'rev_user', $user->getId() );
243 $this->addWhereFld( 'rev_user_text', $params['user'] );
245 } elseif ( $params['excludeuser'] !== null ) {
246 $user = User
::newFromName( $params['excludeuser'] );
247 if ( $user && $user->getId() > 0 ) {
248 $this->addWhere( 'rev_user != ' . $user->getId() );
250 $this->addWhere( 'rev_user_text != ' .
251 $db->addQuotes( $params['excludeuser'] ) );
254 if ( $params['user'] !== null ||
$params['excludeuser'] !== null ) {
255 // Paranoia: avoid brute force searches (bug 17342)
256 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
257 $bitmask = Revision
::DELETED_USER
;
258 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
259 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
264 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
267 } elseif ( $revCount > 0 ) {
268 // Always targets the PRIMARY index
270 $revs = $pageSet->getLiveRevisionIDs();
272 // Get all revision IDs
273 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
275 if ( $params['continue'] !== null ) {
276 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
278 $this->addOption( 'ORDER BY', 'rev_id' );
279 } elseif ( $pageCount > 0 ) {
280 // Always targets the rev_page_id index
282 $titles = $pageSet->getGoodTitles();
284 // When working in multi-page non-enumeration mode,
285 // limit to the latest revision only
286 $this->addWhere( 'page_latest=rev_id' );
289 $this->addWhereFld( 'page_id', array_keys( $titles ) );
290 // Every time someone relies on equality propagation, god kills a kitten :)
291 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
293 if ( $params['continue'] !== null ) {
294 $cont = explode( '|', $params['continue'] );
295 $this->dieContinueUsageIf( count( $cont ) != 2 );
296 $pageid = intval( $cont[0] );
297 $revid = intval( $cont[1] );
299 "rev_page > $pageid OR " .
300 "(rev_page = $pageid AND " .
304 $this->addOption( 'ORDER BY', [
309 ApiBase
::dieDebug( __METHOD__
, 'param validation?' );
312 $this->addOption( 'LIMIT', $this->limit +
1 );
316 $res = $this->select( __METHOD__
);
318 foreach ( $res as $row ) {
319 if ( ++
$count > $this->limit
) {
320 // We've reached the one extra which shows that there are
321 // additional pages to be had. Stop here...
322 if ( $enumRevMode ) {
323 $this->setContinueEnumParameter( 'continue',
324 $row->rev_timestamp
. '|' . intval( $row->rev_id
) );
325 } elseif ( $revCount > 0 ) {
326 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id
) );
328 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page
) .
329 '|' . intval( $row->rev_id
) );
334 if ( $resultPageSet !== null ) {
335 $generated[] = $row->rev_id
;
337 $revision = new Revision( $row );
338 $rev = $this->extractRevisionInfo( $revision, $row );
340 if ( $this->token
!== null ) {
341 $title = $revision->getTitle();
342 $tokenFunctions = $this->getTokenFunctions();
343 foreach ( $this->token
as $t ) {
344 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
345 if ( $val === false ) {
346 $this->setWarning( "Action '$t' is not allowed for the current user" );
348 $rev[$t . 'token'] = $val;
353 $fit = $this->addPageSubItem( $row->rev_page
, $rev, 'rev' );
355 if ( $enumRevMode ) {
356 $this->setContinueEnumParameter( 'continue',
357 $row->rev_timestamp
. '|' . intval( $row->rev_id
) );
358 } elseif ( $revCount > 0 ) {
359 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id
) );
361 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page
) .
362 '|' . intval( $row->rev_id
) );
369 if ( $resultPageSet !== null ) {
370 $resultPageSet->populateFromRevisionIDs( $generated );
374 public function getCacheMode( $params ) {
375 if ( isset( $params['token'] ) ) {
378 return parent
::getCacheMode( $params );
381 public function getAllowedParams() {
382 $ret = parent
::getAllowedParams() +
[
384 ApiBase
::PARAM_TYPE
=> 'integer',
385 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'singlepageonly' ] ],
388 ApiBase
::PARAM_TYPE
=> 'integer',
389 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'singlepageonly' ] ],
392 ApiBase
::PARAM_TYPE
=> 'timestamp',
393 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'singlepageonly' ] ],
396 ApiBase
::PARAM_TYPE
=> 'timestamp',
397 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'singlepageonly' ] ],
400 ApiBase
::PARAM_DFLT
=> 'older',
401 ApiBase
::PARAM_TYPE
=> [
405 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
406 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'singlepageonly' ] ],
409 ApiBase
::PARAM_TYPE
=> 'user',
410 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'singlepageonly' ] ],
413 ApiBase
::PARAM_TYPE
=> 'user',
414 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'singlepageonly' ] ],
418 ApiBase
::PARAM_DEPRECATED
=> true,
419 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
420 ApiBase
::PARAM_ISMULTI
=> true
423 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
427 $ret['limit'][ApiBase
::PARAM_HELP_MSG_INFO
] = [ [ 'singlepageonly' ] ];
432 protected function getExamplesMessages() {
434 'action=query&prop=revisions&titles=API|Main%20Page&' .
435 'rvprop=timestamp|user|comment|content'
436 => 'apihelp-query+revisions-example-content',
437 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
438 'rvprop=timestamp|user|comment'
439 => 'apihelp-query+revisions-example-last5',
440 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
441 'rvprop=timestamp|user|comment&rvdir=newer'
442 => 'apihelp-query+revisions-example-first5',
443 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
444 'rvprop=timestamp|user|comment&rvdir=newer&rvstart=2006-05-01T00:00:00Z'
445 => 'apihelp-query+revisions-example-first5-after',
446 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
447 'rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1'
448 => 'apihelp-query+revisions-example-first5-not-localhost',
449 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
450 'rvprop=timestamp|user|comment&rvuser=MediaWiki%20default'
451 => 'apihelp-query+revisions-example-first5-user',
455 public function getHelpUrls() {
456 return 'https://www.mediawiki.org/wiki/API:Revisions';