4 * Very hacky and inefficient
9 class ForeignAPIFile
extends File
{
10 function __construct( $title, $repo, $info ) {
11 parent
::__construct( $title, $repo );
15 static function newFromTitle( $title, $repo ) {
16 $info = $repo->getImageInfo( $title );
18 return new ForeignAPIFile( $title, $repo, $info );
25 public function exists() {
29 public function getPath() {
33 function transform( $params, $flags = 0 ) {
34 $thumbUrl = $this->repo
->getThumbUrlFromCache(
36 isset( $params['width'] ) ?
$params['width'] : -1,
37 isset( $params['height'] ) ?
$params['height'] : -1 );
39 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );;
44 // Info we can get from API...
45 public function getWidth( $page = 1 ) {
46 return intval( @$this->mInfo
['width'] );
49 public function getHeight( $page = 1 ) {
50 return intval( @$this->mInfo
['height'] );
53 public function getMetadata() {
54 return serialize( (array)@$this->mInfo
['metadata'] );
57 public function getSize() {
58 return intval( @$this->mInfo
['size'] );
61 public function getUrl() {
62 return strval( @$this->mInfo
['url'] );
65 public function getUser( $method='text' ) {
66 return strval( @$this->mInfo
['user'] );
69 public function getDescription() {
70 return strval( @$this->mInfo
['comment'] );
74 return wfBaseConvert( strval( @$this->mInfo
['sha1'] ), 16, 36, 31 );
77 function getTimestamp() {
78 return wfTimestamp( TS_MW
, strval( @$this->mInfo
['timestamp'] ) );
81 function getMimeType() {
82 if( empty( $info['mime'] ) ) {
83 $magic = MimeMagic
::singleton();
84 $info['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
89 /// @fixme May guess wrong on file types that can be eg audio or video
90 function getMediaType() {
91 $magic = MimeMagic
::singleton();
92 return $magic->getMediaType( null, $this->getMimeType() );
95 function getDescriptionUrl() {
96 return isset( $this->mInfo
['descriptionurl'] )
97 ?
$this->mInfo
['descriptionurl']
102 * Only useful if we're locally caching thumbs anyway...
104 function getThumbPath( $suffix = '' ) {
106 if ( $this->repo
->canCacheThumbs() ) {
107 global $wgUploadDirectory;
108 $path = $wgUploadDirectory . '/' . $this->repo
->apiThumbCacheDir
. '/' . $this->repo
->name
. '/';
110 $path = $path . $suffix . '/';
119 function getThumbnails() {
121 $dir = $this->getThumbPath( $this->getName() );
122 if ( is_dir( $dir ) ) {
123 $handle = opendir( $dir );
125 while ( false !== ( $file = readdir($handle) ) ) {
126 if ( $file{0} != '.' ) {
136 function purgeCache() {
137 $this->purgeThumbnails();
138 $this->purgeDescriptionPage();
141 function purgeDescriptionPage() {
143 $url = $this->repo
->getDescriptionRenderUrl( $this->getName() );
144 $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
145 $wgMemc->delete( $key );
148 function purgeThumbnails() {
150 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
151 $wgMemc->delete( $key );
152 $files = $this->getThumbnails();
153 $dir = $this->getThumbPath( $this->getName() );
154 foreach ( $files as $file ) {
155 unlink( $dir . $file );
157 if ( is_dir( $dir ) ) {
158 rmdir( $dir ); // Might have already gone away, spews errors if we don't.