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