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