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