MediaWiki REL1_39
ApiQueryDeletedRevisions.php
Go to the documentation of this file.
1<?php
37
44
46 private $revisionStore;
47
49 private $changeTagDefStore;
50
52 private $linkBatchFactory;
53
66 public function __construct(
67 ApiQuery $query,
68 $moduleName,
69 RevisionStore $revisionStore,
70 IContentHandlerFactory $contentHandlerFactory,
71 ParserFactory $parserFactory,
72 SlotRoleRegistry $slotRoleRegistry,
73 NameTableStore $changeTagDefStore,
74 LinkBatchFactory $linkBatchFactory,
75 ContentRenderer $contentRenderer,
76 ContentTransformer $contentTransformer
77 ) {
78 parent::__construct(
79 $query,
80 $moduleName,
81 'drv',
82 $revisionStore,
83 $contentHandlerFactory,
84 $parserFactory,
85 $slotRoleRegistry,
86 $contentRenderer,
87 $contentTransformer
88 );
89 $this->revisionStore = $revisionStore;
90 $this->changeTagDefStore = $changeTagDefStore;
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( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
129 }
130
131 if ( $params['tag'] !== null ) {
132 $this->addTables( 'change_tag' );
133 $this->addJoinConds(
134 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
135 );
136 try {
137 $this->addWhereFld( 'ct_tag_id', $this->changeTagDefStore->getId( $params['tag'] ) );
138 } catch ( NameTableAccessException $exception ) {
139 // Return nothing.
140 $this->addWhere( '1=0' );
141 }
142 }
143
144 // This means stricter restrictions
145 if ( ( $this->fld_comment || $this->fld_parsedcomment ) &&
146 !$this->getAuthority()->isAllowed( 'deletedhistory' )
147 ) {
148 $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
149 }
150 if ( $this->fetchContent && !$this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' ) ) {
151 $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
152 }
153
154 $dir = $params['dir'];
155
156 if ( $revCount !== 0 ) {
157 $this->addWhere( [
158 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
159 ] );
160 } else {
161 // We need a custom WHERE clause that matches all titles.
162 $lb = $this->linkBatchFactory->newLinkBatch( $pageSet->getGoodAndMissingPages() );
163 $where = $lb->constructSet( 'ar', $db );
164 $this->addWhere( $where );
165 }
166
167 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
168 // In the non-generator case, the actor join will already be present.
169 if ( $resultPageSet !== null ) {
170 $this->addTables( 'actor' );
171 $this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id=ar_actor' ] ] );
172 }
173 if ( $params['user'] !== null ) {
174 $this->addWhereFld( 'actor_name', $params['user'] );
175 } elseif ( $params['excludeuser'] !== null ) {
176 $this->addWhere( 'actor_name<>' . $db->addQuotes( $params['excludeuser'] ) );
177 }
178 }
179
180 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
181 // Paranoia: avoid brute force searches (T19342)
182 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
183 $bitmask = RevisionRecord::DELETED_USER;
184 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
185 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
186 } else {
187 $bitmask = 0;
188 }
189 if ( $bitmask ) {
190 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
191 }
192 }
193
194 if ( $params['continue'] !== null ) {
195 $cont = explode( '|', $params['continue'] );
196 $op = ( $dir == 'newer' ? '>' : '<' );
197 if ( $revCount !== 0 ) {
198 $this->dieContinueUsageIf( count( $cont ) != 2 );
199 $rev = (int)$cont[0];
200 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
201 $ar_id = (int)$cont[1];
202 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
203 $this->addWhere( "ar_rev_id $op $rev OR " .
204 "(ar_rev_id = $rev AND " .
205 "ar_id $op= $ar_id)" );
206 } else {
207 $this->dieContinueUsageIf( count( $cont ) != 4 );
208 $ns = (int)$cont[0];
209 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
210 $title = $db->addQuotes( $cont[1] );
211 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
212 $ar_id = (int)$cont[3];
213 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
214 $this->addWhere( "ar_namespace $op $ns OR " .
215 "(ar_namespace = $ns AND " .
216 "(ar_title $op $title OR " .
217 "(ar_title = $title AND " .
218 "(ar_timestamp $op $ts OR " .
219 "(ar_timestamp = $ts AND " .
220 "ar_id $op= $ar_id)))))" );
221 }
222 }
223
224 $this->addOption( 'LIMIT', $this->limit + 1 );
225
226 if ( $revCount !== 0 ) {
227 // Sort by ar_rev_id when querying by ar_rev_id
228 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
229 } else {
230 // Sort by ns and title in the same order as timestamp for efficiency
231 // But only when not already unique in the query
232 if ( count( $pageMap ) > 1 ) {
233 $this->addWhereRange( 'ar_namespace', $dir, null, null );
234 }
235 $oneTitle = key( reset( $pageMap ) );
236 foreach ( $pageMap as $pages ) {
237 if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
238 $this->addWhereRange( 'ar_title', $dir, null, null );
239 break;
240 }
241 }
242 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
243 }
244 // Include in ORDER BY for uniqueness
245 $this->addWhereRange( 'ar_id', $dir, null, null );
246
247 $res = $this->select( __METHOD__ );
248 $count = 0;
249 $generated = [];
250 foreach ( $res as $row ) {
251 if ( ++$count > $this->limit ) {
252 // We've had enough
253 $this->setContinueEnumParameter( 'continue',
254 $revCount
255 ? "$row->ar_rev_id|$row->ar_id"
256 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
257 );
258 break;
259 }
260
261 if ( $resultPageSet !== null ) {
262 $generated[] = $row->ar_rev_id;
263 } else {
264 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
265 // Was it converted?
266 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
267 $converted = $pageSet->getConvertedTitles();
268 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
269 $title = Title::newFromText( $converted[$title->getPrefixedText()] );
270 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
271 $pageMap[$row->ar_namespace][$row->ar_title] =
272 $pageMap[$title->getNamespace()][$title->getDBkey()];
273 }
274 }
275 }
276 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
278 __METHOD__,
279 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
280 );
281 }
282
283 $fit = $this->addPageSubItem(
284 $pageMap[$row->ar_namespace][$row->ar_title],
285 $this->extractRevisionInfo( $this->revisionStore->newRevisionFromArchiveRow( $row ), $row ),
286 'rev'
287 );
288 if ( !$fit ) {
289 $this->setContinueEnumParameter( 'continue',
290 $revCount
291 ? "$row->ar_rev_id|$row->ar_id"
292 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
293 );
294 break;
295 }
296 }
297 }
298
299 if ( $resultPageSet !== null ) {
300 $resultPageSet->populateFromRevisionIDs( $generated );
301 }
302 }
303
304 public function getAllowedParams() {
305 return parent::getAllowedParams() + [
306 'start' => [
307 ParamValidator::PARAM_TYPE => 'timestamp',
308 ],
309 'end' => [
310 ParamValidator::PARAM_TYPE => 'timestamp',
311 ],
312 'dir' => [
313 ParamValidator::PARAM_TYPE => [
314 'newer',
315 'older'
316 ],
317 ParamValidator::PARAM_DEFAULT => 'older',
318 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
320 'newer' => 'api-help-paramvalue-direction-newer',
321 'older' => 'api-help-paramvalue-direction-older',
322 ],
323 ],
324 'tag' => null,
325 'user' => [
326 ParamValidator::PARAM_TYPE => 'user',
327 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
328 ],
329 'excludeuser' => [
330 ParamValidator::PARAM_TYPE => 'user',
331 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
332 ],
333 'continue' => [
334 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
335 ],
336 ];
337 }
338
339 protected function getExamplesMessages() {
340 $title = Title::newMainPage();
341 $talkTitle = $title->getTalkPageIfDefined();
342 $examples = [];
343
344 if ( $talkTitle ) {
345 $title = rawurlencode( $title->getPrefixedText() );
346 $talkTitle = rawurlencode( $talkTitle->getPrefixedText() );
347 $examples = [
348 "action=query&prop=deletedrevisions&titles={$title}|{$talkTitle}&" .
349 'drvslots=*&drvprop=user|comment|content'
350 => 'apihelp-query+deletedrevisions-example-titles',
351 ];
352 }
353
354 return array_merge( $examples, [
355 'action=query&prop=deletedrevisions&revids=123456'
356 => 'apihelp-query+deletedrevisions-example-revids',
357 ] );
358 }
359
360 public function getHelpUrls() {
361 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
362 }
363}
getAuthority()
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1454
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:886
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1643
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1656
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition ApiBase.php:196
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:938
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:163
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)
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:41
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
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.
Service for formatting and validating API parameters.