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