Merge "Get to the point about howto download Vector :p"
[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 JSON callback mode, no tokens can be obtained
57 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
58 return array();
59 }
60
61 $this->tokenFunctions = array(
62 'rollback' => array( 'ApiQueryRevisions', 'getRollbackToken' )
63 );
64 Hooks::run( 'APIQueryRevisionsTokens', array( &$this->tokenFunctions ) );
65
66 return $this->tokenFunctions;
67 }
68
69 /**
70 * @deprecated since 1.24
71 * @param int $pageid
72 * @param Title $title
73 * @param Revision $rev
74 * @return bool|string
75 */
76 public static function getRollbackToken( $pageid, $title, $rev ) {
77 global $wgUser;
78 if ( !$wgUser->isAllowed( 'rollback' ) ) {
79 return false;
80 }
81
82 return $wgUser->getEditToken(
83 array( $title->getPrefixedText(), $rev->getUserText() ) );
84 }
85
86 protected function run( ApiPageSet $resultPageSet = null ) {
87 $params = $this->extractRequestParams( false );
88
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'] ) );
97
98 $pageSet = $this->getPageSet();
99 $pageCount = $pageSet->getGoodTitleCount();
100 $revCount = $pageSet->getRevisionCount();
101
102 // Optimization -- nothing to do
103 if ( $revCount === 0 && $pageCount === 0 ) {
104 // Nothing to do
105 return;
106 }
107 if ( $revCount > 0 && count( $pageSet->getLiveRevisionIDs() ) === 0 ) {
108 // We're in revisions mode but all given revisions are deleted
109 return;
110 }
111
112 if ( $revCount > 0 && $enumRevMode ) {
113 $this->dieUsage(
114 'The revids= parameter may not be used with the list options ' .
115 '(limit, startid, endid, dirNewer, start, end).',
116 'revids'
117 );
118 }
119
120 if ( $pageCount > 1 && $enumRevMode ) {
121 $this->dieUsage(
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.',
125 'multpages'
126 );
127 }
128
129 // In non-enum mode, rvlimit can't be directly used. Use the maximum
130 // allowed value.
131 if ( !$enumRevMode ) {
132 $this->setParsedLimit = false;
133 $params['limit'] = 'max';
134 }
135
136 $db = $this->getDB();
137 $this->addTables( array( 'revision', 'page' ) );
138 $this->addJoinConds(
139 array( 'page' => array( 'INNER JOIN', array( 'page_id = rev_page' ) ) )
140 );
141
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() );
148 }
149 } else {
150 $this->limit = $this->getParameter( 'limit' ) ?: 10;
151 $this->addFields( array( 'rev_id', 'rev_page' ) );
152 }
153
154 if ( $this->fld_tags ) {
155 $this->addTables( 'tag_summary' );
156 $this->addJoinConds(
157 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
158 );
159 $this->addFields( 'ts_tags' );
160 }
161
162 if ( !is_null( $params['tag'] ) ) {
163 $this->addTables( 'change_tag' );
164 $this->addJoinConds(
165 array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
166 );
167 $this->addWhereFld( 'ct_tag', $params['tag'] );
168 }
169
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 ) ) {
176 $this->dieUsage(
177 'The current user is not allowed to read ' . $title->getPrefixedText(),
178 'accessdenied' );
179 }
180 }
181
182 $this->addTables( 'text' );
183 $this->addJoinConds(
184 array( 'text' => array( 'INNER JOIN', array( 'rev_text_id=old_id' ) ) )
185 );
186 $this->addFields( 'old_id' );
187 $this->addFields( Revision::selectTextFields() );
188 }
189
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() );
195 }
196
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' );
201 }
202
203 if ( !is_null( $params['endid'] ) && !is_null( $params['end'] ) ) {
204 $this->dieUsage( 'end and endid cannot be used together', 'badparams' );
205 }
206
207 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
208 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
209 }
210
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;
219 }
220
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'] );
230 } else {
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 );
237 }
238
239 // There is only one ID, use it
240 $ids = array_keys( $pageSet->getGoodTitles() );
241 $this->addWhereFld( 'rev_page', reset( $ids ) );
242
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'] ) );
248 }
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;
255 } else {
256 $bitmask = 0;
257 }
258 if ( $bitmask ) {
259 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
260 }
261 }
262 } elseif ( $revCount > 0 ) {
263 $revs = $pageSet->getLiveRevisionIDs();
264
265 // Get all revision IDs
266 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
267
268 if ( !is_null( $params['continue'] ) ) {
269 $this->addWhere( 'rev_id >= ' . intval( $params['continue'] ) );
270 }
271 $this->addOption( 'ORDER BY', 'rev_id' );
272 } elseif ( $pageCount > 0 ) {
273 $titles = $pageSet->getGoodTitles();
274
275 // When working in multi-page non-enumeration mode,
276 // limit to the latest revision only
277 $this->addWhere( 'page_latest=rev_id' );
278
279 // Get all page IDs
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 ) );
283
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] );
289 $this->addWhere(
290 "rev_page > $pageid OR " .
291 "(rev_page = $pageid AND " .
292 "rev_id >= $revid)"
293 );
294 }
295 $this->addOption( 'ORDER BY', array(
296 'rev_page',
297 'rev_id'
298 ) );
299 } else {
300 ApiBase::dieDebug( __METHOD__, 'param validation?' );
301 }
302
303 $this->addOption( 'LIMIT', $this->limit + 1 );
304
305 $count = 0;
306 $generated = array();
307 $res = $this->select( __METHOD__ );
308
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 ) );
317 } else {
318 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
319 '|' . intval( $row->rev_id ) );
320 }
321 break;
322 }
323
324 if ( $resultPageSet !== null ) {
325 $generated[] = $row->rev_id;
326 } else {
327 $revision = new Revision( $row );
328 $rev = $this->extractRevisionInfo( $revision, $row );
329
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" );
337 } else {
338 $rev[$t . 'token'] = $val;
339 }
340 }
341 }
342
343 $fit = $this->addPageSubItem( $row->rev_page, $rev, 'rev' );
344 if ( !$fit ) {
345 if ( $enumRevMode ) {
346 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
347 } elseif ( $revCount > 0 ) {
348 $this->setContinueEnumParameter( 'continue', intval( $row->rev_id ) );
349 } else {
350 $this->setContinueEnumParameter( 'continue', intval( $row->rev_page ) .
351 '|' . intval( $row->rev_id ) );
352 }
353 break;
354 }
355 }
356 }
357
358 if ( $resultPageSet !== null ) {
359 $resultPageSet->populateFromRevisionIDs( $generated );
360 }
361 }
362
363 public function getCacheMode( $params ) {
364 if ( isset( $params['token'] ) ) {
365 return 'private';
366 }
367 return parent::getCacheMode( $params );
368 }
369
370 public function getAllowedParams() {
371 $ret = parent::getAllowedParams() + array(
372 'startid' => array(
373 ApiBase::PARAM_TYPE => 'integer',
374 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
375 ),
376 'endid' => array(
377 ApiBase::PARAM_TYPE => 'integer',
378 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
379 ),
380 'start' => array(
381 ApiBase::PARAM_TYPE => 'timestamp',
382 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
383 ),
384 'end' => array(
385 ApiBase::PARAM_TYPE => 'timestamp',
386 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
387 ),
388 'dir' => array(
389 ApiBase::PARAM_DFLT => 'older',
390 ApiBase::PARAM_TYPE => array(
391 'newer',
392 'older'
393 ),
394 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
395 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
396 ),
397 'user' => array(
398 ApiBase::PARAM_TYPE => 'user',
399 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
400 ),
401 'excludeuser' => array(
402 ApiBase::PARAM_TYPE => 'user',
403 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'singlepageonly' ) ),
404 ),
405 'tag' => null,
406 'token' => array(
407 ApiBase::PARAM_DEPRECATED => true,
408 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
409 ApiBase::PARAM_ISMULTI => true
410 ),
411 'continue' => array(
412 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
413 ),
414 );
415
416 $ret['limit'][ApiBase::PARAM_HELP_MSG_INFO] = array( array( 'singlepageonly' ) );
417
418 return $ret;
419 }
420
421 protected function getExamplesMessages() {
422 return array(
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',
441 );
442 }
443
444 public function getHelpUrls() {
445 return 'https://www.mediawiki.org/wiki/API:Properties#revisions_.2F_rv';
446 }
447 }