Debian Codegraph @

includes/api/ApiQueryRevisions.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Api;
24
25 use MediaWiki\CommentFormatter\CommentFormatter;
26 use MediaWiki\Content\IContentHandlerFactory;
27 use MediaWiki\Content\Renderer\ContentRenderer;
28 use MediaWiki\Content\Transform\ContentTransformer;
29 use MediaWiki\MediaWikiServices;
30 use MediaWiki\Page\PageIdentity;
31 use MediaWiki\ParamValidator\TypeDef\UserDef;
32 use MediaWiki\Parser\ParserFactory;
33 use MediaWiki\Revision\RevisionRecord;
34 use MediaWiki\Revision\RevisionStore;
35 use MediaWiki\Revision\SlotRoleRegistry;
36 use MediaWiki\Status\Status;
37 use MediaWiki\Storage\NameTableAccessException;
38 use MediaWiki\Storage\NameTableStore;
39 use MediaWiki\Title\Title;
40 use MediaWiki\Title\TitleFormatter;
41 use MediaWiki\User\ActorMigration;
42 use MediaWiki\User\TempUser\TempUserCreator;
43 use MediaWiki\User\UserFactory;
44 use Wikimedia\ParamValidator\ParamValidator;
45
46 /**
47 * A query action to enumerate revisions of a given page, or show top revisions
48 * of multiple pages. Various pieces of information may be shown - flags,
49 * comments, and the actual wiki markup of the rev. In the enumeration mode,
50 * ranges of revisions may be requested and filtered.
51 *
52 * @ingroup API
53 */
54 class ApiQueryRevisions extends ApiQueryRevisionsBase {
55
56 private RevisionStore $revisionStore;
57 private NameTableStore $changeTagDefStore;
58 private ActorMigration $actorMigration;
59 private TitleFormatter $titleFormatter;
60
61 public function __construct(
62 ApiQuery $query,
63 string $moduleName,
64 RevisionStore $revisionStore,
65 IContentHandlerFactory $contentHandlerFactory,
66 ParserFactory $parserFactory,
67 SlotRoleRegistry $slotRoleRegistry,
68 NameTableStore $changeTagDefStore,
69 ActorMigration $actorMigration,
70 ContentRenderer $contentRenderer,
71 ContentTransformer $contentTransformer,
72 CommentFormatter $commentFormatter,
73 TempUserCreator $tempUserCreator,
74 UserFactory $userFactory,
75 TitleFormatter $titleFormatter
76 ) {
77 parent::__construct(
78 $query,
79 $moduleName,
80 'rv',
81 $revisionStore,
82 $contentHandlerFactory,
83 $parserFactory,
84 $slotRoleRegistry,
85 $contentRenderer,
86 $contentTransformer,
87 $commentFormatter,
88 $tempUserCreator,
89 $userFactory
90 );
91 $this->revisionStore = $revisionStore;
92 $this->changeTagDefStore = $changeTagDefStore;
93 $this->actorMigration = $actorMigration;
94 $this->titleFormatter = $titleFormatter;
95 }
96
97 protected function run( ?ApiPageSet $resultPageSet = null ) {
98 $params = $this->extractRequestParams( false );
99
100 // If any of those parameters are used, work in 'enumeration' mode.
101 // Enum mode can only be used when exactly one page is provided.
102 // Enumerating revisions on multiple pages make it extremely
103 // difficult to manage continuations and require additional SQL indexes
104 $enumRevMode = ( $params['user'] !== null || $params['excludeuser'] !== null ||
105 $params['limit'] !== null || $params['startid'] !== null ||
106 $params['endid'] !== null || $params['dir'] === 'newer' ||
107 $params['start'] !== null || $params['end'] !== null );
108
109 $pageSet = $this->getPageSet();
110 $pageCount = $pageSet->getGoodTitleCount();
111 $revCount = $pageSet->getRevisionCount();
112
113 // Optimization -- nothing to do
114 if ( $revCount === 0 && $pageCount === 0 ) {
115 // Nothing to do
116 return;
117 }
118 if ( $revCount > 0 && count( $pageSet->getLiveRevisionIDs() ) === 0 ) {
119 // We're in revisions mode but all given revisions are deleted
120 return;
121 }
122
123 if ( $revCount > 0 && $enumRevMode ) {
124 $this->dieWithError(
125 [ 'apierror-revisions-norevids', $this->getModulePrefix() ], 'invalidparammix'
126 );
127 }
128
129 if ( $pageCount > 1 && $enumRevMode ) {
130 $this->dieWithError(
131 [ 'apierror-revisions-singlepage', $this->getModulePrefix() ], 'invalidparammix'
132 );
133 }
134
135 // In non-enum mode, rvlimit can't be directly used. Use the maximum
136 // allowed value.
137 if ( !$enumRevMode ) {
138 $this->setParsedLimit = false;
139 $params['limit'] = 'max';
140 }
141
142 $db = $this->getDB();
143
144 $idField = 'rev_id';
145 $tsField = 'rev_timestamp';
146 $pageField = 'rev_page';
147
148 $ignoreIndex = [
149 // T224017: `rev_timestamp` is never the correct index to use for this module, but
150 // MariaDB sometimes insists on trying to use it anyway. Tell it not to.
151 // Last checked with MariaDB 10.4.13
152 'revision' => 'rev_timestamp',
153 ];
154 $useIndex = [];
155 if ( $resultPageSet === null ) {
156 $this->parseParameters( $params );
157 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $db )
158 ->joinComment()
159 ->joinPage();
160 if ( $this->fld_user ) {
161 $queryBuilder->joinUser();
162 }
163 $this->getQueryBuilder()->merge( $queryBuilder );
164 } else {
165 $this->limit = $this->getParameter( 'limit' ) ?: 10;
166 // Always join 'page' so orphaned revisions are filtered out
167 $this->addTables( [ 'revision', 'page' ] );
168 $this->addJoinConds(
169 [ 'page' => [ 'JOIN', [ 'page_id = rev_page' ] ] ]
170 );
171 $this->addFields( [
172 'rev_id' => $idField, 'rev_timestamp' => $tsField, 'rev_page' => $pageField
173 ] );
174 }
175
176 if ( $this->fld_tags ) {
177 $this->addFields( [
178 'ts_tags' => MediaWikiServices::getInstance()->getChangeTagsStore()
179 ->makeTagSummarySubquery( 'revision' )
180 ] );
181 }
182
183 if ( $params['tag'] !== null ) {
184 $this->addTables( 'change_tag' );
185 $this->addJoinConds(
186 [ 'change_tag' => [ 'JOIN', [ 'rev_id=ct_rev_id' ] ] ]
187 );
188 try {
189 $this->addWhereFld( 'ct_tag_id', $this->changeTagDefStore->getId( $params['tag'] ) );
190 } catch ( NameTableAccessException $exception ) {
191 // Return nothing.
192 $this->addWhere( '1=0' );
193 }
194 }
195
196 if ( $resultPageSet === null && $this->fetchContent ) {
197 // For each page we will request, the user must have read rights for that page
198 $status = Status::newGood();
199
200 /** @var PageIdentity $pageIdentity */
201 foreach ( $pageSet->getGoodPages() as $pageIdentity ) {
202 if ( !$this->getAuthority()->authorizeRead( 'read', $pageIdentity ) ) {
203 $status->fatal( ApiMessage::create(
204 [
205 'apierror-cannotviewtitle',
206 wfEscapeWikiText( $this->titleFormatter->getPrefixedText( $pageIdentity ) ),
207 ],
208 'accessdenied'
209 ) );
210 }
211 }
212 if ( !$status->isGood() ) {
213 $this->dieStatus( $status );
214 }
215 }
216
217 if ( $enumRevMode ) {
218 // Indexes targeted:
219 // page_timestamp if we don't have rvuser
220 // page_actor_timestamp (on revision_actor_temp) if we have rvuser in READ_NEW mode
221 // page_user_timestamp if we have a logged-in rvuser
222 // page_timestamp or usertext_timestamp if we have an IP rvuser
223
224 // This is mostly to prevent parameter errors (and optimize SQL?)
225 $this->requireMaxOneParameter( $params, 'startid', 'start' );
226 $this->requireMaxOneParameter( $params, 'endid', 'end' );
227 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
228
229 if ( $params['continue'] !== null ) {
230 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int' ] );
231 $op = ( $params['dir'] === 'newer' ? '>=' : '<=' );
232 $continueTimestamp = $db->timestamp( $cont[0] );
233 $continueId = (int)$cont[1];
234 $this->addWhere( $db->buildComparison( $op, [
235 $tsField => $continueTimestamp,
236 $idField => $continueId,
237 ] ) );
238 }
239
240 // Convert startid/endid to timestamps (T163532)
241 $revids = [];
242 if ( $params['startid'] !== null ) {
243 $revids[] = (int)$params['startid'];
244 }
245 if ( $params['endid'] !== null ) {
246 $revids[] = (int)$params['endid'];
247 }
248 if ( $revids ) {
249 $db = $this->getDB();
250 $uqb = $db->newUnionQueryBuilder();
251 $uqb->add(
252 $db->newSelectQueryBuilder()
253 ->select( [ 'id' => 'rev_id', 'ts' => 'rev_timestamp' ] )
254 ->from( 'revision' )
255 ->where( [ 'rev_id' => $revids ] )
256 );
257 $uqb->add(
258 $db->newSelectQueryBuilder()
259 ->select( [ 'id' => 'ar_rev_id', 'ts' => 'ar_timestamp' ] )
260 ->from( 'archive' )
261 ->where( [ 'ar_rev_id' => $revids ] )
262 );
263 $res = $uqb->caller( __METHOD__ )->fetchResultSet();
264 foreach ( $res as $row ) {
265 if ( (int)$row->id === (int)$params['startid'] ) {
266 $params['start'] = $row->ts;
267 }
268 if ( (int)$row->id === (int)$params['endid'] ) {
269 $params['end'] = $row->ts;
270 }
271 }
272 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
273 if ( $params['startid'] !== null && $params['start'] === null ) {
274 $p = $this->encodeParamName( 'startid' );
275 $this->dieWithError( [ 'apierror-revisions-badid', $p ], "badid_$p" );
276 }
277 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
278 if ( $params['endid'] !== null && $params['end'] === null ) {
279 $p = $this->encodeParamName( 'endid' );
280 $this->dieWithError( [ 'apierror-revisions-badid', $p ], "badid_$p" );
281 }
282
283 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
284 if ( $params['start'] !== null ) {
285 $op = ( $params['dir'] === 'newer' ? '>=' : '<=' );
286 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
287 $ts = $db->timestampOrNull( $params['start'] );
288 if ( $params['startid'] !== null ) {
289 $this->addWhere( $db->buildComparison( $op, [
290 $tsField => $ts,
291 $idField => (int)$params['startid'],
292 ] ) );
293 } else {
294 $this->addWhere( $db->buildComparison( $op, [ $tsField => $ts ] ) );
295 }
296 }
297 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
298 if ( $params['end'] !== null ) {
299 $op = ( $params['dir'] === 'newer' ? '<=' : '>=' ); // Yes, opposite of the above
300 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
301 $ts = $db->timestampOrNull( $params['end'] );
302 if ( $params['endid'] !== null ) {
303 $this->addWhere( $db->buildComparison( $op, [
304 $tsField => $ts,
305 $idField => (int)$params['endid'],
306 ] ) );
307 } else {
308 $this->addWhere( $db->buildComparison( $op, [ $tsField => $ts ] ) );
309 }
310 }
311 } else {
312 $this->addTimestampWhereRange( $tsField, $params['dir'],
313 $params['start'], $params['end'] );
314 }
315
316 $sort = ( $params['dir'] === 'newer' ? '' : 'DESC' );
317 $this->addOption( 'ORDER BY', [ "rev_timestamp $sort", "rev_id $sort" ] );
318
319 // There is only one ID, use it
320 $ids = array_keys( $pageSet->getGoodPages() );
321 $this->addWhereFld( $pageField, reset( $ids ) );
322
323 if ( $params['user'] !== null ) {
324 $actorQuery = $this->actorMigration->getWhere( $db, 'rev_user', $params['user'] );
325 $this->addTables( $actorQuery['tables'] );
326 $this->addJoinConds( $actorQuery['joins'] );
327 $this->addWhere( $actorQuery['conds'] );
328 } elseif ( $params['excludeuser'] !== null ) {
329 $actorQuery = $this->actorMigration->getWhere( $db, 'rev_user', $params['excludeuser'] );
330 $this->addTables( $actorQuery['tables'] );
331 $this->addJoinConds( $actorQuery['joins'] );
332 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
333 } else {
334 // T258480: MariaDB ends up using rev_page_actor_timestamp in some cases here.
335 // Last checked with MariaDB 10.4.13
336 // Unless we are filtering by user (see above), we always want to use the
337 // "history" index on the revision table, namely page_timestamp.
338 $useIndex['revision'] = 'rev_page_timestamp';
339 }
340
341 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
342 // Paranoia: avoid brute force searches (T19342)
343 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
344 $bitmask = RevisionRecord::DELETED_USER;
345 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
346 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
347 } else {
348 $bitmask = 0;
349 }
350 if ( $bitmask ) {
351 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
352 }
353 }
354 } elseif ( $revCount > 0 ) {
355 // Always targets the PRIMARY index
356
357 $revs = $pageSet->getLiveRevisionIDs();
358
359 // Get all revision IDs
360 $this->addWhereFld( 'rev_id', array_keys( $revs ) );
361
362 if ( $params['continue'] !== null ) {
363 $this->addWhere( $db->buildComparison( '>=', [
364 'rev_id' => (int)$params['continue']
365 ] ) );
366 }
367 $this->addOption( 'ORDER BY', 'rev_id' );
368 } elseif ( $pageCount > 0 ) {
369 // Always targets the rev_page_id index
370
371 $pageids = array_keys( $pageSet->getGoodPages() );
372
373 // When working in multi-page non-enumeration mode,
374 // limit to the latest revision only
375 $this->addWhere( 'page_latest=rev_id' );
376
377 // Get all page IDs
378 $this->addWhereFld( 'page_id', $pageids );
379 // Every time someone relies on equality propagation, god kills a kitten :)
380 $this->addWhereFld( 'rev_page', $pageids );
381
382 if ( $params['continue'] !== null ) {
383 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'int' ] );
384 $this->addWhere( $db->buildComparison( '>=', [
385 'rev_page' => $cont[0],
386 'rev_id' => $cont[1],
387 ] ) );
388 }
389 $this->addOption( 'ORDER BY', [
390 'rev_page',
391 'rev_id'
392 ] );
393 } else {
394 ApiBase::dieDebug( __METHOD__, 'param validation?' );
395 }
396
397 $this->addOption( 'LIMIT', $this->limit + 1 );
398
399 $this->addOption( 'IGNORE INDEX', $ignoreIndex );
400
401 if ( $useIndex ) {
402 $this->addOption( 'USE INDEX', $useIndex );
403 }
404
405 $count = 0;
406 $generated = [];
407 $hookData = [];
408 $res = $this->select( __METHOD__, [], $hookData );
409
410 foreach ( $res as $row ) {
411 if ( ++$count > $this->limit ) {
412 // We've reached the one extra which shows that there are
413 // additional pages to be had. Stop here...
414 if ( $enumRevMode ) {
415 $this->setContinueEnumParameter( 'continue',
416 $row->rev_timestamp . '|' . (int)$row->rev_id );
417 } elseif ( $revCount > 0 ) {
418 $this->setContinueEnumParameter( 'continue', (int)$row->rev_id );
419 } else {
420 $this->setContinueEnumParameter( 'continue', (int)$row->rev_page .
421 '|' . (int)$row->rev_id );
422 }
423 break;
424 }
425
426 if ( $resultPageSet !== null ) {
427 $generated[] = $row->rev_id;
428 } else {
429 $revision = $this->revisionStore->newRevisionFromRow( $row, 0, Title::newFromRow( $row ) );
430 $rev = $this->extractRevisionInfo( $revision, $row );
431 $fit = $this->processRow( $row, $rev, $hookData ) &&
432 $this->addPageSubItem( $row->rev_page, $rev, 'rev' );
433 if ( !$fit ) {
434 if ( $enumRevMode ) {
435 $this->setContinueEnumParameter( 'continue',
436 $row->rev_timestamp . '|' . (int)$row->rev_id );
437 } elseif ( $revCount > 0 ) {
438 $this->setContinueEnumParameter( 'continue', (int)$row->rev_id );
439 } else {
440 $this->setContinueEnumParameter( 'continue', (int)$row->rev_page .
441 '|' . (int)$row->rev_id );
442 }
443 break;
444 }
445 }
446 }
447
448 if ( $resultPageSet !== null ) {
449 $resultPageSet->populateFromRevisionIDs( $generated );
450 }
451 }
452
453 public function getAllowedParams() {
454 $ret = parent::getAllowedParams() + [
455 'startid' => [
456 ParamValidator::PARAM_TYPE => 'integer',
457 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
458 ],
459 'endid' => [
460 ParamValidator::PARAM_TYPE => 'integer',
461 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
462 ],
463 'start' => [
464 ParamValidator::PARAM_TYPE => 'timestamp',
465 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
466 ],
467 'end' => [
468 ParamValidator::PARAM_TYPE => 'timestamp',
469 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
470 ],
471 'dir' => [
472 ParamValidator::PARAM_DEFAULT => 'older',
473 ParamValidator::PARAM_TYPE => [
474 'newer',
475 'older'
476 ],
477 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
478 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
479 'newer' => 'api-help-paramvalue-direction-newer',
480 'older' => 'api-help-paramvalue-direction-older',
481 ],
482 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
483 ],
484 'user' => [
485 ParamValidator::PARAM_TYPE => 'user',
486 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
487 UserDef::PARAM_RETURN_OBJECT => true,
488 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
489 ],
490 'excludeuser' => [
491 ParamValidator::PARAM_TYPE => 'user',
492 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
493 UserDef::PARAM_RETURN_OBJECT => true,
494 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'singlepageonly' ] ],
495 ],
496 'tag' => null,
497 'continue' => [
498 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
499 ],
500 ];
501
502 $ret['limit'][ApiBase::PARAM_HELP_MSG_INFO] = [ [ 'singlepageonly' ] ];
503
504 return $ret;
505 }
506
507 protected function getExamplesMessages() {
508 $title = Title::newMainPage()->getPrefixedText();
509 $mp = rawurlencode( $title );
510
511 return [
512 "action=query&prop=revisions&titles=API|{$mp}&" .
513 'rvslots=*&rvprop=timestamp|user|comment|content'
514 => 'apihelp-query+revisions-example-content',
515 "action=query&prop=revisions&titles={$mp}&rvlimit=5&" .
516 'rvprop=timestamp|user|comment'
517 => 'apihelp-query+revisions-example-last5',
518 "action=query&prop=revisions&titles={$mp}&rvlimit=5&" .
519 'rvprop=timestamp|user|comment&rvdir=newer'
520 => 'apihelp-query+revisions-example-first5',
521 "action=query&prop=revisions&titles={$mp}&rvlimit=5&" .
522 'rvprop=timestamp|user|comment&rvdir=newer&rvstart=2006-05-01T00:00:00Z'
523 => 'apihelp-query+revisions-example-first5-after',
524 "action=query&prop=revisions&titles={$mp}&rvlimit=5&" .
525 'rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1'
526 => 'apihelp-query+revisions-example-first5-not-localhost',
527 "action=query&prop=revisions&titles={$mp}&rvlimit=5&" .
528 'rvprop=timestamp|user|comment&rvuser=MediaWiki%20default'
529 => 'apihelp-query+revisions-example-first5-user',
530 ];
531 }
532
533 public function getHelpUrls() {
534 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Revisions';
535 }
536 }
537
538 /** @deprecated class alias since 1.43 */
539 class_alias( ApiQueryRevisions::class, 'ApiQueryRevisions' );