3 * Handler for SVG images.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use Wikimedia\ScopedCallback
;
26 * Handler for SVG images.
30 class SvgHandler
extends ImageHandler
{
31 const SVG_METADATA_VERSION
= 2;
33 /** @var array A list of metadata tags that can be converted
34 * to the commonly used exif tags. This allows messages
35 * to be reused, and consistent tag names for {{#formatmetadata:..}}
37 private static $metaConversion = [
38 'originalwidth' => 'ImageWidth',
39 'originalheight' => 'ImageLength',
40 'description' => 'ImageDescription',
41 'title' => 'ObjectName',
44 function isEnabled() {
45 global $wgSVGConverters, $wgSVGConverter;
46 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
47 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
55 public function mustRender( $file ) {
59 function isVectorized( $file ) {
67 function isAnimatedImage( $file ) {
68 # @todo Detect animated SVGs
69 $metadata = $file->getMetadata();
71 $metadata = $this->unpackMetadata( $metadata );
72 if ( isset( $metadata['animated'] ) ) {
73 return $metadata['animated'];
81 * Which languages (systemLanguage attribute) is supported.
83 * @note This list is not guaranteed to be exhaustive.
84 * To avoid OOM errors, we only look at first bit of a file.
85 * Thus all languages on this list are present in the file,
86 * but its possible for the file to have a language not on
90 * @return array Array of language codes, or empty if no language switching supported.
92 public function getAvailableLanguages( File
$file ) {
93 $metadata = $file->getMetadata();
96 $metadata = $this->unpackMetadata( $metadata );
97 if ( isset( $metadata['translations'] ) ) {
98 foreach ( $metadata['translations'] as $lang => $langType ) {
99 if ( $langType === SVGReader
::LANG_FULL_MATCH
) {
109 * What language to render file in if none selected.
112 * @return string Language code.
114 public function getDefaultRenderLanguage( File
$file ) {
119 * We do not support making animated svg thumbnails
123 function canAnimateThumbnail( $file ) {
129 * @param array &$params
132 function normaliseParams( $image, &$params ) {
133 global $wgSVGMaxSize;
134 if ( !parent
::normaliseParams( $image, $params ) ) {
137 # Don't make an image bigger than wgMaxSVGSize on the smaller side
138 if ( $params['physicalWidth'] <= $params['physicalHeight'] ) {
139 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
140 $srcWidth = $image->getWidth( $params['page'] );
141 $srcHeight = $image->getHeight( $params['page'] );
142 $params['physicalWidth'] = $wgSVGMaxSize;
143 $params['physicalHeight'] = File
::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
146 if ( $params['physicalHeight'] > $wgSVGMaxSize ) {
147 $srcWidth = $image->getWidth( $params['page'] );
148 $srcHeight = $image->getHeight( $params['page'] );
149 $params['physicalWidth'] = File
::scaleHeight( $srcHeight, $srcWidth, $wgSVGMaxSize );
150 $params['physicalHeight'] = $wgSVGMaxSize;
159 * @param string $dstPath
160 * @param string $dstUrl
161 * @param array $params
163 * @return bool|MediaTransformError|ThumbnailImage|TransformParameterError
165 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
166 if ( !$this->normaliseParams( $image, $params ) ) {
167 return new TransformParameterError( $params );
169 $clientWidth = $params['width'];
170 $clientHeight = $params['height'];
171 $physicalWidth = $params['physicalWidth'];
172 $physicalHeight = $params['physicalHeight'];
173 $lang = isset( $params['lang'] ) ?
$params['lang'] : $this->getDefaultRenderLanguage( $image );
175 if ( $flags & self
::TRANSFORM_LATER
) {
176 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
179 $metadata = $this->unpackMetadata( $image->getMetadata() );
180 if ( isset( $metadata['error'] ) ) { // sanity check
181 $err = wfMessage( 'svg-long-error', $metadata['error']['message'] );
183 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
186 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__
) ) {
187 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
188 wfMessage( 'thumbnail_dest_directory' ) );
191 $srcPath = $image->getLocalRefPath();
192 if ( $srcPath === false ) { // Failed to get local copy
193 wfDebugLog( 'thumbnail',
194 sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
195 wfHostname(), $image->getName() ) );
197 return new MediaTransformError( 'thumbnail_error',
198 $params['width'], $params['height'],
199 wfMessage( 'filemissing' )
203 // Make a temp dir with a symlink to the local copy in it.
204 // This plays well with rsvg-convert policy for external entities.
205 // https://git.gnome.org/browse/librsvg/commit/?id=f01aded72c38f0e18bc7ff67dee800e380251c8e
206 $tmpDir = wfTempDir() . '/svg_' . wfRandomString( 24 );
207 $lnPath = "$tmpDir/" . basename( $srcPath );
208 $ok = mkdir( $tmpDir, 0771 );
210 wfDebugLog( 'thumbnail',
211 sprintf( 'Thumbnail failed on %s: could not create temporary directory %s',
212 wfHostname(), $tmpDir ) );
213 return new MediaTransformError( 'thumbnail_error',
214 $params['width'], $params['height'],
215 wfMessage( 'thumbnail-temp-create' )->text()
218 $ok = symlink( $srcPath, $lnPath );
219 /** @noinspection PhpUnusedLocalVariableInspection */
220 $cleaner = new ScopedCallback( function () use ( $tmpDir, $lnPath ) {
221 MediaWiki\
suppressWarnings();
224 MediaWiki\restoreWarnings
();
227 wfDebugLog( 'thumbnail',
228 sprintf( 'Thumbnail failed on %s: could not link %s to %s',
229 wfHostname(), $lnPath, $srcPath ) );
230 return new MediaTransformError( 'thumbnail_error',
231 $params['width'], $params['height'],
232 wfMessage( 'thumbnail-temp-create' )
236 $status = $this->rasterize( $lnPath, $dstPath, $physicalWidth, $physicalHeight, $lang );
237 if ( $status === true ) {
238 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
240 return $status; // MediaTransformError
245 * Transform an SVG file to PNG
246 * This function can be called outside of thumbnail contexts
247 * @param string $srcPath
248 * @param string $dstPath
249 * @param string $width
250 * @param string $height
251 * @param bool|string $lang Language code of the language to render the SVG in
252 * @throws MWException
253 * @return bool|MediaTransformError
255 public function rasterize( $srcPath, $dstPath, $width, $height, $lang = false ) {
256 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
259 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
260 if ( is_array( $wgSVGConverters[$wgSVGConverter] ) ) {
261 // This is a PHP callable
262 $func = $wgSVGConverters[$wgSVGConverter][0];
263 $args = array_merge( [ $srcPath, $dstPath, $width, $height, $lang ],
264 array_slice( $wgSVGConverters[$wgSVGConverter], 1 ) );
265 if ( !is_callable( $func ) ) {
266 throw new MWException( "$func is not callable" );
268 $err = call_user_func_array( $func, $args );
269 $retval = (bool)$err;
273 [ '$path/', '$width', '$height', '$input', '$output' ],
274 [ $wgSVGConverterPath ?
wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
277 wfEscapeShellArg( $srcPath ),
278 wfEscapeShellArg( $dstPath ) ],
279 $wgSVGConverters[$wgSVGConverter]
283 if ( $lang !== false ) {
284 $env['LANG'] = $lang;
287 wfDebug( __METHOD__
. ": $cmd\n" );
288 $err = wfShellExecWithStderr( $cmd, $retval, $env );
291 $removed = $this->removeBadFile( $dstPath, $retval );
292 if ( $retval != 0 ||
$removed ) {
293 $this->logErrorForExternalProcess( $retval, $err, $cmd );
294 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
300 public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) {
301 $im = new Imagick( $srcPath );
302 $im->setImageFormat( 'png' );
303 $im->setBackgroundColor( 'transparent' );
304 $im->setImageDepth( 8 );
306 if ( !$im->thumbnailImage( intval( $width ), intval( $height ), /* fit */ false ) ) {
307 return 'Could not resize image';
309 if ( !$im->writeImage( $dstPath ) ) {
310 return "Could not write to $dstPath";
315 * @param File|FSFile $file
316 * @param string $path Unused
317 * @param bool|array $metadata
320 function getImageSize( $file, $path, $metadata = false ) {
321 if ( $metadata === false && $file instanceof File
) {
322 $metadata = $file->getMetadata();
324 $metadata = $this->unpackMetadata( $metadata );
326 if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
327 return [ $metadata['width'], $metadata['height'], 'SVG',
328 "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" ];
330 return [ 0, 0, 'SVG', "width=\"0\" height=\"0\"" ];
334 function getThumbType( $ext, $mime, $params = null ) {
335 return [ 'png', 'image/png' ];
339 * Subtitle for the image. Different from the base
340 * class so it can be denoted that SVG's have
341 * a "nominal" resolution, and not a fixed one,
342 * as well as so animation can be denoted.
347 function getLongDesc( $file ) {
350 $metadata = $this->unpackMetadata( $file->getMetadata() );
351 if ( isset( $metadata['error'] ) ) {
352 return wfMessage( 'svg-long-error', $metadata['error']['message'] )->text();
355 $size = $wgLang->formatSize( $file->getSize() );
357 if ( $this->isAnimatedImage( $file ) ) {
358 $msg = wfMessage( 'svg-long-desc-animated' );
360 $msg = wfMessage( 'svg-long-desc' );
363 $msg->numParams( $file->getWidth(), $file->getHeight() )->params( $size );
365 return $msg->parse();
369 * @param File|FSFile $file
370 * @param string $filename
371 * @return string Serialised metadata
373 function getMetadata( $file, $filename ) {
374 $metadata = [ 'version' => self
::SVG_METADATA_VERSION
];
376 $metadata +
= SVGMetadataExtractor
::getMetadata( $filename );
377 } catch ( Exception
$e ) { // @todo SVG specific exceptions
378 // File not found, broken, etc.
379 $metadata['error'] = [
380 'message' => $e->getMessage(),
381 'code' => $e->getCode()
383 wfDebug( __METHOD__
. ': ' . $e->getMessage() . "\n" );
386 return serialize( $metadata );
389 function unpackMetadata( $metadata ) {
390 MediaWiki\
suppressWarnings();
391 $unser = unserialize( $metadata );
392 MediaWiki\restoreWarnings
();
393 if ( isset( $unser['version'] ) && $unser['version'] == self
::SVG_METADATA_VERSION
) {
400 function getMetadataType( $image ) {
404 function isMetadataValid( $image, $metadata ) {
405 $meta = $this->unpackMetadata( $metadata );
406 if ( $meta === false ) {
407 return self
::METADATA_BAD
;
409 if ( !isset( $meta['originalWidth'] ) ) {
410 // Old but compatible
411 return self
::METADATA_COMPATIBLE
;
414 return self
::METADATA_GOOD
;
417 protected function visibleMetadataFields() {
418 $fields = [ 'objectname', 'imagedescription' ];
425 * @param bool|IContextSource $context Context to use (optional)
428 function formatMetadata( $file, $context = false ) {
433 $metadata = $file->getMetadata();
437 $metadata = $this->unpackMetadata( $metadata );
438 if ( !$metadata ||
isset( $metadata['error'] ) ) {
442 /* @todo Add a formatter
443 $format = new FormatSVG( $metadata );
444 $formatted = $format->getFormattedData();
447 // Sort fields into visible and collapsed
448 $visibleFields = $this->visibleMetadataFields();
451 foreach ( $metadata as $name => $value ) {
452 $tag = strtolower( $name );
453 if ( isset( self
::$metaConversion[$tag] ) ) {
454 $tag = strtolower( self
::$metaConversion[$tag] );
456 // Do not output other metadata not in list
460 self
::addMeta( $result,
461 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',
468 return $showMeta ?
$result : false;
472 * @param string $name Parameter name
473 * @param mixed $value Parameter value
474 * @return bool Validity
476 public function validateParam( $name, $value ) {
477 if ( in_array( $name, [ 'width', 'height' ] ) ) {
478 // Reject negative heights, widths
479 return ( $value > 0 );
480 } elseif ( $name == 'lang' ) {
482 if ( $value === '' ||
!Language
::isValidBuiltInCode( $value ) ) {
483 wfDebug( "Invalid user language code\n" );
491 // Only lang, width and height are acceptable keys
496 * @param array $params Name=>value pairs of parameters
497 * @return string Filename to use
499 public function makeParamString( $params ) {
501 if ( isset( $params['lang'] ) && $params['lang'] !== 'en' ) {
502 $params['lang'] = strtolower( $params['lang'] );
503 $lang = "lang{$params['lang']}-";
505 if ( !isset( $params['width'] ) ) {
509 return "$lang{$params['width']}px";
512 public function parseParamString( $str ) {
514 if ( preg_match( '/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/', $str, $m ) ) {
515 return [ 'width' => array_pop( $m ), 'lang' => $m[1] ];
516 } elseif ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
517 return [ 'width' => $m[1], 'lang' => 'en' ];
523 public function getParamMap() {
524 return [ 'img_lang' => 'lang', 'img_width' => 'width' ];
528 * @param array $params
531 function getScriptParams( $params ) {
532 $scriptParams = [ 'width' => $params['width'] ];
533 if ( isset( $params['lang'] ) ) {
534 $scriptParams['lang'] = $params['lang'];
537 return $scriptParams;
540 public function getCommonMetaArray( File
$file ) {
541 $metadata = $file->getMetadata();
545 $metadata = $this->unpackMetadata( $metadata );
546 if ( !$metadata ||
isset( $metadata['error'] ) ) {
550 foreach ( $metadata as $name => $value ) {
551 $tag = strtolower( $name );
552 if ( $tag === 'originalwidth' ||
$tag === 'originalheight' ) {
553 // Skip these. In the exif metadata stuff, it is assumed these
554 // are measured in px, which is not the case here.
557 if ( isset( self
::$metaConversion[$tag] ) ) {
558 $tag = self
::$metaConversion[$tag];
559 $stdMetadata[$tag] = $value;