Minor followup to r64197
[mediawiki.git] / includes / filerepo / ForeignAPIFile.php
blobc46b1f8ff1e46d6b362b201cd3aa5fe185d4d3eb
1 <?php
3 /**
4 * Very hacky and inefficient
5 * do not use :D
7 * @ingroup FileRepo
8 */
9 class ForeignAPIFile extends File {
11 private $mExists;
13 function __construct( $title, $repo, $info, $exists = false ) {
14 parent::__construct( $title, $repo );
15 $this->mInfo = $info;
16 $this->mExists = $exists;
19 static function newFromTitle( $title, $repo ) {
20 $info = $repo->getImageInfo( $title );
21 if( $info ) {
22 return new ForeignAPIFile( $title, $repo, $info, true );
23 } else {
24 return null;
28 // Dummy functions...
29 public function exists() {
30 return $this->mExists;
33 public function getPath() {
34 return false;
37 function transform( $params, $flags = 0 ) {
38 if( !$this->canRender() ) {
39 // show icon
40 return parent::transform( $params, $flags );
42 $thumbUrl = $this->repo->getThumbUrlFromCache(
43 $this->getName(),
44 isset( $params['width'] ) ? $params['width'] : -1,
45 isset( $params['height'] ) ? $params['height'] : -1 );
46 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
49 // Info we can get from API...
50 public function getWidth( $page = 1 ) {
51 return intval( @$this->mInfo['width'] );
54 public function getHeight( $page = 1 ) {
55 return intval( @$this->mInfo['height'] );
58 public function getMetadata() {
59 if ( isset( $this->mInfo['metadata'] ) ) {
60 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
62 return null;
65 public static function parseMetadata( $metadata ) {
66 if( !is_array( $metadata ) ) {
67 return $metadata;
69 $ret = array();
70 foreach( $metadata as $meta ) {
71 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
73 return $ret;
76 public function getSize() {
77 return intval( @$this->mInfo['size'] );
80 public function getUrl() {
81 return strval( @$this->mInfo['url'] );
84 public function getUser( $method='text' ) {
85 return strval( @$this->mInfo['user'] );
88 public function getDescription() {
89 return strval( @$this->mInfo['comment'] );
92 function getSha1() {
93 return wfBaseConvert( strval( @$this->mInfo['sha1'] ), 16, 36, 31 );
96 function getTimestamp() {
97 return wfTimestamp( TS_MW, strval( @$this->mInfo['timestamp'] ) );
100 function getMimeType() {
101 if( !isset( $this->mInfo['mime'] ) ) {
102 $magic = MimeMagic::singleton();
103 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
105 return $this->mInfo['mime'];
108 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
109 function getMediaType() {
110 $magic = MimeMagic::singleton();
111 return $magic->getMediaType( null, $this->getMimeType() );
114 function getDescriptionUrl() {
115 return isset( $this->mInfo['descriptionurl'] )
116 ? $this->mInfo['descriptionurl']
117 : false;
121 * Only useful if we're locally caching thumbs anyway...
123 function getThumbPath( $suffix = '' ) {
124 if ( $this->repo->canCacheThumbs() ) {
125 global $wgUploadDirectory;
126 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
127 if ( $suffix ) {
128 $path = $path . $suffix . '/';
130 return $path;
132 else {
133 return null;
137 function getThumbnails() {
138 $files = array();
139 $dir = $this->getThumbPath( $this->getName() );
140 if ( is_dir( $dir ) ) {
141 $handle = opendir( $dir );
142 if ( $handle ) {
143 while ( false !== ( $file = readdir($handle) ) ) {
144 if ( $file{0} != '.' ) {
145 $files[] = $file;
148 closedir( $handle );
151 return $files;
154 function purgeCache() {
155 $this->purgeThumbnails();
156 $this->purgeDescriptionPage();
159 function purgeDescriptionPage() {
160 global $wgMemc, $wgContLang;
161 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
162 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
163 $wgMemc->delete( $key );
166 function purgeThumbnails() {
167 global $wgMemc;
168 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
169 $wgMemc->delete( $key );
170 $files = $this->getThumbnails();
171 $dir = $this->getThumbPath( $this->getName() );
172 foreach ( $files as $file ) {
173 unlink( $dir . $file );
175 if ( is_dir( $dir ) ) {
176 rmdir( $dir ); // Might have already gone away, spews errors if we don't.