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 );
95 $this->reader->setParserProperty( XMLReader::SUBST_ENTITIES,
true );
103 $this->metadata[
'originalWidth'] =
'100%';
104 $this->metadata[
'originalHeight'] =
'100%';
112 }
catch ( Exception
$e ) {
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 ) {
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();
198 private function readField( $name, $metafield =
null ) {
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 ) {
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 $this->metadata[
$metafield] =
trim( $this->reader->readInnerXml() );
231 $this->reader->next();
241 $this->
debug(
"animate filter for tag $name" );
242 if ( $this->reader->nodeType != XMLReader::ELEMENT ) {
245 if ( $this->reader->isEmptyElement ) {
248 $exitDepth = $this->reader->depth;
249 $keepReading = $this->reader->read();
250 while ( $keepReading ) {
251 if ( $this->reader->localName == $name && $this->reader->depth <= $exitDepth
252 && $this->reader->nodeType == XMLReader::END_ELEMENT
255 }
elseif ( $this->reader->namespaceURI == self::NS_SVG
256 && $this->reader->nodeType == XMLReader::ELEMENT
258 $sysLang = $this->reader->getAttribute(
'systemLanguage' );
259 if ( !
is_null( $sysLang ) && $sysLang !==
'' ) {
261 $langList = explode(
',', $sysLang );
262 foreach ( $langList as $langItem ) {
263 $langItem =
trim( $langItem );
264 if ( Language::isWellFormedLanguageTag( $langItem ) ) {
273 $dash =
strpos( $langItem,
'-' );
276 $itemPrefix =
substr( $langItem, 0, $dash );
277 if ( Language::isWellFormedLanguageTag( $itemPrefix ) ) {
283 switch ( $this->reader->localName ) {
291 case 'animateMotion':
293 case 'animateTransform':
294 $this->
debug(
"HOUSTON WE HAVE ANIMATION" );
295 $this->metadata[
'animated'] =
true;
299 $keepReading = $this->reader->read();
304 if ( $this->mDebug ) {
305 wfDebug(
"SVGReader: $data\n" );
321 if ( $this->reader->getAttribute(
'viewBox' ) ) {
323 $viewBox =
preg_split(
'/\s*[\s,]\s*/',
trim( $this->reader->getAttribute(
'viewBox' ) ) );
324 if ( count( $viewBox ) == 4 ) {
327 if ( $viewWidth > 0 && $viewHeight > 0 ) {
329 $defaultHeight = $defaultWidth /
$aspect;
333 if ( $this->reader->getAttribute(
'width' ) ) {
334 $width = $this->
scaleSVGUnit( $this->reader->getAttribute(
'width' ), $defaultWidth );
335 $this->metadata[
'originalWidth'] = $this->reader->getAttribute(
'width' );
337 if ( $this->reader->getAttribute(
'height' ) ) {
338 $height = $this->
scaleSVGUnit( $this->reader->getAttribute(
'height' ), $defaultHeight );
339 $this->metadata[
'originalHeight'] = $this->reader->getAttribute(
'height' );
342 if ( !
isset( $width ) && !
isset( $height ) ) {
351 if ( $width > 0 && $height > 0 ) {
352 $this->metadata[
'width'] =
intval(
round( $width ) );
353 $this->metadata[
'height'] =
intval(
round( $height ) );
366 static $unitLength = [
379 '/^\s*([-+]?\d*(?:\.\d+|\d+)(?:[Ee][-+]?\d+)?)\s*(em|ex|px|pt|pc|cm|mm|in|%|)\s*$/',
385 if ( $unit ==
'%' ) {
388 return $length * $unitLength[
$unit];
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$wgSVGMetadataCutoff
Don't read SVG metadata beyond this point.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
static scaleSVGUnit( $length, $viewportSize=512)
Return a rounded pixel equivalent for a labeled CSS/SVG length.
handleSVGAttribs()
Parse the attributes of an SVG element.
readXml( $metafield=null)
Read an XML snippet from an element.
animateFilterAndLang( $name)
Filter all children, looking for animated elements.
readField( $name, $metafield=null)
Read a textelement from an element.
__construct( $source)
Creates an SVGReader drawing from the source provided.
returning false will NOT prevent logging $e
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))