3 * Foreign file accessible through api.php requests.
10 * Foreign file accessible through api.php requests.
11 * Very hacky and inefficient, do not use :D
15 class ForeignAPIFile
extends File
{
19 function __construct( $title, $repo, $info, $exists = false ) {
20 parent
::__construct( $title, $repo );
22 $this->mExists
= $exists;
25 static function newFromTitle( $title, $repo ) {
26 $data = $repo->fetchImageQuery( array(
27 'titles' => 'File:' . $title->getDBKey(),
28 'iiprop' => self
::getProps(),
29 'prop' => 'imageinfo' ) );
31 $info = $repo->getImageInfo( $data );
34 if( isset( $data['query']['redirects'][0] ) ) {
35 $newtitle = Title
::newFromText( $data['query']['redirects'][0]['to']);
36 $img = new ForeignAPIFile( $newtitle, $repo, $info, true );
37 if( $img ) $img->redirectedFrom( $title->getDBkey() );
39 $img = new ForeignAPIFile( $title, $repo, $info, true );
48 * Get the property string for iiprop and aiprop
50 static function getProps() {
51 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
55 public function exists() {
56 return $this->mExists
;
59 public function getPath() {
63 function transform( $params, $flags = 0 ) {
64 if( !$this->canRender() ) {
66 return parent
::transform( $params, $flags );
68 $thumbUrl = $this->repo
->getThumbUrlFromCache(
70 isset( $params['width'] ) ?
$params['width'] : -1,
71 isset( $params['height'] ) ?
$params['height'] : -1 );
72 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
75 // Info we can get from API...
76 public function getWidth( $page = 1 ) {
77 return intval( @$this->mInfo
['width'] );
80 public function getHeight( $page = 1 ) {
81 return intval( @$this->mInfo
['height'] );
84 public function getMetadata() {
85 if ( isset( $this->mInfo
['metadata'] ) ) {
86 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
91 public static function parseMetadata( $metadata ) {
92 if( !is_array( $metadata ) ) {
96 foreach( $metadata as $meta ) {
97 $ret[ $meta['name'] ] = self
::parseMetadata( $meta['value'] );
102 public function getSize() {
103 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
106 public function getUrl() {
107 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
110 public function getUser( $method='text' ) {
111 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
114 public function getDescription() {
115 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
119 return isset( $this->mInfo
['sha1'] ) ?
120 wfBaseConvert( strval( $this->mInfo
['sha1'] ), 16, 36, 31 ) :
124 function getTimestamp() {
125 return wfTimestamp( TS_MW
,
126 isset( $this->mInfo
['timestamp'] ) ?
127 strval( $this->mInfo
['timestamp'] ) :
132 function getMimeType() {
133 if( !isset( $this->mInfo
['mime'] ) ) {
134 $magic = MimeMagic
::singleton();
135 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
137 return $this->mInfo
['mime'];
140 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
141 function getMediaType() {
142 $magic = MimeMagic
::singleton();
143 return $magic->getMediaType( null, $this->getMimeType() );
146 function getDescriptionUrl() {
147 return isset( $this->mInfo
['descriptionurl'] )
148 ?
$this->mInfo
['descriptionurl']
153 * Only useful if we're locally caching thumbs anyway...
155 function getThumbPath( $suffix = '' ) {
156 if ( $this->repo
->canCacheThumbs() ) {
157 $path = $this->repo
->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
159 $path = $path . $suffix . '/';
167 function getThumbnails() {
169 $dir = $this->getThumbPath( $this->getName() );
170 if ( is_dir( $dir ) ) {
171 $handle = opendir( $dir );
173 while ( false !== ( $file = readdir($handle) ) ) {
174 if ( $file{0} != '.' ) {
184 function purgeCache() {
185 $this->purgeThumbnails();
186 $this->purgeDescriptionPage();
189 function purgeDescriptionPage() {
190 global $wgMemc, $wgContLang;
191 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
192 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
193 $wgMemc->delete( $key );
196 function purgeThumbnails() {
198 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
199 $wgMemc->delete( $key );
200 $files = $this->getThumbnails();
201 $dir = $this->getThumbPath( $this->getName() );
202 foreach ( $files as $file ) {
203 unlink( $dir . $file );
205 if ( is_dir( $dir ) ) {
206 rmdir( $dir ); // Might have already gone away, spews errors if we don't.