QA: Upgrade mediawiki_selenium to 1.2 for Raita logging
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 7, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
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.
32 *
33 * @ingroup API
34 */
35 class ApiQueryRevisions extends ApiQueryRevisionsBase {
36
37 private $token = null;
38
39 public function __construct( ApiQuery $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'rv' );
41 }
42
43 private $tokenFunctions;
44
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
50
51 // Don't call the hooks twice
52 if ( isset( $this->tokenFunctions ) ) {
53 return $this->tokenFunctions;
54 }
55
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();
60 }
61
62 $this->tokenFunctions = array(
63 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
64 );
65 Hooks::run( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions ) );
66
67 return $this->tokenFunctions;
68 }
69
70 /**
71 * @deprecated since 1.24
72 * @param int $pageid
73 * @param Title $title
74 * @param Revision $rev
75 * @return bool|string
76 */
77 public static function getRollbackToken( $pageid, $title, $rev ) {
78 global $wgUser;
79 if ( !$wgUser->isAllowed( 'rollback' ) ) {
80 return false;
81 }
82
83 return $wgUser->getEditToken(
84 array( $title->getPrefixedText(), $rev->getUserText() ) );
85 }
86
87 protected function run( ApiPageSet $resultPageSet = null ) {
88 $params = $this->extractRequestParams( false );
89
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 );
98
99 $pageSet = $this->getPageSet();
100 $pageCount = $pageSet->getGoodTitleCount();
101 $revCount = $pageSet->getRevisionCount();
102
103 // Optimization -- nothing to do
104 if ( $revCount === 0 && $pageCount === 0 ) {
105 // Nothing to do
106 return;
107 }
108 if ( $revCount > 0 && count( $pageSet->getLiveRevisionIDs() ) === 0 ) {
109 // We're in revisions mode but all given revisions are deleted
110 return;
111 }
112
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'
118 );
119 }
120
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'
127 );
128 }
129
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';
135 }
136
137 $db = $this->getDB();
138 $this->addTables( array( 'revision', 'page' ) );
139 $this->addJoinConds(
140 array( 'page' => array( 'INNER JOIN', array( 'page_id = rev_page' ) ) )
141 );
142
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() );
149 }
150 } else {
151 $this->limit = $this->getParameter( 'limit' ) ?: 10;
152 $this->addFields( array( 'rev_id', 'rev_timestamp', 'rev_page' ) );
153 }
154
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' ) ) )
159 );
160 $this->addFields( 'ts_tags' );
161 }
162
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' ) ) )
167 );
168 $this->addWhereFld( 'ct_tag', $params['tag'] );
169 }
170
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' );
180 }
181 }
182
183 $this->addTables( 'text' );
184 $this->addJoinConds(
185 array( 'text' => array( 'INNER JOIN', array( 'rev_text_id=old_id' ) ) )
186 );
187 $this->addFields( 'old_id' );
188 $this->addFields( Revision::selectTextFields() );
189 }
190
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() );
196 }
197
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
203
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' );
207 }
208
209 if ( $params['endid'] !== null && $params['end'] !== null ) {
210 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
211 }
212
213 if ( $params['user'] !== null && $params['excludeuser'] !== null ) {
214 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
215 }
216
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)"
227 );
228 }
229
230 // Query optimization: since we're targeting ranges of
231 // rev_timestamp,rev_id, if we're given an id then extract the
232 // corresponding timestamp from the DB.
233 // Note we don't use Revision::getTimestampFromId() since we don't
234 // have a Title to pass it and there's not any real need to create one.
235 if ( $params['startid'] !== null ) {
236 $params['start'] = $db->selectField( 'revision', 'rev_timestamp',
237 array( 'rev_id' => $params['startid'] ), __METHOD__ );
238 }
239 if ( $params['endid'] !== null ) {
240 $params['end'] = $db->selectField( 'revision', 'rev_timestamp',
241 array( 'rev_id' => $params['endid'] ), __METHOD__ );
242 }
243
244 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
245 $params['start'], $params['end'] );
246 $this->addWhereRange( 'rev_id', $params['dir'],
247 $params['startid'], $params['endid'] );
248
249 // There is only one ID, use it
250 $ids = array_keys( $pageSet->getGoodTitles() );
251 $this->addWhereFld( 'rev_page', reset( $ids ) );
252
253 if ( $params['user'] !== null ) {
254 $user = User::newFromName( $params['user'] );
255 if ( $user && $user->getId() > 0 ) {
256 $this->addWhereFld( 'rev_user', $user->getId() );
257 } else {
258 $this->addWhereFld( 'rev_user_text', $params['user'] );
259 }
260 } elseif ( $params['excludeuser'] !== null ) {
261 $user = User::newFromName( $params['excludeuser'] );
262 if ( $user && $user->getId() > 0 ) {
263 $this->addWhere( 'rev_user != ' . $user->getId() );
264 } else {
265 $this->addWhere( 'rev_user_text != ' .
266 $db->addQuotes( $params['excludeuser'] ) );
267 }
268 }
269 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
270 // Paranoia: avoid brute force searches (bug 17342)
271 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
272 $bitmask = Revision::DELETED_USER;
273 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
274 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
275 } else {
276 $bitmask = 0;
277 }
278 if ( $bitmask ) {
279 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
280 }
281 }
282 } elseif ( $revCount > 0 ) {
283 // Always targets the PRIMARY index
284
285 $revs = $pageSet->getLiveRevisionIDs();
286
287 // Get all revision IDs
288 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
289
290 if ( $params['continue'] !== null ) {
291 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
292 }
293 $this->addOption( 'ORDER BY', 'rev_id' );
294 } elseif ( $pageCount > 0 ) {
295 // Always targets the rev_page_id index
296
297 $titles = $pageSet->getGoodTitles();
298
299 // When working in multi-page non-enumeration mode,
300 // limit to the latest revision only
301 $this->addWhere( 'page_latest=rev_id' );
302
303 // Get all page IDs
304 $this->addWhereFld( 'page_id', array_keys( $titles ) );
305 // Every time someone relies on equality propagation, god kills a kitten :)
306 $this->addWhereFld( 'rev_page', array_keys( $titles ) );
307
308 if ( $params['continue'] !== null ) {
309 $cont = explode( '|', $params['continue'] );
310 $this->dieContinueUsageIf( count( $cont ) != 2 );
311 $pageid = intval( $cont[0] );
312 $revid = intval( $cont[1] );
313 $this->addWhere(
314 "rev_page > $pageid OR " .
315 "(rev_page = $pageid AND " .
316 "rev_id >= $revid)"
317 );
318 }
319 $this->addOption( 'ORDER BY', array(
320 'rev_page',
321 'rev_id'
322 ) );
323 } else {
324 ApiBase::dieDebug( __METHOD__, 'param validation?' );
325 }
326
327 $this->addOption( 'LIMIT', $this->limit + 1 );
328
329 $count = 0;
330 $generated = array();
331 $res = $this->select( __METHOD__ );
332
333 foreach ( $res as $row ) {
334 if ( ++$count > $this->limit ) {
335 // We've reached the one extra which shows that there are
336 // additional pages to be had. Stop here...
337 if ( $enumRevMode ) {
338 $this->setContinueEnumParameter( 'continue',
339 $row->rev_timestamp . '|' . intval( $row->rev_id ) );
340 } elseif ( $revCount > 0 ) {
341 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
342 } else {
343 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
344 '|' . intval( $row->rev_id ) );
345 }
346 break;
347 }
348
349 if ( $resultPageSet !== null ) {
350 $generated[] = $row->rev_id;
351 } else {
352 $revision = new Revision( $row );
353 $rev = $this->extractRevisionInfo( $revision, $row );
354
355 if ( $this->token !== null ) {
356 $title = $revision->getTitle();
357 $tokenFunctions = $this->getTokenFunctions();
358 foreach ( $this->token as $t ) {
359 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
360 if ( $val === false ) {
361 $this->setWarning( "Action '$t' is not allowed for the current user" );
362 } else {
363 $rev[$t . 'token'] = $val;
364 }
365 }
366 }
367
368 $fit = $this->addPageSubItem( $row->rev_page, $rev, 'rev' );
369 if ( !$fit ) {
370 if ( $enumRevMode ) {
371 $this->setContinueEnumParameter( 'continue',
372 $row->rev_timestamp . '|' . intval( $row->rev_id ) );
373 } elseif ( $revCount > 0 ) {
374 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
375 } else {
376 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
377 '|' . intval( $row->rev_id ) );
378 }
379 break;
380 }
381 }
382 }
383
384 if ( $resultPageSet !== null ) {
385 $resultPageSet->populateFromRevisionIDs( $generated );
386 }
387 }
388
389 public function getCacheMode( $params ) {
390 if ( isset( $params['token'] ) ) {
391 return 'private';
392 }
393 return parent::getCacheMode( $params );
394 }
395
396 public function getAllowedParams() {
397 $ret = parent::getAllowedParams() + array(
398 'startid' => array(
399 ApiBase::PARAM_TYPE => 'integer',
400 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
401 ),
402 'endid' => array(
403 ApiBase::PARAM_TYPE => 'integer',
404 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
405 ),
406 'start' => array(
407 ApiBase::PARAM_TYPE => 'timestamp',
408 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
409 ),
410 'end' => array(
411 ApiBase::PARAM_TYPE => 'timestamp',
412 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
413 ),
414 'dir' => array(
415 ApiBase::PARAM_DFLT => 'older',
416 ApiBase::PARAM_TYPE => array(
417 'newer',
418 'older'
419 ),
420 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
421 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
422 ),
423 'user' => array(
424 ApiBase::PARAM_TYPE => 'user',
425 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
426 ),
427 'excludeuser' => array(
428 ApiBase::PARAM_TYPE => 'user',
429 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
430 ),
431 'tag' => null,
432 'token' => array(
433 ApiBase::PARAM_DEPRECATED => true,
434 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
435 ApiBase::PARAM_ISMULTI => true
436 ),
437 'continue' => array(
438 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
439 ),
440 );
441
442 $ret['limit'][ApiBase::PARAM_HELP_MSG_INFO] = array( array( 'singlepageonly' ) );
443
444 return $ret;
445 }
446
447 protected function getExamplesMessages() {
448 return array(
449 'action=query&prop=revisions&titles=API|Main%20Page&' .
450 'rvprop=timestamp|user|comment|content'
451 => 'apihelp-query+revisions-example-content',
452 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
453 'rvprop=timestamp|user|comment'
454 => 'apihelp-query+revisions-example-last5',
455 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
456 'rvprop=timestamp|user|comment&rvdir=newer'
457 => 'apihelp-query+revisions-example-first5',
458 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
459 'rvprop=timestamp|user|comment&rvdir=newer&rvstart=2006-05-01T00:00:00Z'
460 => 'apihelp-query+revisions-example-first5-after',
461 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
462 'rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1'
463 => 'apihelp-query+revisions-example-first5-not-localhost',
464 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
465 'rvprop=timestamp|user|comment&rvuser=MediaWiki%20default'
466 => 'apihelp-query+revisions-example-first5-user',
467 );
468 }
469
470 public function getHelpUrls() {
471 return 'https://www.mediawiki.org/wiki/API:Revisions';
472 }
473 }