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 return serialize( (array)@$this->mInfo
['metadata'] );
65 public function getSize() {
66 return intval( @$this->mInfo
['size'] );
69 public function getUrl() {
70 return strval( @$this->mInfo
['url'] );
73 public function getUser( $method='text' ) {
74 return strval( @$this->mInfo
['user'] );
77 public function getDescription() {
78 return strval( @$this->mInfo
['comment'] );
82 return wfBaseConvert( strval( @$this->mInfo
['sha1'] ), 16, 36, 31 );
85 function getTimestamp() {
86 return wfTimestamp( TS_MW
, strval( @$this->mInfo
['timestamp'] ) );
89 function getMimeType() {
90 if( empty( $info['mime'] ) ) {
91 $magic = MimeMagic
::singleton();
92 $info['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
97 /// @fixme May guess wrong on file types that can be eg audio or video
98 function getMediaType() {
99 $magic = MimeMagic
::singleton();
100 return $magic->getMediaType( null, $this->getMimeType() );
103 function getDescriptionUrl() {
104 return isset( $this->mInfo
['descriptionurl'] )
105 ?
$this->mInfo
['descriptionurl']
110 * Only useful if we're locally caching thumbs anyway...
112 function getThumbPath( $suffix = '' ) {
113 if ( $this->repo
->canCacheThumbs() ) {
114 global $wgUploadDirectory;
115 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
117 $path = $path . $suffix . '/';
126 function getThumbnails() {
128 $dir = $this->getThumbPath( $this->getName() );
129 if ( is_dir( $dir ) ) {
130 $handle = opendir( $dir );
132 while ( false !== ( $file = readdir($handle) ) ) {
133 if ( $file{0} != '.' ) {
143 function purgeCache() {
144 $this->purgeThumbnails();
145 $this->purgeDescriptionPage();
148 function purgeDescriptionPage() {
150 $url = $this->repo
->getDescriptionRenderUrl( $this->getName() );
151 $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
152 $wgMemc->delete( $key );
155 function purgeThumbnails() {
157 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
158 $wgMemc->delete( $key );
159 $files = $this->getThumbnails();
160 $dir = $this->getThumbPath( $this->getName() );
161 foreach ( $files as $file ) {
162 unlink( $dir . $file );
164 if ( is_dir( $dir ) ) {
165 rmdir( $dir ); // Might have already gone away, spews errors if we don't.