Localisation updates for core from Betawiki
[lhc/web/wiklou.git] / includes / media / Generic.php
1 <?php
2 /**
3 * Media-handling base classes and generic functionality
4 * @file
5 * @ingroup Media
6 */
7
8 /**
9 * Base media handler class
10 *
11 * @ingroup Media
12 */
13 abstract class MediaHandler {
14 const TRANSFORM_LATER = 1;
15
16 /**
17 * Instance cache
18 */
19 static $handlers = array();
20
21 /**
22 * Get a MediaHandler for a given MIME type from the instance cache
23 */
24 static function getHandler( $type ) {
25 global $wgMediaHandlers;
26 if ( !isset( $wgMediaHandlers[$type] ) ) {
27 wfDebug( __METHOD__ . ": no handler found for $type.\n");
28 return false;
29 }
30 $class = $wgMediaHandlers[$type];
31 if ( !isset( self::$handlers[$class] ) ) {
32 self::$handlers[$class] = new $class;
33 if ( !self::$handlers[$class]->isEnabled() ) {
34 self::$handlers[$class] = false;
35 }
36 }
37 return self::$handlers[$class];
38 }
39
40 /**
41 * Get an associative array mapping magic word IDs to parameter names.
42 * Will be used by the parser to identify parameters.
43 */
44 abstract function getParamMap();
45
46 /*
47 * Validate a thumbnail parameter at parse time.
48 * Return true to accept the parameter, and false to reject it.
49 * If you return false, the parser will do something quiet and forgiving.
50 */
51 abstract function validateParam( $name, $value );
52
53 /**
54 * Merge a parameter array into a string appropriate for inclusion in filenames
55 */
56 abstract function makeParamString( $params );
57
58 /**
59 * Parse a param string made with makeParamString back into an array
60 */
61 abstract function parseParamString( $str );
62
63 /**
64 * Changes the parameter array as necessary, ready for transformation.
65 * Should be idempotent.
66 * Returns false if the parameters are unacceptable and the transform should fail
67 */
68 abstract function normaliseParams( $image, &$params );
69
70 /**
71 * Get an image size array like that returned by getimagesize(), or false if it
72 * can't be determined.
73 *
74 * @param Image $image The image object, or false if there isn't one
75 * @param string $fileName The filename
76 * @return array
77 */
78 abstract function getImageSize( $image, $path );
79
80 /**
81 * Get handler-specific metadata which will be saved in the img_metadata field.
82 *
83 * @param Image $image The image object, or false if there isn't one
84 * @param string $fileName The filename
85 * @return string
86 */
87 function getMetadata( $image, $path ) { return ''; }
88
89 /**
90 * Get a string describing the type of metadata, for display purposes.
91 */
92 function getMetadataType( $image ) { return false; }
93
94 /**
95 * Check if the metadata string is valid for this handler.
96 * If it returns false, Image will reload the metadata from the file and update the database
97 */
98 function isMetadataValid( $image, $metadata ) { return true; }
99
100
101 /**
102 * Get a MediaTransformOutput object representing an alternate of the transformed
103 * output which will call an intermediary thumbnail assist script.
104 *
105 * Used when the repository has a thumbnailScriptUrl option configured.
106 *
107 * Return false to fall back to the regular getTransform().
108 */
109 function getScriptedTransform( $image, $script, $params ) {
110 return false;
111 }
112
113 /**
114 * Get a MediaTransformOutput object representing the transformed output. Does not
115 * actually do the transform.
116 *
117 * @param Image $image The image object
118 * @param string $dstPath Filesystem destination path
119 * @param string $dstUrl Destination URL to use in output HTML
120 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
121 */
122 function getTransform( $image, $dstPath, $dstUrl, $params ) {
123 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
124 }
125
126 /**
127 * Get a MediaTransformOutput object representing the transformed output. Does the
128 * transform unless $flags contains self::TRANSFORM_LATER.
129 *
130 * @param Image $image The image object
131 * @param string $dstPath Filesystem destination path
132 * @param string $dstUrl Destination URL to use in output HTML
133 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
134 * @param integer $flags A bitfield, may contain self::TRANSFORM_LATER
135 */
136 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
137
138 /**
139 * Get the thumbnail extension and MIME type for a given source MIME type
140 * @return array thumbnail extension and MIME type
141 */
142 function getThumbType( $ext, $mime ) {
143 return array( $ext, $mime );
144 }
145
146 /**
147 * True if the handled types can be transformed
148 */
149 function canRender( $file ) { return true; }
150 /**
151 * True if handled types cannot be displayed directly in a browser
152 * but can be rendered
153 */
154 function mustRender( $file ) { return false; }
155 /**
156 * True if the type has multi-page capabilities
157 */
158 function isMultiPage( $file ) { return false; }
159 /**
160 * Page count for a multi-page document, false if unsupported or unknown
161 */
162 function pageCount( $file ) { return false; }
163 /**
164 * False if the handler is disabled for all files
165 */
166 function isEnabled() { return true; }
167
168 /**
169 * Get an associative array of page dimensions
170 * Currently "width" and "height" are understood, but this might be
171 * expanded in the future.
172 * Returns false if unknown or if the document is not multi-page.
173 */
174 function getPageDimensions( $image, $page ) {
175 $gis = $this->getImageSize( $image, $image->getPath() );
176 return array(
177 'width' => $gis[0],
178 'height' => $gis[1]
179 );
180 }
181
182 /**
183 * Get an array structure that looks like this:
184 *
185 * array(
186 * 'visible' => array(
187 * 'Human-readable name' => 'Human readable value',
188 * ...
189 * ),
190 * 'collapsed' => array(
191 * 'Human-readable name' => 'Human readable value',
192 * ...
193 * )
194 * )
195 * The UI will format this into a table where the visible fields are always
196 * visible, and the collapsed fields are optionally visible.
197 *
198 * The function should return false if there is no metadata to display.
199 */
200
201 /**
202 * FIXME: I don't really like this interface, it's not very flexible
203 * I think the media handler should generate HTML instead. It can do
204 * all the formatting according to some standard. That makes it possible
205 * to do things like visual indication of grouped and chained streams
206 * in ogg container files.
207 */
208 function formatMetadata( $image ) {
209 return false;
210 }
211
212 /**
213 * @fixme document this!
214 * 'value' thingy goes into a wikitext table; it used to be escaped but
215 * that was incompatible with previous practice of customized display
216 * with wikitext formatting via messages such as 'exif-model-value'.
217 * So the escaping is taken back out, but generally this seems a confusing
218 * interface.
219 */
220 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
221 $array[$visibility][] = array(
222 'id' => "$type-$id",
223 'name' => wfMsg( "$type-$id", $param ),
224 'value' => $value
225 );
226 }
227
228 function getShortDesc( $file ) {
229 global $wgLang;
230 $nbytes = '(' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
231 $wgLang->formatNum( $file->getSize() ) ) . ')';
232 return "$nbytes";
233 }
234
235 function getLongDesc( $file ) {
236 global $wgUser;
237 $sk = $wgUser->getSkin();
238 return wfMsgExt( 'file-info', 'parseinline',
239 $sk->formatSize( $file->getSize() ),
240 $file->getMimeType() );
241 }
242
243 function getDimensionsString( $file ) {
244 return '';
245 }
246
247 /**
248 * Modify the parser object post-transform
249 */
250 function parserTransformHook( $parser, $file ) {}
251
252 /**
253 * Check for zero-sized thumbnails. These can be generated when
254 * no disk space is available or some other error occurs
255 *
256 * @param $dstPath The location of the suspect file
257 * @param $retval Return value of some shell process, file will be deleted if this is non-zero
258 * @return true if removed, false otherwise
259 */
260 function removeBadFile( $dstPath, $retval = 0 ) {
261 if( file_exists( $dstPath ) ) {
262 $thumbstat = stat( $dstPath );
263 if( $thumbstat['size'] == 0 || $retval != 0 ) {
264 wfDebugLog( 'thumbnail',
265 sprintf( 'Removing bad %d-byte thumbnail "%s"',
266 $thumbstat['size'], $dstPath ) );
267 unlink( $dstPath );
268 return true;
269 }
270 }
271 return false;
272 }
273 }
274
275 /**
276 * Media handler abstract base class for images
277 *
278 * @ingroup Media
279 */
280 abstract class ImageHandler extends MediaHandler {
281 function canRender( $file ) {
282 if ( $file->getWidth() && $file->getHeight() ) {
283 return true;
284 } else {
285 return false;
286 }
287 }
288
289 function getParamMap() {
290 return array( 'img_width' => 'width' );
291 }
292
293 function validateParam( $name, $value ) {
294 if ( in_array( $name, array( 'width', 'height' ) ) ) {
295 if ( $value <= 0 ) {
296 return false;
297 } else {
298 return true;
299 }
300 } else {
301 return false;
302 }
303 }
304
305 function makeParamString( $params ) {
306 if ( isset( $params['physicalWidth'] ) ) {
307 $width = $params['physicalWidth'];
308 } elseif ( isset( $params['width'] ) ) {
309 $width = $params['width'];
310 } else {
311 throw new MWException( 'No width specified to '.__METHOD__ );
312 }
313 # Removed for ProofreadPage
314 #$width = intval( $width );
315 return "{$width}px";
316 }
317
318 function parseParamString( $str ) {
319 $m = false;
320 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
321 return array( 'width' => $m[1] );
322 } else {
323 return false;
324 }
325 }
326
327 function getScriptParams( $params ) {
328 return array( 'width' => $params['width'] );
329 }
330
331 function normaliseParams( $image, &$params ) {
332 $mimeType = $image->getMimeType();
333
334 if ( !isset( $params['width'] ) ) {
335 return false;
336 }
337 if ( !isset( $params['page'] ) ) {
338 $params['page'] = 1;
339 }
340 $srcWidth = $image->getWidth( $params['page'] );
341 $srcHeight = $image->getHeight( $params['page'] );
342 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
343 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
344 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
345 }
346 }
347 $params['height'] = File::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
348 if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
349 return false;
350 }
351 return true;
352 }
353
354 /**
355 * Get a transform output object without actually doing the transform
356 */
357 function getTransform( $image, $dstPath, $dstUrl, $params ) {
358 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
359 }
360
361 /**
362 * Validate thumbnail parameters and fill in the correct height
363 *
364 * @param integer &$width Specified width (input/output)
365 * @param integer &$height Height (output only)
366 * @return false to indicate that an error should be returned to the user.
367 */
368 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
369 $width = intval( $width );
370
371 # Sanity check $width
372 if( $width <= 0) {
373 wfDebug( __METHOD__.": Invalid destination width: $width\n" );
374 return false;
375 }
376 if ( $srcWidth <= 0 ) {
377 wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
378 return false;
379 }
380
381 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
382 return true;
383 }
384
385 function getScriptedTransform( $image, $script, $params ) {
386 if ( !$this->normaliseParams( $image, $params ) ) {
387 return false;
388 }
389 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
390 $page = isset( $params['page'] ) ? $params['page'] : false;
391
392 if( $image->mustRender() || $params['width'] < $image->getWidth() ) {
393 return new ThumbnailImage( $image, $url, $params['width'], $params['height'], $page );
394 }
395 }
396
397 function getImageSize( $image, $path ) {
398 wfSuppressWarnings();
399 $gis = getimagesize( $path );
400 wfRestoreWarnings();
401 return $gis;
402 }
403
404 function getShortDesc( $file ) {
405 global $wgLang;
406 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
407 $wgLang->formatNum( $file->getSize() ) );
408 $widthheight = wfMsgHtml( 'widthheight', $wgLang->formatNum( $file->getWidth() ) ,$wgLang->formatNum( $file->getHeight() ) );
409
410 return "$widthheight ($nbytes)";
411 }
412
413 function getLongDesc( $file ) {
414 global $wgLang;
415 return wfMsgExt('file-info-size', 'parseinline',
416 $wgLang->formatNum( $file->getWidth() ),
417 $wgLang->formatNum( $file->getHeight() ),
418 $wgLang->formatSize( $file->getSize() ),
419 $file->getMimeType() );
420 }
421
422 function getDimensionsString( $file ) {
423 global $wgLang;
424 $pages = $file->pageCount();
425 $width = $wgLang->formatNum( $file->getWidth() );
426 $height = $wgLang->formatNum( $file->getHeight() );
427 $pagesFmt = $wgLang->formatNum( $pages );
428
429 if ( $pages > 1 ) {
430 return wfMsgExt( 'widthheightpage', 'parsemag', $width, $height, $pagesFmt );
431 } else {
432 return wfMsg( 'widthheight', $width, $height );
433 }
434 }
435 }