Remove ancient comment, collapse if, move USE INDEX into if further down doing the...
[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 if ( !defined( 'MEDIAWIKI' ) ) {
30 // Eclipse helper - will be ignored in production
31 require_once( 'ApiQueryBase.php' );
32 }
33
34 /**
35 * Query module to enumerate all deleted files.
36 *
37 * @ingroup API
38 */
39 class ApiQueryFilearchive extends ApiQueryBase {
40
41 public function __construct( $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'fa' );
43 }
44
45 public function execute() {
46 global $wgUser;
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' );
50 }
51
52 $db = $this->getDB();
53
54 $params = $this->extractRequestParams();
55
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'] );
66
67 $this->addTables( 'filearchive' );
68
69 $this->addFields( 'fa_name' );
70 $this->addFieldsIf( 'fa_storage_key', $fld_sha1 );
71 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
72 $this->addFieldsIf( 'fa_user', $fld_user );
73 $this->addFieldsIf( 'fa_size', $fld_size );
74
75 if ( $fld_dimensions ) {
76 $this->addFields( array( 'fa_height', 'fa_width' ) );
77 }
78
79 $this->addFieldsIf( 'fa_description', $fld_description );
80
81 if ( $fld_mime ) {
82 $this->addFields( array( 'fa_major_mime', 'fa_minor_mime' ) );
83 }
84
85 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
86 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
87
88 // Image filters
89 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
90 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
91 $this->addWhereRange( 'fa_name', $dir, $from, null );
92 if ( isset( $params['prefix'] ) )
93 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
94
95 if ( isset( $params['minsize'] ) ) {
96 $this->addWhere( 'fa_size>=' . intval( $params['minsize'] ) );
97 }
98
99 if ( isset( $params['maxsize'] ) ) {
100 $this->addWhere( 'fa_size<=' . intval( $params['maxsize'] ) );
101 }
102
103 $sha1 = false;
104 if ( isset( $params['sha1'] ) ) {
105 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
106 } elseif ( isset( $params['sha1base36'] ) ) {
107 $sha1 = $params['sha1base36'];
108 }
109 if ( $sha1 ) {
110 $this->addWhere( 'fa_storage_key=' . $db->addQuotes( $sha1 ) );
111 }
112
113 $limit = $params['limit'];
114 $this->addOption( 'LIMIT', $limit + 1 );
115 $this->addOption( 'ORDER BY', 'fa_name' .
116 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
117
118 $res = $this->select( __METHOD__ );
119
120 $count = 0;
121 $result = $this->getResult();
122 foreach ( $res as $row ) {
123 if ( ++$count > $limit ) {
124 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
125 // TODO: Security issue - if the user has no right to view next title, it will still be shown
126 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
127 break;
128 }
129
130 $file = array();
131 $file['name'] = $row->fa_name;
132
133 if ( $fld_sha1 ) {
134 $file['sha1'] = wfBaseConvert( $row->fa_storage_key, 36, 16, 40 );
135 }
136 if ( $fld_timestamp ) {
137 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
138 }
139 if ( $fld_user ) {
140 $file['user'] = $row->fa_user;
141 }
142 if ( $fld_size ) {
143 $file['size'] = $row->fa_size;
144 }
145 if ( $fld_dimensions ) {
146 $file['height'] = $row->fa_height;
147 $file['width'] = $row->fa_width;
148 }
149 if ( $fld_description ) {
150 $file['description'] = $row->fa_description;
151 }
152 if ( $fld_metadata ) {
153 $file['metadata'] = $row->fa_metadata ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result ) : null;
154 }
155 if ( $fld_bitdepth ) {
156 $file['bitdepth'] = $row->fa_bits;
157 }
158 if ( $fld_mime ) {
159 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
160 }
161
162 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
163 if ( !$fit ) {
164 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
165 break;
166 }
167 }
168
169 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
170 }
171
172 public function getAllowedParams() {
173 return array (
174 'from' => null,
175 'prefix' => null,
176 'minsize' => array(
177 ApiBase::PARAM_TYPE => 'integer',
178 ),
179 'maxsize' => array(
180 ApiBase::PARAM_TYPE => 'integer',
181 ),
182 'limit' => array(
183 ApiBase::PARAM_DFLT => 10,
184 ApiBase::PARAM_TYPE => 'limit',
185 ApiBase::PARAM_MIN => 1,
186 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
187 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
188 ),
189 'dir' => array(
190 ApiBase::PARAM_DFLT => 'ascending',
191 ApiBase::PARAM_TYPE => array(
192 'ascending',
193 'descending'
194 )
195 ),
196 'sha1' => null,
197 'sha1base36' => null,
198 'prop' => array(
199 ApiBase::PARAM_DFLT => 'timestamp',
200 ApiBase::PARAM_ISMULTI => true,
201 ApiBase::PARAM_TYPE => array(
202 'sha1',
203 'timestamp',
204 'user',
205 'size',
206 'dimensions',
207 'description',
208 'mime',
209 'metadata',
210 'bitdepth'
211 ),
212 ),
213 );
214 }
215
216 public function getParamDescription() {
217 return array(
218 'from' => 'The image title to start enumerating from',
219 'prefix' => 'Search for all image titles that begin with this value',
220 'dir' => 'The direction in which to list',
221 'minsize' => 'Limit to images with at least this many bytes',
222 'maxsize' => 'Limit to images with at most this many bytes',
223 'limit' => 'How many total images to return',
224 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
225 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
226 'prop' => array(
227 'What image information to get:',
228 ' sha1 - Adds SHA-1 hash for the image',
229 ' timestamp - Adds timestamp for the uploaded version',
230 ' user - Adds user for uploaded the image version',
231 ' size - Adds the size of the image in bytes',
232 ' dimensions - Adds the height and width of the image',
233 ' description - Adds description the image version',
234 ' mime - Adds MIME of the image',
235 ' metadata - Lists EXIF metadata for the version of the image',
236 ' bitdepth - Adds the bit depth of the version',
237 ),
238 );
239 }
240
241 public function getDescription() {
242 return 'Enumerate all deleted files sequentially';
243 }
244
245 public function getPossibleErrors() {
246 return array_merge( parent::getPossibleErrors(), array(
247 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
248 ) );
249 }
250
251 protected function getExamples() {
252 return array(
253 'Simple Use',
254 ' Show a list of all deleted files',
255 ' api.php?action=query&list=filearchive',
256 );
257 }
258
259 public function getVersion() {
260 return __CLASS__ . ': $Id$';
261 }
262 }