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