Merge "Use ParserCache in CategoryMembershipChangeJob"
[lhc/web/wiklou.git] / includes / api / ApiQueryAllDeletedRevisions.php
1 <?php
2 /**
3 * Copyright © 2014 Wikimedia Foundation and contributors
4 *
5 * Heavily based on ApiQueryDeletedrevs,
6 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\Revision\RevisionRecord;
28 use MediaWiki\Storage\NameTableAccessException;
29
30 /**
31 * Query module to enumerate all deleted revisions.
32 *
33 * @ingroup API
34 */
35 class ApiQueryAllDeletedRevisions extends ApiQueryRevisionsBase {
36
37 public function __construct( ApiQuery $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'adr' );
39 }
40
41 /**
42 * @param ApiPageSet|null $resultPageSet
43 * @return void
44 */
45 protected function run( ApiPageSet $resultPageSet = null ) {
46 // Before doing anything at all, let's check permissions
47 $this->checkUserRightsAny( 'deletedhistory' );
48
49 $user = $this->getUser();
50 $db = $this->getDB();
51 $params = $this->extractRequestParams( false );
52 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
53
54 $result = $this->getResult();
55
56 // If the user wants no namespaces, they get no pages.
57 if ( $params['namespace'] === [] ) {
58 if ( $resultPageSet === null ) {
59 $result->addValue( 'query', $this->getModuleName(), [] );
60 }
61 return;
62 }
63
64 // This module operates in two modes:
65 // 'user': List deleted revs by a certain user
66 // 'all': List all deleted revs in NS
67 $mode = 'all';
68 if ( !is_null( $params['user'] ) ) {
69 $mode = 'user';
70 }
71
72 if ( $mode == 'user' ) {
73 foreach ( [ 'from', 'to', 'prefix', 'excludeuser' ] as $param ) {
74 if ( !is_null( $params[$param] ) ) {
75 $p = $this->getModulePrefix();
76 $this->dieWithError(
77 [ 'apierror-invalidparammix-cannotusewith', $p . $param, "{$p}user" ],
78 'invalidparammix'
79 );
80 }
81 }
82 } else {
83 foreach ( [ 'start', 'end' ] as $param ) {
84 if ( !is_null( $params[$param] ) ) {
85 $p = $this->getModulePrefix();
86 $this->dieWithError(
87 [ 'apierror-invalidparammix-mustusewith', $p . $param, "{$p}user" ],
88 'invalidparammix'
89 );
90 }
91 }
92 }
93
94 // If we're generating titles only, we can use DISTINCT for a better
95 // query. But we can't do that in 'user' mode (wrong index), and we can
96 // only do it when sorting ASC (because MySQL apparently can't use an
97 // index backwards for grouping even though it can for ORDER BY, WTF?)
98 $dir = $params['dir'];
99 $optimizeGenerateTitles = false;
100 if ( $mode === 'all' && $params['generatetitles'] && $resultPageSet !== null ) {
101 if ( $dir === 'newer' ) {
102 $optimizeGenerateTitles = true;
103 } else {
104 $p = $this->getModulePrefix();
105 $this->addWarning( [ 'apiwarn-alldeletedrevisions-performance', $p ], 'performance' );
106 }
107 }
108
109 if ( $resultPageSet === null ) {
110 $this->parseParameters( $params );
111 $arQuery = $revisionStore->getArchiveQueryInfo();
112 $this->addTables( $arQuery['tables'] );
113 $this->addJoinConds( $arQuery['joins'] );
114 $this->addFields( $arQuery['fields'] );
115 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
116 } else {
117 $this->limit = $this->getParameter( 'limit' ) ?: 10;
118 $this->addTables( 'archive' );
119 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
120 if ( $optimizeGenerateTitles ) {
121 $this->addOption( 'DISTINCT' );
122 } else {
123 $this->addFields( [ 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
124 }
125 }
126
127 if ( $this->fld_tags ) {
128 $this->addTables( 'tag_summary' );
129 $this->addJoinConds(
130 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
131 );
132 $this->addFields( 'ts_tags' );
133 }
134
135 if ( !is_null( $params['tag'] ) ) {
136 $this->addTables( 'change_tag' );
137 $this->addJoinConds(
138 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
139 );
140 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
141 try {
142 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
143 } catch ( NameTableAccessException $exception ) {
144 // Return nothing.
145 $this->addWhere( '1=0' );
146 }
147 }
148
149 if ( $this->fetchContent ) {
150 $this->addTables( 'text' );
151 $this->addJoinConds(
152 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
153 );
154 $this->addFields( [ 'old_text', 'old_flags' ] );
155
156 // This also means stricter restrictions
157 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
158 }
159
160 $miser_ns = null;
161
162 if ( $mode == 'all' ) {
163 $namespaces = $params['namespace'] ?? MWNamespace::getValidNamespaces();
164 $this->addWhereFld( 'ar_namespace', $namespaces );
165
166 // For from/to/prefix, we have to consider the potential
167 // transformations of the title in all specified namespaces.
168 // Generally there will be only one transformation, but wikis with
169 // some namespaces case-sensitive could have two.
170 if ( $params['from'] !== null || $params['to'] !== null ) {
171 $isDirNewer = ( $dir === 'newer' );
172 $after = ( $isDirNewer ? '>=' : '<=' );
173 $before = ( $isDirNewer ? '<=' : '>=' );
174 $where = [];
175 foreach ( $namespaces as $ns ) {
176 $w = [];
177 if ( $params['from'] !== null ) {
178 $w[] = 'ar_title' . $after .
179 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
180 }
181 if ( $params['to'] !== null ) {
182 $w[] = 'ar_title' . $before .
183 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
184 }
185 $w = $db->makeList( $w, LIST_AND );
186 $where[$w][] = $ns;
187 }
188 if ( count( $where ) == 1 ) {
189 $where = key( $where );
190 $this->addWhere( $where );
191 } else {
192 $where2 = [];
193 foreach ( $where as $w => $ns ) {
194 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND );
195 }
196 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
197 }
198 }
199
200 if ( isset( $params['prefix'] ) ) {
201 $where = [];
202 foreach ( $namespaces as $ns ) {
203 $w = 'ar_title' . $db->buildLike(
204 $this->titlePartToKey( $params['prefix'], $ns ),
205 $db->anyString() );
206 $where[$w][] = $ns;
207 }
208 if ( count( $where ) == 1 ) {
209 $where = key( $where );
210 $this->addWhere( $where );
211 } else {
212 $where2 = [];
213 foreach ( $where as $w => $ns ) {
214 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND );
215 }
216 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
217 }
218 }
219 } else {
220 if ( $this->getConfig()->get( 'MiserMode' ) ) {
221 $miser_ns = $params['namespace'];
222 } else {
223 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
224 }
225 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
226 }
227
228 if ( !is_null( $params['user'] ) ) {
229 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
230 $actorQuery = ActorMigration::newMigration()
231 ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
232 $this->addTables( $actorQuery['tables'] );
233 $this->addJoinConds( $actorQuery['joins'] );
234 $this->addWhere( $actorQuery['conds'] );
235 } elseif ( !is_null( $params['excludeuser'] ) ) {
236 // Here there's no chance of using ar_usertext_timestamp.
237 $actorQuery = ActorMigration::newMigration()
238 ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
239 $this->addTables( $actorQuery['tables'] );
240 $this->addJoinConds( $actorQuery['joins'] );
241 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
242 }
243
244 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
245 // Paranoia: avoid brute force searches (T19342)
246 // (shouldn't be able to get here without 'deletedhistory', but
247 // check it again just in case)
248 if ( !$user->isAllowed( 'deletedhistory' ) ) {
249 $bitmask = RevisionRecord::DELETED_USER;
250 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
251 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
252 } else {
253 $bitmask = 0;
254 }
255 if ( $bitmask ) {
256 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
257 }
258 }
259
260 if ( !is_null( $params['continue'] ) ) {
261 $cont = explode( '|', $params['continue'] );
262 $op = ( $dir == 'newer' ? '>' : '<' );
263 if ( $optimizeGenerateTitles ) {
264 $this->dieContinueUsageIf( count( $cont ) != 2 );
265 $ns = intval( $cont[0] );
266 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
267 $title = $db->addQuotes( $cont[1] );
268 $this->addWhere( "ar_namespace $op $ns OR " .
269 "(ar_namespace = $ns AND ar_title $op= $title)" );
270 } elseif ( $mode == 'all' ) {
271 $this->dieContinueUsageIf( count( $cont ) != 4 );
272 $ns = intval( $cont[0] );
273 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
274 $title = $db->addQuotes( $cont[1] );
275 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
276 $ar_id = (int)$cont[3];
277 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
278 $this->addWhere( "ar_namespace $op $ns OR " .
279 "(ar_namespace = $ns AND " .
280 "(ar_title $op $title OR " .
281 "(ar_title = $title AND " .
282 "(ar_timestamp $op $ts OR " .
283 "(ar_timestamp = $ts AND " .
284 "ar_id $op= $ar_id)))))" );
285 } else {
286 $this->dieContinueUsageIf( count( $cont ) != 2 );
287 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
288 $ar_id = (int)$cont[1];
289 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
290 $this->addWhere( "ar_timestamp $op $ts OR " .
291 "(ar_timestamp = $ts AND " .
292 "ar_id $op= $ar_id)" );
293 }
294 }
295
296 $this->addOption( 'LIMIT', $this->limit + 1 );
297
298 $sort = ( $dir == 'newer' ? '' : ' DESC' );
299 $orderby = [];
300 if ( $optimizeGenerateTitles ) {
301 // Targeting index name_title_timestamp
302 if ( $params['namespace'] === null || count( array_unique( $params['namespace'] ) ) > 1 ) {
303 $orderby[] = "ar_namespace $sort";
304 }
305 $orderby[] = "ar_title $sort";
306 } elseif ( $mode == 'all' ) {
307 // Targeting index name_title_timestamp
308 if ( $params['namespace'] === null || count( array_unique( $params['namespace'] ) ) > 1 ) {
309 $orderby[] = "ar_namespace $sort";
310 }
311 $orderby[] = "ar_title $sort";
312 $orderby[] = "ar_timestamp $sort";
313 $orderby[] = "ar_id $sort";
314 } else {
315 // Targeting index usertext_timestamp
316 // 'user' is always constant.
317 $orderby[] = "ar_timestamp $sort";
318 $orderby[] = "ar_id $sort";
319 }
320 $this->addOption( 'ORDER BY', $orderby );
321
322 $res = $this->select( __METHOD__ );
323 $pageMap = []; // Maps ns&title to array index
324 $count = 0;
325 $nextIndex = 0;
326 $generated = [];
327 foreach ( $res as $row ) {
328 if ( ++$count > $this->limit ) {
329 // We've had enough
330 if ( $optimizeGenerateTitles ) {
331 $this->setContinueEnumParameter( 'continue', "$row->ar_namespace|$row->ar_title" );
332 } elseif ( $mode == 'all' ) {
333 $this->setContinueEnumParameter( 'continue',
334 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
335 );
336 } else {
337 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
338 }
339 break;
340 }
341
342 // Miser mode namespace check
343 if ( $miser_ns !== null && !in_array( $row->ar_namespace, $miser_ns ) ) {
344 continue;
345 }
346
347 if ( $resultPageSet !== null ) {
348 if ( $params['generatetitles'] ) {
349 $key = "{$row->ar_namespace}:{$row->ar_title}";
350 if ( !isset( $generated[$key] ) ) {
351 $generated[$key] = Title::makeTitle( $row->ar_namespace, $row->ar_title );
352 }
353 } else {
354 $generated[] = $row->ar_rev_id;
355 }
356 } else {
357 $revision = $revisionStore->newRevisionFromArchiveRow( $row );
358 $rev = $this->extractRevisionInfo( $revision, $row );
359
360 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
361 $index = $nextIndex++;
362 $pageMap[$row->ar_namespace][$row->ar_title] = $index;
363 $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
364 $a = [
365 'pageid' => $title->getArticleID(),
366 'revisions' => [ $rev ],
367 ];
368 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
369 ApiQueryBase::addTitleInfo( $a, $title );
370 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
371 } else {
372 $index = $pageMap[$row->ar_namespace][$row->ar_title];
373 $fit = $result->addValue(
374 [ 'query', $this->getModuleName(), $index, 'revisions' ],
375 null, $rev );
376 }
377 if ( !$fit ) {
378 if ( $mode == 'all' ) {
379 $this->setContinueEnumParameter( 'continue',
380 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
381 );
382 } else {
383 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
384 }
385 break;
386 }
387 }
388 }
389
390 if ( $resultPageSet !== null ) {
391 if ( $params['generatetitles'] ) {
392 $resultPageSet->populateFromTitles( $generated );
393 } else {
394 $resultPageSet->populateFromRevisionIDs( $generated );
395 }
396 } else {
397 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
398 }
399 }
400
401 public function getAllowedParams() {
402 $ret = parent::getAllowedParams() + [
403 'user' => [
404 ApiBase::PARAM_TYPE => 'user'
405 ],
406 'namespace' => [
407 ApiBase::PARAM_ISMULTI => true,
408 ApiBase::PARAM_TYPE => 'namespace',
409 ],
410 'start' => [
411 ApiBase::PARAM_TYPE => 'timestamp',
412 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'useronly' ] ],
413 ],
414 'end' => [
415 ApiBase::PARAM_TYPE => 'timestamp',
416 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'useronly' ] ],
417 ],
418 'dir' => [
419 ApiBase::PARAM_TYPE => [
420 'newer',
421 'older'
422 ],
423 ApiBase::PARAM_DFLT => 'older',
424 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
425 ],
426 'from' => [
427 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
428 ],
429 'to' => [
430 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
431 ],
432 'prefix' => [
433 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
434 ],
435 'excludeuser' => [
436 ApiBase::PARAM_TYPE => 'user',
437 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
438 ],
439 'tag' => null,
440 'continue' => [
441 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
442 ],
443 'generatetitles' => [
444 ApiBase::PARAM_DFLT => false
445 ],
446 ];
447
448 if ( $this->getConfig()->get( 'MiserMode' ) ) {
449 $ret['user'][ApiBase::PARAM_HELP_MSG_APPEND] = [
450 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
451 ];
452 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
453 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
454 ];
455 }
456
457 return $ret;
458 }
459
460 protected function getExamplesMessages() {
461 return [
462 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
463 => 'apihelp-query+alldeletedrevisions-example-user',
464 'action=query&list=alldeletedrevisions&adrdir=newer&adrnamespace=0&adrlimit=50'
465 => 'apihelp-query+alldeletedrevisions-example-ns-main',
466 ];
467 }
468
469 public function getHelpUrls() {
470 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Alldeletedrevisions';
471 }
472 }