Merge "convertExtensionToRegistration: Still convert $wgTrackingCategories"
[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 = ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ||
95 !is_null( $params['limit'] ) || !is_null( $params['startid'] ) ||
96 !is_null( $params['endid'] ) || $params['dir'] === 'newer' ||
97 !is_null( $params['start'] ) || !is_null( $params['end'] ) );
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_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 ( !is_null( $params['tag'] ) ) {
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 // This is mostly to prevent parameter errors (and optimize SQL?)
200 if ( !is_null( $params['startid'] ) && !is_null( $params['start'] ) ) {
201 $this->dieUsage( 'start and startid cannot be used together', 'badparams' );
202 }
203
204 if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) ) {
205 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
206 }
207
208 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
209 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
210 }
211
212 // Continuing effectively uses startid. But we can't use rvstartid
213 // directly, because there is no way to tell the client to ''not''
214 // send rvstart if it sent it in the original query. So instead we
215 // send the continuation startid as rvcontinue, and ignore both
216 // rvstart and rvstartid when that is supplied.
217 if ( !is_null( $params['continue'] ) ) {
218 $params['startid'] = $params['continue'];
219 $params['start'] = null;
220 }
221
222 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
223 // the same result. This way users may request revisions starting at a given time,
224 // but to page through results use the rev_id returned after each page.
225 // Switching to rev_id removes the potential problem of having more than
226 // one row with the same timestamp for the same page.
227 // The order needs to be the same as start parameter to avoid SQL filesort.
228 if ( is_null( $params['startid'] ) && is_null( $params['endid'] ) ) {
229 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
230 $params['start'], $params['end'] );
231 } else {
232 $this->addWhereRange( 'rev_id', $params['dir'],
233 $params['startid'], $params['endid'] );
234 // One of start and end can be set
235 // If neither is set, this does nothing
236 $this->addTimestampWhereRange( 'rev_timestamp', $params['dir'],
237 $params['start'], $params['end'], false );
238 }
239
240 // There is only one ID, use it
241 $ids = array_keys( $pageSet->getGoodTitles() );
242 $this->addWhereFld( 'rev_page', reset( $ids ) );
243
244 if ( !is_null( $params['user'] ) ) {
245 $this->addWhereFld( 'rev_user_text', $params['user'] );
246 } elseif ( !is_null( $params['excludeuser'] ) ) {
247 $this->addWhere( 'rev_user_text != ' .
248 $db->addQuotes( $params['excludeuser'] ) );
249 }
250 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
251 // Paranoia: avoid brute force searches (bug 17342)
252 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
253 $bitmask = Revision::DELETED_USER;
254 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
255 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
256 } else {
257 $bitmask = 0;
258 }
259 if ( $bitmask ) {
260 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
261 }
262 }
263 } elseif ( $revCount > 0 ) {
264 $revs = $pageSet->getLiveRevisionIDs();
265
266 // Get all revision IDs
267 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
268
269 if ( !is_null( $params['continue'] ) ) {
270 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
271 }
272 $this->addOption( 'ORDER BY', 'rev_id' );
273 } elseif ( $pageCount > 0 ) {
274 $titles = $pageSet->getGoodTitles();
275
276 // When working in multi-page non-enumeration mode,
277 // limit to the latest revision only
278 $this->addWhere( 'page_latest=rev_id' );
279
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 ) );
284
285 if ( !is_null( $params['continue'] ) ) {
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)"
294 );
295 }
296 $this->addOption( 'ORDER BY', array(
297 'rev_page',
298 'rev_id'
299 ) );
300 } else {
301 ApiBase::dieDebug( __METHOD__, 'param validation?' );
302 }
303
304 $this->addOption( 'LIMIT', $this->limit + 1 );
305
306 $count = 0;
307 $generated = array();
308 $res = $this->select( __METHOD__ );
309
310 foreach ( $res as $row ) {
311 if ( ++$count > $this->limit ) {
312 // We've reached the one extra which shows that there are
313 // additional pages to be had. Stop here...
314 if ( $enumRevMode ) {
315 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
316 } elseif ( $revCount > 0 ) {
317 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
318 } else {
319 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
320 '|' . intval( $row->rev_id ) );
321 }
322 break;
323 }
324
325 if ( $resultPageSet !== null ) {
326 $generated[] = $row->rev_id;
327 } else {
328 $revision = new Revision( $row );
329 $rev = $this->extractRevisionInfo( $revision, $row );
330
331 if ( $this->token !== null ) {
332 $title = $revision->getTitle();
333 $tokenFunctions = $this->getTokenFunctions();
334 foreach ( $this->token as $t ) {
335 $val = call_user_func( $tokenFunctions[$t], $title->getArticleID(), $title, $revision );
336 if ( $val === false ) {
337 $this->setWarning( "Action '$t' is not allowed for the current user" );
338 } else {
339 $rev[$t . 'token'] = $val;
340 }
341 }
342 }
343
344 $fit = $this->addPageSubItem( $row->rev_page, $rev, 'rev' );
345 if ( !$fit ) {
346 if ( $enumRevMode ) {
347 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
348 } elseif ( $revCount > 0 ) {
349 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
350 } else {
351 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
352 '|' . intval( $row->rev_id ) );
353 }
354 break;
355 }
356 }
357 }
358
359 if ( $resultPageSet !== null ) {
360 $resultPageSet->populateFromRevisionIDs( $generated );
361 }
362 }
363
364 public function getCacheMode( $params ) {
365 if ( isset( $params['token'] ) ) {
366 return 'private';
367 }
368 return parent::getCacheMode( $params );
369 }
370
371 public function getAllowedParams() {
372 $ret = parent::getAllowedParams() + array(
373 'startid' => array(
374 ApiBase::PARAM_TYPE => 'integer',
375 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
376 ),
377 'endid' => array(
378 ApiBase::PARAM_TYPE => 'integer',
379 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
380 ),
381 'start' => array(
382 ApiBase::PARAM_TYPE => 'timestamp',
383 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
384 ),
385 'end' => array(
386 ApiBase::PARAM_TYPE => 'timestamp',
387 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
388 ),
389 'dir' => array(
390 ApiBase::PARAM_DFLT => 'older',
391 ApiBase::PARAM_TYPE => array(
392 'newer',
393 'older'
394 ),
395 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
396 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
397 ),
398 'user' => array(
399 ApiBase::PARAM_TYPE => 'user',
400 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
401 ),
402 'excludeuser' => array(
403 ApiBase::PARAM_TYPE => 'user',
404 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
405 ),
406 'tag' => null,
407 'token' => array(
408 ApiBase::PARAM_DEPRECATED => true,
409 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
410 ApiBase::PARAM_ISMULTI => true
411 ),
412 'continue' => array(
413 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
414 ),
415 );
416
417 $ret['limit'][ApiBase::PARAM_HELP_MSG_INFO] = array( array( 'singlepageonly' ) );
418
419 return $ret;
420 }
421
422 protected function getExamplesMessages() {
423 return array(
424 'action=query&prop=revisions&titles=API|Main%20Page&' .
425 'rvprop=timestamp|user|comment|content'
426 => 'apihelp-query+revisions-example-content',
427 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
428 'rvprop=timestamp|user|comment'
429 => 'apihelp-query+revisions-example-last5',
430 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
431 'rvprop=timestamp|user|comment&rvdir=newer'
432 => 'apihelp-query+revisions-example-first5',
433 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
434 'rvprop=timestamp|user|comment&rvdir=newer&rvstart=2006-05-01T00:00:00Z'
435 => 'apihelp-query+revisions-example-first5-after',
436 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
437 'rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1'
438 => 'apihelp-query+revisions-example-first5-not-localhost',
439 'action=query&prop=revisions&titles=Main%20Page&rvlimit=5&' .
440 'rvprop=timestamp|user|comment&rvuser=MediaWiki%20default'
441 => 'apihelp-query+revisions-example-first5-user',
442 );
443 }
444
445 public function getHelpUrls() {
446 return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';
447 }
448 }