Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / filerepo / file / ForeignAPIFile.php
blob56482611f8a0ba42c7ac242c68cee363de23a268
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
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.
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.
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
20 * @file
21 * @ingroup FileAbstraction
24 /**
25 * Foreign file accessible through api.php requests.
26 * Very hacky and inefficient, do not use :D
28 * @ingroup FileAbstraction
30 class ForeignAPIFile extends File {
31 private $mExists;
33 protected $repoClass = 'ForeignApiRepo';
35 /**
36 * @param $title
37 * @param $repo ForeignApiRepo
38 * @param $info
39 * @param bool $exists
41 function __construct( $title, $repo, $info, $exists = false ) {
42 parent::__construct( $title, $repo );
44 $this->mInfo = $info;
45 $this->mExists = $exists;
47 $this->assertRepoDefined();
50 /**
51 * @param $title Title
52 * @param $repo ForeignApiRepo
53 * @return ForeignAPIFile|null
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 ) );
63 $info = $repo->getImageInfo( $data );
65 if( $info ) {
66 $lastRedirect = isset( $data['query']['redirects'] )
67 ? count( $data['query']['redirects'] ) - 1
68 : -1;
69 if( $lastRedirect >= 0 ) {
70 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
71 $img = new self( $newtitle, $repo, $info, true );
72 if( $img ) {
73 $img->redirectedFrom( $title->getDBkey() );
75 } else {
76 $img = new self( $title, $repo, $info, true );
78 return $img;
79 } else {
80 return null;
84 /**
85 * Get the property string for iiprop and aiprop
86 * @return string
88 static function getProps() {
89 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
92 // Dummy functions...
94 /**
95 * @return bool
97 public function exists() {
98 return $this->mExists;
102 * @return bool
104 public function getPath() {
105 return false;
109 * @param Array $params
110 * @param int $flags
111 * @return bool|MediaTransformOutput
113 function transform( $params, $flags = 0 ) {
114 if( !$this->canRender() ) {
115 // show icon
116 return parent::transform( $params, $flags );
119 // Note, the this->canRender() check above implies
120 // that we have a handler, and it can do makeParamString.
121 $otherParams = $this->handler->makeParamString( $params );
123 $thumbUrl = $this->repo->getThumbUrlFromCache(
124 $this->getName(),
125 isset( $params['width'] ) ? $params['width'] : -1,
126 isset( $params['height'] ) ? $params['height'] : -1,
127 $otherParams );
128 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
131 // Info we can get from API...
134 * @param $page int
135 * @return int|number
137 public function getWidth( $page = 1 ) {
138 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
142 * @param $page int
143 * @return int
145 public function getHeight( $page = 1 ) {
146 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
150 * @return bool|null|string
152 public function getMetadata() {
153 if ( isset( $this->mInfo['metadata'] ) ) {
154 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
156 return null;
160 * @param $metadata array
161 * @return array
163 public static function parseMetadata( $metadata ) {
164 if( !is_array( $metadata ) ) {
165 return $metadata;
167 $ret = array();
168 foreach( $metadata as $meta ) {
169 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
171 return $ret;
175 * @return bool|int|null
177 public function getSize() {
178 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
182 * @return null|string
184 public function getUrl() {
185 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
189 * @param string $method
190 * @return int|null|string
192 public function getUser( $method='text' ) {
193 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
197 * @return null|string
199 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
200 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
204 * @return null|String
206 function getSha1() {
207 return isset( $this->mInfo['sha1'] )
208 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
209 : null;
213 * @return bool|Mixed|string
215 function getTimestamp() {
216 return wfTimestamp( TS_MW,
217 isset( $this->mInfo['timestamp'] )
218 ? strval( $this->mInfo['timestamp'] )
219 : null
224 * @return string
226 function getMimeType() {
227 if( !isset( $this->mInfo['mime'] ) ) {
228 $magic = MimeMagic::singleton();
229 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
231 return $this->mInfo['mime'];
235 * @todo FIXME: May guess wrong on file types that can be eg audio or video
236 * @return int|string
238 function getMediaType() {
239 $magic = MimeMagic::singleton();
240 return $magic->getMediaType( null, $this->getMimeType() );
244 * @return bool|string
246 function getDescriptionUrl() {
247 return isset( $this->mInfo['descriptionurl'] )
248 ? $this->mInfo['descriptionurl']
249 : false;
253 * Only useful if we're locally caching thumbs anyway...
254 * @param $suffix string
255 * @return null|string
257 function getThumbPath( $suffix = '' ) {
258 if ( $this->repo->canCacheThumbs() ) {
259 $path = $this->repo->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
260 if ( $suffix ) {
261 $path = $path . $suffix . '/';
263 return $path;
264 } else {
265 return null;
270 * @return array
272 function getThumbnails() {
273 $dir = $this->getThumbPath( $this->getName() );
274 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
276 $files = array();
277 foreach ( $iter as $file ) {
278 $files[] = $file;
281 return $files;
285 * @see File::purgeCache()
287 function purgeCache( $options = array() ) {
288 $this->purgeThumbnails( $options );
289 $this->purgeDescriptionPage();
292 function purgeDescriptionPage() {
293 global $wgMemc, $wgContLang;
295 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
296 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
298 $wgMemc->delete( $key );
302 * @param $options array
304 function purgeThumbnails( $options = array() ) {
305 global $wgMemc;
307 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
308 $wgMemc->delete( $key );
310 $files = $this->getThumbnails();
311 // Give media handler a chance to filter the purge list
312 $handler = $this->getHandler();
313 if ( $handler ) {
314 $handler->filterThumbnailPurgeList( $files, $options );
317 $dir = $this->getThumbPath( $this->getName() );
318 $purgeList = array();
319 foreach ( $files as $file ) {
320 $purgeList[] = "{$dir}{$file}";
323 # Delete the thumbnails
324 $this->repo->quickPurgeBatch( $purgeList );
325 # Clear out the thumbnail directory if empty
326 $this->repo->quickCleanDir( $dir );