Stylize API up to date
[lhc/web/wiklou.git] / includes / api / ApiQueryFilearchive.php
1 <?php
2
3 /**
4 * Created on May 10, 2010
5 *
6 * API for MediaWiki 1.12+
7 *
8 * Copyright © 2010 Sam Reed
9 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
10 * based on ApiQueryAllpages.php
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 */
27
28 if ( !defined( 'MEDIAWIKI' ) ) {
29 // Eclipse helper - will be ignored in production
30 require_once( 'ApiQueryBase.php' );
31 }
32
33 /**
34 * Query module to enumerate all deleted files.
35 *
36 * @ingroup API
37 */
38 class ApiQueryFilearchive extends ApiQueryBase {
39
40 public function __construct( $query, $moduleName ) {
41 parent::__construct( $query, $moduleName, 'fa' );
42 }
43
44 public function execute() {
45 global $wgUser;
46 // Before doing anything at all, let's check permissions
47 if ( !$wgUser->isAllowed( 'deletedhistory' ) ) {
48 $this->dieUsage( 'You don\'t have permission to view deleted file information', 'permissiondenied' );
49 }
50
51 $db = $this->getDB();
52
53 $params = $this->extractRequestParams();
54
55 $prop = array_flip( $params['prop'] );
56 $fld_sha1 = isset( $prop['sha1'] );
57 $fld_timestamp = isset( $prop['timestamp'] );
58 $fld_user = isset( $prop['user'] );
59 $fld_size = isset( $prop['size'] );
60 $fld_dimensions = isset( $prop['dimensions'] );
61 $fld_description = isset( $prop['description'] );
62 $fld_mime = isset( $prop['mime'] );
63 $fld_metadata = isset( $prop['metadata'] );
64 $fld_bitdepth = isset( $prop['bitdepth'] );
65
66 $this->addTables( 'filearchive' );
67
68 $this->addFields( 'fa_name' );
69 $this->addFieldsIf( 'fa_storage_key', $fld_sha1 );
70 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
71 $this->addFieldsIf( 'fa_user', $fld_user );
72 $this->addFieldsIf( 'fa_size', $fld_size );
73
74 if ( $fld_dimensions ) {
75 $this->addFields( array( 'fa_height', 'fa_width' ) );
76 }
77
78 $this->addFieldsIf( 'fa_description', $fld_description );
79
80 if ( $fld_mime ) {
81 $this->addFields( array( 'fa_major_mime', 'fa_minor_mime' ) );
82 }
83
84 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
85 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
86
87 // Image filters
88 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
89 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
90 $this->addWhereRange( 'fa_name', $dir, $from, null );
91 if ( isset( $params['prefix'] ) )
92 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
93
94 if ( isset( $params['minsize'] ) ) {
95 $this->addWhere( 'fa_size>=' . intval( $params['minsize'] ) );
96 }
97
98 if ( isset( $params['maxsize'] ) ) {
99 $this->addWhere( 'fa_size<=' . intval( $params['maxsize'] ) );
100 }
101
102 $sha1 = false;
103 if ( isset( $params['sha1'] ) ) {
104 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
105 } elseif ( isset( $params['sha1base36'] ) ) {
106 $sha1 = $params['sha1base36'];
107 }
108 if ( $sha1 ) {
109 $this->addWhere( 'fa_storage_key=' . $db->addQuotes( $sha1 ) );
110 }
111
112 $limit = $params['limit'];
113 $this->addOption( 'LIMIT', $limit + 1 );
114 $this->addOption( 'ORDER BY', 'fa_name' .
115 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
116
117 $res = $this->select( __METHOD__ );
118
119 $count = 0;
120 $result = $this->getResult();
121 foreach ( $res as $row ) {
122 if ( ++$count > $limit ) {
123 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
124 // TODO: Security issue - if the user has no right to view next title, it will still be shown
125 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
126 break;
127 }
128
129 $file = array();
130 $file['name'] = $row->fa_name;
131
132 if ( $fld_sha1 ) {
133 $file['sha1'] = wfBaseConvert( $row->fa_storage_key, 36, 16, 40 );
134 }
135 if ( $fld_timestamp ) {
136 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
137 }
138 if ( $fld_user ) {
139 $file['user'] = $row->fa_user;
140 }
141 if ( $fld_size ) {
142 $file['size'] = $row->fa_size;
143 }
144 if ( $fld_dimensions ) {
145 $file['height'] = $row->fa_height;
146 $file['width'] = $row->fa_width;
147 }
148 if ( $fld_description ) {
149 $file['description'] = $row->fa_description;
150 }
151 if ( $fld_metadata ) {
152 $file['metadata'] = $row->fa_metadata ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result ) : null;
153 }
154 if ( $fld_bitdepth ) {
155 $file['bitdepth'] = $row->fa_bits;
156 }
157 if ( $fld_mime ) {
158 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
159 }
160
161 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
162 if ( !$fit ) {
163 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
164 break;
165 }
166 }
167
168 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
169 }
170
171 public function getAllowedParams() {
172 return array (
173 'from' => null,
174 'prefix' => null,
175 'minsize' => array(
176 ApiBase::PARAM_TYPE => 'integer',
177 ),
178 'maxsize' => array(
179 ApiBase::PARAM_TYPE => 'integer',
180 ),
181 'limit' => array(
182 ApiBase::PARAM_DFLT => 10,
183 ApiBase::PARAM_TYPE => 'limit',
184 ApiBase::PARAM_MIN => 1,
185 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
186 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
187 ),
188 'dir' => array(
189 ApiBase::PARAM_DFLT => 'ascending',
190 ApiBase::PARAM_TYPE => array(
191 'ascending',
192 'descending'
193 )
194 ),
195 'sha1' => null,
196 'sha1base36' => null,
197 'prop' => array(
198 ApiBase::PARAM_DFLT => 'timestamp',
199 ApiBase::PARAM_ISMULTI => true,
200 ApiBase::PARAM_TYPE => array(
201 'sha1',
202 'timestamp',
203 'user',
204 'size',
205 'dimensions',
206 'description',
207 'mime',
208 'metadata',
209 'bitdepth'
210 ),
211 ),
212 );
213 }
214
215 public function getParamDescription() {
216 return array(
217 'from' => 'The image title to start enumerating from',
218 'prefix' => 'Search for all image titles that begin with this value',
219 'dir' => 'The direction in which to list',
220 'minsize' => 'Limit to images with at least this many bytes',
221 'maxsize' => 'Limit to images with at most this many bytes',
222 'limit' => 'How many total images to return',
223 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
224 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
225 'prop' => array(
226 'What image information to get:',
227 ' sha1 - Adds sha1 hash for the image',
228 ' timestamp - Adds timestamp for the uploaded version',
229 ' user - Adds user for uploaded the image version',
230 ' size - Adds the size of the image in bytes',
231 ' dimensions - Adds the height and width of the image',
232 ' description - Adds description the image version',
233 ' mime - Adds MIME of the image',
234 ' metadata - Lists EXIF metadata for the version of the image',
235 ' bitdepth - Adds the bit depth of the version',
236 ),
237 );
238 }
239
240 public function getDescription() {
241 return 'Enumerate all deleted files sequentially';
242 }
243
244 public function getPossibleErrors() {
245 return array_merge( parent::getPossibleErrors(), array(
246 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
247 ) );
248 }
249
250 protected function getExamples() {
251 return array(
252 'Simple Use',
253 ' Show a list of all deleted files',
254 ' api.php?action=query&list=filearchive',
255 );
256 }
257
258 public function getVersion() {
259 return __CLASS__ . ': $Id$';
260 }
261 }