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'] );
63 $this->addTables( 'filearchive' );
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 );
77 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
78 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
79 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
80 $this->addWhereRange( 'fa_name', $dir, $from, $to );
81 if ( isset( $params['prefix'] ) ) {
82 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
85 $sha1Set = isset( $params['sha1'] );
86 $sha1base36Set = isset( $params['sha1base36'] );
87 if ( $sha1Set ||
$sha1base36Set ) {
90 $this->dieUsage( 'Search by hash disabled in Miser Mode', 'hashsearchdisabled' );
95 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
96 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
98 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
99 } elseif ( $sha1base36Set ) {
100 if ( !$this->validateSha1Base36Hash( $params['sha1base36'] ) ) {
101 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
103 $sha1 = $params['sha1base36'];
106 $this->addWhere( 'fa_storage_key ' . $db->buildLike( "{$sha1}.", $db->anyString() ) );
110 if ( !$user->isAllowed( 'suppressrevision' ) ) {
111 // Filter out revisions that the user is not allowed to see. There
112 // is no way to indicate that we have skipped stuff because the
113 // continuation parameter is fa_name
115 // Note that this field is unindexed. This should however not be
116 // a big problem as files with fa_deleted are rare
117 $this->addWhereFld( 'fa_deleted', 0 );
120 $limit = $params['limit'];
121 $this->addOption( 'LIMIT', $limit +
1 );
122 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
123 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
125 $res = $this->select( __METHOD__
);
128 $result = $this->getResult();
129 foreach ( $res as $row ) {
130 if ( ++
$count > $limit ) {
131 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
132 // TODO: Security issue - if the user has no right to view next title, it will still be shown
133 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
138 $file['name'] = $row->fa_name
;
139 $title = Title
::makeTitle( NS_FILE
, $row->fa_name
);
140 self
::addTitleInfo( $file, $title );
143 $file['sha1'] = wfBaseConvert( LocalRepo
::getHashFromKey( $row->fa_storage_key
), 36, 16, 40 );
145 if ( $fld_timestamp ) {
146 $file['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->fa_timestamp
);
149 $file['userid'] = $row->fa_user
;
150 $file['user'] = $row->fa_user_text
;
152 if ( $fld_size ||
$fld_dimensions ) {
153 $file['size'] = $row->fa_size
;
155 $pageCount = ArchivedFile
::newFromRow( $row )->pageCount();
156 if ( $pageCount !== false ) {
157 $vals['pagecount'] = $pageCount;
160 $file['height'] = $row->fa_height
;
161 $file['width'] = $row->fa_width
;
163 if ( $fld_description ) {
164 $file['description'] = $row->fa_description
;
165 if ( isset( $prop['parseddescription'] ) ) {
166 $file['parseddescription'] = Linker
::formatComment(
167 $row->fa_description
, $title );
170 if ( $fld_mediatype ) {
171 $file['mediatype'] = $row->fa_media_type
;
173 if ( $fld_metadata ) {
174 $file['metadata'] = $row->fa_metadata
175 ? ApiQueryImageInfo
::processMetaData( unserialize( $row->fa_metadata
), $result )
178 if ( $fld_bitdepth ) {
179 $file['bitdepth'] = $row->fa_bits
;
182 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
185 if ( $row->fa_deleted
& File
::DELETED_FILE
) {
186 $file['filehidden'] = '';
188 if ( $row->fa_deleted
& File
::DELETED_COMMENT
) {
189 $file['commenthidden'] = '';
191 if ( $row->fa_deleted
& File
::DELETED_USER
) {
192 $file['userhidden'] = '';
194 if ( $row->fa_deleted
& File
::DELETED_RESTRICTED
) {
195 // This file is deleted for normal admins
196 $file['suppressed'] = '';
200 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
202 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
207 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
210 public function getAllowedParams() {
216 ApiBase
::PARAM_DFLT
=> 10,
217 ApiBase
::PARAM_TYPE
=> 'limit',
218 ApiBase
::PARAM_MIN
=> 1,
219 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
220 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
223 ApiBase
::PARAM_DFLT
=> 'ascending',
224 ApiBase
::PARAM_TYPE
=> array(
230 'sha1base36' => null,
232 ApiBase
::PARAM_DFLT
=> 'timestamp',
233 ApiBase
::PARAM_ISMULTI
=> true,
234 ApiBase
::PARAM_TYPE
=> array(
251 public function getParamDescription() {
253 'from' => 'The image title to start enumerating from',
254 'to' => 'The image title to stop enumerating at',
255 'prefix' => 'Search for all image titles that begin with this value',
256 'dir' => 'The direction in which to list',
257 'limit' => 'How many images to return in total',
258 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36. Disabled in Miser Mode",
259 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki). Disabled in Miser Mode',
261 'What image information to get:',
262 ' sha1 - Adds SHA-1 hash for the image',
263 ' timestamp - Adds timestamp for the uploaded version',
264 ' user - Adds user who uploaded the image version',
265 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
266 ' dimensions - Alias for size',
267 ' description - Adds description the image version',
268 ' parseddescription - Parse the description on the version',
269 ' mime - Adds MIME of the image',
270 ' mediatype - Adds the media type of the image',
271 ' metadata - Lists EXIF metadata for the version of the image',
272 ' bitdepth - Adds the bit depth of the version',
277 public function getResultProperties() {
283 'filehidden' => 'boolean',
284 'commenthidden' => 'boolean',
285 'userhidden' => 'boolean',
286 'suppressed' => 'boolean'
291 'timestamp' => array(
292 'timestamp' => 'timestamp'
295 'userid' => 'integer',
300 'pagecount' => array(
301 ApiBase
::PROP_TYPE
=> 'integer',
302 ApiBase
::PROP_NULLABLE
=> true
304 'height' => 'integer',
307 'dimensions' => array(
309 'pagecount' => array(
310 ApiBase
::PROP_TYPE
=> 'integer',
311 ApiBase
::PROP_NULLABLE
=> true
313 'height' => 'integer',
316 'description' => array(
317 'description' => 'string'
319 'parseddescription' => array(
320 'description' => 'string',
321 'parseddescription' => 'string'
324 'metadata' => 'string'
327 'bitdepth' => 'integer'
332 'mediatype' => array(
333 'mediatype' => 'string'
338 public function getDescription() {
339 return 'Enumerate all deleted files sequentially';
342 public function getPossibleErrors() {
343 return array_merge( parent
::getPossibleErrors(), array(
344 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
345 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
346 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
347 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
351 public function getExamples() {
353 'api.php?action=query&list=filearchive' => array(
355 'Show a list of all deleted files',
360 public function getVersion() {
361 return __CLASS__
. ': $Id$';