fb422c756e6aae398c5a6b1d0be526794caedeb8
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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_id = isset( $prop['id'] );
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 $titles = array();
121 $count = 0;
122 $result = $this->getResult();
123 foreach ( $res as $row ) {
124 if ( ++$count > $limit ) {
125 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
126 // TODO: Security issue - if the user has no right to view next title, it will still be shown
127 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
128 break;
129 }
130
131 $file = array();
132 $file['name'] = $row->fa_name;
133
134 if ( $fld_sha1 ) {
135 $file['sha1'] = wfBaseConvert( $row->fa_storage_key, 36, 16, 40 );
136 }
137 if ( $fld_timestamp ) {
138 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
139 }
140 if ( $fld_user ) {
141 $file['user'] = $row->fa_user;
142 }
143 if ( $fld_size ) {
144 $file['size'] = $row->fa_size;
145 }
146 if ( $fld_dimensions ) {
147 $file['height'] = $row->fa_height;
148 $file['width'] = $row->fa_width;
149 }
150 if ( $fld_description ){
151 $file['description'] = $row->fa_description;
152 }
153 if ( $fld_metadata ){
154 $file['metadata'] = $row->fa_metadata ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result ) : null;
155 }
156 if ( $fld_bitdepth ){
157 $file['bitdepth'] = $row->fa_bits;
158 }
159 if ( $fld_mime ) {
160 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
161 }
162
163 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
164 if ( !$fit ) {
165 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
166 break;
167 }
168 }
169
170 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
171 }
172
173 public function getAllowedParams() {
174 return array (
175 'from' => null,
176 'prefix' => null,
177 'minsize' => array(
178 ApiBase::PARAM_TYPE => 'integer',
179 ),
180 'maxsize' => array(
181 ApiBase::PARAM_TYPE => 'integer',
182 ),
183 'limit' => array(
184 ApiBase::PARAM_DFLT => 10,
185 ApiBase::PARAM_TYPE => 'limit',
186 ApiBase::PARAM_MIN => 1,
187 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
188 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
189 ),
190 'dir' => array(
191 ApiBase::PARAM_DFLT => 'ascending',
192 ApiBase::PARAM_TYPE => array(
193 'ascending',
194 'descending'
195 )
196 ),
197 'sha1' => null,
198 'sha1base36' => null,
199 'prop' => array(
200 ApiBase::PARAM_DFLT => 'timestamp',
201 ApiBase::PARAM_ISMULTI => true,
202 ApiBase::PARAM_TYPE => array(
203 'sha1',
204 'timestamp',
205 'user',
206 'size',
207 'dimensions',
208 'description',
209 'mime',
210 'metadata',
211 'bitdepth'
212 ),
213 ),
214 );
215 }
216
217 public function getParamDescription() {
218 return array(
219 'from' => 'The image title to start enumerating from',
220 'prefix' => 'Search for all image titles that begin with this value',
221 'dir' => 'The direction in which to list',
222 'minsize' => 'Limit to images with at least this many bytes',
223 'maxsize' => 'Limit to images with at most this many bytes',
224 'limit' => 'How many total images to return',
225 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
226 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
227 'prop' => 'Which properties to get',
228 );
229 }
230
231 public function getDescription() {
232 return 'Enumerate all deleted files sequentially';
233 }
234
235 public function getPossibleErrors() {
236 return array_merge( parent::getPossibleErrors(), array(
237 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
238 ) );
239 }
240
241 protected function getExamples() {
242 return array(
243 'Simple Use',
244 ' Show a list of all deleted files',
245 ' api.php?action=query&list=filearchive',
246 );
247 }
248
249 public function getVersion() {
250 return __CLASS__ . ': $Id$';
251 }
252 }