4 * Very hacky and inefficient
9 class ForeignAPIFile
extends File
{
13 function __construct( $title, $repo, $info, $exists = false ) {
14 parent
::__construct( $title, $repo );
16 $this->mExists
= $exists;
19 static function newFromTitle( $title, $repo ) {
20 $info = $repo->getImageInfo( $title );
22 return new ForeignAPIFile( $title, $repo, $info, true );
29 public function exists() {
30 return $this->mExists
;
33 public function getPath() {
37 function transform( $params, $flags = 0 ) {
38 if( !$this->canRender() ) {
40 return parent
::transform( $params, $flags );
42 $thumbUrl = $this->repo
->getThumbUrlFromCache(
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'] ) );
65 public static function parseMetadata( $metadata ) {
66 if( !is_array( $metadata ) ) {
70 foreach( $metadata as $meta ) {
71 $ret[ $meta['name'] ] = self
::parseMetadata( $meta['value'] );
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'] );
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']
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() );
128 $path = $path . $suffix . '/';
137 function getThumbnails() {
139 $dir = $this->getThumbPath( $this->getName() );
140 if ( is_dir( $dir ) ) {
141 $handle = opendir( $dir );
143 while ( false !== ( $file = readdir($handle) ) ) {
144 if ( $file{0} != '.' ) {
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() {
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.