* r109659: actually return the exact type we say we do
[lhc/web/wiklou.git] / includes / filerepo / file / 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 private $mExists;
17
18 protected $repoClass = 'ForeignApiRepo';
19
20 /**
21 * @param $title
22 * @param $repo ForeignApiRepo
23 * @param $info
24 * @param bool $exists
25 */
26 function __construct( $title, $repo, $info, $exists = false ) {
27 parent::__construct( $title, $repo );
28
29 $this->mInfo = $info;
30 $this->mExists = $exists;
31
32 $this->assertRepoDefined();
33 }
34
35 /**
36 * @param $title Title
37 * @param $repo ForeignApiRepo
38 * @return ForeignAPIFile|null
39 */
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 ) );
47
48 $info = $repo->getImageInfo( $data );
49
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() );
59 }
60 } else {
61 $img = new self( $title, $repo, $info, true );
62 }
63 return $img;
64 } else {
65 return null;
66 }
67 }
68
69 /**
70 * Get the property string for iiprop and aiprop
71 */
72 static function getProps() {
73 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
74 }
75
76 // Dummy functions...
77 public function exists() {
78 return $this->mExists;
79 }
80
81 public function getPath() {
82 return false;
83 }
84
85 function transform( $params, $flags = 0 ) {
86 if( !$this->canRender() ) {
87 // show icon
88 return parent::transform( $params, $flags );
89 }
90
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 );
94
95 $thumbUrl = $this->repo->getThumbUrlFromCache(
96 $this->getName(),
97 isset( $params['width'] ) ? $params['width'] : -1,
98 isset( $params['height'] ) ? $params['height'] : -1,
99 $otherParams );
100 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
101 }
102
103 // Info we can get from API...
104 public function getWidth( $page = 1 ) {
105 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
106 }
107
108 /**
109 * @param $page int
110 * @return int
111 */
112 public function getHeight( $page = 1 ) {
113 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
114 }
115
116 public function getMetadata() {
117 if ( isset( $this->mInfo['metadata'] ) ) {
118 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
119 }
120 return null;
121 }
122
123 public static function parseMetadata( $metadata ) {
124 if( !is_array( $metadata ) ) {
125 return $metadata;
126 }
127 $ret = array();
128 foreach( $metadata as $meta ) {
129 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
130 }
131 return $ret;
132 }
133
134 public function getSize() {
135 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
136 }
137
138 public function getUrl() {
139 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
140 }
141
142 public function getUser( $method='text' ) {
143 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
144 }
145
146 public function getDescription() {
147 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
148 }
149
150 function getSha1() {
151 return isset( $this->mInfo['sha1'] )
152 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
153 : null;
154 }
155
156 function getTimestamp() {
157 return wfTimestamp( TS_MW,
158 isset( $this->mInfo['timestamp'] )
159 ? strval( $this->mInfo['timestamp'] )
160 : null
161 );
162 }
163
164 function getMimeType() {
165 if( !isset( $this->mInfo['mime'] ) ) {
166 $magic = MimeMagic::singleton();
167 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
168 }
169 return $this->mInfo['mime'];
170 }
171
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() );
176 }
177
178 function getDescriptionUrl() {
179 return isset( $this->mInfo['descriptionurl'] )
180 ? $this->mInfo['descriptionurl']
181 : false;
182 }
183
184 /**
185 * Only useful if we're locally caching thumbs anyway...
186 */
187 function getThumbPath( $suffix = '' ) {
188 if ( $this->repo->canCacheThumbs() ) {
189 $path = $this->repo->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
190 if ( $suffix ) {
191 $path = $path . $suffix . '/';
192 }
193 return $path;
194 } else {
195 return null;
196 }
197 }
198
199 function getThumbnails() {
200 $dir = $this->getThumbPath( $this->getName() );
201 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
202
203 $files = array();
204 foreach ( $iter as $file ) {
205 $files[] = $file;
206 }
207
208 return $files;
209 }
210
211 /**
212 * @see File::purgeCache()
213 */
214 function purgeCache( $options = array() ) {
215 $this->purgeThumbnails( $options );
216 $this->purgeDescriptionPage();
217 }
218
219 function purgeDescriptionPage() {
220 global $wgMemc, $wgContLang;
221
222 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
223 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
224
225 $wgMemc->delete( $key );
226 }
227
228 function purgeThumbnails( $options = array() ) {
229 global $wgMemc;
230
231 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
232 $wgMemc->delete( $key );
233
234 $files = $this->getThumbnails();
235 // Give media handler a chance to filter the purge list
236 $handler = $this->getHandler();
237 if ( $handler ) {
238 $handler->filterThumbnailPurgeList( $files, $options );
239 }
240
241 $dir = $this->getThumbPath( $this->getName() );
242 $purgeList = array();
243 foreach ( $files as $file ) {
244 $purgeList[] = "{$dir}{$file}";
245 }
246
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 ) );
251 }
252 }