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