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 JSON callback mode, no tokens can be obtained
57 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
61 $this->tokenFunctions
= array(
62 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
64 Hooks
::run( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions
) );
66 return $this->tokenFunctions
;
70 * @deprecated since 1.24
73 * @param Revision $rev
76 public static function getRollbackToken( $pageid, $title, $rev ) {
78 if ( !$wgUser->isAllowed( 'rollback' ) ) {
82 return $wgUser->getEditToken(
83 array( $title->getPrefixedText(), $rev->getUserText() ) );
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 = ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ||
94 !is_null( $params['limit'] ) ||
!is_null( $params['startid'] ) ||
95 !is_null( $params['endid'] ) ||
$params['dir'] === 'newer' ||
96 !is_null( $params['start'] ) ||
!is_null( $params['end'] ) );
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( array( 'revision', 'page' ) );
139 array( 'page' => array( 'INNER JOIN', array( '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( array( 'rev_id', 'rev_page' ) );
154 if ( $this->fld_tags
) {
155 $this->addTables( 'tag_summary' );
157 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
159 $this->addFields( 'ts_tags' );
162 if ( !is_null( $params['tag'] ) ) {
163 $this->addTables( 'change_tag' );
165 array( 'change_tag' => array( 'INNER JOIN', array( '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 array( 'text' => array( 'INNER JOIN', array( '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( array( 'user' => Revision
::userJoinCond() ) );
194 $this->addFields( Revision
::selectUserFields() );
197 if ( $enumRevMode ) {
198 // This is mostly to prevent parameter errors (and optimize SQL?)
199 if ( !is_null( $params['startid'] ) && !is_null( $params['start'] ) ) {
200 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
203 if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) ) {
204 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
207 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
208 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
211 // Continuing effectively uses startid. But we can't use rvstartid
212 // directly, because there is no way to tell the client to ''not''
213 // send rvstart if it sent it in the original query. So instead we
214 // send the continuation startid as rvcontinue, and ignore both
215 // rvstart and rvstartid when that is supplied.
216 if ( !is_null( $params['continue'] ) ) {
217 $params['startid'] = $params['continue'];
218 $params['start'] = null;
221 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
222 // the same result. This way users may request revisions starting at a given time,
223 // but to page through results use the rev_id returned after each page.
224 // Switching to rev_id removes the potential problem of having more than
225 // one row with the same timestamp for the same page.
226 // The order needs to be the same as start parameter to avoid SQL filesort.
227 if ( is_null( $params['startid'] ) && is_null( $params['endid'] ) ) {
228 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
229 $params['start'], $params['end'] );
231 $this->addWhereRange( 'rev_id', $params['dir'],
232 $params['startid'], $params['endid'] );
233 // One of start and end can be set
234 // If neither is set, this does nothing
235 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
236 $params['start'], $params['end'], false );
239 // There is only one ID, use it
240 $ids = array_keys( $pageSet->getGoodTitles() );
241 $this->addWhereFld( 'rev_page', reset( $ids ) );
243 if ( !is_null( $params['user'] ) ) {
244 $this->addWhereFld( 'rev_user_text', $params['user'] );
245 } elseif ( !is_null( $params['excludeuser'] ) ) {
246 $this->addWhere( 'rev_user_text != ' .
247 $db->addQuotes( $params['excludeuser'] ) );
249 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
250 // Paranoia: avoid brute force searches (bug 17342)
251 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
252 $bitmask = Revision
::DELETED_USER
;
253 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
254 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
259 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
262 } elseif ( $revCount > 0 ) {
263 $revs = $pageSet->getLiveRevisionIDs();
265 // Get all revision IDs
266 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
268 if ( !is_null( $params['continue'] ) ) {
269 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
271 $this->addOption( 'ORDER BY', 'rev_id' );
272 } elseif ( $pageCount > 0 ) {
273 $titles = $pageSet->getGoodTitles();
275 // When working in multi-page non-enumeration mode,
276 // limit to the latest revision only
277 $this->addWhere( 'page_latest=rev_id' );
280 $this->addWhereFld( 'page_id', array_keys( $titles ) );
281 // Every time someone relies on equality propagation, god kills a kitten :)
282 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
284 if ( !is_null( $params['continue'] ) ) {
285 $cont = explode( '|', $params['continue'] );
286 $this->dieContinueUsageIf( count( $cont ) != 2 );
287 $pageid = intval( $cont[0] );
288 $revid = intval( $cont[1] );
290 "rev_page > $pageid OR " .
291 "(rev_page = $pageid AND " .
295 $this->addOption( 'ORDER BY', array(
300 ApiBase
::dieDebug( __METHOD__
, 'param validation?' );
303 $this->addOption( 'LIMIT', $this->limit +
1 );
306 $generated = array();
307 $res = $this->select( __METHOD__
);
309 foreach ( $res as $row ) {
310 if ( ++
$count > $this->limit
) {
311 // We've reached the one extra which shows that there are
312 // additional pages to be had. Stop here...
313 if ( $enumRevMode ) {
314 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id
) );
315 } elseif ( $revCount > 0 ) {
316 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id
) );
318 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page
) .
319 '|' . intval( $row->rev_id
) );
324 if ( $resultPageSet !== null ) {
325 $generated[] = $row->rev_id
;
327 $revision = new Revision( $row );
328 $rev = $this->extractRevisionInfo( $revision, $row );
330 if ( $this->token
!== null ) {
331 $title = $revision->getTitle();
332 $tokenFunctions = $this->getTokenFunctions();
333 foreach ( $this->token
as $t ) {
334 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
335 if ( $val === false ) {
336 $this->setWarning( "Action '$t' is not allowed for the current user" );
338 $rev[$t . 'token'] = $val;
343 $fit = $this->addPageSubItem( $row->rev_page
, $rev, 'rev' );
345 if ( $enumRevMode ) {
346 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id
) );
347 } elseif ( $revCount > 0 ) {
348 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id
) );
350 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page
) .
351 '|' . intval( $row->rev_id
) );
358 if ( $resultPageSet !== null ) {
359 $resultPageSet->populateFromRevisionIDs( $generated );
363 public function getCacheMode( $params ) {
364 if ( isset( $params['token'] ) ) {
367 return parent
::getCacheMode( $params );
370 public function getAllowedParams() {
371 $ret = parent
::getAllowedParams() +
array(
373 ApiBase
::PARAM_TYPE
=> 'integer',
374 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'singlepageonly' ) ),
377 ApiBase
::PARAM_TYPE
=> 'integer',
378 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'singlepageonly' ) ),
381 ApiBase
::PARAM_TYPE
=> 'timestamp',
382 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'singlepageonly' ) ),
385 ApiBase
::PARAM_TYPE
=> 'timestamp',
386 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'singlepageonly' ) ),
389 ApiBase
::PARAM_DFLT
=> 'older',
390 ApiBase
::PARAM_TYPE
=> array(
394 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
395 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'singlepageonly' ) ),
398 ApiBase
::PARAM_TYPE
=> 'user',
399 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'singlepageonly' ) ),
401 'excludeuser' => array(
402 ApiBase
::PARAM_TYPE
=> 'user',
403 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'singlepageonly' ) ),
407 ApiBase
::PARAM_DEPRECATED
=> true,
408 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
409 ApiBase
::PARAM_ISMULTI
=> true
412 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
416 $ret['limit'][ApiBase
::PARAM_HELP_MSG_INFO
] = array( array( 'singlepageonly' ) );
421 protected function getExamplesMessages() {
423 'action=query&prop=revisions&titles=API|Main%20Page&' .
424 'rvprop=timestamp|user|comment|content'
425 => 'apihelp-query+revisions-example-content',
426 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
427 'rvprop=timestamp|user|comment'
428 => 'apihelp-query+revisions-example-last5',
429 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
430 'rvprop=timestamp|user|comment&rvdir=newer'
431 => 'apihelp-query+revisions-example-first5',
432 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
433 'rvprop=timestamp|user|comment&rvdir=newer&rvstart=2006-05-01T00:00:00Z'
434 => 'apihelp-query+revisions-example-first5-after',
435 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
436 'rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1'
437 => 'apihelp-query+revisions-example-first5-not-localhost',
438 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
439 'rvprop=timestamp|user|comment&rvuser=MediaWiki%20default'
440 => 'apihelp-query+revisions-example-first5-user',
444 public function getHelpUrls() {
445 return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';