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