Update formatting of file repo classes
[lhc/web/wiklou.git] / includes / filerepo / file / ForeignAPIFile.php
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
24 /**
25 * Foreign file accessible through api.php requests.
26 * Very hacky and inefficient, do not use :D
27 *
28 * @ingroup FileAbstraction
29 */
30 class ForeignAPIFile extends File {
31 private $mExists;
32
33 protected $repoClass = 'ForeignApiRepo';
34
35 /**
36 * @param $title
37 * @param $repo ForeignApiRepo
38 * @param $info
39 * @param bool $exists
40 */
41 function __construct( $title, $repo, $info, $exists = false ) {
42 parent::__construct( $title, $repo );
43
44 $this->mInfo = $info;
45 $this->mExists = $exists;
46
47 $this->assertRepoDefined();
48 }
49
50 /**
51 * @param $title Title
52 * @param $repo ForeignApiRepo
53 * @return ForeignAPIFile|null
54 */
55 static function newFromTitle( Title $title, $repo ) {
56 $data = $repo->fetchImageQuery( array(
57 'titles' => 'File:' . $title->getDBkey(),
58 'iiprop' => self::getProps(),
59 'prop' => 'imageinfo',
60 'iimetadataversion' => MediaHandler::getMetadataVersion(),
61 // extmetadata is language-dependant, accessing the current language here
62 // would be problematic, so we just get them all
63 'iiextmetadatamultilang' => 1,
64 ) );
65
66 $info = $repo->getImageInfo( $data );
67
68 if ( $info ) {
69 $lastRedirect = isset( $data['query']['redirects'] )
70 ? count( $data['query']['redirects'] ) - 1
71 : -1;
72 if ( $lastRedirect >= 0 ) {
73 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
74 $img = new self( $newtitle, $repo, $info, true );
75 if ( $img ) {
76 $img->redirectedFrom( $title->getDBkey() );
77 }
78 } else {
79 $img = new self( $title, $repo, $info, true );
80 }
81
82 return $img;
83 } else {
84 return null;
85 }
86 }
87
88 /**
89 * Get the property string for iiprop and aiprop
90 * @return string
91 */
92 static function getProps() {
93 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
94 }
95
96 // Dummy functions...
97
98 /**
99 * @return bool
100 */
101 public function exists() {
102 return $this->mExists;
103 }
104
105 /**
106 * @return bool
107 */
108 public function getPath() {
109 return false;
110 }
111
112 /**
113 * @param array $params
114 * @param int $flags
115 * @return bool|MediaTransformOutput
116 */
117 function transform( $params, $flags = 0 ) {
118 if ( !$this->canRender() ) {
119 // show icon
120 return parent::transform( $params, $flags );
121 }
122
123 // Note, the this->canRender() check above implies
124 // that we have a handler, and it can do makeParamString.
125 $otherParams = $this->handler->makeParamString( $params );
126 $width = isset( $params['width'] ) ? $params['width'] : -1;
127 $height = isset( $params['height'] ) ? $params['height'] : -1;
128
129 $thumbUrl = $this->repo->getThumbUrlFromCache(
130 $this->getName(),
131 $width,
132 $height,
133 $otherParams
134 );
135 if ( $thumbUrl === false ) {
136 global $wgLang;
137
138 return $this->repo->getThumbError(
139 $this->getName(),
140 $width,
141 $height,
142 $otherParams,
143 $wgLang->getCode()
144 );
145 }
146
147 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
148 }
149
150 // Info we can get from API...
151
152 /**
153 * @param $page int
154 * @return int|number
155 */
156 public function getWidth( $page = 1 ) {
157 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
158 }
159
160 /**
161 * @param $page int
162 * @return int
163 */
164 public function getHeight( $page = 1 ) {
165 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
166 }
167
168 /**
169 * @return bool|null|string
170 */
171 public function getMetadata() {
172 if ( isset( $this->mInfo['metadata'] ) ) {
173 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
174 }
175
176 return null;
177 }
178
179 /**
180 * @return array|null extended metadata (see imageinfo API for format) or null on error
181 */
182 public function getExtendedMetadata() {
183 if ( isset( $this->mInfo['extmetadata'] ) ) {
184 return $this->mInfo['extmetadata'];
185 }
186
187 return null;
188 }
189
190 /**
191 * @param $metadata array
192 * @return array
193 */
194 public static function parseMetadata( $metadata ) {
195 if ( !is_array( $metadata ) ) {
196 return $metadata;
197 }
198 $ret = array();
199 foreach ( $metadata as $meta ) {
200 $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
201 }
202
203 return $ret;
204 }
205
206 /**
207 * @return bool|int|null
208 */
209 public function getSize() {
210 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
211 }
212
213 /**
214 * @return null|string
215 */
216 public function getUrl() {
217 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
218 }
219
220 /**
221 * @param string $method
222 * @return int|null|string
223 */
224 public function getUser( $method = 'text' ) {
225 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
226 }
227
228 /**
229 * @return null|string
230 */
231 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
232 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
233 }
234
235 /**
236 * @return null|String
237 */
238 function getSha1() {
239 return isset( $this->mInfo['sha1'] )
240 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
241 : null;
242 }
243
244 /**
245 * @return bool|Mixed|string
246 */
247 function getTimestamp() {
248 return wfTimestamp( TS_MW,
249 isset( $this->mInfo['timestamp'] )
250 ? strval( $this->mInfo['timestamp'] )
251 : null
252 );
253 }
254
255 /**
256 * @return string
257 */
258 function getMimeType() {
259 if ( !isset( $this->mInfo['mime'] ) ) {
260 $magic = MimeMagic::singleton();
261 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
262 }
263
264 return $this->mInfo['mime'];
265 }
266
267 /**
268 * @return int|string
269 */
270 function getMediaType() {
271 if ( isset( $this->mInfo['mediatype'] ) ) {
272 return $this->mInfo['mediatype'];
273 }
274 $magic = MimeMagic::singleton();
275
276 return $magic->getMediaType( null, $this->getMimeType() );
277 }
278
279 /**
280 * @return bool|string
281 */
282 function getDescriptionUrl() {
283 return isset( $this->mInfo['descriptionurl'] )
284 ? $this->mInfo['descriptionurl']
285 : false;
286 }
287
288 /**
289 * Only useful if we're locally caching thumbs anyway...
290 * @param $suffix string
291 * @return null|string
292 */
293 function getThumbPath( $suffix = '' ) {
294 if ( $this->repo->canCacheThumbs() ) {
295 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
296 if ( $suffix ) {
297 $path = $path . $suffix . '/';
298 }
299
300 return $path;
301 } else {
302 return null;
303 }
304 }
305
306 /**
307 * @return array
308 */
309 function getThumbnails() {
310 $dir = $this->getThumbPath( $this->getName() );
311 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
312
313 $files = array();
314 foreach ( $iter as $file ) {
315 $files[] = $file;
316 }
317
318 return $files;
319 }
320
321 /**
322 * @see File::purgeCache()
323 */
324 function purgeCache( $options = array() ) {
325 $this->purgeThumbnails( $options );
326 $this->purgeDescriptionPage();
327 }
328
329 function purgeDescriptionPage() {
330 global $wgMemc, $wgContLang;
331
332 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
333 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
334
335 $wgMemc->delete( $key );
336 }
337
338 /**
339 * @param $options array
340 */
341 function purgeThumbnails( $options = array() ) {
342 global $wgMemc;
343
344 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
345 $wgMemc->delete( $key );
346
347 $files = $this->getThumbnails();
348 // Give media handler a chance to filter the purge list
349 $handler = $this->getHandler();
350 if ( $handler ) {
351 $handler->filterThumbnailPurgeList( $files, $options );
352 }
353
354 $dir = $this->getThumbPath( $this->getName() );
355 $purgeList = array();
356 foreach ( $files as $file ) {
357 $purgeList[] = "{$dir}{$file}";
358 }
359
360 # Delete the thumbnails
361 $this->repo->quickPurgeBatch( $purgeList );
362 # Clear out the thumbnail directory if empty
363 $this->repo->quickCleanDir( $dir );
364 }
365 }