Add some $retval = '' before some wfShellExec
[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 function isEnabled() {
16 global $wgSVGConverters, $wgSVGConverter;
17 if ( !isset( $wgSVGConverters[$wgSVGConverter] ) ) {
18 wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering.\n" );
19 return false;
20 } else {
21 return true;
22 }
23 }
24
25 function mustRender( $file ) {
26 return true;
27 }
28
29 function normaliseParams( $image, &$params ) {
30 global $wgSVGMaxSize;
31 if ( !parent::normaliseParams( $image, $params ) ) {
32 return false;
33 }
34 # Don't make an image bigger than wgMaxSVGSize
35 $params['physicalWidth'] = $params['width'];
36 $params['physicalHeight'] = $params['height'];
37 if ( $params['physicalWidth'] > $wgSVGMaxSize ) {
38 $srcWidth = $image->getWidth( $params['page'] );
39 $srcHeight = $image->getHeight( $params['page'] );
40 $params['physicalWidth'] = $wgSVGMaxSize;
41 $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
42 }
43 return true;
44 }
45
46 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
47 if ( !$this->normaliseParams( $image, $params ) ) {
48 return new TransformParameterError( $params );
49 }
50 $clientWidth = $params['width'];
51 $clientHeight = $params['height'];
52 $physicalWidth = $params['physicalWidth'];
53 $physicalHeight = $params['physicalHeight'];
54 $srcPath = $image->getPath();
55
56 if ( $flags & self::TRANSFORM_LATER ) {
57 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
58 }
59
60 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
61 return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
62 wfMsg( 'thumbnail_dest_directory' ) );
63 }
64
65 $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
66 if( $status === true ) {
67 return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
68 } else {
69 return $status; // MediaTransformError
70 }
71 }
72
73 /*
74 * Transform an SVG file to PNG
75 * This function can be called outside of thumbnail contexts
76 * @param string $srcPath
77 * @param string $dstPath
78 * @param string $width
79 * @param string $height
80 * @returns TRUE/MediaTransformError
81 */
82 public function rasterize( $srcPath, $dstPath, $width, $height ) {
83 global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
84 $err = false;
85 $retval = '';
86 if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
87 $cmd = str_replace(
88 array( '$path/', '$width', '$height', '$input', '$output' ),
89 array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
90 intval( $width ),
91 intval( $height ),
92 wfEscapeShellArg( $srcPath ),
93 wfEscapeShellArg( $dstPath ) ),
94 $wgSVGConverters[$wgSVGConverter]
95 ) . " 2>&1";
96 wfProfileIn( 'rsvg' );
97 wfDebug( __METHOD__.": $cmd\n" );
98 $err = wfShellExec( $cmd, &$retval );
99 wfProfileOut( 'rsvg' );
100 }
101 $removed = $this->removeBadFile( $dstPath, $retval );
102 if ( $retval != 0 || $removed ) {
103 wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
104 wfHostname(), $retval, trim($err), $cmd ) );
105 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
106 }
107 return true;
108 }
109
110 function getImageSize( $image, $path ) {
111 return wfGetSVGsize( $path );
112 }
113
114 function getThumbType( $ext, $mime, $params = null ) {
115 return array( 'png', 'image/png' );
116 }
117
118 function getLongDesc( $file ) {
119 global $wgLang;
120 return wfMsgExt( 'svg-long-desc', 'parseinline',
121 $wgLang->formatNum( $file->getWidth() ),
122 $wgLang->formatNum( $file->getHeight() ),
123 $wgLang->formatSize( $file->getSize() ) );
124 }
125 }