New hook getOtherBlockLogLink, called in Special:IPBlockList to show links to block...
[lhc/web/wiklou.git] / includes / media / SVG.php
index 5307e26..f0519e8 100644 (file)
@@ -1,7 +1,11 @@
 <?php
+/**
+ * @file
+ * @ingroup Media
+ */
 
 /**
- * @addtogroup Media
+ * @ingroup Media
  */
 class SvgHandler extends ImageHandler {
        function isEnabled() {
@@ -14,7 +18,7 @@ class SvgHandler extends ImageHandler {
                }
        }
 
-       function mustRender() {
+       function mustRender( $file ) {
                return true;
        }
 
@@ -23,7 +27,6 @@ class SvgHandler extends ImageHandler {
                if ( !parent::normaliseParams( $image, $params ) ) {
                        return false;
                }
-
                # Don't make an image bigger than wgMaxSVGSize
                $params['physicalWidth'] = $params['width'];
                $params['physicalHeight'] = $params['height'];
@@ -31,14 +34,14 @@ class SvgHandler extends ImageHandler {
                        $srcWidth = $image->getWidth( $params['page'] );
                        $srcHeight = $image->getHeight( $params['page'] );
                        $params['physicalWidth'] = $wgSVGMaxSize;
-                       $params['physicalHeight'] = Image::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
+                       $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $wgSVGMaxSize );
                }
                return true;
        }
-       
+
        function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
                global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
-               
+
                if ( !$this->normaliseParams( $image, $params ) ) {
                        return new TransformParameterError( $params );
                }
@@ -46,44 +49,59 @@ class SvgHandler extends ImageHandler {
                $clientHeight = $params['height'];
                $physicalWidth = $params['physicalWidth'];
                $physicalHeight = $params['physicalHeight'];
-               $srcWidth = $image->getWidth();
-               $srcHeight = $image->getHeight();
-               $srcPath = $image->getImagePath();
+               $srcPath = $image->getPath();
 
                if ( $flags & self::TRANSFORM_LATER ) {
-                       return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight, $dstPath );
+                       return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
                }
 
                if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
-                       return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, 
+                       return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
                                wfMsg( 'thumbnail_dest_directory' ) );
                }
-
+               
+               $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
+               if( $status === true ) {
+                       return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath );
+               } else {
+                       return $status; // MediaTransformError
+               }
+       }
+       
+       /*
+       * Transform an SVG file to PNG
+       * This function can be called outside of thumbnail contexts
+       * @param string $srcPath
+       * @param string $dstPath
+       * @param string $width
+       * @param string $height
+       * @returns TRUE/MediaTransformError
+       */
+       public function rasterize( $srcPath, $dstPath, $width, $height ) {
+               global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath;
                $err = false;
-               if( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
+               if ( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
                        $cmd = str_replace(
                                array( '$path/', '$width', '$height', '$input', '$output' ),
                                array( $wgSVGConverterPath ? wfEscapeShellArg( "$wgSVGConverterPath/" ) : "",
-                                          intval( $physicalWidth ),
-                                          intval( $physicalHeight ),
+                                          intval( $width ),
+                                          intval( $height ),
                                           wfEscapeShellArg( $srcPath ),
                                           wfEscapeShellArg( $dstPath ) ),
-                               $wgSVGConverters[$wgSVGConverter] ) . " 2>&1";
+                               $wgSVGConverters[$wgSVGConverter]
+                       ) . " 2>&1";
                        wfProfileIn( 'rsvg' );
                        wfDebug( __METHOD__.": $cmd\n" );
                        $err = wfShellExec( $cmd, $retval );
                        wfProfileOut( 'rsvg' );
                }
-
                $removed = $this->removeBadFile( $dstPath, $retval );
                if ( $retval != 0 || $removed ) {
-                       wfDebugLog( 'thumbnail',
-                               sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
+                       wfDebugLog( 'thumbnail', sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
                                        wfHostname(), $retval, trim($err), $cmd ) );
-                       return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
-               } else {
-                       return new ThumbnailImage( $dstUrl, $clientWidth, $clientHeight, $dstPath );
+                       return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
                }
+               return true;
        }
 
        function getImageSize( $image, $path ) {
@@ -93,5 +111,12 @@ class SvgHandler extends ImageHandler {
        function getThumbType( $ext, $mime ) {
                return array( 'png', 'image/png' );
        }
+
+       function getLongDesc( $file ) {
+               global $wgLang;
+               return wfMsgExt( 'svg-long-desc', 'parseinline',
+                       $wgLang->formatNum( $file->getWidth() ),
+                       $wgLang->formatNum( $file->getHeight() ),
+                       $wgLang->formatSize( $file->getSize() ) );
+       }
 }
-?>