MediaWiki REL1_32
ApiQueryDeletedRevisions.php
Go to the documentation of this file.
1<?php
29
36
37 public function __construct( ApiQuery $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'drv' );
39 }
40
41 protected function run( ApiPageSet $resultPageSet = null ) {
43
44 $user = $this->getUser();
45 // Before doing anything at all, let's check permissions
46 $this->checkUserRightsAny( 'deletedhistory' );
47
48 $pageSet = $this->getPageSet();
49 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
50 $pageCount = count( $pageSet->getGoodAndMissingTitles() );
51 $revCount = $pageSet->getRevisionCount();
52 if ( $revCount === 0 && $pageCount === 0 ) {
53 // Nothing to do
54 return;
55 }
56 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
57 // Nothing to do, revisions were supplied but none are deleted
58 return;
59 }
60
61 $params = $this->extractRequestParams( false );
62
63 $db = $this->getDB();
64 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
65
66 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
67
68 if ( $resultPageSet === null ) {
69 $this->parseParameters( $params );
70 $arQuery = $revisionStore->getArchiveQueryInfo();
71 $this->addTables( $arQuery['tables'] );
72 $this->addFields( $arQuery['fields'] );
73 $this->addJoinConds( $arQuery['joins'] );
74 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
75 } else {
76 $this->limit = $this->getParameter( 'limit' ) ?: 10;
77 $this->addTables( 'archive' );
78 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
79 }
80
81 if ( $this->fld_tags ) {
82 $this->addTables( 'tag_summary' );
83 $this->addJoinConds(
84 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
85 );
86 $this->addFields( 'ts_tags' );
87 }
88
89 if ( !is_null( $params['tag'] ) ) {
90 $this->addTables( 'change_tag' );
91 $this->addJoinConds(
92 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
93 );
94 if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
95 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
96 try {
97 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
98 } catch ( NameTableAccessException $exception ) {
99 // Return nothing.
100 $this->addWhere( '1=0' );
101 }
102 } else {
103 $this->addWhereFld( 'ct_tag', $params['tag'] );
104 }
105 }
106
107 if ( $this->fetchContent ) {
108 $this->addTables( 'text' );
109 $this->addJoinConds(
110 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
111 );
112 $this->addFields( [ 'old_text', 'old_flags' ] );
113
114 // This also means stricter restrictions
115 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
116 }
117
118 $dir = $params['dir'];
119
120 if ( $revCount !== 0 ) {
121 $this->addWhere( [
122 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
123 ] );
124 } else {
125 // We need a custom WHERE clause that matches all titles.
126 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
127 $where = $lb->constructSet( 'ar', $db );
128 $this->addWhere( $where );
129 }
130
131 if ( !is_null( $params['user'] ) ) {
132 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
133 $actorQuery = ActorMigration::newMigration()
134 ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
135 $this->addTables( $actorQuery['tables'] );
136 $this->addJoinConds( $actorQuery['joins'] );
137 $this->addWhere( $actorQuery['conds'] );
138 } elseif ( !is_null( $params['excludeuser'] ) ) {
139 // Here there's no chance of using ar_usertext_timestamp.
140 $actorQuery = ActorMigration::newMigration()
141 ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
142 $this->addTables( $actorQuery['tables'] );
143 $this->addJoinConds( $actorQuery['joins'] );
144 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
145 }
146
147 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
148 // Paranoia: avoid brute force searches (T19342)
149 // (shouldn't be able to get here without 'deletedhistory', but
150 // check it again just in case)
151 if ( !$user->isAllowed( 'deletedhistory' ) ) {
152 $bitmask = RevisionRecord::DELETED_USER;
153 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
154 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
155 } else {
156 $bitmask = 0;
157 }
158 if ( $bitmask ) {
159 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
160 }
161 }
162
163 if ( !is_null( $params['continue'] ) ) {
164 $cont = explode( '|', $params['continue'] );
165 $op = ( $dir == 'newer' ? '>' : '<' );
166 if ( $revCount !== 0 ) {
167 $this->dieContinueUsageIf( count( $cont ) != 2 );
168 $rev = intval( $cont[0] );
169 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
170 $ar_id = (int)$cont[1];
171 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
172 $this->addWhere( "ar_rev_id $op $rev OR " .
173 "(ar_rev_id = $rev AND " .
174 "ar_id $op= $ar_id)" );
175 } else {
176 $this->dieContinueUsageIf( count( $cont ) != 4 );
177 $ns = intval( $cont[0] );
178 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
179 $title = $db->addQuotes( $cont[1] );
180 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
181 $ar_id = (int)$cont[3];
182 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
183 $this->addWhere( "ar_namespace $op $ns OR " .
184 "(ar_namespace = $ns AND " .
185 "(ar_title $op $title OR " .
186 "(ar_title = $title AND " .
187 "(ar_timestamp $op $ts OR " .
188 "(ar_timestamp = $ts AND " .
189 "ar_id $op= $ar_id)))))" );
190 }
191 }
192
193 $this->addOption( 'LIMIT', $this->limit + 1 );
194
195 if ( $revCount !== 0 ) {
196 // Sort by ar_rev_id when querying by ar_rev_id
197 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
198 } else {
199 // Sort by ns and title in the same order as timestamp for efficiency
200 // But only when not already unique in the query
201 if ( count( $pageMap ) > 1 ) {
202 $this->addWhereRange( 'ar_namespace', $dir, null, null );
203 }
204 $oneTitle = key( reset( $pageMap ) );
205 foreach ( $pageMap as $pages ) {
206 if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
207 $this->addWhereRange( 'ar_title', $dir, null, null );
208 break;
209 }
210 }
211 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
212 }
213 // Include in ORDER BY for uniqueness
214 $this->addWhereRange( 'ar_id', $dir, null, null );
215
216 $res = $this->select( __METHOD__ );
217 $count = 0;
218 $generated = [];
219 foreach ( $res as $row ) {
220 if ( ++$count > $this->limit ) {
221 // We've had enough
222 $this->setContinueEnumParameter( 'continue',
223 $revCount
224 ? "$row->ar_rev_id|$row->ar_id"
225 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
226 );
227 break;
228 }
229
230 if ( $resultPageSet !== null ) {
231 $generated[] = $row->ar_rev_id;
232 } else {
233 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
234 // Was it converted?
235 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
236 $converted = $pageSet->getConvertedTitles();
237 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
238 $title = Title::newFromText( $converted[$title->getPrefixedText()] );
239 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
240 $pageMap[$row->ar_namespace][$row->ar_title] =
241 $pageMap[$title->getNamespace()][$title->getDBkey()];
242 }
243 }
244 }
245 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
247 __METHOD__,
248 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
249 );
250 }
251
252 $fit = $this->addPageSubItem(
253 $pageMap[$row->ar_namespace][$row->ar_title],
254 $this->extractRevisionInfo( $revisionStore->newRevisionFromArchiveRow( $row ), $row ),
255 'rev'
256 );
257 if ( !$fit ) {
258 $this->setContinueEnumParameter( 'continue',
259 $revCount
260 ? "$row->ar_rev_id|$row->ar_id"
261 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
262 );
263 break;
264 }
265 }
266 }
267
268 if ( $resultPageSet !== null ) {
269 $resultPageSet->populateFromRevisionIDs( $generated );
270 }
271 }
272
273 public function getAllowedParams() {
274 return parent::getAllowedParams() + [
275 'start' => [
276 ApiBase::PARAM_TYPE => 'timestamp',
277 ],
278 'end' => [
279 ApiBase::PARAM_TYPE => 'timestamp',
280 ],
281 'dir' => [
283 'newer',
284 'older'
285 ],
286 ApiBase::PARAM_DFLT => 'older',
287 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
288 ],
289 'tag' => null,
290 'user' => [
291 ApiBase::PARAM_TYPE => 'user'
292 ],
293 'excludeuser' => [
294 ApiBase::PARAM_TYPE => 'user'
295 ],
296 'continue' => [
297 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
298 ],
299 ];
300 }
301
302 protected function getExamplesMessages() {
303 return [
304 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
305 'drvslots=*&drvprop=user|comment|content'
306 => 'apihelp-query+deletedrevisions-example-titles',
307 'action=query&prop=deletedrevisions&revids=123456'
308 => 'apihelp-query+deletedrevisions-example-revids',
309 ];
310 }
311
312 public function getHelpUrls() {
313 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
314 }
315}
int $wgChangeTagsSchemaMigrationStage
change_tag table schema migration stage.
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:2095
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:2155
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:2167
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:87
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:48
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:770
requireMaxOneParameter( $params, $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:939
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:124
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)
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere(array($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)
getExamplesMessages()
Returns usage examples for this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
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:36
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
MediaWikiServices is the service locator for the application scope of MediaWiki.
Page revision base class.
Exception representing a failure to look up a row from a name table.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:592
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like select() and insert() are usually more convenient. They take care of things like table prefixes and escaping for you. If you really need to make your own SQL
$res
Definition database.txt:21
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition hooks.txt:2214
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1656
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1818
const MIGRATION_WRITE_BOTH
Definition Defines.php:316
$params