f798fec9a97f08cbceefe8fbe2047e34e2b1fd06
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 );
47 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );;
52 // Info we can get from API...
53 public function getWidth( $page = 1 ) {
54 return intval( @$this->mInfo
['width'] );
57 public function getHeight( $page = 1 ) {
58 return intval( @$this->mInfo
['height'] );
61 public function getMetadata() {
62 if ( isset( $this->mInfo
['metadata'] ) ) {
63 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
68 public static function parseMetadata( $metadata ) {
69 if( !is_array( $metadata ) ) {
73 foreach( $metadata as $meta ) {
74 $ret[ $meta['name'] ] = self
::parseMetadata( $meta['value'] );
79 public function getSize() {
80 return intval( @$this->mInfo
['size'] );
83 public function getUrl() {
84 return strval( @$this->mInfo
['url'] );
87 public function getUser( $method='text' ) {
88 return strval( @$this->mInfo
['user'] );
91 public function getDescription() {
92 return strval( @$this->mInfo
['comment'] );
96 return wfBaseConvert( strval( @$this->mInfo
['sha1'] ), 16, 36, 31 );
99 function getTimestamp() {
100 return wfTimestamp( TS_MW
, strval( @$this->mInfo
['timestamp'] ) );
103 function getMimeType() {
104 if( !isset( $this->mInfo
['mime'] ) ) {
105 $magic = MimeMagic
::singleton();
106 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
108 return $this->mInfo
['mime'];
111 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
112 function getMediaType() {
113 $magic = MimeMagic
::singleton();
114 return $magic->getMediaType( null, $this->getMimeType() );
117 function getDescriptionUrl() {
118 return isset( $this->mInfo
['descriptionurl'] )
119 ?
$this->mInfo
['descriptionurl']
124 * Only useful if we're locally caching thumbs anyway...
126 function getThumbPath( $suffix = '' ) {
127 if ( $this->repo
->canCacheThumbs() ) {
128 global $wgUploadDirectory;
129 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
131 $path = $path . $suffix . '/';
140 function getThumbnails() {
142 $dir = $this->getThumbPath( $this->getName() );
143 if ( is_dir( $dir ) ) {
144 $handle = opendir( $dir );
146 while ( false !== ( $file = readdir($handle) ) ) {
147 if ( $file{0} != '.' ) {
157 function purgeCache() {
158 $this->purgeThumbnails();
159 $this->purgeDescriptionPage();
162 function purgeDescriptionPage() {
163 global $wgMemc, $wgContLang;
164 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
165 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
166 $wgMemc->delete( $key );
169 function purgeThumbnails() {
171 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
172 $wgMemc->delete( $key );
173 $files = $this->getThumbnails();
174 $dir = $this->getThumbPath( $this->getName() );
175 foreach ( $files as $file ) {
176 unlink( $dir . $file );
178 if ( is_dir( $dir ) ) {
179 rmdir( $dir ); // Might have already gone away, spews errors if we don't.