Never serve 304s from RL in debug mode. This causes inadvertent caching of debug...
[mediawiki.git] / includes / filerepo / ForeignAPIFile.php
blobf78a2a3ed3e0a36a57b87d172ebd95605e090f92
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
5 * @file
6 * @ingroup FileRepo
7 */
9 /**
10 * Foreign file accessible through api.php requests.
11 * Very hacky and inefficient, do not use :D
13 * @ingroup FileRepo
15 class ForeignAPIFile extends File {
17 private $mExists;
19 function __construct( $title, $repo, $info, $exists = false ) {
20 parent::__construct( $title, $repo );
21 $this->mInfo = $info;
22 $this->mExists = $exists;
25 /**
26 * @static
27 * @param $title Title
28 * @param $repo ForeignApiRepo
29 * @return ForeignAPIFile|null
31 static function newFromTitle( $title, $repo ) {
32 $data = $repo->fetchImageQuery( array(
33 'titles' => 'File:' . $title->getDBKey(),
34 'iiprop' => self::getProps(),
35 'prop' => 'imageinfo' ) );
37 $info = $repo->getImageInfo( $data );
39 if( $info ) {
40 $lastRedirect = isset( $data['query']['redirects'] )
41 ? count( $data['query']['redirects'] ) - 1
42 : -1;
43 if( $lastRedirect >= 0 ) {
44 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
45 $img = new ForeignAPIFile( $newtitle, $repo, $info, true );
46 if( $img ) {
47 $img->redirectedFrom( $title->getDBkey() );
49 } else {
50 $img = new ForeignAPIFile( $title, $repo, $info, true );
52 return $img;
53 } else {
54 return null;
58 /**
59 * Get the property string for iiprop and aiprop
61 static function getProps() {
62 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
65 // Dummy functions...
66 public function exists() {
67 return $this->mExists;
70 public function getPath() {
71 return false;
74 function transform( $params, $flags = 0 ) {
75 if( !$this->canRender() ) {
76 // show icon
77 return parent::transform( $params, $flags );
80 $otherParams = "";
81 // Not using implode, since associative array.
82 foreach ( $params as $name => $value ) {
83 if ( $name === 'width' || $name === 'height' ) {
84 continue;
86 $otherParams .= "{$name}={$value}|";
88 // Remove the last |
89 if ( $otherParams !== "" ) {
90 $otherParams = substr( $otherParams, 0, -1 );
93 $thumbUrl = $this->repo->getThumbUrlFromCache(
94 $this->getName(),
95 isset( $params['width'] ) ? $params['width'] : -1,
96 isset( $params['height'] ) ? $params['height'] : -1,
97 $otherParams );
98 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
101 // Info we can get from API...
102 public function getWidth( $page = 1 ) {
103 return intval( @$this->mInfo['width'] );
106 public function getHeight( $page = 1 ) {
107 return intval( @$this->mInfo['height'] );
110 public function getMetadata() {
111 if ( isset( $this->mInfo['metadata'] ) ) {
112 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
114 return null;
117 public static function parseMetadata( $metadata ) {
118 if( !is_array( $metadata ) ) {
119 return $metadata;
121 $ret = array();
122 foreach( $metadata as $meta ) {
123 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
125 return $ret;
128 public function getSize() {
129 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
132 public function getUrl() {
133 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
136 public function getUser( $method='text' ) {
137 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
140 public function getDescription() {
141 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
144 function getSha1() {
145 return isset( $this->mInfo['sha1'] ) ?
146 wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) :
147 null;
150 function getTimestamp() {
151 return wfTimestamp( TS_MW,
152 isset( $this->mInfo['timestamp'] ) ?
153 strval( $this->mInfo['timestamp'] ) :
154 null
158 function getMimeType() {
159 if( !isset( $this->mInfo['mime'] ) ) {
160 $magic = MimeMagic::singleton();
161 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
163 return $this->mInfo['mime'];
166 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
167 function getMediaType() {
168 $magic = MimeMagic::singleton();
169 return $magic->getMediaType( null, $this->getMimeType() );
172 function getDescriptionUrl() {
173 return isset( $this->mInfo['descriptionurl'] )
174 ? $this->mInfo['descriptionurl']
175 : false;
179 * Only useful if we're locally caching thumbs anyway...
181 function getThumbPath( $suffix = '' ) {
182 if ( $this->repo->canCacheThumbs() ) {
183 $path = $this->repo->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
184 if ( $suffix ) {
185 $path = $path . $suffix . '/';
187 return $path;
188 } else {
189 return null;
193 function getThumbnails() {
194 $files = array();
195 $dir = $this->getThumbPath( $this->getName() );
196 if ( is_dir( $dir ) ) {
197 $handle = opendir( $dir );
198 if ( $handle ) {
199 while ( false !== ( $file = readdir($handle) ) ) {
200 if ( $file{0} != '.' ) {
201 $files[] = $file;
204 closedir( $handle );
207 return $files;
210 function purgeCache() {
211 $this->purgeThumbnails();
212 $this->purgeDescriptionPage();
215 function purgeDescriptionPage() {
216 global $wgMemc, $wgContLang;
217 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
218 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
219 $wgMemc->delete( $key );
222 function purgeThumbnails() {
223 global $wgMemc;
224 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
225 $wgMemc->delete( $key );
226 $files = $this->getThumbnails();
227 $dir = $this->getThumbPath( $this->getName() );
228 foreach ( $files as $file ) {
229 unlink( $dir . $file );
231 if ( is_dir( $dir ) ) {
232 rmdir( $dir ); // Might have already gone away, spews errors if we don't.