Reverted "FOR UPDATE" in 61f246aa786d824d0653522ed679c16be719da80.
[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
63 $this->addTables( 'filearchive' );
64
65 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
66 $this->addFieldsIf( 'fa_storage_key', $fld_sha1 );
67 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
68 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
69 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
70 $this->addFieldsIf( 'fa_description', $fld_description );
71 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
72 $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
73 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
74 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
75
76 if ( !is_null( $params['continue'] ) ) {
77 $cont = explode( '|', $params['continue'] );
78 if ( count( $cont ) != 1 ) {
79 $this->dieUsage( "Invalid continue param. You should pass the " .
80 "original value returned by the previous query", "_badcontinue" );
81 }
82 $op = $params['dir'] == 'descending' ? '<' : '>';
83 $cont_from = $db->addQuotes( $cont[0] );
84 $this->addWhere( "fa_name $op= $cont_from" );
85 }
86
87 // Image filters
88 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
89 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
90 if ( !is_null( $params['continue'] ) ) {
91 $from = $params['continue'];
92 }
93 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
94 $this->addWhereRange( 'fa_name', $dir, $from, $to );
95 if ( isset( $params['prefix'] ) ) {
96 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
97 }
98
99 $sha1Set = isset( $params['sha1'] );
100 $sha1base36Set = isset( $params['sha1base36'] );
101 if ( $sha1Set || $sha1base36Set ) {
102 global $wgMiserMode;
103 if ( $wgMiserMode ) {
104 $this->dieUsage( 'Search by hash disabled in Miser Mode', 'hashsearchdisabled' );
105 }
106
107 $sha1 = false;
108 if ( $sha1Set ) {
109 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
110 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
111 }
112 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
113 } elseif ( $sha1base36Set ) {
114 if ( !$this->validateSha1Base36Hash( $params['sha1base36'] ) ) {
115 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
116 }
117 $sha1 = $params['sha1base36'];
118 }
119 if ( $sha1 ) {
120 $this->addWhere( 'fa_storage_key ' . $db->buildLike( "{$sha1}.", $db->anyString() ) );
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 additional pages to be had. Stop here...
146 $this->setContinueEnumParameter( 'continue', $row->fa_name );
147 break;
148 }
149
150 $file = array();
151 $file['name'] = $row->fa_name;
152 $title = Title::makeTitle( NS_FILE, $row->fa_name );
153 self::addTitleInfo( $file, $title );
154
155 if ( $fld_sha1 ) {
156 $file['sha1'] = wfBaseConvert( LocalRepo::getHashFromKey( $row->fa_storage_key ), 36, 16, 40 );
157 }
158 if ( $fld_timestamp ) {
159 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
160 }
161 if ( $fld_user ) {
162 $file['userid'] = $row->fa_user;
163 $file['user'] = $row->fa_user_text;
164 }
165 if ( $fld_size || $fld_dimensions ) {
166 $file['size'] = $row->fa_size;
167
168 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
169 if ( $pageCount !== false ) {
170 $vals['pagecount'] = $pageCount;
171 }
172
173 $file['height'] = $row->fa_height;
174 $file['width'] = $row->fa_width;
175 }
176 if ( $fld_description ) {
177 $file['description'] = $row->fa_description;
178 if ( isset( $prop['parseddescription'] ) ) {
179 $file['parseddescription'] = Linker::formatComment(
180 $row->fa_description, $title );
181 }
182 }
183 if ( $fld_mediatype ) {
184 $file['mediatype'] = $row->fa_media_type;
185 }
186 if ( $fld_metadata ) {
187 $file['metadata'] = $row->fa_metadata
188 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
189 : null;
190 }
191 if ( $fld_bitdepth ) {
192 $file['bitdepth'] = $row->fa_bits;
193 }
194 if ( $fld_mime ) {
195 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
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 ),
261 ),
262 );
263 }
264
265 public function getParamDescription() {
266 return array(
267 'from' => 'The image title to start enumerating from',
268 'continue' => 'When more results are available, use this to continue',
269 'to' => 'The image title to stop enumerating at',
270 'prefix' => 'Search for all image titles that begin with this value',
271 'dir' => 'The direction in which to list',
272 'limit' => 'How many images to return in total',
273 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36. Disabled in Miser Mode",
274 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki). Disabled in Miser Mode',
275 'prop' => array(
276 'What image information to get:',
277 ' sha1 - Adds SHA-1 hash for the image',
278 ' timestamp - Adds timestamp for the uploaded version',
279 ' user - Adds user who uploaded the image version',
280 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
281 ' dimensions - Alias for size',
282 ' description - Adds description the image version',
283 ' parseddescription - Parse the description on the version',
284 ' mime - Adds MIME of the image',
285 ' mediatype - Adds the media type of the image',
286 ' metadata - Lists EXIF metadata for the version of the image',
287 ' bitdepth - Adds the bit depth of the version',
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 );
351 }
352
353 public function getDescription() {
354 return 'Enumerate all deleted files sequentially';
355 }
356
357 public function getPossibleErrors() {
358 return array_merge( parent::getPossibleErrors(), array(
359 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
360 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
361 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
362 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
363 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
364 ) );
365 }
366
367 public function getExamples() {
368 return array(
369 'api.php?action=query&list=filearchive' => array(
370 'Simple Use',
371 'Show a list of all deleted files',
372 ),
373 );
374 }
375
376 public function getVersion() {
377 return __CLASS__ . ': $Id$';
378 }
379 }