3 * Foreign file accessible through api.php requests.
6 * @ingroup FileAbstraction
10 * Foreign file accessible through api.php requests.
11 * Very hacky and inefficient, do not use :D
13 * @ingroup FileAbstraction
15 class ForeignAPIFile
extends File
{
18 protected $repoClass = 'ForeignApiRepo';
22 * @param $repo ForeignApiRepo
26 function __construct( $title, $repo, $info, $exists = false ) {
27 parent
::__construct( $title, $repo );
30 $this->mExists
= $exists;
32 $this->assertRepoDefined();
37 * @param $repo ForeignApiRepo
38 * @return ForeignAPIFile|null
40 static function newFromTitle( Title
$title, $repo ) {
41 $data = $repo->fetchImageQuery( array(
42 'titles' => 'File:' . $title->getDBKey(),
43 'iiprop' => self
::getProps(),
44 'prop' => 'imageinfo',
45 'iimetadataversion' => MediaHandler
::getMetadataVersion()
48 $info = $repo->getImageInfo( $data );
51 $lastRedirect = isset( $data['query']['redirects'] )
52 ?
count( $data['query']['redirects'] ) - 1
54 if( $lastRedirect >= 0 ) {
55 $newtitle = Title
::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
56 $img = new self( $newtitle, $repo, $info, true );
58 $img->redirectedFrom( $title->getDBkey() );
61 $img = new self( $title, $repo, $info, true );
70 * 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...
187 * @return null|string
189 function getThumbPath( $suffix = '' ) {
190 if ( $this->repo
->canCacheThumbs() ) {
191 $path = $this->repo
->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
193 $path = $path . $suffix . '/';
201 function getThumbnails() {
202 $dir = $this->getThumbPath( $this->getName() );
203 $iter = $this->repo
->getBackend()->getFileList( array( 'dir' => $dir ) );
206 foreach ( $iter as $file ) {
214 * @see File::purgeCache()
216 function purgeCache( $options = array() ) {
217 $this->purgeThumbnails( $options );
218 $this->purgeDescriptionPage();
221 function purgeDescriptionPage() {
222 global $wgMemc, $wgContLang;
224 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
225 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
227 $wgMemc->delete( $key );
230 function purgeThumbnails( $options = array() ) {
233 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
234 $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 $purgeList = array();
245 foreach ( $files as $file ) {
246 $purgeList[] = "{$dir}{$file}";
249 # Delete the thumbnails
250 $this->repo
->quickPurgeBatch( $purgeList );
251 # Clear out the thumbnail directory if empty
252 $this->repo
->cleanDir( $dir );