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
{
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
72 static function getProps() {
73 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
77 public function exists() {
78 return $this->mExists
;
81 public function getPath() {
85 function transform( $params, $flags = 0 ) {
86 if( !$this->canRender() ) {
88 return parent
::transform( $params, $flags );
91 // Note, the this->canRender() check above implies
92 // that we have a handler, and it can do makeParamString.
93 $otherParams = $this->handler
->makeParamString( $params );
95 $thumbUrl = $this->repo
->getThumbUrlFromCache(
97 isset( $params['width'] ) ?
$params['width'] : -1,
98 isset( $params['height'] ) ?
$params['height'] : -1,
100 return $this->handler
->getTransform( $this, 'bogus', $thumbUrl, $params );
103 // Info we can get from API...
104 public function getWidth( $page = 1 ) {
105 return isset( $this->mInfo
['width'] ) ?
intval( $this->mInfo
['width'] ) : 0;
112 public function getHeight( $page = 1 ) {
113 return isset( $this->mInfo
['height'] ) ?
intval( $this->mInfo
['height'] ) : 0;
116 public function getMetadata() {
117 if ( isset( $this->mInfo
['metadata'] ) ) {
118 return serialize( self
::parseMetadata( $this->mInfo
['metadata'] ) );
123 public static function parseMetadata( $metadata ) {
124 if( !is_array( $metadata ) ) {
128 foreach( $metadata as $meta ) {
129 $ret[ $meta['name'] ] = self
::parseMetadata( $meta['value'] );
134 public function getSize() {
135 return isset( $this->mInfo
['size'] ) ?
intval( $this->mInfo
['size'] ) : null;
138 public function getUrl() {
139 return isset( $this->mInfo
['url'] ) ?
strval( $this->mInfo
['url'] ) : null;
142 public function getUser( $method='text' ) {
143 return isset( $this->mInfo
['user'] ) ?
strval( $this->mInfo
['user'] ) : null;
146 public function getDescription() {
147 return isset( $this->mInfo
['comment'] ) ?
strval( $this->mInfo
['comment'] ) : null;
151 return isset( $this->mInfo
['sha1'] )
152 ?
wfBaseConvert( strval( $this->mInfo
['sha1'] ), 16, 36, 31 )
156 function getTimestamp() {
157 return wfTimestamp( TS_MW
,
158 isset( $this->mInfo
['timestamp'] )
159 ?
strval( $this->mInfo
['timestamp'] )
164 function getMimeType() {
165 if( !isset( $this->mInfo
['mime'] ) ) {
166 $magic = MimeMagic
::singleton();
167 $this->mInfo
['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
169 return $this->mInfo
['mime'];
172 /// @todo FIXME: May guess wrong on file types that can be eg audio or video
173 function getMediaType() {
174 $magic = MimeMagic
::singleton();
175 return $magic->getMediaType( null, $this->getMimeType() );
178 function getDescriptionUrl() {
179 return isset( $this->mInfo
['descriptionurl'] )
180 ?
$this->mInfo
['descriptionurl']
185 * Only useful if we're locally caching thumbs anyway...
187 function getThumbPath( $suffix = '' ) {
188 if ( $this->repo
->canCacheThumbs() ) {
189 $path = $this->repo
->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
191 $path = $path . $suffix . '/';
199 function getThumbnails() {
200 $dir = $this->getThumbPath( $this->getName() );
201 $iter = $this->repo
->getBackend()->getFileList( array( 'dir' => $dir ) );
204 foreach ( $iter as $file ) {
212 * @see File::purgeCache()
214 function purgeCache( $options = array() ) {
215 $this->purgeThumbnails( $options );
216 $this->purgeDescriptionPage();
219 function purgeDescriptionPage() {
220 global $wgMemc, $wgContLang;
222 $url = $this->repo
->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
223 $key = $this->repo
->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
225 $wgMemc->delete( $key );
228 function purgeThumbnails( $options = array() ) {
231 $key = $this->repo
->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
232 $wgMemc->delete( $key );
234 $files = $this->getThumbnails();
235 // Give media handler a chance to filter the purge list
236 $handler = $this->getHandler();
238 $handler->filterThumbnailPurgeList( $files, $options );
241 $dir = $this->getThumbPath( $this->getName() );
242 $purgeList = array();
243 foreach ( $files as $file ) {
244 $purgeList[] = "{$dir}{$file}";
247 # Delete the thumbnails
248 $this->repo
->cleanupBatch( $purgeList );
249 # Clear out the thumbnail directory if empty
250 $this->repo
->getBackend()->clean( array( 'dir' => $dir ) );