Store "bad metadata" placeholder metadata for SVGs.
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 26 Nov 2012 21:17:41 +0000 (13:17 -0800)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 28 Nov 2012 09:01:53 +0000 (09:01 +0000)
* This prevents excessive download (getLocalReference()) and metadata
  extraction operations to keep reloading metadata again and again.
* This also avoids DB load and lock timeouts due to constants UPDATEs.

Change-Id: I9d35467d3a5e23a07e880dbdf59fccda4b598faf

includes/media/SVG.php
languages/messages/MessagesEn.php
languages/messages/MessagesQqq.php

index 53716df..f7e988f 100644 (file)
@@ -120,6 +120,12 @@ class SvgHandler extends ImageHandler {
                        return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
                }
 
+               $metadata = $this->unpackMetadata( $image->getMetadata() );
+               if ( isset( $metadata['error'] ) ) { // sanity check
+                       $err = wfMessage( 'svg-long-error', $metadata['error']['message'] )->text();
+                       return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err );
+               }
+
                if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
                        return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight,
                                wfMessage( 'thumbnail_dest_directory' )->text() );
@@ -127,7 +133,7 @@ class SvgHandler extends ImageHandler {
 
                $srcPath = $image->getLocalRefPath();
                $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight );
-               if( $status === true ) {
+               if ( $status === true ) {
                        return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
                } else {
                        return $status; // MediaTransformError
@@ -214,6 +220,8 @@ class SvgHandler extends ImageHandler {
                if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
                        return array( $metadata['width'], $metadata['height'], 'SVG',
                                        "width=\"{$metadata['width']}\" height=\"{$metadata['height']}\"" );
+               } else { // error
+                       return array( 0, 0, 'SVG', "width=\"0\" height=\"0\"" );
                }
        }
 
@@ -232,6 +240,12 @@ class SvgHandler extends ImageHandler {
         */
        function getLongDesc( $file ) {
                global $wgLang;
+
+               $metadata = $this->unpackMetadata( $file->getMetadata() );
+               if ( isset( $metadata['error'] ) ) {
+                       return wfMessage( 'svg-long-error', $metadata['error']['message'] )->text();
+               }
+
                $size = $wgLang->formatSize( $file->getSize() );
 
                if ( $this->isAnimatedImage( $file ) ) {
@@ -240,23 +254,23 @@ class SvgHandler extends ImageHandler {
                        $msg = wfMessage( 'svg-long-desc' );
                }
 
-               $msg->numParams(
-                       $file->getWidth(),
-                       $file->getHeight()
-               );
-               $msg->Params( $size );
+               $msg->numParams( $file->getWidth(), $file->getHeight() )->params( $size );
+
                return $msg->parse();
        }
 
        function getMetadata( $file, $filename ) {
+               $metadata = array( 'version' => self::SVG_METADATA_VERSION );
                try {
-                       $metadata = SVGMetadataExtractor::getMetadata( $filename );
-               } catch( Exception $e ) {
-                       // Broken file?
+                       $metadata += SVGMetadataExtractor::getMetadata( $filename );
+               } catch( MWException $e ) { // @TODO: SVG specific exceptions
+                       // File not found, broken, etc.
+                       $metadata['error'] = array(
+                               'message' => $e->getMessage(),
+                               'code'    => $e->getCode()
+                       );
                        wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
-                       return '0';
                }
-               $metadata['version'] = self::SVG_METADATA_VERSION;
                return serialize( $metadata );
        }
 
@@ -306,7 +320,7 @@ class SvgHandler extends ImageHandler {
                        return false;
                }
                $metadata = $this->unpackMetadata( $metadata );
-               if ( !$metadata ) {
+               if ( !$metadata || isset( $metadata['error'] ) ) {
                        return false;
                }
 
index 56dd88c..2d18a4a 100644 (file)
@@ -3852,6 +3852,7 @@ By executing it, your system may be compromised.",
 'file-nohires'                => 'No higher resolution available.',
 'svg-long-desc'               => 'SVG file, nominally $1 × $2 pixels, file size: $3',
 'svg-long-desc-animated'      => 'Animated SVG file, nominally $1 × $2 pixels, file size: $3',
+'svg-long-error'              => 'Invalid SVG file: $1',
 'show-big-image'              => 'Full resolution',
 'show-big-image-preview'      => 'Size of this preview: $1.',
 'show-big-image-other'        => 'Other {{PLURAL:$2|resolution|resolutions}}: $1.',
index c7274d3..e16e557 100644 (file)
@@ -3709,6 +3709,9 @@ Start with a lowercase letter, unless the first word is “SVG”.',
 * $3 is the file size including a unit (for example "10 KB").
 
 Start with a lowercase letter, unless the first word is “SVG”.',
+
+'svg-long-error' => 'Displayed for invalid SVG file metadata.
+* $1 is the error message.',
 'show-big-image' => 'Displayed under an image at the image description page, when it is displayed smaller there than it was uploaded.',
 'show-big-image-other' => 'Message shown under the image description page thumbnail, next to {{msg-mw|show-big-image-preview}}, if the image is in high resolution.',
 'show-big-image-size' => '