Add "extended" file metadata to API
[lhc/web/wiklou.git] / includes / filerepo / file / ForeignAPIFile.php
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
24 /**
25 * Foreign file accessible through api.php requests.
26 * Very hacky and inefficient, do not use :D
27 *
28 * @ingroup FileAbstraction
29 */
30 class ForeignAPIFile extends File {
31 private $mExists;
32
33 protected $repoClass = 'ForeignApiRepo';
34
35 /**
36 * @param $title
37 * @param $repo ForeignApiRepo
38 * @param $info
39 * @param bool $exists
40 */
41 function __construct( $title, $repo, $info, $exists = false ) {
42 parent::__construct( $title, $repo );
43
44 $this->mInfo = $info;
45 $this->mExists = $exists;
46
47 $this->assertRepoDefined();
48 }
49
50 /**
51 * @param $title Title
52 * @param $repo ForeignApiRepo
53 * @return ForeignAPIFile|null
54 */
55 static function newFromTitle( Title $title, $repo ) {
56 $data = $repo->fetchImageQuery( array(
57 'titles' => 'File:' . $title->getDBkey(),
58 'iiprop' => self::getProps(),
59 'prop' => 'imageinfo',
60 'iimetadataversion' => MediaHandler::getMetadataVersion()
61 ) );
62
63 $info = $repo->getImageInfo( $data );
64
65 if ( $info ) {
66 $lastRedirect = isset( $data['query']['redirects'] )
67 ? count( $data['query']['redirects'] ) - 1
68 : -1;
69 if ( $lastRedirect >= 0 ) {
70 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
71 $img = new self( $newtitle, $repo, $info, true );
72 if ( $img ) {
73 $img->redirectedFrom( $title->getDBkey() );
74 }
75 } else {
76 $img = new self( $title, $repo, $info, true );
77 }
78 return $img;
79 } else {
80 return null;
81 }
82 }
83
84 /**
85 * Get the property string for iiprop and aiprop
86 * @return string
87 */
88 static function getProps() {
89 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype';
90 }
91
92 // Dummy functions...
93
94 /**
95 * @return bool
96 */
97 public function exists() {
98 return $this->mExists;
99 }
100
101 /**
102 * @return bool
103 */
104 public function getPath() {
105 return false;
106 }
107
108 /**
109 * @param array $params
110 * @param int $flags
111 * @return bool|MediaTransformOutput
112 */
113 function transform( $params, $flags = 0 ) {
114 if ( !$this->canRender() ) {
115 // show icon
116 return parent::transform( $params, $flags );
117 }
118
119 // Note, the this->canRender() check above implies
120 // that we have a handler, and it can do makeParamString.
121 $otherParams = $this->handler->makeParamString( $params );
122 $width = isset( $params['width'] ) ? $params['width'] : -1;
123 $height = isset( $params['height'] ) ? $params['height'] : -1;
124
125 $thumbUrl = $this->repo->getThumbUrlFromCache(
126 $this->getName(),
127 $width,
128 $height,
129 $otherParams
130 );
131 if ( $thumbUrl === false ) {
132 global $wgLang;
133 return $this->repo->getThumbError(
134 $this->getName(),
135 $width,
136 $height,
137 $otherParams,
138 $wgLang->getCode()
139 );
140 }
141 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
142 }
143
144 // Info we can get from API...
145
146 /**
147 * @param $page int
148 * @return int|number
149 */
150 public function getWidth( $page = 1 ) {
151 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
152 }
153
154 /**
155 * @param $page int
156 * @return int
157 */
158 public function getHeight( $page = 1 ) {
159 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
160 }
161
162 /**
163 * @return bool|null|string
164 */
165 public function getMetadata() {
166 if ( isset( $this->mInfo['metadata'] ) ) {
167 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
168 }
169 return null;
170 }
171
172 /**
173 * @return array|null extended metadata (see imageinfo API for format) or null on error
174 */
175 public function getExtendedMetadata() {
176 if ( isset( $this->mInfo['extmetadata'] ) ) {
177 return $this->mInfo['extmetadata'];
178 }
179 return null;
180 }
181
182 /**
183 * @param $metadata array
184 * @return array
185 */
186 public static function parseMetadata( $metadata ) {
187 if ( !is_array( $metadata ) ) {
188 return $metadata;
189 }
190 $ret = array();
191 foreach ( $metadata as $meta ) {
192 $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
193 }
194 return $ret;
195 }
196
197 /**
198 * @return bool|int|null
199 */
200 public function getSize() {
201 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
202 }
203
204 /**
205 * @return null|string
206 */
207 public function getUrl() {
208 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
209 }
210
211 /**
212 * @param string $method
213 * @return int|null|string
214 */
215 public function getUser( $method = 'text' ) {
216 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
217 }
218
219 /**
220 * @return null|string
221 */
222 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
223 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
224 }
225
226 /**
227 * @return null|String
228 */
229 function getSha1() {
230 return isset( $this->mInfo['sha1'] )
231 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
232 : null;
233 }
234
235 /**
236 * @return bool|Mixed|string
237 */
238 function getTimestamp() {
239 return wfTimestamp( TS_MW,
240 isset( $this->mInfo['timestamp'] )
241 ? strval( $this->mInfo['timestamp'] )
242 : null
243 );
244 }
245
246 /**
247 * @return string
248 */
249 function getMimeType() {
250 if ( !isset( $this->mInfo['mime'] ) ) {
251 $magic = MimeMagic::singleton();
252 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
253 }
254 return $this->mInfo['mime'];
255 }
256
257 /**
258 * @return int|string
259 */
260 function getMediaType() {
261 if ( isset( $this->mInfo['mediatype'] ) ) {
262 return $this->mInfo['mediatype'];
263 }
264 $magic = MimeMagic::singleton();
265 return $magic->getMediaType( null, $this->getMimeType() );
266 }
267
268 /**
269 * @return bool|string
270 */
271 function getDescriptionUrl() {
272 return isset( $this->mInfo['descriptionurl'] )
273 ? $this->mInfo['descriptionurl']
274 : false;
275 }
276
277 /**
278 * Only useful if we're locally caching thumbs anyway...
279 * @param $suffix string
280 * @return null|string
281 */
282 function getThumbPath( $suffix = '' ) {
283 if ( $this->repo->canCacheThumbs() ) {
284 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
285 if ( $suffix ) {
286 $path = $path . $suffix . '/';
287 }
288 return $path;
289 } else {
290 return null;
291 }
292 }
293
294 /**
295 * @return array
296 */
297 function getThumbnails() {
298 $dir = $this->getThumbPath( $this->getName() );
299 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
300
301 $files = array();
302 foreach ( $iter as $file ) {
303 $files[] = $file;
304 }
305
306 return $files;
307 }
308
309 /**
310 * @see File::purgeCache()
311 */
312 function purgeCache( $options = array() ) {
313 $this->purgeThumbnails( $options );
314 $this->purgeDescriptionPage();
315 }
316
317 function purgeDescriptionPage() {
318 global $wgMemc, $wgContLang;
319
320 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
321 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
322
323 $wgMemc->delete( $key );
324 }
325
326 /**
327 * @param $options array
328 */
329 function purgeThumbnails( $options = array() ) {
330 global $wgMemc;
331
332 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
333 $wgMemc->delete( $key );
334
335 $files = $this->getThumbnails();
336 // Give media handler a chance to filter the purge list
337 $handler = $this->getHandler();
338 if ( $handler ) {
339 $handler->filterThumbnailPurgeList( $files, $options );
340 }
341
342 $dir = $this->getThumbPath( $this->getName() );
343 $purgeList = array();
344 foreach ( $files as $file ) {
345 $purgeList[] = "{$dir}{$file}";
346 }
347
348 # Delete the thumbnails
349 $this->repo->quickPurgeBatch( $purgeList );
350 # Clear out the thumbnail directory if empty
351 $this->repo->quickCleanDir( $dir );
352 }
353 }