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
29 if ( !defined( 'MEDIAWIKI' ) ) {
30 // Eclipse helper - will be ignored in production
31 require_once( 'ApiQueryBase.php' );
35 * Query module to enumerate all deleted files.
39 class ApiQueryFilearchive
extends ApiQueryBase
{
41 public function __construct( $query, $moduleName ) {
42 parent
::__construct( $query, $moduleName, 'fa' );
45 public function execute() {
47 // Before doing anything at all, let's check permissions
48 if ( !$wgUser->isAllowed( 'deletedhistory' ) ) {
49 $this->dieUsage( 'You don\'t have permission to view deleted file information', 'permissiondenied' );
54 $params = $this->extractRequestParams();
56 $prop = array_flip( $params['prop'] );
57 $fld_sha1 = isset( $prop['sha1'] );
58 $fld_timestamp = isset( $prop['timestamp'] );
59 $fld_user = isset( $prop['user'] );
60 $fld_size = isset( $prop['size'] );
61 $fld_dimensions = isset( $prop['dimensions'] );
62 $fld_description = isset( $prop['description'] );
63 $fld_mime = isset( $prop['mime'] );
64 $fld_metadata = isset( $prop['metadata'] );
65 $fld_bitdepth = isset( $prop['bitdepth'] );
67 $this->addTables( 'filearchive' );
69 $this->addFields( 'fa_name' );
70 $this->addFieldsIf( 'fa_storage_key', $fld_sha1 );
71 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
74 $this->addFields( array( 'fa_user', 'fa_user_text' ) );
76 $this->addFieldsIf( 'fa_size', $fld_size );
78 if ( $fld_dimensions ) {
79 $this->addFields( array( 'fa_height', 'fa_width' ) );
82 $this->addFieldsIf( 'fa_description', $fld_description );
85 $this->addFields( array( 'fa_major_mime', 'fa_minor_mime' ) );
88 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
89 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
92 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
93 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
94 $this->addWhereRange( 'fa_name', $dir, $from, null );
95 if ( isset( $params['prefix'] ) )
96 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
98 if ( isset( $params['minsize'] ) ) {
99 $this->addWhere( 'fa_size>=' . intval( $params['minsize'] ) );
102 if ( isset( $params['maxsize'] ) ) {
103 $this->addWhere( 'fa_size<=' . intval( $params['maxsize'] ) );
107 if ( isset( $params['sha1'] ) ) {
108 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
109 } elseif ( isset( $params['sha1base36'] ) ) {
110 $sha1 = $params['sha1base36'];
113 $this->addWhere( 'fa_storage_key=' . $db->addQuotes( $sha1 ) );
116 $limit = $params['limit'];
117 $this->addOption( 'LIMIT', $limit +
1 );
118 $this->addOption( 'ORDER BY', 'fa_name' .
119 ( $params['dir'] == 'descending' ?
' DESC' : '' ) );
121 $res = $this->select( __METHOD__
);
124 $result = $this->getResult();
125 foreach ( $res as $row ) {
126 if ( ++
$count > $limit ) {
127 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
128 // TODO: Security issue - if the user has no right to view next title, it will still be shown
129 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
134 $file['name'] = $row->fa_name
;
135 self
::addTitleInfo( $file, Title
::makeTitle( NS_FILE
, $row->fa_name
) );
138 $file['sha1'] = LocalRepo
::getHashFromKey( $row->fa_storage_key
);
140 if ( $fld_timestamp ) {
141 $file['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->fa_timestamp
);
144 $file['userid'] = $row->fa_user
;
145 $file['user'] = $row->fa_user_text
;
148 $file['size'] = $row->fa_size
;
150 if ( $fld_dimensions ) {
151 $file['height'] = $row->fa_height
;
152 $file['width'] = $row->fa_width
;
154 if ( $fld_description ) {
155 $file['description'] = $row->fa_description
;
157 if ( $fld_metadata ) {
158 $file['metadata'] = $row->fa_metadata ? ApiQueryImageInfo
::processMetaData( unserialize( $row->fa_metadata
), $result ) : null;
160 if ( $fld_bitdepth ) {
161 $file['bitdepth'] = $row->fa_bits
;
164 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
167 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
169 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
174 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
177 public function getAllowedParams() {
182 ApiBase
::PARAM_TYPE
=> 'integer',
185 ApiBase
::PARAM_TYPE
=> 'integer',
188 ApiBase
::PARAM_DFLT
=> 10,
189 ApiBase
::PARAM_TYPE
=> 'limit',
190 ApiBase
::PARAM_MIN
=> 1,
191 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
192 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
195 ApiBase
::PARAM_DFLT
=> 'ascending',
196 ApiBase
::PARAM_TYPE
=> array(
202 'sha1base36' => null,
204 ApiBase
::PARAM_DFLT
=> 'timestamp',
205 ApiBase
::PARAM_ISMULTI
=> true,
206 ApiBase
::PARAM_TYPE
=> array(
221 public function getParamDescription() {
223 'from' => 'The image title to start enumerating from',
224 'prefix' => 'Search for all image titles that begin with this value',
225 'dir' => 'The direction in which to list',
226 'minsize' => 'Limit to images with at least this many bytes',
227 'maxsize' => 'Limit to images with at most this many bytes',
228 'limit' => 'How many total images to return',
229 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
230 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
232 'What image information to get:',
233 ' sha1 - Adds SHA-1 hash for the image',
234 ' timestamp - Adds timestamp for the uploaded version',
235 ' user - Adds user who uploaded the image version',
236 ' size - Adds the size of the image in bytes',
237 ' dimensions - Adds the height and width of the image',
238 ' description - Adds description the image version',
239 ' mime - Adds MIME of the image',
240 ' metadata - Lists EXIF metadata for the version of the image',
241 ' bitdepth - Adds the bit depth of the version',
246 public function getDescription() {
247 return 'Enumerate all deleted files sequentially';
250 public function getPossibleErrors() {
251 return array_merge( parent
::getPossibleErrors(), array(
252 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
256 protected function getExamples() {
259 ' Show a list of all deleted files',
260 ' api.php?action=query&list=filearchive',
264 public function getVersion() {
265 return __CLASS__
. ': $Id$';