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 if ( $this->repo
->apiThumbCacheExpiry
> 0 && $this->repo
->thumbDir
) {
35 $thumbUrl = $this->repo
->getThumbUrlFromCache(
37 isset( $params['width'] ) ?
$params['width'] : -1,
38 isset( $params['height'] ) ?
$params['height'] : -1 );
40 $thumbUrl = $this->repo
->getThumbUrl(
42 isset( $params['width'] ) ?
$params['width'] : -1,
43 isset( $params['height'] ) ?
$params['height'] : -1 );
46 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );;
51 // Info we can get from API...
52 public function getWidth( $page = 1 ) {
53 return intval( @$this->mInfo
['width'] );
56 public function getHeight( $page = 1 ) {
57 return intval( @$this->mInfo
['height'] );
60 public function getMetadata() {
61 return serialize( (array)@$this->mInfo
['metadata'] );
64 public function getSize() {
65 return intval( @$this->mInfo
['size'] );
68 public function getUrl() {
69 return strval( @$this->mInfo
['url'] );
72 public function getUser( $method='text' ) {
73 return strval( @$this->mInfo
['user'] );
76 public function getDescription() {
77 return strval( @$this->mInfo
['comment'] );
81 return wfBaseConvert( strval( @$this->mInfo
['sha1'] ), 16, 36, 31 );
84 function getTimestamp() {
85 return wfTimestamp( TS_MW
, strval( @$this->mInfo
['timestamp'] ) );
88 function getMimeType() {
89 if( empty( $info['mime'] ) ) {
90 $magic = MimeMagic
::singleton();
91 $info['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
96 /// @fixme May guess wrong on file types that can be eg audio or video
97 function getMediaType() {
98 $magic = MimeMagic
::singleton();
99 return $magic->getMediaType( null, $this->getMimeType() );
102 function getDescriptionUrl() {
103 return isset( $this->mInfo
['descriptionurl'] )
104 ?
$this->mInfo
['descriptionurl']
109 * Only useful if we're locally caching thumbs anyway...
111 function getThumbPath( $suffix = '' ) {
113 if ( $this->repo
->apiThumbCacheExpiry
> 0 && $this->repo
->thumbDir
) {
114 global $wgUploadDirectory;
115 $path = $wgUploadDirectory . '/' . $this->repo
->thumbDir
. '/' . $this->repo
->name
. '/';
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.