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