* The SVGMetadataExtractor now based on XmlReader
[lhc/web/wiklou.git] / includes / media / SVG.php
1 <?php
2 /**
3 * Handler for SVG images.
4 *
5 * @file
6 * @ingroup Media
7 */
8
9 /**
10 * Handler for SVG images.
11 *
12 * @ingroup Media
13 */
14 class SvgHandler extends ImageHandler {
15 const SVG_METADATA_VERSION = 2;
16
17 function isEnabled() {
18 global $wgSVGConverters, $wgSVGConverter;
19 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
20 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
21 return false;
22 } else {
23 return true;
24 }
25 }
26
27 function mustRender( $file ) {
28 return true;
29 }
30
31 function isVectorized( $file ) {
32 return true;
33 }
34
35 function isAnimatedImage( $file ) {
36 # TODO: detect animated SVGs
37 $metadata = $file->getMetadata();
38 if ( $metadata ) {
39 $metadata = $this->unpackMetadata( $metadata );
40 if( isset( $metadata['animated'] ) ) {
41 return $metadata['animated'];
42 }
43 }
44 return false;
45 }
46
47 function normaliseParams( $image, &$params ) {
48 global $wgSVGMaxSize;
49 if ( !parent::normaliseParams( $image, $params ) ) {
50 return false;
51 }
52 # Don't make an image bigger than wgMaxSVGSize
53 $params['physicalWidth'] = $params['width'];
54 $params['physicalHeight'] = $params['height'];
55 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
56 $srcWidth = $image->getWidth( $params['page'] );
57 $srcHeight = $image->getHeight( $params['page'] );
58 $params['physicalWidth'] = $wgSVGMaxSize;
59 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
60 }
61 return true;
62 }
63
64 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
65 if ( !$this->normaliseParams( $image, $params ) ) {
66 return new TransformParameterError( $params );
67 }
68 $clientWidth = $params['width'];
69 $clientHeight = $params['height'];
70 $physicalWidth = $params['physicalWidth'];
71 $physicalHeight = $params['physicalHeight'];
72 $srcPath = $image->getPath();
73
74 if ( $flags & self::TRANSFORM_LATER ) {
75 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
76 }
77
78 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
79 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
80 wfMsg( 'thumbnail_dest_directory' ) );
81 }
82
83 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
84 if( $status === true ) {
85 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
86 } else {
87 return $status; // MediaTransformError
88 }
89 }
90
91 /*
92 * Transform an SVG file to PNG
93 * This function can be called outside of thumbnail contexts
94 * @param string $srcPath
95 * @param string $dstPath
96 * @param string $width
97 * @param string $height
98 * @returns TRUE/MediaTransformError
99 */
100 public function rasterize( $srcPath, $dstPath, $width, $height ) {
101 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
102 $err = false;
103 $retval = '';
104 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
105 $cmd = str_replace(
106 array( '$path/', '$width', '$height', '$input', '$output' ),
107 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
108 intval( $width ),
109 intval( $height ),
110 wfEscapeShellArg( $srcPath ),
111 wfEscapeShellArg( $dstPath ) ),
112 $wgSVGConverters[$wgSVGConverter]
113 ) . " 2>&1";
114 wfProfileIn( 'rsvg' );
115 wfDebug( __METHOD__.": $cmd\n" );
116 $err = wfShellExec( $cmd, $retval );
117 wfProfileOut( 'rsvg' );
118 }
119 $removed = $this->removeBadFile( $dstPath, $retval );
120 if ( $retval != 0 || $removed ) {
121 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
122 wfHostname(), $retval, trim($err), $cmd ) );
123 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
124 }
125 return true;
126 }
127
128 function getImageSize( $file, $path, $metadata = false ) {
129 if ( $metadata === false ) {
130 $metadata = $file->getMetaData();
131 }
132 $metadata = $this->unpackMetaData( $metadata );
133
134 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
135 return array( $metadata['width'], $metadata['height'], 'SVG',
136 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
137 }
138 }
139
140 function getThumbType( $ext, $mime, $params = null ) {
141 return array( 'png', 'image/png' );
142 }
143
144 function getLongDesc( $file ) {
145 global $wgLang;
146 return wfMsgExt( 'svg-long-desc', 'parseinline',
147 $wgLang->formatNum( $file->getWidth() ),
148 $wgLang->formatNum( $file->getHeight() ),
149 $wgLang->formatSize( $file->getSize() ) );
150 }
151
152 function getMetadata( $file, $filename ) {
153 $metadata = array();
154 try {
155 $metadata = SVGMetadataExtractor::getMetadata( $filename );
156 } catch( Exception $e ) {
157 // Broken file?
158 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
159 return '0';
160 }
161 $metadata['version'] = self::SVG_METADATA_VERSION;
162 return serialize( $metadata );
163 }
164
165 function unpackMetadata( $metadata ) {
166 $unser = @unserialize( $metadata );
167 if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) {
168 return $unser;
169 } else {
170 return false;
171 }
172 }
173
174 function getMetadataType( $image ) {
175 return 'parsed-svg';
176 }
177
178 function isMetadataValid( $image, $metadata ) {
179 return $this->unpackMetadata( $metadata ) !== false;
180 }
181
182 function visibleMetadataFields() {
183 $fields = array( 'title', 'description', 'animated' );
184 return $fields;
185 }
186
187 function formatMetadata( $file ) {
188 $result = array(
189 'visible' => array(),
190 'collapsed' => array()
191 );
192 $metadata = $file->getMetadata();
193 if ( !$metadata ) {
194 return false;
195 }
196 $metadata = $this->unpackMetadata( $metadata );
197 if ( !$metadata ) {
198 return false;
199 }
200 unset( $metadata['version'] );
201 unset( $metadata['metadata'] ); /* non-formatted XML */
202
203 /* TODO: add a formatter
204 $format = new FormatSVG( $metadata );
205 $formatted = $format->getFormattedData();
206 */
207
208 // Sort fields into visible and collapsed
209 $visibleFields = $this->visibleMetadataFields();
210 foreach ( $metadata as $name => $value ) {
211 $tag = strtolower( $name );
212 self::addMeta( $result,
213 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
214 'svg',
215 $tag,
216 $value
217 );
218 }
219 return $result;
220 }
221 }