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