Localisation updates for core and extension messages from translatewiki.net (2011...
[mediawiki.git] / includes / filerepo / ForeignAPIFile.php
blobdaf2149159766e5da47a70924ce7b057478fca7a
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
5 * @file
6 * @ingroup FileRepo
7 */
9 /**
10 * Foreign file accessible through api.php requests.
11 * Very hacky and inefficient, do not use :D
13 * @ingroup FileRepo
15 class ForeignAPIFile extends File {
17 private $mExists;
19 /**
20 * @param $title
21 * @param $repo ForeignApiRepo
22 * @param $info
23 * @param bool $exists
25 function __construct( $title, $repo, $info, $exists = false ) {
26 parent::__construct( $title, $repo );
27 $this->mInfo = $info;
28 $this->mExists = $exists;
31 /**
32 * @static
33 * @param $title Title
34 * @param $repo ForeignApiRepo
35 * @return ForeignAPIFile|null
37 static function newFromTitle( $title, $repo ) {
38 $data = $repo->fetchImageQuery( array(
39 'titles' => 'File:' . $title->getDBKey(),
40 'iiprop' => self::getProps(),
41 'prop' => 'imageinfo' ) );
43 $info = $repo->getImageInfo( $data );
45 if( $info ) {
46 $lastRedirect = isset( $data['query']['redirects'] )
47 ? count( $data['query']['redirects'] ) - 1
48 : -1;
49 if( $lastRedirect >= 0 ) {
50 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
51 $img = new ForeignAPIFile( $newtitle, $repo, $info, true );
52 if( $img ) {
53 $img->redirectedFrom( $title->getDBkey() );
55 } else {
56 $img = new ForeignAPIFile( $title, $repo, $info, true );
58 return $img;
59 } else {
60 return null;
64 /**
65 * Get the property string for iiprop and aiprop
67 static function getProps() {
68 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
71 // Dummy functions...
72 public function exists() {
73 return $this->mExists;
76 public function getPath() {
77 return false;
80 function transform( $params, $flags = 0 ) {
81 if( !$this->canRender() ) {
82 // show icon
83 return parent::transform( $params, $flags );
86 // Note, the this->canRender() check above implies
87 // that we have a handler, and it can do makeParamString.
88 $otherParams = $this->handler->makeParamString( $params );
90 $thumbUrl = $this->repo->getThumbUrlFromCache(
91 $this->getName(),
92 isset( $params['width'] ) ? $params['width'] : -1,
93 isset( $params['height'] ) ? $params['height'] : -1,
94 $otherParams );
95 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
98 // Info we can get from API...
99 public function getWidth( $page = 1 ) {
100 return intval( @$this->mInfo['width'] );
103 public function getHeight( $page = 1 ) {
104 return intval( @$this->mInfo['height'] );
107 public function getMetadata() {
108 if ( isset( $this->mInfo['metadata'] ) ) {
109 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
111 return null;
114 public static function parseMetadata( $metadata ) {
115 if( !is_array( $metadata ) ) {
116 return $metadata;
118 $ret = array();
119 foreach( $metadata as $meta ) {
120 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
122 return $ret;
125 public function getSize() {
126 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
129 public function getUrl() {
130 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
133 public function getUser( $method='text' ) {
134 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
137 public function getDescription() {
138 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
141 function getSha1() {
142 return isset( $this->mInfo['sha1'] ) ?
143 wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) :
144 null;
147 function getTimestamp() {
148 return wfTimestamp( TS_MW,
149 isset( $this->mInfo['timestamp'] ) ?
150 strval( $this->mInfo['timestamp'] ) :
151 null
155 function getMimeType() {
156 if( !isset( $this->mInfo['mime'] ) ) {
157 $magic = MimeMagic::singleton();
158 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
160 return $this->mInfo['mime'];
163 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
164 function getMediaType() {
165 $magic = MimeMagic::singleton();
166 return $magic->getMediaType( null, $this->getMimeType() );
169 function getDescriptionUrl() {
170 return isset( $this->mInfo['descriptionurl'] )
171 ? $this->mInfo['descriptionurl']
172 : false;
176 * Only useful if we're locally caching thumbs anyway...
178 function getThumbPath( $suffix = '' ) {
179 if ( $this->repo->canCacheThumbs() ) {
180 $path = $this->repo->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
181 if ( $suffix ) {
182 $path = $path . $suffix . '/';
184 return $path;
185 } else {
186 return null;
190 function getThumbnails() {
191 $files = array();
192 $dir = $this->getThumbPath( $this->getName() );
193 if ( is_dir( $dir ) ) {
194 $handle = opendir( $dir );
195 if ( $handle ) {
196 while ( false !== ( $file = readdir($handle) ) ) {
197 if ( $file{0} != '.' ) {
198 $files[] = $file;
201 closedir( $handle );
204 return $files;
207 function purgeCache() {
208 $this->purgeThumbnails();
209 $this->purgeDescriptionPage();
212 function purgeDescriptionPage() {
213 global $wgMemc, $wgContLang;
214 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
215 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
216 $wgMemc->delete( $key );
219 function purgeThumbnails() {
220 global $wgMemc;
221 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
222 $wgMemc->delete( $key );
223 $files = $this->getThumbnails();
224 $dir = $this->getThumbPath( $this->getName() );
225 foreach ( $files as $file ) {
226 unlink( $dir . $file );
228 if ( is_dir( $dir ) ) {
229 rmdir( $dir ); // Might have already gone away, spews errors if we don't.