(follow-up r81558) Per suggestion, make this use media handler's getParamString/parse...
[lhc/web/wiklou.git] / includes / filerepo / ForeignAPIFile.php
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
4 *
5 * @file
6 * @ingroup FileRepo
7 */
8
9 /**
10 * Foreign file accessible through api.php requests.
11 * Very hacky and inefficient, do not use :D
12 *
13 * @ingroup FileRepo
14 */
15 class ForeignAPIFile extends File {
16
17 private $mExists;
18
19 function __construct( $title, $repo, $info, $exists = false ) {
20 parent::__construct( $title, $repo );
21 $this->mInfo = $info;
22 $this->mExists = $exists;
23 }
24
25 /**
26 * @static
27 * @param $title Title
28 * @param $repo ForeignApiRepo
29 * @return ForeignAPIFile|null
30 */
31 static function newFromTitle( $title, $repo ) {
32 $data = $repo->fetchImageQuery( array(
33 'titles' => 'File:' . $title->getDBKey(),
34 'iiprop' => self::getProps(),
35 'prop' => 'imageinfo' ) );
36
37 $info = $repo->getImageInfo( $data );
38
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() );
48 }
49 } else {
50 $img = new ForeignAPIFile( $title, $repo, $info, true );
51 }
52 return $img;
53 } else {
54 return null;
55 }
56 }
57
58 /**
59 * Get the property string for iiprop and aiprop
60 */
61 static function getProps() {
62 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
63 }
64
65 // Dummy functions...
66 public function exists() {
67 return $this->mExists;
68 }
69
70 public function getPath() {
71 return false;
72 }
73
74 function transform( $params, $flags = 0 ) {
75 if( !$this->canRender() ) {
76 // show icon
77 return parent::transform( $params, $flags );
78 }
79
80 // Note, the this->canRender() check above implies
81 // that we have a handler, and it can do makeParamString.
82 $otherParams = $this->handler->makeParamString( $params );
83
84 $thumbUrl = $this->repo->getThumbUrlFromCache(
85 $this->getName(),
86 isset( $params['width'] ) ? $params['width'] : -1,
87 isset( $params['height'] ) ? $params['height'] : -1,
88 $otherParams );
89 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
90 }
91
92 // Info we can get from API...
93 public function getWidth( $page = 1 ) {
94 return intval( @$this->mInfo['width'] );
95 }
96
97 public function getHeight( $page = 1 ) {
98 return intval( @$this->mInfo['height'] );
99 }
100
101 public function getMetadata() {
102 if ( isset( $this->mInfo['metadata'] ) ) {
103 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
104 }
105 return null;
106 }
107
108 public static function parseMetadata( $metadata ) {
109 if( !is_array( $metadata ) ) {
110 return $metadata;
111 }
112 $ret = array();
113 foreach( $metadata as $meta ) {
114 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
115 }
116 return $ret;
117 }
118
119 public function getSize() {
120 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
121 }
122
123 public function getUrl() {
124 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
125 }
126
127 public function getUser( $method='text' ) {
128 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
129 }
130
131 public function getDescription() {
132 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
133 }
134
135 function getSha1() {
136 return isset( $this->mInfo['sha1'] ) ?
137 wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) :
138 null;
139 }
140
141 function getTimestamp() {
142 return wfTimestamp( TS_MW,
143 isset( $this->mInfo['timestamp'] ) ?
144 strval( $this->mInfo['timestamp'] ) :
145 null
146 );
147 }
148
149 function getMimeType() {
150 if( !isset( $this->mInfo['mime'] ) ) {
151 $magic = MimeMagic::singleton();
152 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
153 }
154 return $this->mInfo['mime'];
155 }
156
157 /// @todo Fixme: may guess wrong on file types that can be eg audio or video
158 function getMediaType() {
159 $magic = MimeMagic::singleton();
160 return $magic->getMediaType( null, $this->getMimeType() );
161 }
162
163 function getDescriptionUrl() {
164 return isset( $this->mInfo['descriptionurl'] )
165 ? $this->mInfo['descriptionurl']
166 : false;
167 }
168
169 /**
170 * Only useful if we're locally caching thumbs anyway...
171 */
172 function getThumbPath( $suffix = '' ) {
173 if ( $this->repo->canCacheThumbs() ) {
174 $path = $this->repo->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
175 if ( $suffix ) {
176 $path = $path . $suffix . '/';
177 }
178 return $path;
179 } else {
180 return null;
181 }
182 }
183
184 function getThumbnails() {
185 $files = array();
186 $dir = $this->getThumbPath( $this->getName() );
187 if ( is_dir( $dir ) ) {
188 $handle = opendir( $dir );
189 if ( $handle ) {
190 while ( false !== ( $file = readdir($handle) ) ) {
191 if ( $file{0} != '.' ) {
192 $files[] = $file;
193 }
194 }
195 closedir( $handle );
196 }
197 }
198 return $files;
199 }
200
201 function purgeCache() {
202 $this->purgeThumbnails();
203 $this->purgeDescriptionPage();
204 }
205
206 function purgeDescriptionPage() {
207 global $wgMemc, $wgContLang;
208 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
209 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
210 $wgMemc->delete( $key );
211 }
212
213 function purgeThumbnails() {
214 global $wgMemc;
215 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
216 $wgMemc->delete( $key );
217 $files = $this->getThumbnails();
218 $dir = $this->getThumbPath( $this->getName() );
219 foreach ( $files as $file ) {
220 unlink( $dir . $file );
221 }
222 if ( is_dir( $dir ) ) {
223 rmdir( $dir ); // Might have already gone away, spews errors if we don't.
224 }
225 }
226 }