Merge "Show a warning on page deletion if a page is linked to"
[lhc/web/wiklou.git] / includes / api / ApiQueryFilearchive.php
1 <?php
2 /**
3 * API for MediaWiki 1.12+
4 *
5 * Created on May 10, 2010
6 *
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 /**
30 * Query module to enumerate all deleted files.
31 *
32 * @ingroup API
33 */
34 class ApiQueryFilearchive extends ApiQueryBase {
35
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'fa' );
38 }
39
40 public function execute() {
41 $user = $this->getUser();
42 // Before doing anything at all, let's check permissions
43 if ( !$user->isAllowed( 'deletedhistory' ) ) {
44 $this->dieUsage(
45 'You don\'t have permission to view deleted file information',
46 'permissiondenied'
47 );
48 }
49
50 $db = $this->getDB();
51
52 $params = $this->extractRequestParams();
53
54 $prop = array_flip( $params['prop'] );
55 $fld_sha1 = isset( $prop['sha1'] );
56 $fld_timestamp = isset( $prop['timestamp'] );
57 $fld_user = isset( $prop['user'] );
58 $fld_size = isset( $prop['size'] );
59 $fld_dimensions = isset( $prop['dimensions'] );
60 $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
61 $fld_mime = isset( $prop['mime'] );
62 $fld_mediatype = isset( $prop['mediatype'] );
63 $fld_metadata = isset( $prop['metadata'] );
64 $fld_bitdepth = isset( $prop['bitdepth'] );
65 $fld_archivename = isset( $prop['archivename'] );
66
67 $this->addTables( 'filearchive' );
68
69 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
70 $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
71 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
72 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
73 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
74 $this->addFieldsIf( 'fa_description', $fld_description );
75 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
76 $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
77 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
78 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
79 $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
80
81 if ( !is_null( $params['continue'] ) ) {
82 $cont = explode( '|', $params['continue'] );
83 $this->dieContinueUsageIf( count( $cont ) != 1 );
84 $op = $params['dir'] == 'descending' ? '<' : '>';
85 $cont_from = $db->addQuotes( $cont[0] );
86 $this->addWhere( "fa_name $op= $cont_from" );
87 }
88
89 // Image filters
90 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
91 $from = ( $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE ) );
92 if ( !is_null( $params['continue'] ) ) {
93 $from = $params['continue'];
94 }
95 $to = ( $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE ) );
96 $this->addWhereRange( 'fa_name', $dir, $from, $to );
97 if ( isset( $params['prefix'] ) ) {
98 $this->addWhere( 'fa_name' . $db->buildLike(
99 $this->titlePartToKey( $params['prefix'], NS_FILE ),
100 $db->anyString() ) );
101 }
102
103 $sha1Set = isset( $params['sha1'] );
104 $sha1base36Set = isset( $params['sha1base36'] );
105 if ( $sha1Set || $sha1base36Set ) {
106 $sha1 = false;
107 if ( $sha1Set ) {
108 $sha1 = strtolower( $params['sha1'] );
109 if ( !$this->validateSha1Hash( $sha1 ) ) {
110 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
111 }
112 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
113 } elseif ( $sha1base36Set ) {
114 $sha1 = strtolower( $params['sha1base36'] );
115 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
116 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
117 }
118 }
119 if ( $sha1 ) {
120 $this->addWhereFld( 'fa_sha1', $sha1 );
121 }
122 }
123
124 if ( !$user->isAllowed( 'suppressrevision' ) ) {
125 // Filter out revisions that the user is not allowed to see. There
126 // is no way to indicate that we have skipped stuff because the
127 // continuation parameter is fa_name
128
129 // Note that this field is unindexed. This should however not be
130 // a big problem as files with fa_deleted are rare
131 $this->addWhereFld( 'fa_deleted', 0 );
132 }
133
134 $limit = $params['limit'];
135 $this->addOption( 'LIMIT', $limit + 1 );
136 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
137 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
138
139 $res = $this->select( __METHOD__ );
140
141 $count = 0;
142 $result = $this->getResult();
143 foreach ( $res as $row ) {
144 if ( ++$count > $limit ) {
145 // We've reached the one extra which shows that there are
146 // additional pages to be had. Stop here...
147 $this->setContinueEnumParameter( 'continue', $row->fa_name );
148 break;
149 }
150
151 $file = array();
152 $file['name'] = $row->fa_name;
153 $title = Title::makeTitle( NS_FILE, $row->fa_name );
154 self::addTitleInfo( $file, $title );
155
156 if ( $fld_sha1 ) {
157 $file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
158 }
159 if ( $fld_timestamp ) {
160 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
161 }
162 if ( $fld_user ) {
163 $file['userid'] = $row->fa_user;
164 $file['user'] = $row->fa_user_text;
165 }
166 if ( $fld_size || $fld_dimensions ) {
167 $file['size'] = $row->fa_size;
168
169 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
170 if ( $pageCount !== false ) {
171 $vals['pagecount'] = $pageCount;
172 }
173
174 $file['height'] = $row->fa_height;
175 $file['width'] = $row->fa_width;
176 }
177 if ( $fld_description ) {
178 $file['description'] = $row->fa_description;
179 if ( isset( $prop['parseddescription'] ) ) {
180 $file['parseddescription'] = Linker::formatComment(
181 $row->fa_description, $title );
182 }
183 }
184 if ( $fld_mediatype ) {
185 $file['mediatype'] = $row->fa_media_type;
186 }
187 if ( $fld_metadata ) {
188 $file['metadata'] = $row->fa_metadata
189 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
190 : null;
191 }
192 if ( $fld_bitdepth ) {
193 $file['bitdepth'] = $row->fa_bits;
194 }
195 if ( $fld_mime ) {
196 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
197 }
198 if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
199 $file['archivename'] = $row->fa_archive_name;
200 }
201
202 if ( $row->fa_deleted & File::DELETED_FILE ) {
203 $file['filehidden'] = '';
204 }
205 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
206 $file['commenthidden'] = '';
207 }
208 if ( $row->fa_deleted & File::DELETED_USER ) {
209 $file['userhidden'] = '';
210 }
211 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
212 // This file is deleted for normal admins
213 $file['suppressed'] = '';
214 }
215
216 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
217 if ( !$fit ) {
218 $this->setContinueEnumParameter( 'continue', $row->fa_name );
219 break;
220 }
221 }
222
223 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
224 }
225
226 public function getAllowedParams() {
227 return array(
228 'from' => null,
229 'continue' => null,
230 'to' => null,
231 'prefix' => null,
232 'limit' => array(
233 ApiBase::PARAM_DFLT => 10,
234 ApiBase::PARAM_TYPE => 'limit',
235 ApiBase::PARAM_MIN => 1,
236 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
237 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
238 ),
239 'dir' => array(
240 ApiBase::PARAM_DFLT => 'ascending',
241 ApiBase::PARAM_TYPE => array(
242 'ascending',
243 'descending'
244 )
245 ),
246 'sha1' => null,
247 'sha1base36' => null,
248 'prop' => array(
249 ApiBase::PARAM_DFLT => 'timestamp',
250 ApiBase::PARAM_ISMULTI => true,
251 ApiBase::PARAM_TYPE => array(
252 'sha1',
253 'timestamp',
254 'user',
255 'size',
256 'dimensions',
257 'description',
258 'parseddescription',
259 'mime',
260 'mediatype',
261 'metadata',
262 'bitdepth',
263 'archivename',
264 ),
265 ),
266 );
267 }
268
269 public function getParamDescription() {
270 return array(
271 'from' => 'The image title to start enumerating from',
272 'continue' => 'When more results are available, use this to continue',
273 'to' => 'The image title to stop enumerating at',
274 'prefix' => 'Search for all image titles that begin with this value',
275 'dir' => 'The direction in which to list',
276 'limit' => 'How many images to return in total',
277 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
278 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
279 'prop' => array(
280 'What image information to get:',
281 ' sha1 - Adds SHA-1 hash for the image',
282 ' timestamp - Adds timestamp for the uploaded version',
283 ' user - Adds user who uploaded the image version',
284 ' size - Adds the size of the image in bytes and the height, ' .
285 'width and page count (if applicable)',
286 ' dimensions - Alias for size',
287 ' description - Adds description the image version',
288 ' parseddescription - Parse the description on the version',
289 ' mime - Adds MIME of the image',
290 ' mediatype - Adds the media type of the image',
291 ' metadata - Lists Exif metadata for the version of the image',
292 ' bitdepth - Adds the bit depth of the version',
293 ' archivename - Adds the file name of the archive version for non-latest versions'
294 ),
295 );
296 }
297
298 public function getResultProperties() {
299 return array(
300 '' => array(
301 'name' => 'string',
302 'ns' => 'namespace',
303 'title' => 'string',
304 'filehidden' => 'boolean',
305 'commenthidden' => 'boolean',
306 'userhidden' => 'boolean',
307 'suppressed' => 'boolean'
308 ),
309 'sha1' => array(
310 'sha1' => 'string'
311 ),
312 'timestamp' => array(
313 'timestamp' => 'timestamp'
314 ),
315 'user' => array(
316 'userid' => 'integer',
317 'user' => 'string'
318 ),
319 'size' => array(
320 'size' => 'integer',
321 'pagecount' => array(
322 ApiBase::PROP_TYPE => 'integer',
323 ApiBase::PROP_NULLABLE => true
324 ),
325 'height' => 'integer',
326 'width' => 'integer'
327 ),
328 'dimensions' => array(
329 'size' => 'integer',
330 'pagecount' => array(
331 ApiBase::PROP_TYPE => 'integer',
332 ApiBase::PROP_NULLABLE => true
333 ),
334 'height' => 'integer',
335 'width' => 'integer'
336 ),
337 'description' => array(
338 'description' => 'string'
339 ),
340 'parseddescription' => array(
341 'description' => 'string',
342 'parseddescription' => 'string'
343 ),
344 'metadata' => array(
345 'metadata' => 'string'
346 ),
347 'bitdepth' => array(
348 'bitdepth' => 'integer'
349 ),
350 'mime' => array(
351 'mime' => 'string'
352 ),
353 'mediatype' => array(
354 'mediatype' => 'string'
355 ),
356 'archivename' => array(
357 'archivename' => 'string'
358 ),
359 );
360 }
361
362 public function getDescription() {
363 return 'Enumerate all deleted files sequentially';
364 }
365
366 public function getPossibleErrors() {
367 return array_merge( parent::getPossibleErrors(), array(
368 array(
369 'code' => 'permissiondenied',
370 'info' => 'You don\'t have permission to view deleted file information'
371 ),
372 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
373 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA-1 hash provided is not valid' ),
374 array(
375 'code' => 'invalidsha1base36hash',
376 'info' => 'The SHA1Base36 hash provided is not valid'
377 ),
378 ) );
379 }
380
381 public function getExamples() {
382 return array(
383 'api.php?action=query&list=filearchive' => array(
384 'Simple Use',
385 'Show a list of all deleted files',
386 ),
387 );
388 }
389
390 public function getHelpUrls() {
391 return 'https://www.mediawiki.org/wiki/API:Filearchive';
392 }
393 }