* (bug 14268) SVG image sizes now extracted with proper XML parser
authorBrion Vibber <brion@users.mediawiki.org>
Tue, 9 Dec 2008 00:26:09 +0000 (00:26 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Tue, 9 Dec 2008 00:26:09 +0000 (00:26 +0000)
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.

RELEASE-NOTES
includes/ImageFunctions.php

index 359e32f..64855a5 100644 (file)
@@ -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 ===
 
index af05c1c..8f40c81 100644 (file)
@@ -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( '/<svg\s*([^>]*)\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;
 }
 
 /**