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' ) );
43 $info = $repo->getImageInfo( $data );
46 $lastRedirect = isset( $data['query']['redirects'] )
47 ?
count( $data['query']['redirects'] ) - 1
49 if( $lastRedirect >= 0 ) {
50 $newtitle = Title
::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
51 $img = new ForeignAPIFile( $newtitle, $repo, $info, true );
53 $img->redirectedFrom( $title->getDBkey() );
56 $img = new ForeignAPIFile( $title, $repo, $info, true );
65 * Get the property string for iiprop and aiprop
67 static function getProps() {
68 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
72 public function exists() {
73 return $this->mExists
;
76 public function getPath() {
80 function transform( $params, $flags = 0 ) {
81 if( !$this->canRender() ) {
83 return parent
::transform( $params, $flags );
86 // Note, the this->canRender() check above implies
87 // that we have a handler, and it can do makeParamString.
88 $otherParams = $this->handler
->makeParamString( $params );
90 $thumbUrl = $this->repo
->getThumbUrlFromCache(
92 isset( $params['width'] ) ?
$params['width'] : -1,
93 isset( $params['height'] ) ?
$params['height'] : -1,
95 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
98 // Info we can get from API...
99 public function getWidth( $page = 1 ) {
100 return intval( @$this->mInfo
['width'] );
103 public function getHeight( $page = 1 ) {
104 return intval( @$this->mInfo
['height'] );
107 public function getMetadata() {
108 if ( isset( $this->mInfo
['metadata'] ) ) {
109 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
114 public static function parseMetadata( $metadata ) {
115 if( !is_array( $metadata ) ) {
119 foreach( $metadata as $meta ) {
120 $ret[ $meta['name'] ] = self
::parseMetadata( $meta['value'] );
125 public function getSize() {
126 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
129 public function getUrl() {
130 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
133 public function getUser( $method='text' ) {
134 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
137 public function getDescription() {
138 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
142 return isset( $this->mInfo
['sha1'] ) ?
143 wfBaseConvert( strval( $this->mInfo
['sha1'] ), 16, 36, 31 ) :
147 function getTimestamp() {
148 return wfTimestamp( TS_MW
,
149 isset( $this->mInfo
['timestamp'] ) ?
150 strval( $this->mInfo
['timestamp'] ) :
155 function getMimeType() {
156 if( !isset( $this->mInfo
['mime'] ) ) {
157 $magic = MimeMagic
::singleton();
158 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
160 return $this->mInfo
['mime'];
163 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
164 function getMediaType() {
165 $magic = MimeMagic
::singleton();
166 return $magic->getMediaType( null, $this->getMimeType() );
169 function getDescriptionUrl() {
170 return isset( $this->mInfo
['descriptionurl'] )
171 ?
$this->mInfo
['descriptionurl']
176 * Only useful if we're locally caching thumbs anyway...
178 function getThumbPath( $suffix = '' ) {
179 if ( $this->repo
->canCacheThumbs() ) {
180 $path = $this->repo
->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
182 $path = $path . $suffix . '/';
190 function getThumbnails() {
192 $dir = $this->getThumbPath( $this->getName() );
193 if ( is_dir( $dir ) ) {
194 $handle = opendir( $dir );
196 while ( false !== ( $file = readdir($handle) ) ) {
197 if ( $file{0} != '.' ) {
207 function purgeCache() {
208 $this->purgeThumbnails();
209 $this->purgeDescriptionPage();
212 function purgeDescriptionPage() {
213 global $wgMemc, $wgContLang;
214 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
215 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
216 $wgMemc->delete( $key );
219 function purgeThumbnails() {
221 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
222 $wgMemc->delete( $key );
223 $files = $this->getThumbnails();
224 $dir = $this->getThumbPath( $this->getName() );
225 foreach ( $files as $file ) {
226 unlink( $dir . $file );
228 if ( is_dir( $dir ) ) {
229 rmdir( $dir ); // Might have already gone away, spews errors if we don't.