resourceloader: Throw InvalidArgumentException for invalid constructor arguments
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderImage.php
index 6788c6f..d12975a 100644 (file)
@@ -44,7 +44,7 @@ class ResourceLoaderImage {
         * @param string|array $descriptor Path to image file, or array structure containing paths
         * @param string $basePath Directory to which paths in descriptor refer
         * @param array $variants
-        * @throws MWException
+        * @throws InvalidArgumentException
         */
        public function __construct( $name, $module, $descriptor, $basePath, $variants ) {
                $this->name = $name;
@@ -61,11 +61,11 @@ class ResourceLoaderImage {
                } );
                $extensions = array_unique( $extensions );
                if ( count( $extensions ) !== 1 ) {
-                       throw new MWException( 'Image type for various images differs.' );
+                       throw new InvalidArgumentException( "File type for different image files of '$name' not the same." );
                }
                $ext = $extensions[0];
                if ( !isset( self::$fileTypes[$ext] ) ) {
-                       throw new MWException( 'Invalid image type; svg, png, gif or jpg required.' );
+                       throw new InvalidArgumentException( "Invalid file type for image files of '$name' (valid: svg, png, gif, jpg)." );
                }
                $this->extension = $ext;
        }
@@ -200,18 +200,22 @@ class ResourceLoaderImage {
                        $format = $context->getFormat();
                }
 
+               $path = $this->getPath( $context );
                if ( $this->getExtension() !== 'svg' ) {
-                       return file_get_contents( $this->getPath( $context ) );
+                       return file_get_contents( $path );
                }
 
                if ( $variant && isset( $this->variants[$variant] ) ) {
                        $data = $this->variantize( $this->variants[$variant], $context );
                } else {
-                       $data = file_get_contents( $this->getPath( $context ) );
+                       $data = file_get_contents( $path );
                }
 
                if ( $format === 'rasterized' ) {
                        $data = $this->rasterize( $data );
+                       if ( !$data ) {
+                               wfDebugLog( 'ResourceLoaderImage', __METHOD__  . " failed to rasterize for $path" );
+                       }
                }
 
                return $data;
@@ -307,7 +311,8 @@ class ResourceLoaderImage {
 
                $svg = $this->massageSvgPathdata( $svg );
 
-               if ( $wgSVGConverter === 'rsvg' ) {
+               // Sometimes this might be 'rsvg-secure'. Long as it's rsvg.
+               if ( strpos( $wgSVGConverter, 'rsvg' ) === 0 ) {
                        $command = 'rsvg-convert';
                        if ( $wgSVGConverterPath ) {
                                $command = wfEscapeShellArg( "$wgSVGConverterPath/" ) . $command;
@@ -339,16 +344,24 @@ class ResourceLoaderImage {
 
                        $metadata = SVGMetadataExtractor::getMetadata( $tempFilenameSvg );
                        if ( !isset( $metadata['width'] ) || !isset( $metadata['height'] ) ) {
+                               unlink( $tempFilenameSvg );
                                return false;
                        }
 
                        $handler = new SvgHandler;
-                       $handler->rasterize( $tempFilenameSvg, $tempFilenamePng, $metadata['width'], $metadata['height'] );
-
-                       $png = file_get_contents( $tempFilenamePng );
-
+                       $res = $handler->rasterize(
+                               $tempFilenameSvg,
+                               $tempFilenamePng,
+                               $metadata['width'],
+                               $metadata['height']
+                       );
                        unlink( $tempFilenameSvg );
-                       unlink( $tempFilenamePng );
+
+                       $png = null;
+                       if ( $res === true ) {
+                               $png = file_get_contents( $tempFilenamePng );
+                               unlink( $tempFilenamePng );
+                       }
 
                        return $png ?: false;
                }