Merge "Remove JS for FancyCaptcha adjustment"
[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 $this->addTables( 'archive' );
86 if ( $resultPageSet === null ) {
87 $this->parseParameters( $params );
88 $this->addFields( Revision::selectArchiveFields() );
89 $this->addFields( array( 'ar_title', 'ar_namespace' ) );
90 } else {
91 $this->limit = $this->getParameter( 'limit' ) ?: 10;
92 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ) );
93 }
94
95 if ( $this->fld_tags ) {
96 $this->addTables( 'tag_summary' );
97 $this->addJoinConds(
98 array( 'tag_summary' => array( 'LEFT JOIN', array( 'ar_rev_id=ts_rev_id' ) ) )
99 );
100 $this->addFields( 'ts_tags' );
101 }
102
103 if ( !is_null( $params['tag'] ) ) {
104 $this->addTables( 'change_tag' );
105 $this->addJoinConds(
106 array( 'change_tag' => array( 'INNER JOIN', array( 'ar_rev_id=ct_rev_id' ) ) )
107 );
108 $this->addWhereFld( 'ct_tag', $params['tag'] );
109 }
110
111 if ( $this->fetchContent ) {
112 // Modern MediaWiki has the content for deleted revs in the 'text'
113 // table using fields old_text and old_flags. But revisions deleted
114 // pre-1.5 store the content in the 'archive' table directly using
115 // fields ar_text and ar_flags, and no corresponding 'text' row. So
116 // we have to LEFT JOIN and fetch all four fields.
117 $this->addTables( 'text' );
118 $this->addJoinConds(
119 array( 'text' => array( 'LEFT JOIN', array( 'ar_text_id=old_id' ) ) )
120 );
121 $this->addFields( array( 'ar_text', 'ar_flags', 'old_text', 'old_flags' ) );
122
123 // This also means stricter restrictions
124 if ( !$user->isAllowedAny( 'undelete', 'deletedtext' ) ) {
125 $this->dieUsage(
126 'You don\'t have permission to view deleted revision content',
127 'permissiondenied'
128 );
129 }
130 }
131
132 $dir = $params['dir'];
133 $miser_ns = null;
134
135 if ( $mode == 'all' ) {
136 if ( $params['namespace'] !== null ) {
137 $namespaces = $params['namespace'];
138 $this->addWhereFld( 'ar_namespace', $namespaces );
139 } else {
140 $namespaces = MWNamespace::getValidNamespaces();
141 }
142
143 // For from/to/prefix, we have to consider the potential
144 // transformations of the title in all specified namespaces.
145 // Generally there will be only one transformation, but wikis with
146 // some namespaces case-sensitive could have two.
147 if ( $params['from'] !== null || $params['to'] !== null ) {
148 $isDirNewer = ( $dir === 'newer' );
149 $after = ( $isDirNewer ? '>=' : '<=' );
150 $before = ( $isDirNewer ? '<=' : '>=' );
151 $where = array();
152 foreach ( $namespaces as $ns ) {
153 $w = array();
154 if ( $params['from'] !== null ) {
155 $w[] = 'ar_title' . $after .
156 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
157 }
158 if ( $params['to'] !== null ) {
159 $w[] = 'ar_title' . $before .
160 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
161 }
162 $w = $db->makeList( $w, LIST_AND );
163 $where[$w][] = $ns;
164 }
165 if ( count( $where ) == 1 ) {
166 $where = key( $where );
167 $this->addWhere( $where );
168 } else {
169 $where2 = array();
170 foreach ( $where as $w => $ns ) {
171 $where2[] = $db->makeList( array( $w, 'ar_namespace' => $ns ), LIST_AND );
172 }
173 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
174 }
175 }
176
177 if ( isset( $params['prefix'] ) ) {
178 $where = array();
179 foreach ( $namespaces as $ns ) {
180 $w = 'ar_title' . $db->buildLike(
181 $this->titlePartToKey( $params['prefix'], $ns ),
182 $db->anyString() );
183 $where[$w][] = $ns;
184 }
185 if ( count( $where ) == 1 ) {
186 $where = key( $where );
187 $this->addWhere( $where );
188 } else {
189 $where2 = array();
190 foreach ( $where as $w => $ns ) {
191 $where2[] = $db->makeList( array( $w, 'ar_namespace' => $ns ), LIST_AND );
192 }
193 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
194 }
195 }
196 } else {
197 if ( $this->getConfig()->get( 'MiserMode' ) ) {
198 $miser_ns = $params['namespace'];
199 } else {
200 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
201 }
202 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
203 }
204
205 if ( !is_null( $params['user'] ) ) {
206 $this->addWhereFld( 'ar_user_text', $params['user'] );
207 } elseif ( !is_null( $params['excludeuser'] ) ) {
208 $this->addWhere( 'ar_user_text != ' .
209 $db->addQuotes( $params['excludeuser'] ) );
210 }
211
212 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
213 // Paranoia: avoid brute force searches (bug 17342)
214 // (shouldn't be able to get here without 'deletedhistory', but
215 // check it again just in case)
216 if ( !$user->isAllowed( 'deletedhistory' ) ) {
217 $bitmask = Revision::DELETED_USER;
218 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
219 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
220 } else {
221 $bitmask = 0;
222 }
223 if ( $bitmask ) {
224 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
225 }
226 }
227
228 if ( !is_null( $params['continue'] ) ) {
229 $cont = explode( '|', $params['continue'] );
230 $op = ( $dir == 'newer' ? '>' : '<' );
231 if ( $mode == 'all' ) {
232 $this->dieContinueUsageIf( count( $cont ) != 4 );
233 $ns = intval( $cont[0] );
234 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
235 $title = $db->addQuotes( $cont[1] );
236 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
237 $ar_id = (int)$cont[3];
238 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
239 $this->addWhere( "ar_namespace $op $ns OR " .
240 "(ar_namespace = $ns AND " .
241 "(ar_title $op $title OR " .
242 "(ar_title = $title AND " .
243 "(ar_timestamp $op $ts OR " .
244 "(ar_timestamp = $ts AND " .
245 "ar_id $op= $ar_id)))))" );
246 } else {
247 $this->dieContinueUsageIf( count( $cont ) != 2 );
248 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
249 $ar_id = (int)$cont[1];
250 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
251 $this->addWhere( "ar_timestamp $op $ts OR " .
252 "(ar_timestamp = $ts AND " .
253 "ar_id $op= $ar_id)" );
254 }
255 }
256
257 $this->addOption( 'LIMIT', $this->limit + 1 );
258
259 $sort = ( $dir == 'newer' ? '' : ' DESC' );
260 $orderby = array();
261 if ( $mode == 'all' ) {
262 // Targeting index name_title_timestamp
263 if ( $params['namespace'] === null || count( array_unique( $params['namespace'] ) ) > 1 ) {
264 $orderby[] = "ar_namespace $sort";
265 }
266 $orderby[] = "ar_title $sort";
267 $orderby[] = "ar_timestamp $sort";
268 $orderby[] = "ar_id $sort";
269 } else {
270 // Targeting index usertext_timestamp
271 // 'user' is always constant.
272 $orderby[] = "ar_timestamp $sort";
273 $orderby[] = "ar_id $sort";
274 }
275 $this->addOption( 'ORDER BY', $orderby );
276
277 $res = $this->select( __METHOD__ );
278 $pageMap = array(); // Maps ns&title to array index
279 $count = 0;
280 $nextIndex = 0;
281 $generated = array();
282 foreach ( $res as $row ) {
283 if ( ++$count > $this->limit ) {
284 // We've had enough
285 if ( $mode == 'all' ) {
286 $this->setContinueEnumParameter( 'continue',
287 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
288 );
289 } else {
290 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
291 }
292 break;
293 }
294
295 // Miser mode namespace check
296 if ( $miser_ns !== null && !in_array( $row->ar_namespace, $miser_ns ) ) {
297 continue;
298 }
299
300 if ( $resultPageSet !== null ) {
301 if ( $params['generatetitles'] ) {
302 $key = "{$row->ar_namespace}:{$row->ar_title}";
303 if ( !isset( $generated[$key] ) ) {
304 $generated[$key] = Title::makeTitle( $row->ar_namespace, $row->ar_title );
305 }
306 } else {
307 $generated[] = $row->ar_rev_id;
308 }
309 } else {
310 $revision = Revision::newFromArchiveRow( $row );
311 $rev = $this->extractRevisionInfo( $revision, $row );
312
313 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
314 $index = $nextIndex++;
315 $pageMap[$row->ar_namespace][$row->ar_title] = $index;
316 $title = $revision->getTitle();
317 $a = array(
318 'pageid' => $title->getArticleID(),
319 'revisions' => array( $rev ),
320 );
321 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
322 ApiQueryBase::addTitleInfo( $a, $title );
323 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $index, $a );
324 } else {
325 $index = $pageMap[$row->ar_namespace][$row->ar_title];
326 $fit = $result->addValue(
327 array( 'query', $this->getModuleName(), $index, 'revisions' ),
328 null, $rev );
329 }
330 if ( !$fit ) {
331 if ( $mode == 'all' ) {
332 $this->setContinueEnumParameter( 'continue',
333 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
334 );
335 } else {
336 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
337 }
338 break;
339 }
340 }
341 }
342
343 if ( $resultPageSet !== null ) {
344 if ( $params['generatetitles'] ) {
345 $resultPageSet->populateFromTitles( $generated );
346 } else {
347 $resultPageSet->populateFromRevisionIDs( $generated );
348 }
349 } else {
350 $result->addIndexedTagName( array( 'query', $this->getModuleName() ), 'page' );
351 }
352 }
353
354 public function getAllowedParams() {
355 $ret = parent::getAllowedParams() + array(
356 'user' => array(
357 ApiBase::PARAM_TYPE => 'user'
358 ),
359 'namespace' => array(
360 ApiBase::PARAM_ISMULTI => true,
361 ApiBase::PARAM_TYPE => 'namespace',
362 ApiBase::PARAM_DFLT => null,
363 ),
364 'start' => array(
365 ApiBase::PARAM_TYPE => 'timestamp',
366 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'useronly' ) ),
367 ),
368 'end' => array(
369 ApiBase::PARAM_TYPE => 'timestamp',
370 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'useronly' ) ),
371 ),
372 'dir' => array(
373 ApiBase::PARAM_TYPE => array(
374 'newer',
375 'older'
376 ),
377 ApiBase::PARAM_DFLT => 'older',
378 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
379 ),
380 'from' => array(
381 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'nonuseronly' ) ),
382 ),
383 'to' => array(
384 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'nonuseronly' ) ),
385 ),
386 'prefix' => array(
387 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'nonuseronly' ) ),
388 ),
389 'excludeuser' => array(
390 ApiBase::PARAM_TYPE => 'user',
391 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'nonuseronly' ) ),
392 ),
393 'tag' => null,
394 'continue' => array(
395 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
396 ),
397 'generatetitles' => array(
398 ApiBase::PARAM_DFLT => false
399 ),
400 );
401
402 if ( $this->getConfig()->get( 'MiserMode' ) ) {
403 $ret['user'][ApiBase::PARAM_HELP_MSG_APPEND] = array(
404 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
405 );
406 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = array(
407 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
408 );
409 }
410
411 return $ret;
412 }
413
414 protected function getExamplesMessages() {
415 return array(
416 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
417 => 'apihelp-query+alldeletedrevisions-example-user',
418 'action=query&list=alldeletedrevisions&adrdir=newer&adrlimit=50'
419 => 'apihelp-query+alldeletedrevisions-example-ns-main',
420 );
421 }
422
423 public function getHelpUrls() {
424 return 'https://www.mediawiki.org/wiki/API:Alldeletedrevisions';
425 }
426 }