[FileRepo] Avoid performance degeration due to thumbnail requests with a read-only...
[mediawiki.git] / includes / filerepo / file / ForeignAPIFile.php
blob9ae501a43df7f22c6b56d935a3ee087c556f1ae4
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
5 * @file
6 * @ingroup FileAbstraction
7 */
9 /**
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 {
16 private $mExists;
18 protected $repoClass = 'ForeignApiRepo';
20 /**
21 * @param $title
22 * @param $repo ForeignApiRepo
23 * @param $info
24 * @param bool $exists
26 function __construct( $title, $repo, $info, $exists = false ) {
27 parent::__construct( $title, $repo );
29 $this->mInfo = $info;
30 $this->mExists = $exists;
32 $this->assertRepoDefined();
35 /**
36 * @param $title Title
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()
46 ) );
48 $info = $repo->getImageInfo( $data );
50 if( $info ) {
51 $lastRedirect = isset( $data['query']['redirects'] )
52 ? count( $data['query']['redirects'] ) - 1
53 : -1;
54 if( $lastRedirect >= 0 ) {
55 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
56 $img = new self( $newtitle, $repo, $info, true );
57 if( $img ) {
58 $img->redirectedFrom( $title->getDBkey() );
60 } else {
61 $img = new self( $title, $repo, $info, true );
63 return $img;
64 } else {
65 return null;
69 /**
70 * Get the property string for iiprop and aiprop
71 * @return string
73 static function getProps() {
74 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
77 // Dummy functions...
78 public function exists() {
79 return $this->mExists;
82 public function getPath() {
83 return false;
86 function transform( $params, $flags = 0 ) {
87 if( !$this->canRender() ) {
88 // show icon
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(
97 $this->getName(),
98 isset( $params['width'] ) ? $params['width'] : -1,
99 isset( $params['height'] ) ? $params['height'] : -1,
100 $otherParams );
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;
110 * @param $page int
111 * @return int
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'] ) );
121 return null;
124 public static function parseMetadata( $metadata ) {
125 if( !is_array( $metadata ) ) {
126 return $metadata;
128 $ret = array();
129 foreach( $metadata as $meta ) {
130 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
132 return $ret;
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;
151 function getSha1() {
152 return isset( $this->mInfo['sha1'] )
153 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
154 : null;
157 function getTimestamp() {
158 return wfTimestamp( TS_MW,
159 isset( $this->mInfo['timestamp'] )
160 ? strval( $this->mInfo['timestamp'] )
161 : null
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']
182 : false;
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() );
192 if ( $suffix ) {
193 $path = $path . $suffix . '/';
195 return $path;
196 } else {
197 return null;
201 function getThumbnails() {
202 $dir = $this->getThumbPath( $this->getName() );
203 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
205 $files = array();
206 foreach ( $iter as $file ) {
207 $files[] = $file;
210 return $files;
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() ) {
231 global $wgMemc;
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();
239 if ( $handler ) {
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 );