34 const NS_SVG =
'http://www.w3.org/2000/svg';
56 $this->reader =
new XMLReader();
60 if ( $size ===
false ) {
61 throw new MWException(
"Error getting filesize of SVG." );
65 $this->
debug(
"SVG is $size bytes, which is bigger than $wgSVGMetadataCutoff. Truncating." );
67 if ( $contents ===
false ) {
70 $this->reader->XML( $contents,
null, LIBXML_NOERROR | LIBXML_NOWARNING );
72 $this->reader->open(
$source,
null, LIBXML_NOERROR | LIBXML_NOWARNING );
83 $oldDisable = libxml_disable_entity_loader(
true );
84 $this->reader->setParserProperty( XMLReader::SUBST_ENTITIES,
true );
92 $this->metadata[
'originalWidth'] =
'100%';
93 $this->metadata[
'originalHeight'] =
'100%';
98 Wikimedia\suppressWarnings();
101 }
catch ( Exception $e ) {
104 Wikimedia\restoreWarnings();
105 libxml_disable_entity_loader( $oldDisable );
108 Wikimedia\restoreWarnings();
109 libxml_disable_entity_loader( $oldDisable );
125 $keepReading = $this->reader->read();
128 while ( $keepReading && $this->reader->nodeType != XMLReader::ELEMENT ) {
129 $keepReading = $this->reader->read();
132 if ( $this->reader->localName !=
'svg' || $this->reader->namespaceURI != self::NS_SVG ) {
133 throw new MWException(
"Expected <svg> tag, got " .
134 $this->reader->localName .
" in NS " . $this->reader->namespaceURI );
136 $this->
debug(
"<svg> tag is correct." );
139 $exitDepth = $this->reader->depth;
140 $keepReading = $this->reader->read();
141 while ( $keepReading ) {
142 $tag = $this->reader->localName;
143 $type = $this->reader->nodeType;
144 $isSVG = ( $this->reader->namespaceURI ==
self::NS_SVG );
146 $this->
debug(
"$tag" );
148 if ( $isSVG && $tag ==
'svg' &&
$type == XMLReader::END_ELEMENT
149 && $this->reader->depth <= $exitDepth
152 } elseif ( $isSVG && $tag ==
'title' ) {
154 } elseif ( $isSVG && $tag ==
'desc' ) {
156 } elseif ( $isSVG && $tag ==
'metadata' &&
$type == XMLReader::ELEMENT ) {
158 } elseif ( $isSVG && $tag ==
'script' ) {
162 $this->metadata[
'animated'] =
true;
163 } elseif ( $tag !==
'#text' ) {
164 $this->
debug(
"Unhandled top-level XML tag $tag" );
171 $keepReading = $this->reader->next();
174 $this->reader->close();
187 private function readField( $name, $metafield =
null ) {
188 $this->
debug(
"Read field $metafield" );
189 if ( !$metafield || $this->reader->nodeType != XMLReader::ELEMENT ) {
192 $keepReading = $this->reader->read();
193 while ( $keepReading ) {
194 if ( $this->reader->localName == $name
195 && $this->reader->namespaceURI == self::NS_SVG
196 && $this->reader->nodeType == XMLReader::END_ELEMENT
199 } elseif ( $this->reader->nodeType == XMLReader::TEXT ) {
200 $this->metadata[$metafield] = trim( $this->reader->value );
202 $keepReading = $this->reader->read();
212 private function readXml( $metafield =
null ) {
213 $this->
debug(
"Read top level metadata" );
214 if ( !$metafield || $this->reader->nodeType != XMLReader::ELEMENT ) {
218 $this->metadata[$metafield] = trim( $this->reader->readInnerXml() );
220 $this->reader->next();
230 $this->
debug(
"animate filter for tag $name" );
231 if ( $this->reader->nodeType != XMLReader::ELEMENT ) {
234 if ( $this->reader->isEmptyElement ) {
237 $exitDepth = $this->reader->depth;
238 $keepReading = $this->reader->read();
239 while ( $keepReading ) {
240 if ( $this->reader->localName == $name && $this->reader->depth <= $exitDepth
241 && $this->reader->nodeType == XMLReader::END_ELEMENT
244 } elseif ( $this->reader->namespaceURI == self::NS_SVG
245 && $this->reader->nodeType == XMLReader::ELEMENT
247 $sysLang = $this->reader->getAttribute(
'systemLanguage' );
248 if ( !is_null( $sysLang ) && $sysLang !==
'' ) {
250 $langList = explode(
',', $sysLang );
251 foreach ( $langList as $langItem ) {
252 $langItem = trim( $langItem );
262 $dash = strpos( $langItem,
'-' );
265 $itemPrefix = substr( $langItem, 0, $dash );
272 switch ( $this->reader->localName ) {
280 case 'animateMotion':
282 case 'animateTransform':
283 $this->
debug(
"HOUSTON WE HAVE ANIMATION" );
284 $this->metadata[
'animated'] =
true;
288 $keepReading = $this->reader->read();
293 if ( $this->mDebug ) {
294 wfDebug(
"SVGReader: $data\n" );
310 if ( $this->reader->getAttribute(
'viewBox' ) ) {
312 $viewBox = preg_split(
'/\s*[\s,]\s*/', trim( $this->reader->getAttribute(
'viewBox' ) ) );
313 if ( count( $viewBox ) == 4 ) {
316 if ( $viewWidth > 0 && $viewHeight > 0 ) {
317 $aspect = $viewWidth / $viewHeight;
318 $defaultHeight = $defaultWidth / $aspect;
322 if ( $this->reader->getAttribute(
'width' ) ) {
323 $width = $this->
scaleSVGUnit( $this->reader->getAttribute(
'width' ), $defaultWidth );
324 $this->metadata[
'originalWidth'] = $this->reader->getAttribute(
'width' );
326 if ( $this->reader->getAttribute(
'height' ) ) {
327 $height = $this->
scaleSVGUnit( $this->reader->getAttribute(
'height' ), $defaultHeight );
328 $this->metadata[
'originalHeight'] = $this->reader->getAttribute(
'height' );
331 if ( !isset( $width ) && !isset( $height ) ) {
332 $width = $defaultWidth;
333 $height = $width / $aspect;
334 } elseif ( isset( $width ) && !isset( $height ) ) {
335 $height = $width / $aspect;
336 } elseif ( isset( $height ) && !isset( $width ) ) {
337 $width = $height * $aspect;
340 if ( $width > 0 && $height > 0 ) {
341 $this->metadata[
'width'] = intval( round( $width ) );
342 $this->metadata[
'height'] = intval( round( $height ) );
355 static $unitLength = [
368 '/^\s*([-+]?\d*(?:\.\d+|\d+)(?:[Ee][-+]?\d+)?)\s*(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/',
374 if ( $unit ==
'%' ) {
375 return $length * 0.01 * $viewportSize;
377 return $length * $unitLength[$unit];
381 return floatval( $length );