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;
33 * @param $repo ForeignApiRepo
34 * @return ForeignAPIFile|null
36 static function newFromTitle( $title, $repo ) {
37 $data = $repo->fetchImageQuery( array(
38 'titles' => 'File:' . $title->getDBKey(),
39 'iiprop' => self
::getProps(),
40 'prop' => 'imageinfo',
41 'iimetadataversion' => MediaHandler
::getMetadataVersion()
44 $info = $repo->getImageInfo( $data );
47 $lastRedirect = isset( $data['query']['redirects'] )
48 ?
count( $data['query']['redirects'] ) - 1
50 if( $lastRedirect >= 0 ) {
51 $newtitle = Title
::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
52 $img = new ForeignAPIFile( $newtitle, $repo, $info, true );
54 $img->redirectedFrom( $title->getDBkey() );
57 $img = new ForeignAPIFile( $title, $repo, $info, true );
66 * Get the property string for iiprop and aiprop
68 static function getProps() {
69 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
73 public function exists() {
74 return $this->mExists
;
77 public function getPath() {
81 function transform( $params, $flags = 0 ) {
82 if( !$this->canRender() ) {
84 return parent
::transform( $params, $flags );
87 // Note, the this->canRender() check above implies
88 // that we have a handler, and it can do makeParamString.
89 $otherParams = $this->handler
->makeParamString( $params );
91 $thumbUrl = $this->repo
->getThumbUrlFromCache(
93 isset( $params['width'] ) ?
$params['width'] : -1,
94 isset( $params['height'] ) ?
$params['height'] : -1,
96 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
99 // Info we can get from API...
100 public function getWidth( $page = 1 ) {
101 return isset( $this->mInfo
['width'] ) ?
intval( $this->mInfo
['width'] ) : 0;
104 public function getHeight( $page = 1 ) {
105 return isset( $this->mInfo
['height'] ) ?
intval( $this->mInfo
['height'] ) : 0;
108 public function getMetadata() {
109 if ( isset( $this->mInfo
['metadata'] ) ) {
110 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
115 public static function parseMetadata( $metadata ) {
116 if( !is_array( $metadata ) ) {
120 foreach( $metadata as $meta ) {
121 $ret[ $meta['name'] ] = self
::parseMetadata( $meta['value'] );
126 public function getSize() {
127 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
130 public function getUrl() {
131 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
134 public function getUser( $method='text' ) {
135 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
138 public function getDescription() {
139 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
143 return isset( $this->mInfo
['sha1'] ) ?
144 wfBaseConvert( strval( $this->mInfo
['sha1'] ), 16, 36, 31 ) :
148 function getTimestamp() {
149 return wfTimestamp( TS_MW
,
150 isset( $this->mInfo
['timestamp'] ) ?
151 strval( $this->mInfo
['timestamp'] ) :
156 function getMimeType() {
157 if( !isset( $this->mInfo
['mime'] ) ) {
158 $magic = MimeMagic
::singleton();
159 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
161 return $this->mInfo
['mime'];
164 /// @todo FIXME: May guess wrong on file types that can be eg audio or video
165 function getMediaType() {
166 $magic = MimeMagic
::singleton();
167 return $magic->getMediaType( null, $this->getMimeType() );
170 function getDescriptionUrl() {
171 return isset( $this->mInfo
['descriptionurl'] )
172 ?
$this->mInfo
['descriptionurl']
177 * Only useful if we're locally caching thumbs anyway...
179 function getThumbPath( $suffix = '' ) {
180 if ( $this->repo
->canCacheThumbs() ) {
181 $path = $this->repo
->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
183 $path = $path . $suffix . '/';
191 function getThumbnails() {
193 $dir = $this->getThumbPath( $this->getName() );
194 if ( is_dir( $dir ) ) {
195 $handle = opendir( $dir );
197 while ( false !== ( $file = readdir($handle) ) ) {
198 if ( $file[0] != '.' ) {
208 function purgeCache() {
209 $this->purgeThumbnails();
210 $this->purgeDescriptionPage();
213 function purgeDescriptionPage() {
214 global $wgMemc, $wgContLang;
215 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
216 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
217 $wgMemc->delete( $key );
220 function purgeThumbnails() {
222 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
223 $wgMemc->delete( $key );
224 $files = $this->getThumbnails();
225 $dir = $this->getThumbPath( $this->getName() );
226 foreach ( $files as $file ) {
227 unlink( $dir . $file );
229 if ( is_dir( $dir ) ) {
230 rmdir( $dir ); // Might have already gone away, spews errors if we don't.