From: Aaron Schulz Date: Mon, 26 Nov 2012 21:17:41 +0000 (-0800) Subject: Store "bad metadata" placeholder metadata for SVGs. X-Git-Tag: 1.31.0-rc.0~21487 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dcompta/comptes/journal.php?a=commitdiff_plain;h=502b0a22ebf969fb897b66045551bd31c499af46;p=lhc%2Fweb%2Fwiklou.git Store "bad metadata" placeholder metadata for SVGs. * 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 --- diff --git a/includes/media/SVG.php b/includes/media/SVG.php index 53716df6b9..f7e988f0e7 100644 --- a/includes/media/SVG.php +++ b/includes/media/SVG.php @@ -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; } diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 56dd88cff9..2d18a4ac22 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -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.', diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index c7274d3162..e16e5579b4 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -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' => '