281687b1d10c577c0962f646ea8cd7f9473cba98
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 protected $repoClass = 'ForeignApiRepo';
23 * @param $repo ForeignApiRepo
27 function __construct( $title, $repo, $info, $exists = false ) {
28 parent
::__construct( $title, $repo );
31 $this->mExists
= $exists;
33 $this->assertRepoDefined();
38 * @param $repo ForeignApiRepo
39 * @return ForeignAPIFile|null
41 static function newFromTitle( Title
$title, $repo ) {
42 $data = $repo->fetchImageQuery( array(
43 'titles' => 'File:' . $title->getDBKey(),
44 'iiprop' => self
::getProps(),
45 'prop' => 'imageinfo',
46 'iimetadataversion' => MediaHandler
::getMetadataVersion()
49 $info = $repo->getImageInfo( $data );
52 $lastRedirect = isset( $data['query']['redirects'] )
53 ?
count( $data['query']['redirects'] ) - 1
55 if( $lastRedirect >= 0 ) {
56 $newtitle = Title
::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
57 $img = new self( $newtitle, $repo, $info, true );
59 $img->redirectedFrom( $title->getDBkey() );
62 $img = new self( $title, $repo, $info, true );
71 * Get the property string for iiprop and aiprop
73 static function getProps() {
74 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
78 public function exists() {
79 return $this->mExists
;
82 public function getPath() {
86 function transform( $params, $flags = 0 ) {
87 if( !$this->canRender() ) {
89 return parent
::transform( $params, $flags );
92 // Note, the this->canRender() check above implies
93 // that we have a handler, and it can do makeParamString.
94 $otherParams = $this->handler
->makeParamString( $params );
96 $thumbUrl = $this->repo
->getThumbUrlFromCache(
98 isset( $params['width'] ) ?
$params['width'] : -1,
99 isset( $params['height'] ) ?
$params['height'] : -1,
101 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
104 // Info we can get from API...
105 public function getWidth( $page = 1 ) {
106 return isset( $this->mInfo
['width'] ) ?
intval( $this->mInfo
['width'] ) : 0;
113 public function getHeight( $page = 1 ) {
114 return isset( $this->mInfo
['height'] ) ?
intval( $this->mInfo
['height'] ) : 0;
117 public function getMetadata() {
118 if ( isset( $this->mInfo
['metadata'] ) ) {
119 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
124 public static function parseMetadata( $metadata ) {
125 if( !is_array( $metadata ) ) {
129 foreach( $metadata as $meta ) {
130 $ret[ $meta['name'] ] = self
::parseMetadata( $meta['value'] );
135 public function getSize() {
136 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
139 public function getUrl() {
140 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
143 public function getUser( $method='text' ) {
144 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
147 public function getDescription() {
148 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
152 return isset( $this->mInfo
['sha1'] ) ?
153 wfBaseConvert( strval( $this->mInfo
['sha1'] ), 16, 36, 31 ) :
157 function getTimestamp() {
158 return wfTimestamp( TS_MW
,
159 isset( $this->mInfo
['timestamp'] ) ?
160 strval( $this->mInfo
['timestamp'] ) :
165 function getMimeType() {
166 if( !isset( $this->mInfo
['mime'] ) ) {
167 $magic = MimeMagic
::singleton();
168 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
170 return $this->mInfo
['mime'];
173 /// @todo FIXME: May guess wrong on file types that can be eg audio or video
174 function getMediaType() {
175 $magic = MimeMagic
::singleton();
176 return $magic->getMediaType( null, $this->getMimeType() );
179 function getDescriptionUrl() {
180 return isset( $this->mInfo
['descriptionurl'] )
181 ?
$this->mInfo
['descriptionurl']
186 * Only useful if we're locally caching thumbs anyway...
188 function getThumbPath( $suffix = '' ) {
189 if ( $this->repo
->canCacheThumbs() ) {
190 $path = $this->repo
->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
192 $path = $path . $suffix . '/';
200 function getThumbnails() {
202 $dir = $this->getThumbPath( $this->getName() );
203 if ( is_dir( $dir ) ) {
204 $handle = opendir( $dir );
206 while ( false !== ( $file = readdir($handle) ) ) {
207 if ( $file[0] != '.' ) {
218 * @see File::purgeCache()
220 function purgeCache( $options = array() ) {
221 $this->purgeThumbnails( $options );
222 $this->purgeDescriptionPage();
225 function purgeDescriptionPage() {
226 global $wgMemc, $wgContLang;
227 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
228 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
229 $wgMemc->delete( $key );
232 function purgeThumbnails( $options = array() ) {
234 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
235 $wgMemc->delete( $key );
236 $files = $this->getThumbnails();
237 // Give media handler a chance to filter the purge list
238 $handler = $this->getHandler();
240 $handler->filterThumbnailPurgeList( $files, $options );
243 $dir = $this->getThumbPath( $this->getName() );
244 foreach ( $files as $file ) {
245 unlink( $dir . $file );
247 if ( is_dir( $dir ) ) {
248 rmdir( $dir ); // Might have already gone away, spews errors if we don't.