From: Brion Vibber Date: Tue, 9 Dec 2008 00:26:09 +0000 (+0000) Subject: * (bug 14268) SVG image sizes now extracted with proper XML parser X-Git-Tag: 1.31.0-rc.0~44095 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22suivi_revisions%22%2C%22id_auteur=%24connecte%22%29%20.%20%22?a=commitdiff_plain;h=611b224f3180fcedad2e4c4bd77b24a4fcf506f9;p=lhc%2Fweb%2Fwiklou.git * (bug 14268) SVG image sizes now extracted with proper XML parser Uses a filter with XmlTypeCheck. Yay! Now works with UTF-16 sample file uploaded on the bug. Should work with large comments etc as well. --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 359e32f761..64855a5388 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -391,7 +391,7 @@ The following extensions are migrated into MediaWiki 1.14: * (bug 13342) importScript() generates more consistent URI encoding * (bug 16577) When a blocked user tries to rollback a page, the block message is now only displayed once - +* (bug 14268) SVG image sizes now extracted with proper XML parser === API changes in 1.14 === diff --git a/includes/ImageFunctions.php b/includes/ImageFunctions.php index af05c1c9f8..8f40c81c15 100644 --- a/includes/ImageFunctions.php +++ b/includes/ImageFunctions.php @@ -18,7 +18,7 @@ function wfScaleSVGUnit( $length ) { '%' => 2.0, // Fake it! ); $matches = array(); - if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) { + if( preg_match( '/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length, $matches ) ) { $length = floatval( $matches[1] ); $unit = $matches[2]; return round( $length * $unitLength[$unit] ); @@ -28,6 +28,23 @@ function wfScaleSVGUnit( $length ) { } } +class XmlSizeFilter { + var $first = true; + var $width = 256; + var $height = 256; + function filter( $name, $attribs ) { + if( $this->first ) { + if( isset( $attribs['width'] ) ) { + $this->width = wfScaleSVGUnit( $attribs['width'] ); + } + if( isset( $attribs['height'] ) ) { + $this->height = wfScaleSVGUnit( $attribs['height'] ); + } + $this->first = false; + } + } +} + /** * Compatible with PHP getimagesize() * @todo support gzipped SVGZ @@ -38,30 +55,14 @@ function wfScaleSVGUnit( $length ) { * @return array */ function wfGetSVGsize( $filename ) { - $width = 256; - $height = 256; - - // Read a chunk of the file - $f = fopen( $filename, "rt" ); - if( !$f ) return false; - $chunk = fread( $f, 4096 ); - fclose( $f ); - - // Uber-crappy hack! Run through a real XML parser. - $matches = array(); - if( !preg_match( '/]*)\s*>/s', $chunk, $matches ) ) { - return false; + $filter = new XmlSizeFilter(); + $xml = new XmlTypeCheck( $filename, array( $filter, 'filter' ) ); + if( $xml->wellFormed ) { + return array( $filter->width, $filter->height, 'SVG', + "width=\"$filter->width\" height=\"$filter->height\"" ); } - $tag = $matches[1]; - if( preg_match( '/(?:^|\s)width\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) { - $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) ); - } - if( preg_match( '/(?:^|\s)height\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) { - $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) ); - } - - return array( $width, $height, 'SVG', - "width=\"$width\" height=\"$height\"" ); + + return false; } /**