f6ecad238ab93af50f0bb0c35457e2706c06de12
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
{
21 * @param $repo ForeignApiRepo
25 function __construct( $title, $repo, $info, $exists = false ) {
26 parent
::__construct( $title, $repo );
28 $this->mExists
= $exists;
34 * @param $repo ForeignApiRepo
35 * @return ForeignAPIFile|null
37 static function newFromTitle( $title, $repo ) {
38 $data = $repo->fetchImageQuery( array(
39 'titles' => 'File:' . $title->getDBKey(),
40 'iiprop' => self
::getProps(),
41 'prop' => 'imageinfo',
42 'iimetadataversion' => mediaHandler
::getMetadataVersion()
45 $info = $repo->getImageInfo( $data );
48 $lastRedirect = isset( $data['query']['redirects'] )
49 ?
count( $data['query']['redirects'] ) - 1
51 if( $lastRedirect >= 0 ) {
52 $newtitle = Title
::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
53 $img = new ForeignAPIFile( $newtitle, $repo, $info, true );
55 $img->redirectedFrom( $title->getDBkey() );
58 $img = new ForeignAPIFile( $title, $repo, $info, true );
67 * Get the property string for iiprop and aiprop
69 static function getProps() {
70 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
74 public function exists() {
75 return $this->mExists
;
78 public function getPath() {
82 function transform( $params, $flags = 0 ) {
83 if( !$this->canRender() ) {
85 return parent
::transform( $params, $flags );
88 // Note, the this->canRender() check above implies
89 // that we have a handler, and it can do makeParamString.
90 $otherParams = $this->handler
->makeParamString( $params );
92 $thumbUrl = $this->repo
->getThumbUrlFromCache(
94 isset( $params['width'] ) ?
$params['width'] : -1,
95 isset( $params['height'] ) ?
$params['height'] : -1,
97 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
100 // Info we can get from API...
101 public function getWidth( $page = 1 ) {
102 return intval( @$this->mInfo
['width'] );
105 public function getHeight( $page = 1 ) {
106 return intval( @$this->mInfo
['height'] );
109 public function getMetadata() {
110 if ( isset( $this->mInfo
['metadata'] ) ) {
111 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
116 public static function parseMetadata( $metadata ) {
117 if( !is_array( $metadata ) ) {
121 foreach( $metadata as $meta ) {
122 $ret[ $meta['name'] ] = self
::parseMetadata( $meta['value'] );
127 public function getSize() {
128 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
131 public function getUrl() {
132 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
135 public function getUser( $method='text' ) {
136 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
139 public function getDescription() {
140 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
144 return isset( $this->mInfo
['sha1'] ) ?
145 wfBaseConvert( strval( $this->mInfo
['sha1'] ), 16, 36, 31 ) :
149 function getTimestamp() {
150 return wfTimestamp( TS_MW
,
151 isset( $this->mInfo
['timestamp'] ) ?
152 strval( $this->mInfo
['timestamp'] ) :
157 function getMimeType() {
158 if( !isset( $this->mInfo
['mime'] ) ) {
159 $magic = MimeMagic
::singleton();
160 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
162 return $this->mInfo
['mime'];
165 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
166 function getMediaType() {
167 $magic = MimeMagic
::singleton();
168 return $magic->getMediaType( null, $this->getMimeType() );
171 function getDescriptionUrl() {
172 return isset( $this->mInfo
['descriptionurl'] )
173 ?
$this->mInfo
['descriptionurl']
178 * Only useful if we're locally caching thumbs anyway...
180 function getThumbPath( $suffix = '' ) {
181 if ( $this->repo
->canCacheThumbs() ) {
182 $path = $this->repo
->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
184 $path = $path . $suffix . '/';
192 function getThumbnails() {
194 $dir = $this->getThumbPath( $this->getName() );
195 if ( is_dir( $dir ) ) {
196 $handle = opendir( $dir );
198 while ( false !== ( $file = readdir($handle) ) ) {
199 if ( $file{0} != '.' ) {
209 function purgeCache() {
210 $this->purgeThumbnails();
211 $this->purgeDescriptionPage();
214 function purgeDescriptionPage() {
215 global $wgMemc, $wgContLang;
216 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
217 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
218 $wgMemc->delete( $key );
221 function purgeThumbnails() {
223 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
224 $wgMemc->delete( $key );
225 $files = $this->getThumbnails();
226 $dir = $this->getThumbPath( $this->getName() );
227 foreach ( $files as $file ) {
228 unlink( $dir . $file );
230 if ( is_dir( $dir ) ) {
231 rmdir( $dir ); // Might have already gone away, spews errors if we don't.