+
+ function getMetadata( $file, $filename ) {
+ try {
+ $metadata = SVGMetadataExtractor::getMetadata( $filename );
+ } catch( Exception $e ) {
+ // Broken file?
+ wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
+ return '0';
+ }
+ $metadata['version'] = self::SVG_METADATA_VERSION;
+ return serialize( $metadata );
+ }
+
+ function unpackMetadata( $metadata ) {
+ wfSuppressWarnings();
+ $unser = unserialize( $metadata );
+ wfRestoreWarnings();
+ if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) {
+ return $unser;
+ } else {
+ return false;
+ }
+ }
+
+ function getMetadataType( $image ) {
+ return 'parsed-svg';
+ }
+
+ function isMetadataValid( $image, $metadata ) {
+ return $this->unpackMetadata( $metadata ) !== false;
+ }
+
+ function visibleMetadataFields() {
+ $fields = array( 'title', 'description', 'animated' );
+ return $fields;
+ }
+
+ /**
+ * @param $file File
+ * @return array|bool
+ */
+ function formatMetadata( $file ) {
+ $result = array(
+ 'visible' => array(),
+ 'collapsed' => array()
+ );
+ $metadata = $file->getMetadata();
+ if ( !$metadata ) {
+ return false;
+ }
+ $metadata = $this->unpackMetadata( $metadata );
+ if ( !$metadata ) {
+ return false;
+ }
+ unset( $metadata['version'] );
+ unset( $metadata['metadata'] ); /* non-formatted XML */
+
+ /* TODO: add a formatter
+ $format = new FormatSVG( $metadata );
+ $formatted = $format->getFormattedData();
+ */
+
+ // Sort fields into visible and collapsed
+ $visibleFields = $this->visibleMetadataFields();
+
+ // Rename fields to be compatible with exif, so that
+ // the labels for these fields work.
+ $conversion = array( 'width' => 'imagewidth',
+ 'height' => 'imagelength',
+ 'description' => 'imagedescription',
+ 'title' => 'objectname',
+ );
+ foreach ( $metadata as $name => $value ) {
+ $tag = strtolower( $name );
+ if ( isset( $conversion[$tag] ) ) {
+ $tag = $conversion[$tag];
+ }
+ self::addMeta( $result,
+ in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
+ 'exif',
+ $tag,
+ $value
+ );
+ }
+ return $result;
+ }