35 return $svg->getMetadata();
45 const NS_SVG =
'http://www.w3.org/2000/svg';
68 global $wgSVGMetadataCutoff;
69 $this->reader =
new XMLReader();
73 if (
$size ===
false ) {
74 throw new MWException(
"Error getting filesize of SVG." );
77 if (
$size > $wgSVGMetadataCutoff ) {
78 $this->
debug(
"SVG is $size bytes, which is bigger than $wgSVGMetadataCutoff. Truncating." );
79 $contents = file_get_contents(
$source,
false,
null, -1, $wgSVGMetadataCutoff );
80 if ( $contents ===
false ) {
83 $this->reader->XML( $contents,
null, LIBXML_NOERROR | LIBXML_NOWARNING );
85 $this->reader->open(
$source,
null, LIBXML_NOERROR | LIBXML_NOWARNING );
96 $oldDisable = libxml_disable_entity_loader(
true );
97 $this->reader->setParserProperty( XMLReader::SUBST_ENTITIES,
true );
105 $this->metadata[
'originalWidth'] =
'100%';
106 $this->metadata[
'originalHeight'] =
'100%';
114 }
catch ( Exception
$e ) {
118 libxml_disable_entity_loader( $oldDisable );
122 libxml_disable_entity_loader( $oldDisable );
137 protected function read() {
138 $keepReading = $this->reader->read();
141 while ( $keepReading && $this->reader->nodeType != XmlReader::ELEMENT ) {
142 $keepReading = $this->reader->read();
145 if ( $this->reader->localName !=
'svg' || $this->reader->namespaceURI != self::NS_SVG ) {
146 throw new MWException(
"Expected <svg> tag, got " .
147 $this->reader->localName .
" in NS " . $this->reader->namespaceURI );
149 $this->
debug(
"<svg> tag is correct." );
152 $exitDepth = $this->reader->depth;
153 $keepReading = $this->reader->read();
154 while ( $keepReading ) {
155 $tag = $this->reader->localName;
156 $type = $this->reader->nodeType;
157 $isSVG = ( $this->reader->namespaceURI ==
self::NS_SVG );
159 $this->
debug(
"$tag" );
161 if ( $isSVG && $tag ==
'svg' &&
$type == XmlReader::END_ELEMENT
162 && $this->reader->depth <= $exitDepth
165 } elseif ( $isSVG && $tag ==
'title' ) {
167 } elseif ( $isSVG && $tag ==
'desc' ) {
169 } elseif ( $isSVG && $tag ==
'metadata' &&
$type == XmlReader::ELEMENT ) {
170 $this->
readXml( $tag,
'metadata' );
171 } elseif ( $isSVG && $tag ==
'script' ) {
175 $this->metadata[
'animated'] =
true;
176 } elseif ( $tag !==
'#text' ) {
177 $this->
debug(
"Unhandled top-level XML tag $tag" );
184 $keepReading = $this->reader->next();
187 $this->reader->close();
201 $this->
debug(
"Read field $metafield" );
202 if ( !$metafield || $this->reader->nodeType != XmlReader::ELEMENT ) {
205 $keepReading = $this->reader->read();
206 while ( $keepReading ) {
207 if ( $this->reader->localName ==
$name
208 && $this->reader->namespaceURI == self::NS_SVG
209 && $this->reader->nodeType == XmlReader::END_ELEMENT
212 } elseif ( $this->reader->nodeType == XmlReader::TEXT ) {
213 $this->metadata[$metafield] = trim( $this->reader->value );
215 $keepReading = $this->reader->read();
225 private function readXml( $metafield =
null ) {
226 $this->
debug(
"Read top level metadata" );
227 if ( !$metafield || $this->reader->nodeType != XmlReader::ELEMENT ) {
231 if ( method_exists( $this->reader,
'readInnerXML' ) ) {
232 $this->metadata[$metafield] = trim( $this->reader->readInnerXML() );
234 throw new MWException(
"The PHP XMLReader extension does not come " .
235 "with readInnerXML() method. Your libxml is probably out of " .
236 "date (need 2.6.20 or later)." );
238 $this->reader->next();
248 $this->
debug(
"animate filter for tag $name" );
249 if ( $this->reader->nodeType != XmlReader::ELEMENT ) {
252 if ( $this->reader->isEmptyElement ) {
255 $exitDepth = $this->reader->depth;
256 $keepReading = $this->reader->read();
257 while ( $keepReading ) {
258 if ( $this->reader->localName ==
$name && $this->reader->depth <= $exitDepth
259 && $this->reader->nodeType == XmlReader::END_ELEMENT
262 } elseif ( $this->reader->namespaceURI == self::NS_SVG
263 && $this->reader->nodeType == XmlReader::ELEMENT
266 $sysLang = $this->reader->getAttribute(
'systemLanguage' );
267 if ( !is_null( $sysLang ) && $sysLang !==
'' ) {
269 $langList = explode(
',', $sysLang );
270 foreach ( $langList
as $langItem ) {
271 $langItem = trim( $langItem );
281 $dash = strpos( $langItem,
'-' );
284 $itemPrefix = substr( $langItem, 0, $dash );
291 switch ( $this->reader->localName ) {
299 case 'animateMotion':
301 case 'animateTransform':
302 $this->
debug(
"HOUSTON WE HAVE ANIMATION" );
303 $this->metadata[
'animated'] =
true;
307 $keepReading = $this->reader->read();
313 $this->
debug(
"FAILURE: $err" );
317 private function debug( $data ) {
318 if ( $this->mDebug ) {
319 wfDebug(
"SVGReader: $data\n" );
324 private function warn( $data ) {
325 wfDebug(
"SVGReader: $data\n" );
329 private function notice( $data ) {
330 wfDebug(
"SVGReader WARN: $data\n" );
345 if ( $this->reader->getAttribute(
'viewBox' ) ) {
347 $viewBox = preg_split(
'/\s+/', trim( $this->reader->getAttribute(
'viewBox' ) ) );
348 if ( count( $viewBox ) == 4 ) {
351 if ( $viewWidth > 0 && $viewHeight > 0 ) {
352 $aspect = $viewWidth / $viewHeight;
353 $defaultHeight = $defaultWidth / $aspect;
357 if ( $this->reader->getAttribute(
'width' ) ) {
358 $width = $this->
scaleSVGUnit( $this->reader->getAttribute(
'width' ), $defaultWidth );
359 $this->metadata[
'originalWidth'] = $this->reader->getAttribute(
'width' );
361 if ( $this->reader->getAttribute(
'height' ) ) {
362 $height = $this->
scaleSVGUnit( $this->reader->getAttribute(
'height' ), $defaultHeight );
363 $this->metadata[
'originalHeight'] = $this->reader->getAttribute(
'height' );
366 if ( !isset( $width ) && !isset( $height ) ) {
367 $width = $defaultWidth;
368 $height = $width / $aspect;
369 } elseif ( isset( $width ) && !isset( $height ) ) {
370 $height = $width / $aspect;
371 } elseif ( isset( $height ) && !isset( $width ) ) {
372 $width = $height * $aspect;
375 if ( $width > 0 && $height > 0 ) {
376 $this->metadata[
'width'] = intval( round( $width ) );
377 $this->metadata[
'height'] = intval( round( $height ) );
389 static function scaleSVGUnit( $length, $viewportSize = 512 ) {
390 static $unitLength =
array(
402 if ( preg_match(
'/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length,
$matches ) ) {
405 if ( $unit ==
'%' ) {
406 return $length * 0.01 * $viewportSize;
408 return $length * $unitLength[$unit];
412 return floatval( $length );