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();
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 ) {
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 );
269 if ( Language::isWellFormedLanguageTag( $langItem ) ) {
278 $dash = strpos( $langItem,
'-' );
281 $itemPrefix = substr( $langItem, 0, $dash );
282 if ( Language::isWellFormedLanguageTag( $itemPrefix ) ) {
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 );
$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.
when a variable name is used in a it is silently declared as a new local masking the global
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
Allows to change the fields on the form that will be generated $name
returning false will NOT prevent logging $e
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php