MediaWiki master
ApiQueryDeletedRevisions.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Api;
27
45
52
53 private RevisionStore $revisionStore;
54 private NameTableStore $changeTagDefStore;
55 private ChangeTagsStore $changeTagsStore;
56 private LinkBatchFactory $linkBatchFactory;
57
58 public function __construct(
59 ApiQuery $query,
60 string $moduleName,
61 RevisionStore $revisionStore,
62 IContentHandlerFactory $contentHandlerFactory,
63 ParserFactory $parserFactory,
64 SlotRoleRegistry $slotRoleRegistry,
65 NameTableStore $changeTagDefStore,
66 ChangeTagsStore $changeTagsStore,
67 LinkBatchFactory $linkBatchFactory,
68 ContentRenderer $contentRenderer,
69 ContentTransformer $contentTransformer,
70 CommentFormatter $commentFormatter,
71 TempUserCreator $tempUserCreator,
72 UserFactory $userFactory
73 ) {
74 parent::__construct(
75 $query,
76 $moduleName,
77 'drv',
78 $revisionStore,
79 $contentHandlerFactory,
80 $parserFactory,
81 $slotRoleRegistry,
82 $contentRenderer,
83 $contentTransformer,
84 $commentFormatter,
85 $tempUserCreator,
86 $userFactory
87 );
88 $this->revisionStore = $revisionStore;
89 $this->changeTagDefStore = $changeTagDefStore;
90 $this->changeTagsStore = $changeTagsStore;
91 $this->linkBatchFactory = $linkBatchFactory;
92 }
93
94 protected function run( ?ApiPageSet $resultPageSet = null ) {
95 $pageSet = $this->getPageSet();
96 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
97 $pageCount = count( $pageSet->getGoodAndMissingPages() );
98 $revCount = $pageSet->getRevisionCount();
99 if ( $revCount === 0 && $pageCount === 0 ) {
100 // Nothing to do
101 return;
102 }
103 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
104 // Nothing to do, revisions were supplied but none are deleted
105 return;
106 }
107
108 $params = $this->extractRequestParams( false );
109
110 $db = $this->getDB();
111
112 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
113
114 if ( $resultPageSet === null ) {
115 $this->parseParameters( $params );
116 $arQuery = $this->revisionStore->getArchiveQueryInfo();
117 $this->addTables( $arQuery['tables'] );
118 $this->addFields( $arQuery['fields'] );
119 $this->addJoinConds( $arQuery['joins'] );
120 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
121 } else {
122 $this->limit = $this->getParameter( 'limit' ) ?: 10;
123 $this->addTables( 'archive' );
124 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
125 }
126
127 if ( $this->fld_tags ) {
128 $this->addFields( [
129 'ts_tags' => $this->changeTagsStore->makeTagSummarySubquery( 'archive' )
130 ] );
131 }
132
133 if ( $params['tag'] !== null ) {
134 $this->addTables( 'change_tag' );
135 $this->addJoinConds(
136 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
137 );
138 try {
139 $this->addWhereFld( 'ct_tag_id', $this->changeTagDefStore->getId( $params['tag'] ) );
140 } catch ( NameTableAccessException $exception ) {
141 // Return nothing.
142 $this->addWhere( '1=0' );
143 }
144 }
145
146 // This means stricter restrictions
147 if ( ( $this->fld_comment || $this->fld_parsedcomment ) &&
148 !$this->getAuthority()->isAllowed( 'deletedhistory' )
149 ) {
150 $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
151 }
152 if ( $this->fetchContent && !$this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' ) ) {
153 $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
154 }
155
156 $dir = $params['dir'];
157
158 if ( $revCount !== 0 ) {
159 $this->addWhere( [
160 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
161 ] );
162 } else {
163 // We need a custom WHERE clause that matches all titles.
164 $lb = $this->linkBatchFactory->newLinkBatch( $pageSet->getGoodAndMissingPages() );
165 $where = $lb->constructSet( 'ar', $db );
166 $this->addWhere( $where );
167 }
168
169 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
170 // In the non-generator case, the actor join will already be present.
171 if ( $resultPageSet !== null ) {
172 $this->addTables( 'actor' );
173 $this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id=ar_actor' ] ] );
174 }
175 if ( $params['user'] !== null ) {
176 $this->addWhereFld( 'actor_name', $params['user'] );
177 } elseif ( $params['excludeuser'] !== null ) {
178 $this->addWhere( $db->expr( 'actor_name', '!=', $params['excludeuser'] ) );
179 }
180 }
181
182 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
183 // Paranoia: avoid brute force searches (T19342)
184 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
185 $bitmask = RevisionRecord::DELETED_USER;
186 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
187 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
188 } else {
189 $bitmask = 0;
190 }
191 if ( $bitmask ) {
192 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
193 }
194 }
195
196 if ( $params['continue'] !== null ) {
197 $op = ( $dir == 'newer' ? '>=' : '<=' );
198 if ( $revCount !== 0 ) {
199 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'int' ] );
200 $this->addWhere( $db->buildComparison( $op, [
201 'ar_rev_id' => $cont[0],
202 'ar_id' => $cont[1],
203 ] ) );
204 } else {
205 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string', 'timestamp', 'int' ] );
206 $this->addWhere( $db->buildComparison( $op, [
207 'ar_namespace' => $cont[0],
208 'ar_title' => $cont[1],
209 'ar_timestamp' => $db->timestamp( $cont[2] ),
210 'ar_id' => $cont[3],
211 ] ) );
212 }
213 }
214
215 $this->addOption( 'LIMIT', $this->limit + 1 );
216
217 if ( $revCount !== 0 ) {
218 // Sort by ar_rev_id when querying by ar_rev_id
219 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
220 } else {
221 // Sort by ns and title in the same order as timestamp for efficiency
222 // But only when not already unique in the query
223 if ( count( $pageMap ) > 1 ) {
224 $this->addWhereRange( 'ar_namespace', $dir, null, null );
225 }
226 $oneTitle = key( reset( $pageMap ) );
227 foreach ( $pageMap as $pages ) {
228 if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
229 $this->addWhereRange( 'ar_title', $dir, null, null );
230 break;
231 }
232 }
233 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
234 }
235 // Include in ORDER BY for uniqueness
236 $this->addWhereRange( 'ar_id', $dir, null, null );
237
238 $res = $this->select( __METHOD__ );
239 $count = 0;
240 $generated = [];
241 foreach ( $res as $row ) {
242 if ( ++$count > $this->limit ) {
243 // We've had enough
244 $this->setContinueEnumParameter( 'continue',
245 $revCount
246 ? "$row->ar_rev_id|$row->ar_id"
247 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
248 );
249 break;
250 }
251
252 if ( $resultPageSet !== null ) {
253 $generated[] = $row->ar_rev_id;
254 } else {
255 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
256 // Was it converted?
257 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
258 $converted = $pageSet->getConvertedTitles();
259 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
260 $title = Title::newFromText( $converted[$title->getPrefixedText()] );
261 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
262 $pageMap[$row->ar_namespace][$row->ar_title] =
263 $pageMap[$title->getNamespace()][$title->getDBkey()];
264 }
265 }
266 }
267 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
269 __METHOD__,
270 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
271 );
272 }
273
274 $fit = $this->addPageSubItem(
275 $pageMap[$row->ar_namespace][$row->ar_title],
276 $this->extractRevisionInfo( $this->revisionStore->newRevisionFromArchiveRow( $row ), $row ),
277 'rev'
278 );
279 if ( !$fit ) {
280 $this->setContinueEnumParameter( 'continue',
281 $revCount
282 ? "$row->ar_rev_id|$row->ar_id"
283 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
284 );
285 break;
286 }
287 }
288 }
289
290 if ( $resultPageSet !== null ) {
291 $resultPageSet->populateFromRevisionIDs( $generated );
292 }
293 }
294
295 public function getAllowedParams() {
296 return parent::getAllowedParams() + [
297 'start' => [
298 ParamValidator::PARAM_TYPE => 'timestamp',
299 ],
300 'end' => [
301 ParamValidator::PARAM_TYPE => 'timestamp',
302 ],
303 'dir' => [
304 ParamValidator::PARAM_TYPE => [
305 'newer',
306 'older'
307 ],
308 ParamValidator::PARAM_DEFAULT => 'older',
309 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
311 'newer' => 'api-help-paramvalue-direction-newer',
312 'older' => 'api-help-paramvalue-direction-older',
313 ],
314 ],
315 'tag' => null,
316 'user' => [
317 ParamValidator::PARAM_TYPE => 'user',
318 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
319 ],
320 'excludeuser' => [
321 ParamValidator::PARAM_TYPE => 'user',
322 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
323 ],
324 'continue' => [
325 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
326 ],
327 ];
328 }
329
330 protected function getExamplesMessages() {
331 $title = Title::newMainPage();
332 $talkTitle = $title->getTalkPageIfDefined();
333 $examples = [
334 'action=query&prop=deletedrevisions&revids=123456'
335 => 'apihelp-query+deletedrevisions-example-revids',
336 ];
337
338 if ( $talkTitle ) {
339 $title = rawurlencode( $title->getPrefixedText() );
340 $talkTitle = rawurlencode( $talkTitle->getPrefixedText() );
341 $examples["action=query&prop=deletedrevisions&titles={$title}|{$talkTitle}&" .
342 'drvslots=*&drvprop=user|comment|content'] = 'apihelp-query+deletedrevisions-example-titles';
343 }
344
345 return $examples;
346 }
347
348 public function getHelpUrls() {
349 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
350 }
351}
352
354class_alias( ApiQueryDeletedRevisions::class, 'ApiQueryDeletedRevisions' );
array $params
The job parameters.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1565
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1756
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:224
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
Definition ApiBase.php:1025
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1808
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:184
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:973
This class contains a list of pages that the client has requested.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
select( $method, $extraQuery=[], ?array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
Query module to enumerate deleted revisions for pages.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiQuery $query, string $moduleName, RevisionStore $revisionStore, IContentHandlerFactory $contentHandlerFactory, ParserFactory $parserFactory, SlotRoleRegistry $slotRoleRegistry, NameTableStore $changeTagDefStore, ChangeTagsStore $changeTagsStore, LinkBatchFactory $linkBatchFactory, ContentRenderer $contentRenderer, ContentTransformer $contentTransformer, CommentFormatter $commentFormatter, TempUserCreator $tempUserCreator, UserFactory $userFactory)
getHelpUrls()
Return links to more detailed help pages about the module.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
getPageSet()
Get the PageSet object to work on.
A base class for functions common to producing a list of revisions.
parseParameters( $params)
Parse the parameters into the various instance fields.
This is the main query class.
Definition ApiQuery.php:48
Read-write access to the change_tags table.
This is the main service interface for converting single-line comments from various DB comment fields...
Type definition for user types.
Definition UserDef.php:27
Page revision base class.
Service for looking up page revisions.
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
Exception representing a failure to look up a row from a name table.
Represents a title within MediaWiki.
Definition Title.php:78
Service for temporary user creation.
Create User objects.
Service for formatting and validating API parameters.
addTables( $tables, $alias=null)
addWhere( $conds)
addJoinConds( $conds)
addFields( $fields)