Merge "Fixed dependencies for jquery.collapsibleTabs"
[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( 'You don\'t have permission to view deleted file information', 'permissiondenied' );
45 }
46
47 $db = $this->getDB();
48
49 $params = $this->extractRequestParams();
50
51 $prop = array_flip( $params['prop'] );
52 $fld_sha1 = isset( $prop['sha1'] );
53 $fld_timestamp = isset( $prop['timestamp'] );
54 $fld_user = isset( $prop['user'] );
55 $fld_size = isset( $prop['size'] );
56 $fld_dimensions = isset( $prop['dimensions'] );
57 $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
58 $fld_mime = isset( $prop['mime'] );
59 $fld_mediatype = isset( $prop['mediatype'] );
60 $fld_metadata = isset( $prop['metadata'] );
61 $fld_bitdepth = isset( $prop['bitdepth'] );
62 $fld_archivename = isset( $prop['archivename'] );
63
64 $this->addTables( 'filearchive' );
65
66 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
67 $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
68 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
69 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
70 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
71 $this->addFieldsIf( 'fa_description', $fld_description );
72 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
73 $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
74 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
75 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
76 $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
77
78 if ( !is_null( $params['continue'] ) ) {
79 $cont = explode( '|', $params['continue'] );
80 if ( count( $cont ) != 1 ) {
81 $this->dieUsage( "Invalid continue param. You should pass the " .
82 "original value returned by the previous query", "_badcontinue" );
83 }
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 = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
92 if ( !is_null( $params['continue'] ) ) {
93 $from = $params['continue'];
94 }
95 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
96 $this->addWhereRange( 'fa_name', $dir, $from, $to );
97 if ( isset( $params['prefix'] ) ) {
98 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
99 }
100
101 $sha1Set = isset( $params['sha1'] );
102 $sha1base36Set = isset( $params['sha1base36'] );
103 if ( $sha1Set || $sha1base36Set ) {
104 $sha1 = false;
105 if ( $sha1Set ) {
106 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
107 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
108 }
109 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
110 } elseif ( $sha1base36Set ) {
111 if ( !$this->validateSha1Base36Hash( $params['sha1base36'] ) ) {
112 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
113 }
114 $sha1 = $params['sha1base36'];
115 }
116 if ( $sha1 ) {
117 $this->addWhereFld( 'fa_sha1', $sha1 );
118 }
119 }
120
121 if ( !$user->isAllowed( 'suppressrevision' ) ) {
122 // Filter out revisions that the user is not allowed to see. There
123 // is no way to indicate that we have skipped stuff because the
124 // continuation parameter is fa_name
125
126 // Note that this field is unindexed. This should however not be
127 // a big problem as files with fa_deleted are rare
128 $this->addWhereFld( 'fa_deleted', 0 );
129 }
130
131 $limit = $params['limit'];
132 $this->addOption( 'LIMIT', $limit + 1 );
133 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
134 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
135
136 $res = $this->select( __METHOD__ );
137
138 $count = 0;
139 $result = $this->getResult();
140 foreach ( $res as $row ) {
141 if ( ++$count > $limit ) {
142 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
143 $this->setContinueEnumParameter( 'continue', $row->fa_name );
144 break;
145 }
146
147 $file = array();
148 $file['name'] = $row->fa_name;
149 $title = Title::makeTitle( NS_FILE, $row->fa_name );
150 self::addTitleInfo( $file, $title );
151
152 if ( $fld_sha1 ) {
153 $file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
154 }
155 if ( $fld_timestamp ) {
156 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
157 }
158 if ( $fld_user ) {
159 $file['userid'] = $row->fa_user;
160 $file['user'] = $row->fa_user_text;
161 }
162 if ( $fld_size || $fld_dimensions ) {
163 $file['size'] = $row->fa_size;
164
165 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
166 if ( $pageCount !== false ) {
167 $vals['pagecount'] = $pageCount;
168 }
169
170 $file['height'] = $row->fa_height;
171 $file['width'] = $row->fa_width;
172 }
173 if ( $fld_description ) {
174 $file['description'] = $row->fa_description;
175 if ( isset( $prop['parseddescription'] ) ) {
176 $file['parseddescription'] = Linker::formatComment(
177 $row->fa_description, $title );
178 }
179 }
180 if ( $fld_mediatype ) {
181 $file['mediatype'] = $row->fa_media_type;
182 }
183 if ( $fld_metadata ) {
184 $file['metadata'] = $row->fa_metadata
185 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
186 : null;
187 }
188 if ( $fld_bitdepth ) {
189 $file['bitdepth'] = $row->fa_bits;
190 }
191 if ( $fld_mime ) {
192 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
193 }
194 if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
195 $file['archivename'] = $row->fa_archive_name;
196 }
197
198 if ( $row->fa_deleted & File::DELETED_FILE ) {
199 $file['filehidden'] = '';
200 }
201 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
202 $file['commenthidden'] = '';
203 }
204 if ( $row->fa_deleted & File::DELETED_USER ) {
205 $file['userhidden'] = '';
206 }
207 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
208 // This file is deleted for normal admins
209 $file['suppressed'] = '';
210 }
211
212
213 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
214 if ( !$fit ) {
215 $this->setContinueEnumParameter( 'continue', $row->fa_name );
216 break;
217 }
218 }
219
220 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
221 }
222
223 public function getAllowedParams() {
224 return array (
225 'from' => null,
226 'continue' => null,
227 'to' => null,
228 'prefix' => null,
229 'limit' => array(
230 ApiBase::PARAM_DFLT => 10,
231 ApiBase::PARAM_TYPE => 'limit',
232 ApiBase::PARAM_MIN => 1,
233 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
234 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
235 ),
236 'dir' => array(
237 ApiBase::PARAM_DFLT => 'ascending',
238 ApiBase::PARAM_TYPE => array(
239 'ascending',
240 'descending'
241 )
242 ),
243 'sha1' => null,
244 'sha1base36' => null,
245 'prop' => array(
246 ApiBase::PARAM_DFLT => 'timestamp',
247 ApiBase::PARAM_ISMULTI => true,
248 ApiBase::PARAM_TYPE => array(
249 'sha1',
250 'timestamp',
251 'user',
252 'size',
253 'dimensions',
254 'description',
255 'parseddescription',
256 'mime',
257 'mediatype',
258 'metadata',
259 'bitdepth',
260 'archivename',
261 ),
262 ),
263 );
264 }
265
266 public function getParamDescription() {
267 return array(
268 'from' => 'The image title to start enumerating from',
269 'continue' => 'When more results are available, use this to continue',
270 'to' => 'The image title to stop enumerating at',
271 'prefix' => 'Search for all image titles that begin with this value',
272 'dir' => 'The direction in which to list',
273 'limit' => 'How many images to return in total',
274 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
275 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
276 'prop' => array(
277 'What image information to get:',
278 ' sha1 - Adds SHA-1 hash for the image',
279 ' timestamp - Adds timestamp for the uploaded version',
280 ' user - Adds user who uploaded the image version',
281 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
282 ' dimensions - Alias for size',
283 ' description - Adds description the image version',
284 ' parseddescription - Parse the description on the version',
285 ' mime - Adds MIME of the image',
286 ' mediatype - Adds the media type of the image',
287 ' metadata - Lists EXIF metadata for the version of the image',
288 ' bitdepth - Adds the bit depth of the version',
289 ' archivename - Adds the file name of the archive version for non-latest versions'
290 ),
291 );
292 }
293
294 public function getResultProperties() {
295 return array(
296 '' => array(
297 'name' => 'string',
298 'ns' => 'namespace',
299 'title' => 'string',
300 'filehidden' => 'boolean',
301 'commenthidden' => 'boolean',
302 'userhidden' => 'boolean',
303 'suppressed' => 'boolean'
304 ),
305 'sha1' => array(
306 'sha1' => 'string'
307 ),
308 'timestamp' => array(
309 'timestamp' => 'timestamp'
310 ),
311 'user' => array(
312 'userid' => 'integer',
313 'user' => 'string'
314 ),
315 'size' => array(
316 'size' => 'integer',
317 'pagecount' => array(
318 ApiBase::PROP_TYPE => 'integer',
319 ApiBase::PROP_NULLABLE => true
320 ),
321 'height' => 'integer',
322 'width' => 'integer'
323 ),
324 'dimensions' => array(
325 'size' => 'integer',
326 'pagecount' => array(
327 ApiBase::PROP_TYPE => 'integer',
328 ApiBase::PROP_NULLABLE => true
329 ),
330 'height' => 'integer',
331 'width' => 'integer'
332 ),
333 'description' => array(
334 'description' => 'string'
335 ),
336 'parseddescription' => array(
337 'description' => 'string',
338 'parseddescription' => 'string'
339 ),
340 'metadata' => array(
341 'metadata' => 'string'
342 ),
343 'bitdepth' => array(
344 'bitdepth' => 'integer'
345 ),
346 'mime' => array(
347 'mime' => 'string'
348 ),
349 'mediatype' => array(
350 'mediatype' => 'string'
351 ),
352 'archivename' => array(
353 'archivename' => 'string'
354 ),
355 );
356 }
357
358 public function getDescription() {
359 return 'Enumerate all deleted files sequentially';
360 }
361
362 public function getPossibleErrors() {
363 return array_merge( parent::getPossibleErrors(), array(
364 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
365 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
366 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
367 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
368 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
369 ) );
370 }
371
372 public function getExamples() {
373 return array(
374 'api.php?action=query&list=filearchive' => array(
375 'Simple Use',
376 'Show a list of all deleted files',
377 ),
378 );
379 }
380
381 public function getVersion() {
382 return __CLASS__ . ': $Id$';
383 }
384 }