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