* Allow wikitext in file-info, file-info-size, svg-long-desc instead of mix of unesca...
[lhc/web/wiklou.git] / includes / media / SVG.php
1 <?php
2 /**
3 * @file
4 * @ingroup Media
5 */
6
7 /**
8 * @ingroup Media
9 */
10 class SvgHandler extends ImageHandler {
11 function isEnabled() {
12 global $wgSVGConverters, $wgSVGConverter;
13 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
14 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
15 return false;
16 } else {
17 return true;
18 }
19 }
20
21 function mustRender( $file ) {
22 return true;
23 }
24
25 function normaliseParams( $image, &$params ) {
26 global $wgSVGMaxSize;
27 if ( !parent::normaliseParams( $image, $params ) ) {
28 return false;
29 }
30
31 # Don't make an image bigger than wgMaxSVGSize
32 $params['physicalWidth'] = $params['width'];
33 $params['physicalHeight'] = $params['height'];
34 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
35 $srcWidth = $image->getWidth( $params['page'] );
36 $srcHeight = $image->getHeight( $params['page'] );
37 $params['physicalWidth'] = $wgSVGMaxSize;
38 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
39 }
40 return true;
41 }
42
43 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
44 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
45
46 if ( !$this->normaliseParams( $image, $params ) ) {
47 return new TransformParameterError( $params );
48 }
49 $clientWidth = $params['width'];
50 $clientHeight = $params['height'];
51 $physicalWidth = $params['physicalWidth'];
52 $physicalHeight = $params['physicalHeight'];
53 $srcPath = $image->getPath();
54
55 if ( $flags & self::TRANSFORM_LATER ) {
56 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
57 }
58
59 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
60 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
61 wfMsg( 'thumbnail_dest_directory' ) );
62 }
63
64 $err = false;
65 if( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
66 $cmd = str_replace(
67 array( '$path/', '$width', '$height', '$input', '$output' ),
68 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
69 intval( $physicalWidth ),
70 intval( $physicalHeight ),
71 wfEscapeShellArg( $srcPath ),
72 wfEscapeShellArg( $dstPath ) ),
73 $wgSVGConverters[$wgSVGConverter] ) . " 2>&1";
74 wfProfileIn( 'rsvg' );
75 wfDebug( __METHOD__.": $cmd\n" );
76 $err = wfShellExec( $cmd, $retval );
77 wfProfileOut( 'rsvg' );
78 }
79
80 $removed = $this->removeBadFile( $dstPath, $retval );
81 if ( $retval != 0 || $removed ) {
82 wfDebugLog( 'thumbnail',
83 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
84 wfHostname(), $retval, trim($err), $cmd ) );
85 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
86 } else {
87 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
88 }
89 }
90
91 function getImageSize( $image, $path ) {
92 return wfGetSVGsize( $path );
93 }
94
95 function getThumbType( $ext, $mime ) {
96 return array( 'png', 'image/png' );
97 }
98
99 function getLongDesc( $file ) {
100 global $wgLang;
101 return wfMsgExt( 'svg-long-desc', 'parseinline',
102 $wgLang->formatNum( $file->getWidth() ),
103 $wgLang->formatNum( $file->getHeight() ),
104 $wgLang->formatSize( $file->getSize() ) );
105 }
106 }