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 $data = $repo->fetchImageQuery( array(
21 'titles' => 'File:' . $title->getText(),
22 'iiprop' => self
::getProps(),
23 'prop' => 'imageinfo' ) );
25 $info = $repo->getImageInfo( $data );
28 if( isset( $data['query']['redirects'][0] ) ) {
29 $newtitle = Title
::newFromText( $data['query']['redirects'][0]['to']);
30 $img = new ForeignAPIFile( $newtitle, $repo, $info, true );
31 if( $img ) $img->redirectedFrom( $title->getDBkey() );
33 $img = new ForeignAPIFile( $title, $repo, $info, true );
42 * Get the property string for iiprop and aiprop
44 static function getProps() {
45 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
49 public function exists() {
50 return $this->mExists
;
53 public function getPath() {
57 function transform( $params, $flags = 0 ) {
58 if( !$this->canRender() ) {
60 return parent
::transform( $params, $flags );
62 $thumbUrl = $this->repo
->getThumbUrlFromCache(
64 isset( $params['width'] ) ?
$params['width'] : -1,
65 isset( $params['height'] ) ?
$params['height'] : -1 );
66 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );;
69 // Info we can get from API...
70 public function getWidth( $page = 1 ) {
71 return intval( @$this->mInfo
['width'] );
74 public function getHeight( $page = 1 ) {
75 return intval( @$this->mInfo
['height'] );
78 public function getMetadata() {
79 if ( isset( $this->mInfo
['metadata'] ) ) {
80 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
85 public static function parseMetadata( $metadata ) {
86 if( !is_array( $metadata ) ) {
90 foreach( $metadata as $meta ) {
91 $ret[ $meta['name'] ] = self
::parseMetadata( $meta['value'] );
96 public function getSize() {
97 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
100 public function getUrl() {
101 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
104 public function getUser( $method='text' ) {
105 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
108 public function getDescription() {
109 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
113 return isset( $this->mInfo
['sha1'] ) ?
114 wfBaseConvert( strval( $this->mInfo
['sha1'] ), 16, 36, 31 ) :
118 function getTimestamp() {
119 return wfTimestamp( TS_MW
,
120 isset( $this->mInfo
['timestamp'] ) ?
121 strval( $this->mInfo
['timestamp'] ) :
126 function getMimeType() {
127 if( !isset( $this->mInfo
['mime'] ) ) {
128 $magic = MimeMagic
::singleton();
129 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
131 return $this->mInfo
['mime'];
134 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
135 function getMediaType() {
136 $magic = MimeMagic
::singleton();
137 return $magic->getMediaType( null, $this->getMimeType() );
140 function getDescriptionUrl() {
141 return isset( $this->mInfo
['descriptionurl'] )
142 ?
$this->mInfo
['descriptionurl']
147 * Only useful if we're locally caching thumbs anyway...
149 function getThumbPath( $suffix = '' ) {
150 if ( $this->repo
->canCacheThumbs() ) {
151 global $wgUploadDirectory;
152 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
154 $path = $path . $suffix . '/';
163 function getThumbnails() {
165 $dir = $this->getThumbPath( $this->getName() );
166 if ( is_dir( $dir ) ) {
167 $handle = opendir( $dir );
169 while ( false !== ( $file = readdir($handle) ) ) {
170 if ( $file{0} != '.' ) {
180 function purgeCache() {
181 $this->purgeThumbnails();
182 $this->purgeDescriptionPage();
185 function purgeDescriptionPage() {
186 global $wgMemc, $wgContLang;
187 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
188 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
189 $wgMemc->delete( $key );
192 function purgeThumbnails() {
194 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
195 $wgMemc->delete( $key );
196 $files = $this->getThumbnails();
197 $dir = $this->getThumbPath( $this->getName() );
198 foreach ( $files as $file ) {
199 unlink( $dir . $file );
201 if ( is_dir( $dir ) ) {
202 rmdir( $dir ); // Might have already gone away, spews errors if we don't.