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