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 $thumbUrl = $this->repo
->getThumbUrlFromCache(
40 isset( $params['width'] ) ?
$params['width'] : -1,
41 isset( $params['height'] ) ?
$params['height'] : -1 );
43 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );;
48 // Info we can get from API...
49 public function getWidth( $page = 1 ) {
50 return intval( @$this->mInfo
['width'] );
53 public function getHeight( $page = 1 ) {
54 return intval( @$this->mInfo
['height'] );
57 public function getMetadata() {
58 return serialize( (array)@$this->mInfo
['metadata'] );
61 public function getSize() {
62 return intval( @$this->mInfo
['size'] );
65 public function getUrl() {
66 return strval( @$this->mInfo
['url'] );
69 public function getUser( $method='text' ) {
70 return strval( @$this->mInfo
['user'] );
73 public function getDescription() {
74 return strval( @$this->mInfo
['comment'] );
78 return wfBaseConvert( strval( @$this->mInfo
['sha1'] ), 16, 36, 31 );
81 function getTimestamp() {
82 return wfTimestamp( TS_MW
, strval( @$this->mInfo
['timestamp'] ) );
85 function getMimeType() {
86 if( empty( $info['mime'] ) ) {
87 $magic = MimeMagic
::singleton();
88 $info['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
93 /// @fixme May guess wrong on file types that can be eg audio or video
94 function getMediaType() {
95 $magic = MimeMagic
::singleton();
96 return $magic->getMediaType( null, $this->getMimeType() );
99 function getDescriptionUrl() {
100 return isset( $this->mInfo
['descriptionurl'] )
101 ?
$this->mInfo
['descriptionurl']
106 * Only useful if we're locally caching thumbs anyway...
108 function getThumbPath( $suffix = '' ) {
109 if ( $this->repo
->canCacheThumbs() ) {
110 global $wgUploadDirectory;
111 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
113 $path = $path . $suffix . '/';
122 function getThumbnails() {
124 $dir = $this->getThumbPath( $this->getName() );
125 if ( is_dir( $dir ) ) {
126 $handle = opendir( $dir );
128 while ( false !== ( $file = readdir($handle) ) ) {
129 if ( $file{0} != '.' ) {
139 function purgeCache() {
140 $this->purgeThumbnails();
141 $this->purgeDescriptionPage();
144 function purgeDescriptionPage() {
146 $url = $this->repo
->getDescriptionRenderUrl( $this->getName() );
147 $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
148 $wgMemc->delete( $key );
151 function purgeThumbnails() {
153 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
154 $wgMemc->delete( $key );
155 $files = $this->getThumbnails();
156 $dir = $this->getThumbPath( $this->getName() );
157 foreach ( $files as $file ) {
158 unlink( $dir . $file );
160 if ( is_dir( $dir ) ) {
161 rmdir( $dir ); // Might have already gone away, spews errors if we don't.