35 return $svg->getMetadata();
45 const NS_SVG =
'http://www.w3.org/2000/svg';
69 $this->reader =
new XMLReader();
73 if ( $size ===
false ) {
74 throw new MWException(
"Error getting filesize of SVG." );
78 $this->
debug(
"SVG is $size bytes, which is bigger than $wgSVGMetadataCutoff. Truncating." );
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%';
111 MediaWiki\suppressWarnings();
114 }
catch ( Exception
$e ) {
117 MediaWiki\restoreWarnings();
118 libxml_disable_entity_loader( $oldDisable );
121 MediaWiki\restoreWarnings();
122 libxml_disable_entity_loader( $oldDisable );
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 ) {
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
265 $sysLang = $this->reader->getAttribute(
'systemLanguage' );
266 if ( !is_null( $sysLang ) && $sysLang !==
'' ) {
268 $langList = explode(
',', $sysLang );
269 foreach ( $langList
as $langItem ) {
270 $langItem = trim( $langItem );
280 $dash = strpos( $langItem,
'-' );
283 $itemPrefix = substr( $langItem, 0, $dash );
290 switch ( $this->reader->localName ) {
298 case 'animateMotion':
300 case 'animateTransform':
301 $this->
debug(
"HOUSTON WE HAVE ANIMATION" );
302 $this->metadata[
'animated'] =
true;
306 $keepReading = $this->reader->read();
311 if ( $this->mDebug ) {
312 wfDebug(
"SVGReader: $data\n" );
328 if ( $this->reader->getAttribute(
'viewBox' ) ) {
330 $viewBox = preg_split(
'/\s+/', trim( $this->reader->getAttribute(
'viewBox' ) ) );
331 if (
count( $viewBox ) == 4 ) {
334 if ( $viewWidth > 0 && $viewHeight > 0 ) {
335 $aspect = $viewWidth / $viewHeight;
336 $defaultHeight = $defaultWidth / $aspect;
340 if ( $this->reader->getAttribute(
'width' ) ) {
341 $width = $this->
scaleSVGUnit( $this->reader->getAttribute(
'width' ), $defaultWidth );
342 $this->metadata[
'originalWidth'] = $this->reader->getAttribute(
'width' );
344 if ( $this->reader->getAttribute(
'height' ) ) {
345 $height = $this->
scaleSVGUnit( $this->reader->getAttribute(
'height' ), $defaultHeight );
346 $this->metadata[
'originalHeight'] = $this->reader->getAttribute(
'height' );
349 if ( !isset( $width ) && !isset( $height ) ) {
350 $width = $defaultWidth;
351 $height = $width / $aspect;
352 } elseif ( isset( $width ) && !isset( $height ) ) {
353 $height = $width / $aspect;
354 } elseif ( isset( $height ) && !isset( $width ) ) {
355 $width = $height * $aspect;
358 if ( $width > 0 && $height > 0 ) {
359 $this->metadata[
'width'] = intval( round( $width ) );
360 $this->metadata[
'height'] = intval( round( $height ) );
373 static $unitLength = [
385 if ( preg_match(
'/^\s*(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/', $length,
$matches ) ) {
388 if ( $unit ==
'%' ) {
389 return $length * 0.01 * $viewportSize;
391 return $length * $unitLength[$unit];
395 return floatval( $length );