]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/media/SVG.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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                 try {
154                         $metadata = SVGMetadataExtractor::getMetadata( $filename );
155                 } catch( Exception $e ) {
156                         // Broken file?
157                         wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
158                         return '0';
159                 }
160                 $metadata['version'] = self::SVG_METADATA_VERSION;
161                 return serialize( $metadata );
162         }
163
164         function unpackMetadata( $metadata ) {
165                 $unser = @unserialize( $metadata );
166                 if ( isset( $unser['version'] ) && $unser['version'] == self::SVG_METADATA_VERSION ) {
167                         return $unser;
168                 } else {
169                         return false;
170                 }
171         }
172
173         function getMetadataType( $image ) {
174                 return 'parsed-svg';
175         }
176
177         function isMetadataValid( $image, $metadata ) {
178                 return $this->unpackMetadata( $metadata ) !== false;
179         }
180
181         function visibleMetadataFields() {
182                 $fields = array( 'title', 'description', 'animated' );
183                 return $fields;
184         }
185
186         function formatMetadata( $file ) {
187                 $result = array(
188                         'visible' => array(),
189                         'collapsed' => array()
190                 );
191                 $metadata = $file->getMetadata();
192                 if ( !$metadata ) {
193                         return false;
194                 }
195                 $metadata = $this->unpackMetadata( $metadata );
196                 if ( !$metadata ) {
197                         return false;
198                 }
199                 unset( $metadata['version'] );
200                 unset( $metadata['metadata'] ); /* non-formatted XML */
201
202                 /* TODO: add a formatter
203                 $format = new FormatSVG( $metadata );
204                 $formatted = $format->getFormattedData();
205                 */
206
207                 // Sort fields into visible and collapsed
208                 $visibleFields = $this->visibleMetadataFields();
209
210                 // Rename fields to be compatible with exif, so that
211                 // the labels for these fields work.
212                 $conversion = array( 'width' => 'imagewidth',
213                         'height' => 'imagelength',
214                         'description' => 'imagedescription',
215                         'title' => 'objectname',
216                 );
217                 foreach ( $metadata as $name => $value ) {
218                         $tag = strtolower( $name );
219                         if ( isset( $conversion[$tag] ) ) {
220                                 $tag = $conversion[$tag];
221                         }
222                         self::addMeta( $result,
223                                 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
224                                 'exif',
225                                 $tag,
226                                 $value
227                         );
228                 }
229                 return $result;
230         }
231 }