" * @author Brion Vibber * @copyright Copyright © 2010-2010 Brion Vibber, Derk-Jan Hartman * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License */ /** * @ingroup Media */ class SVGMetadataExtractor { static function getMetadata( $filename ) { $svg = new SVGReader( $filename ); return $svg->getMetadata(); } } /** * @ingroup Media */ class SVGReader { const DEFAULT_WIDTH = 512; const DEFAULT_HEIGHT = 512; const NS_SVG = 'http://www.w3.org/2000/svg'; private $reader = null; private $mDebug = false; private $metadata = Array(); /** * Constructor * * Creates an SVGReader drawing from the source provided * @param $source String: URI from which to read */ function __construct( $source ) { global $wgSVGMetadataCutoff; $this->reader = new XMLReader(); // Don't use $file->getSize() since file object passed to SVGHandler::getMetadata is bogus. $size = filesize( $source ); if ( $size === false ) { throw new MWException( "Error getting filesize of SVG." ); } if ( $size > $wgSVGMetadataCutoff ) { $this->debug( "SVG is $size bytes, which is bigger than $wgSVGMetadataCutoff. Truncating." ); $contents = file_get_contents( $source, false, null, -1, $wgSVGMetadataCutoff ); if ($contents === false) { throw new MWException( 'Error reading SVG file.' ); } $this->reader->XML( $contents, null, LIBXML_NOERROR | LIBXML_NOWARNING ); } else { $this->reader->open( $source, null, LIBXML_NOERROR | LIBXML_NOWARNING ); } // Expand entities, since Adobe Illustrator uses them for xmlns // attributes (bug 31719). Note that libxml2 has some protection // against large recursive entity expansions so this is not as // insecure as it might appear to be. $this->reader->setParserProperty( XMLReader::SUBST_ENTITIES, true ); $this->metadata['width'] = self::DEFAULT_WIDTH; $this->metadata['height'] = self::DEFAULT_HEIGHT; // The size in the units specified by the SVG file // (for the metadata box) // Per the SVG spec, if unspecified, default to '100%' $this->metadata['originalWidth'] = '100%'; $this->metadata['originalHeight'] = '100%'; // Because we cut off the end of the svg making an invalid one. Complicated // try catch thing to make sure warnings get restored. Seems like there should // be a better way. wfSuppressWarnings(); try { $this->read(); } catch( Exception $e ) { // Note, if this happens, the width/height will be taken to be 0x0. // Should we consider it the default 512x512 instead? wfRestoreWarnings(); throw $e; } wfRestoreWarnings(); } /** * @return Array with the known metadata */ public function getMetadata() { return $this->metadata; } /** * Read the SVG * @return bool */ public function read() { $keepReading = $this->reader->read(); /* Skip until first element */ while( $keepReading && $this->reader->nodeType != XmlReader::ELEMENT ) { $keepReading = $this->reader->read(); } if ( $this->reader->localName != 'svg' || $this->reader->namespaceURI != self::NS_SVG ) { throw new MWException( "Expected tag, got ". $this->reader->localName . " in NS " . $this->reader->namespaceURI ); } $this->debug( " tag is correct." ); $this->handleSVGAttribs(); $exitDepth = $this->reader->depth; $keepReading = $this->reader->read(); while ( $keepReading ) { $tag = $this->reader->localName; $type = $this->reader->nodeType; $isSVG = ($this->reader->namespaceURI == self::NS_SVG); $this->debug( "$tag" ); if ( $isSVG && $tag == 'svg' && $type == XmlReader::END_ELEMENT && $this->reader->depth <= $exitDepth ) { break; } elseif ( $isSVG && $tag == 'title' ) { $this->readField( $tag, 'title' ); } elseif ( $isSVG && $tag == 'desc' ) { $this->readField( $tag, 'description' ); } elseif ( $isSVG && $tag == 'metadata' && $type == XmlReader::ELEMENT ) { $this->readXml( $tag, 'metadata' ); } elseif ( $isSVG && $tag == 'script' ) { // We normally do not allow scripted svgs. // However its possible to configure MW to let them // in, and such files should be considered animated. $this->metadata['animated'] = true; } elseif ( $tag !== '#text' ) { $this->debug( "Unhandled top-level XML tag $tag" ); if ( !isset( $this->metadata['animated'] ) ) { // Recurse into children of current tag, looking for animation. $this->animateFilter( $tag ); } } // Goto next element, which is sibling of current (Skip children). $keepReading = $this->reader->next(); } $this->reader->close(); return true; } /** * Read a textelement from an element * * @param String $name of the element that we are reading from * @param String $metafield that we will fill with the result */ private function readField( $name, $metafield=null ) { $this->debug ( "Read field $metafield" ); if( !$metafield || $this->reader->nodeType != XmlReader::ELEMENT ) { return; } $keepReading = $this->reader->read(); while( $keepReading ) { if( $this->reader->localName == $name && $this->reader->namespaceURI == self::NS_SVG && $this->reader->nodeType == XmlReader::END_ELEMENT ) { break; } elseif( $this->reader->nodeType == XmlReader::TEXT ){ $this->metadata[$metafield] = trim( $this->reader->value ); } $keepReading = $this->reader->read(); } } /** * Read an XML snippet from an element * * @param String $metafield that we will fill with the result */ private function readXml( $metafield=null ) { $this->debug ( "Read top level metadata" ); if( !$metafield || $this->reader->nodeType != XmlReader::ELEMENT ) { return; } // TODO: find and store type of xml snippet. metadata['metadataType'] = "rdf" if( method_exists( $this->reader, 'readInnerXML' ) ) { $this->metadata[$metafield] = trim( $this->reader->readInnerXML() ); } else { throw new MWException( "The PHP XMLReader extension does not come with readInnerXML() method. Your libxml is probably out of date (need 2.6.20 or later)." ); } $this->reader->next(); } /** * Filter all children, looking for animate elements * * @param String $name of the element that we are reading from */ private function animateFilter( $name ) { $this->debug ( "animate filter for tag $name" ); if( $this->reader->nodeType != XmlReader::ELEMENT ) { return; } if ( $this->reader->isEmptyElement ) { return; } $exitDepth = $this->reader->depth; $keepReading = $this->reader->read(); while( $keepReading ) { if( $this->reader->localName == $name && $this->reader->depth <= $exitDepth && $this->reader->nodeType == XmlReader::END_ELEMENT ) { break; } elseif ( $this->reader->namespaceURI == self::NS_SVG && $this->reader->nodeType == XmlReader::ELEMENT ) { switch( $this->reader->localName ) { case 'script': // Normally we disallow files with //