3 * API for MediaWiki 1.12+
5 * Created on May 10, 2010
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
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.
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.
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
30 * Query module to enumerate all deleted files.
34 class ApiQueryFilearchive
extends ApiQueryBase
{
36 public function __construct( $query, $moduleName ) {
37 parent
::__construct( $query, $moduleName, 'fa' );
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' );
49 $params = $this->extractRequestParams();
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'] );
64 $this->addTables( 'filearchive' );
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 );
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" );
84 $op = $params['dir'] == 'descending' ?
'<' : '>';
85 $cont_from = $db->addQuotes( $cont[0] );
86 $this->addWhere( "fa_name $op= $cont_from" );
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'];
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() ) );
101 $sha1Set = isset( $params['sha1'] );
102 $sha1base36Set = isset( $params['sha1base36'] );
103 if ( $sha1Set ||
$sha1base36Set ) {
105 if ( $wgMiserMode ) {
106 $this->dieUsage( 'Search by hash disabled in Miser Mode', 'hashsearchdisabled' );
111 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
112 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
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' );
119 $sha1 = $params['sha1base36'];
122 $this->addWhere( 'fa_storage_key ' . $db->buildLike( "{$sha1}.", $db->anyString() ) );
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
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 );
136 $limit = $params['limit'];
137 $this->addOption( 'LIMIT', $limit +
1 );
138 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
139 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
141 $res = $this->select( __METHOD__
);
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
);
153 $file['name'] = $row->fa_name
;
154 $title = Title
::makeTitle( NS_FILE
, $row->fa_name
);
155 self
::addTitleInfo( $file, $title );
158 $file['sha1'] = wfBaseConvert( LocalRepo
::getHashFromKey( $row->fa_storage_key
), 36, 16, 40 );
160 if ( $fld_timestamp ) {
161 $file['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->fa_timestamp
);
164 $file['userid'] = $row->fa_user
;
165 $file['user'] = $row->fa_user_text
;
167 if ( $fld_size ||
$fld_dimensions ) {
168 $file['size'] = $row->fa_size
;
170 $pageCount = ArchivedFile
::newFromRow( $row )->pageCount();
171 if ( $pageCount !== false ) {
172 $vals['pagecount'] = $pageCount;
175 $file['height'] = $row->fa_height
;
176 $file['width'] = $row->fa_width
;
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 );
185 if ( $fld_mediatype ) {
186 $file['mediatype'] = $row->fa_media_type
;
188 if ( $fld_metadata ) {
189 $file['metadata'] = $row->fa_metadata
190 ? ApiQueryImageInfo
::processMetaData( unserialize( $row->fa_metadata
), $result )
193 if ( $fld_bitdepth ) {
194 $file['bitdepth'] = $row->fa_bits
;
197 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
199 if ( $fld_archivename && !is_null( $row->fa_archive_name
) ) {
200 $file['archivename'] = $row->fa_archive_name
;
203 if ( $row->fa_deleted
& File
::DELETED_FILE
) {
204 $file['filehidden'] = '';
206 if ( $row->fa_deleted
& File
::DELETED_COMMENT
) {
207 $file['commenthidden'] = '';
209 if ( $row->fa_deleted
& File
::DELETED_USER
) {
210 $file['userhidden'] = '';
212 if ( $row->fa_deleted
& File
::DELETED_RESTRICTED
) {
213 // This file is deleted for normal admins
214 $file['suppressed'] = '';
218 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
220 $this->setContinueEnumParameter( 'continue', $row->fa_name
);
225 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
228 public function getAllowedParams() {
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
242 ApiBase
::PARAM_DFLT
=> 'ascending',
243 ApiBase
::PARAM_TYPE
=> array(
249 'sha1base36' => null,
251 ApiBase
::PARAM_DFLT
=> 'timestamp',
252 ApiBase
::PARAM_ISMULTI
=> true,
253 ApiBase
::PARAM_TYPE
=> array(
271 public function getParamDescription() {
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',
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'
299 public function getResultProperties() {
305 'filehidden' => 'boolean',
306 'commenthidden' => 'boolean',
307 'userhidden' => 'boolean',
308 'suppressed' => 'boolean'
313 'timestamp' => array(
314 'timestamp' => 'timestamp'
317 'userid' => 'integer',
322 'pagecount' => array(
323 ApiBase
::PROP_TYPE
=> 'integer',
324 ApiBase
::PROP_NULLABLE
=> true
326 'height' => 'integer',
329 'dimensions' => array(
331 'pagecount' => array(
332 ApiBase
::PROP_TYPE
=> 'integer',
333 ApiBase
::PROP_NULLABLE
=> true
335 'height' => 'integer',
338 'description' => array(
339 'description' => 'string'
341 'parseddescription' => array(
342 'description' => 'string',
343 'parseddescription' => 'string'
346 'metadata' => 'string'
349 'bitdepth' => 'integer'
354 'mediatype' => array(
355 'mediatype' => 'string'
357 'archivename' => array(
358 'archivename' => 'string'
363 public function getDescription() {
364 return 'Enumerate all deleted files sequentially';
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' ),
377 public function getExamples() {
379 'api.php?action=query&list=filearchive' => array(
381 'Show a list of all deleted files',
386 public function getVersion() {
387 return __CLASS__
. ': $Id$';