Fix Bug #30322 “SVG metadata is read incorrectly” by applying supplied patch
[lhc/web/wiklou.git] / includes / media / Generic.php
1 <?php
2 /**
3 * Media-handling base classes and generic functionality
4 *
5 * @file
6 * @ingroup Media
7 */
8
9 /**
10 * Base media handler class
11 *
12 * @ingroup Media
13 */
14 abstract class MediaHandler {
15 const TRANSFORM_LATER = 1;
16 const METADATA_GOOD = true;
17 const METADATA_BAD = false;
18 const METADATA_COMPATIBLE = 2; // for old but backwards compatible.
19 /**
20 * Instance cache
21 */
22 static $handlers = array();
23
24 /**
25 * Get a MediaHandler for a given MIME type from the instance cache
26 *
27 * @param $type string
28 *
29 * @return MediaHandler
30 */
31 static function getHandler( $type ) {
32 global $wgMediaHandlers;
33 if ( !isset( $wgMediaHandlers[$type] ) ) {
34 wfDebug( __METHOD__ . ": no handler found for $type.\n");
35 return false;
36 }
37 $class = $wgMediaHandlers[$type];
38 if ( !isset( self::$handlers[$class] ) ) {
39 self::$handlers[$class] = new $class;
40 if ( !self::$handlers[$class]->isEnabled() ) {
41 self::$handlers[$class] = false;
42 }
43 }
44 return self::$handlers[$class];
45 }
46
47 /**
48 * Get an associative array mapping magic word IDs to parameter names.
49 * Will be used by the parser to identify parameters.
50 */
51 abstract function getParamMap();
52
53 /**
54 * Validate a thumbnail parameter at parse time.
55 * Return true to accept the parameter, and false to reject it.
56 * If you return false, the parser will do something quiet and forgiving.
57 *
58 * @param $name
59 * @param $value
60 */
61 abstract function validateParam( $name, $value );
62
63 /**
64 * Merge a parameter array into a string appropriate for inclusion in filenames
65 *
66 * @param $params array
67 */
68 abstract function makeParamString( $params );
69
70 /**
71 * Parse a param string made with makeParamString back into an array
72 *
73 * @param $str string
74 */
75 abstract function parseParamString( $str );
76
77 /**
78 * Changes the parameter array as necessary, ready for transformation.
79 * Should be idempotent.
80 * Returns false if the parameters are unacceptable and the transform should fail
81 * @param $image
82 * @param $params
83 */
84 abstract function normaliseParams( $image, &$params );
85
86 /**
87 * Get an image size array like that returned by getimagesize(), or false if it
88 * can't be determined.
89 *
90 * @param $image File: the image object, or false if there isn't one
91 * @param $path String: the filename
92 * @return Array
93 */
94 abstract function getImageSize( $image, $path );
95
96 /**
97 * Get handler-specific metadata which will be saved in the img_metadata field.
98 *
99 * @param $image File: the image object, or false if there isn't one.
100 * Warning, File::getPropsFromPath might pass an (object)array() instead (!)
101 * @param $path String: the filename
102 * @return String
103 */
104 function getMetadata( $image, $path ) { return ''; }
105
106 /**
107 * Get metadata version.
108 *
109 * This is not used for validating metadata, this is used for the api when returning
110 * metadata, since api content formats should stay the same over time, and so things
111 * using ForiegnApiRepo can keep backwards compatibility
112 *
113 * All core media handlers share a common version number, and extensions can
114 * use the GetMetadataVersion hook to append to the array (they should append a unique
115 * string so not to get confusing). If there was a media handler named 'foo' with metadata
116 * version 3 it might add to the end of the array the element 'foo=3'. if the core metadata
117 * version is 2, the end version string would look like '2;foo=3'.
118 *
119 * @return string version string
120 */
121 static function getMetadataVersion () {
122 $version = Array( '2' ); // core metadata version
123 wfRunHooks('GetMetadataVersion', Array(&$version));
124 return implode( ';', $version);
125 }
126
127 /**
128 * Convert metadata version.
129 *
130 * By default just returns $metadata, but can be used to allow
131 * media handlers to convert between metadata versions.
132 *
133 * @param $metadata Mixed String or Array metadata array (serialized if string)
134 * @param $version Integer target version
135 * @return Array serialized metadata in specified version, or $metadata on fail.
136 */
137 function convertMetadataVersion( $metadata, $version = 1 ) {
138 if ( !is_array( $metadata ) ) {
139
140 //unserialize to keep return parameter consistent.
141 wfSuppressWarnings();
142 $ret = unserialize( $metadata );
143 wfRestoreWarnings();
144 return $ret;
145 }
146 return $metadata;
147 }
148
149 /**
150 * Get a string describing the type of metadata, for display purposes.
151 *
152 * @return string
153 */
154 function getMetadataType( $image ) { return false; }
155
156 /**
157 * Check if the metadata string is valid for this handler.
158 * If it returns MediaHandler::METADATA_BAD (or false), Image
159 * will reload the metadata from the file and update the database.
160 * MediaHandler::METADATA_GOOD for if the metadata is a-ok,
161 * MediaHanlder::METADATA_COMPATIBLE if metadata is old but backwards
162 * compatible (which may or may not trigger a metadata reload).
163 */
164 function isMetadataValid( $image, $metadata ) {
165 return self::METADATA_GOOD;
166 }
167
168
169 /**
170 * Get a MediaTransformOutput object representing an alternate of the transformed
171 * output which will call an intermediary thumbnail assist script.
172 *
173 * Used when the repository has a thumbnailScriptUrl option configured.
174 *
175 * Return false to fall back to the regular getTransform().
176 */
177 function getScriptedTransform( $image, $script, $params ) {
178 return false;
179 }
180
181 /**
182 * Get a MediaTransformOutput object representing the transformed output. Does not
183 * actually do the transform.
184 *
185 * @param $image File: the image object
186 * @param $dstPath String: filesystem destination path
187 * @param $dstUrl String: Destination URL to use in output HTML
188 * @param $params Array: Arbitrary set of parameters validated by $this->validateParam()
189 */
190 function getTransform( $image, $dstPath, $dstUrl, $params ) {
191 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
192 }
193
194 /**
195 * Get a MediaTransformOutput object representing the transformed output. Does the
196 * transform unless $flags contains self::TRANSFORM_LATER.
197 *
198 * @param $image File: the image object
199 * @param $dstPath String: filesystem destination path
200 * @param $dstUrl String: destination URL to use in output HTML
201 * @param $params Array: arbitrary set of parameters validated by $this->validateParam()
202 * @param $flags Integer: a bitfield, may contain self::TRANSFORM_LATER
203 */
204 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
205
206 /**
207 * Get the thumbnail extension and MIME type for a given source MIME type
208 * @return array thumbnail extension and MIME type
209 */
210 function getThumbType( $ext, $mime, $params = null ) {
211 $magic = MimeMagic::singleton();
212 if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) {
213 // The extension is not valid for this mime type and we do
214 // recognize the mime type
215 $extensions = $magic->getExtensionsForType( $mime );
216 if ( $extensions ) {
217 return array( strtok( $extensions, ' ' ), $mime );
218 }
219 }
220
221 // The extension is correct (true) or the mime type is unknown to
222 // MediaWiki (null)
223 return array( $ext, $mime );
224 }
225
226 /**
227 * True if the handled types can be transformed
228 */
229 function canRender( $file ) { return true; }
230 /**
231 * True if handled types cannot be displayed directly in a browser
232 * but can be rendered
233 */
234 function mustRender( $file ) { return false; }
235 /**
236 * True if the type has multi-page capabilities
237 */
238 function isMultiPage( $file ) { return false; }
239 /**
240 * Page count for a multi-page document, false if unsupported or unknown
241 */
242 function pageCount( $file ) { return false; }
243 /**
244 * The material is vectorized and thus scaling is lossless
245 */
246 function isVectorized( $file ) { return false; }
247 /**
248 * False if the handler is disabled for all files
249 */
250 function isEnabled() { return true; }
251
252 /**
253 * Get an associative array of page dimensions
254 * Currently "width" and "height" are understood, but this might be
255 * expanded in the future.
256 * Returns false if unknown or if the document is not multi-page.
257 *
258 * @param $image File
259 */
260 function getPageDimensions( $image, $page ) {
261 $gis = $this->getImageSize( $image, $image->getPath() );
262 return array(
263 'width' => $gis[0],
264 'height' => $gis[1]
265 );
266 }
267
268 /**
269 * Generic getter for text layer.
270 * Currently overloaded by PDF and DjVu handlers
271 */
272 function getPageText( $image, $page ) {
273 return false;
274 }
275
276 /**
277 * Get an array structure that looks like this:
278 *
279 * array(
280 * 'visible' => array(
281 * 'Human-readable name' => 'Human readable value',
282 * ...
283 * ),
284 * 'collapsed' => array(
285 * 'Human-readable name' => 'Human readable value',
286 * ...
287 * )
288 * )
289 * The UI will format this into a table where the visible fields are always
290 * visible, and the collapsed fields are optionally visible.
291 *
292 * The function should return false if there is no metadata to display.
293 */
294
295 /**
296 * @todo FIXME: I don't really like this interface, it's not very flexible
297 * I think the media handler should generate HTML instead. It can do
298 * all the formatting according to some standard. That makes it possible
299 * to do things like visual indication of grouped and chained streams
300 * in ogg container files.
301 */
302 function formatMetadata( $image ) {
303 return false;
304 }
305
306 /** sorts the visible/invisible field.
307 * Split off from ImageHandler::formatMetadata, as used by more than
308 * one type of handler.
309 *
310 * This is used by the media handlers that use the FormatMetadata class
311 *
312 * @param $metadataArray Array metadata array
313 * @return array for use displaying metadata.
314 */
315 function formatMetadataHelper( $metadataArray ) {
316 $result = array(
317 'visible' => array(),
318 'collapsed' => array()
319 );
320
321 $formatted = FormatMetadata::getFormattedData( $metadataArray );
322 // Sort fields into visible and collapsed
323 $visibleFields = $this->visibleMetadataFields();
324 foreach ( $formatted as $name => $value ) {
325 $tag = strtolower( $name );
326 self::addMeta( $result,
327 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
328 'exif',
329 $tag,
330 $value
331 );
332 }
333 return $result;
334 }
335
336 /**
337 * Get a list of metadata items which should be displayed when
338 * the metadata table is collapsed.
339 *
340 * @return array of strings
341 * @access protected
342 */
343 function visibleMetadataFields() {
344 $fields = array();
345 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
346 foreach( $lines as $line ) {
347 $matches = array();
348 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
349 $fields[] = $matches[1];
350 }
351 }
352 $fields = array_map( 'strtolower', $fields );
353 return $fields;
354 }
355
356
357 /**
358 * This is used to generate an array element for each metadata value
359 * That array is then used to generate the table of metadata values
360 * on the image page
361 *
362 * @param &$array Array An array containing elements for each type of visibility
363 * and each of those elements being an array of metadata items. This function adds
364 * a value to that array.
365 * @param $visbility string ('visible' or 'collapsed') if this value is hidden
366 * by default.
367 * @param $type String type of metadata tag (currently always 'exif')
368 * @param $id String the name of the metadata tag (like 'artist' for example).
369 * its name in the table displayed is the message "$type-$id" (Ex exif-artist ).
370 * @param $value String thingy goes into a wikitext table; it used to be escaped but
371 * that was incompatible with previous practise of customized display
372 * with wikitext formatting via messages such as 'exif-model-value'.
373 * So the escaping is taken back out, but generally this seems a confusing
374 * interface.
375 * @param $param String value to pass to the message for the name of the field
376 * as $1. Currently this parameter doesn't seem to ever be used.
377 *
378 * Note, everything here is passed through the parser later on (!)
379 */
380 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
381 $msgName = "$type-$id";
382 if ( wfEmptyMsg( $msgName ) ) {
383 // This is for future compatibility when using instant commons.
384 // So as to not display as ugly a name if a new metadata
385 // property is defined that we don't know about
386 // (not a major issue since such a property would be collapsed
387 // by default).
388 wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
389 $name = wfEscapeWikiText( $id );
390 } else {
391 $name = wfMsg( $msgName, $param );
392 }
393 $array[$visibility][] = array(
394 'id' => "$type-$id",
395 'name' => $name,
396 'value' => $value
397 );
398 }
399
400 /**
401 * @param $file File
402 * @return string
403 */
404 function getShortDesc( $file ) {
405 global $wgLang;
406 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
407 $wgLang->formatNum( $file->getSize() ) );
408 return "$nbytes";
409 }
410
411 /**
412 * @param $file File
413 * @return string
414 */
415 function getLongDesc( $file ) {
416 global $wgLang;
417 return wfMsgExt( 'file-info', 'parseinline',
418 $wgLang->formatSize( $file->getSize() ),
419 $file->getMimeType() );
420 }
421
422 /**
423 * @param $file File
424 * @return string
425 */
426 static function getGeneralShortDesc( $file ) {
427 global $wgLang;
428 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
429 $wgLang->formatNum( $file->getSize() ) );
430 return "$nbytes";
431 }
432
433 /**
434 * @param $file File
435 * @return string
436 */
437 static function getGeneralLongDesc( $file ) {
438 global $wgLang;
439 return wfMsgExt( 'file-info', 'parseinline',
440 $wgLang->formatSize( $file->getSize() ),
441 $file->getMimeType() );
442 }
443
444 /**
445 * Calculate the largest thumbnail width for a given original file size
446 * such that the thumbnail's height is at most $maxHeight.
447 * @param $boxWidth Integer Width of the thumbnail box.
448 * @param $boxHeight Integer Height of the thumbnail box.
449 * @param $maxHeight Integer Maximum height expected for the thumbnail.
450 * @return Integer.
451 */
452 public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
453 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
454 $roundedUp = ceil( $idealWidth );
455 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
456 return floor( $idealWidth );
457 } else {
458 return $roundedUp;
459 }
460 }
461
462 function getDimensionsString( $file ) {
463 return '';
464 }
465
466 /**
467 * Modify the parser object post-transform
468 */
469 function parserTransformHook( $parser, $file ) {}
470
471 /**
472 * File validation hook called on upload.
473 *
474 * If the file at the given local path is not valid, or its MIME type does not
475 * match the handler class, a Status object should be returned containing
476 * relevant errors.
477 *
478 * @param $fileName The local path to the file.
479 * @return Status object
480 */
481 function verifyUpload( $fileName ) {
482 return Status::newGood();
483 }
484
485 /**
486 * Check for zero-sized thumbnails. These can be generated when
487 * no disk space is available or some other error occurs
488 *
489 * @param $dstPath The location of the suspect file
490 * @param $retval Return value of some shell process, file will be deleted if this is non-zero
491 * @return true if removed, false otherwise
492 */
493 function removeBadFile( $dstPath, $retval = 0 ) {
494 if( file_exists( $dstPath ) ) {
495 $thumbstat = stat( $dstPath );
496 if( $thumbstat['size'] == 0 || $retval != 0 ) {
497 wfDebugLog( 'thumbnail',
498 sprintf( 'Removing bad %d-byte thumbnail "%s"',
499 $thumbstat['size'], $dstPath ) );
500 unlink( $dstPath );
501 return true;
502 }
503 }
504 return false;
505 }
506 }
507
508 /**
509 * Media handler abstract base class for images
510 *
511 * @ingroup Media
512 */
513 abstract class ImageHandler extends MediaHandler {
514
515 /**
516 * @param $file File
517 * @return bool
518 */
519 function canRender( $file ) {
520 return ( $file->getWidth() && $file->getHeight() );
521 }
522
523 function getParamMap() {
524 return array( 'img_width' => 'width' );
525 }
526
527 function validateParam( $name, $value ) {
528 if ( in_array( $name, array( 'width', 'height' ) ) ) {
529 if ( $value <= 0 ) {
530 return false;
531 } else {
532 return true;
533 }
534 } else {
535 return false;
536 }
537 }
538
539 function makeParamString( $params ) {
540 if ( isset( $params['physicalWidth'] ) ) {
541 $width = $params['physicalWidth'];
542 } elseif ( isset( $params['width'] ) ) {
543 $width = $params['width'];
544 } else {
545 throw new MWException( 'No width specified to '.__METHOD__ );
546 }
547 # Removed for ProofreadPage
548 #$width = intval( $width );
549 return "{$width}px";
550 }
551
552 function parseParamString( $str ) {
553 $m = false;
554 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
555 return array( 'width' => $m[1] );
556 } else {
557 return false;
558 }
559 }
560
561 function getScriptParams( $params ) {
562 return array( 'width' => $params['width'] );
563 }
564
565 /**
566 * @param $image File
567 * @param $params
568 * @return bool
569 */
570 function normaliseParams( $image, &$params ) {
571 $mimeType = $image->getMimeType();
572
573 if ( !isset( $params['width'] ) ) {
574 return false;
575 }
576
577 if ( !isset( $params['page'] ) ) {
578 $params['page'] = 1;
579 } else {
580 if ( $params['page'] > $image->pageCount() ) {
581 $params['page'] = $image->pageCount();
582 }
583
584 if ( $params['page'] < 1 ) {
585 $params['page'] = 1;
586 }
587 }
588
589 $srcWidth = $image->getWidth( $params['page'] );
590 $srcHeight = $image->getHeight( $params['page'] );
591
592 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
593 # Height & width were both set
594 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
595 # Height is the relative smaller dimension, so scale width accordingly
596 $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
597
598 if ( $params['width'] == 0 ) {
599 # Very small image, so we need to rely on client side scaling :(
600 $params['width'] = 1;
601 }
602
603 $params['physicalWidth'] = $params['width'];
604 } else {
605 # Height was crap, unset it so that it will be calculated later
606 unset( $params['height'] );
607 }
608 }
609
610 if ( !isset( $params['physicalWidth'] ) ) {
611 # Passed all validations, so set the physicalWidth
612 $params['physicalWidth'] = $params['width'];
613 }
614
615 # Because thumbs are only referred to by width, the height always needs
616 # to be scaled by the width to keep the thumbnail sizes consistent,
617 # even if it was set inside the if block above
618 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
619 $params['physicalWidth'] );
620
621 # Set the height if it was not validated in the if block higher up
622 if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
623 $params['height'] = $params['physicalHeight'];
624 }
625
626
627 if ( !$this->validateThumbParams( $params['physicalWidth'],
628 $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType ) ) {
629 return false;
630 }
631 return true;
632 }
633
634 /**
635 * Get a transform output object without actually doing the transform
636 */
637 function getTransform( $image, $dstPath, $dstUrl, $params ) {
638 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
639 }
640
641 /**
642 * Validate thumbnail parameters and fill in the correct height
643 *
644 * @param $width Integer: specified width (input/output)
645 * @param $height Integer: height (output only)
646 * @param $srcWidth Integer: width of the source image
647 * @param $srcHeight Integer: height of the source image
648 * @param $mimeType Unused
649 * @return false to indicate that an error should be returned to the user.
650 */
651 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
652 $width = intval( $width );
653
654 # Sanity check $width
655 if( $width <= 0) {
656 wfDebug( __METHOD__.": Invalid destination width: $width\n" );
657 return false;
658 }
659 if ( $srcWidth <= 0 ) {
660 wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
661 return false;
662 }
663
664 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
665 if ( $height == 0 ) {
666 # Force height to be at least 1 pixel
667 $height = 1;
668 }
669 return true;
670 }
671
672 /**
673 * @param $image File
674 * @param $script
675 * @param $params
676 * @return bool|ThumbnailImage
677 */
678 function getScriptedTransform( $image, $script, $params ) {
679 if ( !$this->normaliseParams( $image, $params ) ) {
680 return false;
681 }
682 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
683 $page = isset( $params['page'] ) ? $params['page'] : false;
684
685 if( $image->mustRender() || $params['width'] < $image->getWidth() ) {
686 return new ThumbnailImage( $image, $url, $params['width'], $params['height'], $page );
687 }
688 }
689
690 function getImageSize( $image, $path ) {
691 wfSuppressWarnings();
692 $gis = getimagesize( $path );
693 wfRestoreWarnings();
694 return $gis;
695 }
696
697 function isAnimatedImage( $image ) {
698 return false;
699 }
700
701 /**
702 * @param $file File
703 * @return string
704 */
705 function getShortDesc( $file ) {
706 global $wgLang;
707 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
708 $wgLang->formatNum( $file->getSize() ) );
709 $widthheight = wfMsgHtml( 'widthheight', $wgLang->formatNum( $file->getWidth() ) ,$wgLang->formatNum( $file->getHeight() ) );
710
711 return "$widthheight ($nbytes)";
712 }
713
714 /**
715 * @param $file File
716 * @return string
717 */
718 function getLongDesc( $file ) {
719 global $wgLang;
720 $pages = $file->pageCount();
721 if ( $pages === false || $pages <= 1 ) {
722 $msg = wfMsgExt('file-info-size', 'parseinline',
723 $wgLang->formatNum( $file->getWidth() ),
724 $wgLang->formatNum( $file->getHeight() ),
725 $wgLang->formatSize( $file->getSize() ),
726 $file->getMimeType() );
727 } else {
728 $msg = wfMsgExt('file-info-size-pages', 'parseinline',
729 $wgLang->formatNum( $file->getWidth() ),
730 $wgLang->formatNum( $file->getHeight() ),
731 $wgLang->formatSize( $file->getSize() ),
732 $file->getMimeType(),
733 $wgLang->formatNum( $pages ) );
734 }
735 return $msg;
736 }
737
738 /**
739 * @param $file File
740 * @return string
741 */
742 function getDimensionsString( $file ) {
743 global $wgLang;
744 $pages = $file->pageCount();
745 $width = $wgLang->formatNum( $file->getWidth() );
746 $height = $wgLang->formatNum( $file->getHeight() );
747 $pagesFmt = $wgLang->formatNum( $pages );
748
749 if ( $pages > 1 ) {
750 return wfMsgExt( 'widthheightpage', 'parsemag', $width, $height, $pagesFmt );
751 } else {
752 return wfMsg( 'widthheight', $width, $height );
753 }
754 }
755 }