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'] ) ) {
64 foreach( $this->mInfo
['metadata'] as $meta ) {
65 $ret[ $meta['name'] ] = $meta['value'];
67 return serialize( $ret );
72 public function getSize() {
73 return intval( @$this->mInfo
['size'] );
76 public function getUrl() {
77 return strval( @$this->mInfo
['url'] );
80 public function getUser( $method='text' ) {
81 return strval( @$this->mInfo
['user'] );
84 public function getDescription() {
85 return strval( @$this->mInfo
['comment'] );
89 return wfBaseConvert( strval( @$this->mInfo
['sha1'] ), 16, 36, 31 );
92 function getTimestamp() {
93 return wfTimestamp( TS_MW
, strval( @$this->mInfo
['timestamp'] ) );
96 function getMimeType() {
97 if( !isset( $this->mInfo
['mime'] ) ) {
98 $magic = MimeMagic
::singleton();
99 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
101 return $this->mInfo
['mime'];
104 /// @fixme May guess wrong on file types that can be eg audio or video
105 function getMediaType() {
106 $magic = MimeMagic
::singleton();
107 return $magic->getMediaType( null, $this->getMimeType() );
110 function getDescriptionUrl() {
111 return isset( $this->mInfo
['descriptionurl'] )
112 ?
$this->mInfo
['descriptionurl']
117 * Only useful if we're locally caching thumbs anyway...
119 function getThumbPath( $suffix = '' ) {
120 if ( $this->repo
->canCacheThumbs() ) {
121 global $wgUploadDirectory;
122 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
124 $path = $path . $suffix . '/';
133 function getThumbnails() {
135 $dir = $this->getThumbPath( $this->getName() );
136 if ( is_dir( $dir ) ) {
137 $handle = opendir( $dir );
139 while ( false !== ( $file = readdir($handle) ) ) {
140 if ( $file{0} != '.' ) {
150 function purgeCache() {
151 $this->purgeThumbnails();
152 $this->purgeDescriptionPage();
155 function purgeDescriptionPage() {
156 global $wgMemc, $wgContLang;
157 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
158 $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
159 $wgMemc->delete( $key );
162 function purgeThumbnails() {
164 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
165 $wgMemc->delete( $key );
166 $files = $this->getThumbnails();
167 $dir = $this->getThumbPath( $this->getName() );
168 foreach ( $files as $file ) {
169 unlink( $dir . $file );
171 if ( is_dir( $dir ) ) {
172 rmdir( $dir ); // Might have already gone away, spews errors if we don't.