35 return $svg->getMetadata();
45 const NS_SVG =
'http://www.w3.org/2000/svg';
67 $this->reader =
new XMLReader();
71 if ( $size ===
false ) {
72 throw new MWException(
"Error getting filesize of SVG." );
76 $this->
debug(
"SVG is $size bytes, which is bigger than $wgSVGMetadataCutoff. Truncating." );
78 if ( $contents ===
false ) {
81 $this->reader->XML( $contents,
null, LIBXML_NOERROR | LIBXML_NOWARNING );
83 $this->reader->open(
$source,
null, LIBXML_NOERROR | LIBXML_NOWARNING );
94 $oldDisable = libxml_disable_entity_loader(
true );
95 $this->reader->setParserProperty( XMLReader::SUBST_ENTITIES,
true );
103 $this->metadata[
'originalWidth'] =
'100%';
104 $this->metadata[
'originalHeight'] =
'100%';
109 MediaWiki\suppressWarnings();
112 }
catch ( Exception
$e ) {
115 MediaWiki\restoreWarnings();
116 libxml_disable_entity_loader( $oldDisable );
119 MediaWiki\restoreWarnings();
120 libxml_disable_entity_loader( $oldDisable );
136 $keepReading = $this->reader->read();
139 while ( $keepReading && $this->reader->nodeType != XMLReader::ELEMENT ) {
140 $keepReading = $this->reader->read();
143 if ( $this->reader->localName !=
'svg' || $this->reader->namespaceURI != self::NS_SVG ) {
144 throw new MWException(
"Expected <svg> tag, got " .
145 $this->reader->localName .
" in NS " . $this->reader->namespaceURI );
147 $this->
debug(
"<svg> tag is correct." );
150 $exitDepth = $this->reader->depth;
151 $keepReading = $this->reader->read();
152 while ( $keepReading ) {
153 $tag = $this->reader->localName;
154 $type = $this->reader->nodeType;
155 $isSVG = ( $this->reader->namespaceURI ==
self::NS_SVG );
157 $this->
debug(
"$tag" );
159 if ( $isSVG && $tag ==
'svg' &&
$type == XMLReader::END_ELEMENT
160 && $this->reader->depth <= $exitDepth
163 } elseif ( $isSVG && $tag ==
'title' ) {
165 } elseif ( $isSVG && $tag ==
'desc' ) {
167 } elseif ( $isSVG && $tag ==
'metadata' &&
$type == XMLReader::ELEMENT ) {
168 $this->
readXml( $tag,
'metadata' );
169 } elseif ( $isSVG && $tag ==
'script' ) {
173 $this->metadata[
'animated'] =
true;
174 } elseif ( $tag !==
'#text' ) {
175 $this->
debug(
"Unhandled top-level XML tag $tag" );
182 $keepReading = $this->reader->next();
185 $this->reader->close();
199 $this->
debug(
"Read field $metafield" );
200 if ( !$metafield || $this->reader->nodeType != XMLReader::ELEMENT ) {
203 $keepReading = $this->reader->read();
204 while ( $keepReading ) {
205 if ( $this->reader->localName ==
$name
206 && $this->reader->namespaceURI == self::NS_SVG
207 && $this->reader->nodeType == XMLReader::END_ELEMENT
210 } elseif ( $this->reader->nodeType == XMLReader::TEXT ) {
211 $this->metadata[$metafield] = trim( $this->reader->value );
213 $keepReading = $this->reader->read();
223 private function readXml( $metafield =
null ) {
224 $this->
debug(
"Read top level metadata" );
225 if ( !$metafield || $this->reader->nodeType != XMLReader::ELEMENT ) {
229 if ( method_exists( $this->reader,
'readInnerXML' ) ) {
230 $this->metadata[$metafield] = trim( $this->reader->readInnerXml() );
232 throw new MWException(
"The PHP XMLReader extension does not come " .
233 "with readInnerXML() method. Your libxml is probably out of " .
234 "date (need 2.6.20 or later)." );
236 $this->reader->next();
246 $this->
debug(
"animate filter for tag $name" );
247 if ( $this->reader->nodeType != XMLReader::ELEMENT ) {
250 if ( $this->reader->isEmptyElement ) {
253 $exitDepth = $this->reader->depth;
254 $keepReading = $this->reader->read();
255 while ( $keepReading ) {
256 if ( $this->reader->localName ==
$name && $this->reader->depth <= $exitDepth
257 && $this->reader->nodeType == XMLReader::END_ELEMENT
260 } elseif ( $this->reader->namespaceURI == self::NS_SVG
261 && $this->reader->nodeType == XMLReader::ELEMENT
263 $sysLang = $this->reader->getAttribute(
'systemLanguage' );
264 if ( !is_null( $sysLang ) && $sysLang !==
'' ) {
266 $langList = explode(
',', $sysLang );
267 foreach ( $langList
as $langItem ) {
268 $langItem = trim( $langItem );
278 $dash = strpos( $langItem,
'-' );
281 $itemPrefix = substr( $langItem, 0, $dash );
288 switch ( $this->reader->localName ) {
296 case 'animateMotion':
298 case 'animateTransform':
299 $this->
debug(
"HOUSTON WE HAVE ANIMATION" );
300 $this->metadata[
'animated'] =
true;
304 $keepReading = $this->reader->read();
309 if ( $this->mDebug ) {
310 wfDebug(
"SVGReader: $data\n" );
326 if ( $this->reader->getAttribute(
'viewBox' ) ) {
328 $viewBox = preg_split(
'/\s+/', trim( $this->reader->getAttribute(
'viewBox' ) ) );
329 if (
count( $viewBox ) == 4 ) {
332 if ( $viewWidth > 0 && $viewHeight > 0 ) {
333 $aspect = $viewWidth / $viewHeight;
334 $defaultHeight = $defaultWidth / $aspect;
338 if ( $this->reader->getAttribute(
'width' ) ) {
339 $width = $this->
scaleSVGUnit( $this->reader->getAttribute(
'width' ), $defaultWidth );
340 $this->metadata[
'originalWidth'] = $this->reader->getAttribute(
'width' );
342 if ( $this->reader->getAttribute(
'height' ) ) {
343 $height = $this->
scaleSVGUnit( $this->reader->getAttribute(
'height' ), $defaultHeight );
344 $this->metadata[
'originalHeight'] = $this->reader->getAttribute(
'height' );
347 if ( !isset( $width ) && !isset( $height ) ) {
348 $width = $defaultWidth;
349 $height = $width / $aspect;
350 } elseif ( isset( $width ) && !isset( $height ) ) {
351 $height = $width / $aspect;
352 } elseif ( isset( $height ) && !isset( $width ) ) {
353 $width = $height * $aspect;
356 if ( $width > 0 && $height > 0 ) {
357 $this->metadata[
'width'] = intval( round( $width ) );
358 $this->metadata[
'height'] = intval( round( $height ) );
371 static $unitLength = [
383 if ( preg_match(
'/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length,
$matches ) ) {
386 if ( $unit ==
'%' ) {
387 return $length * 0.01 * $viewportSize;
389 return $length * $unitLength[$unit];
393 return floatval( $length );