36 'svg' =>
'image/svg+xml',
80 if ( is_array( $this->descriptor ) && isset( $this->descriptor[
'lang'] ) ) {
81 foreach ( array_keys( $this->descriptor[
'lang'] ) as $langList ) {
82 if ( strpos( $langList,
',' ) !==
false ) {
83 $this->descriptor[
'lang'] += array_fill_keys(
84 explode(
',', $langList ),
85 $this->descriptor[
'lang'][$langList]
87 unset( $this->descriptor[
'lang'][$langList] );
92 if ( is_array( $this->descriptor ) ) {
93 unset( $this->descriptor[
'deprecated'] );
102 $extensions = array_unique( $extensions );
103 if ( count( $extensions ) !== 1 ) {
104 throw new InvalidArgumentException(
105 "File type for different image files of '$name' not the same in module '$module'"
108 $ext = $extensions[0];
109 if ( !isset( self::$fileTypes[
$ext] ) ) {
110 throw new InvalidArgumentException(
111 "Invalid file type for image files of '$name' (valid: svg, png, gif, jpg) in module '$module'"
114 $this->extension =
$ext;
141 return array_keys( $this->variants );
153 if ( !is_array( $desc ) ) {
156 if ( isset( $desc[
'lang'] ) ) {
157 $contextLang =
$context->getLanguage();
158 if ( isset( $desc[
'lang'][$contextLang] ) ) {
159 return $this->
getLocalPath( $desc[
'lang'][$contextLang] );
162 foreach ( $fallbacks as
$lang ) {
163 if ( isset( $desc[
'lang'][
$lang] ) ) {
168 if ( isset( $desc[
$context->getDirection()] ) ) {
171 if ( isset( $desc[
'default'] ) ) {
184 return $path->getLocalPath();
187 return "{$this->basePath}/$path";
197 if ( $format ===
'rasterized' && $this->extension ===
'svg' ) {
211 return self::$fileTypes[
$ext];
227 'variant' => $variant,
231 $query[
'lang'] =
$context->getLanguage();
234 $query[
'skin'] =
$context->getSkin();
235 $rl =
$context->getResourceLoader();
251 $contents = $this->
getImageData( $context, $variant, $format );
252 return CSSMin::encodeStringAsDataURI( $contents,
$type );
272 if ( $variant ===
false ) {
275 if ( $format ===
false ) {
280 if ( !file_exists(
$path ) ) {
281 throw new MWException(
"File '$path' does not exist" );
285 return file_get_contents(
$path );
288 if ( $variant && isset( $this->variants[$variant] ) ) {
294 file_get_contents(
$path );
297 if ( $format ===
'rasterized' ) {
300 wfDebugLog(
'ResourceLoaderImage', __METHOD__ .
" failed to rasterize for $path" );
320 header(
'Content-Type: ' . $mime );
321 header(
'Content-Disposition: ' .
333 $dom =
new DOMDocument;
334 $dom->loadXML( file_get_contents( $this->
getPath( $context ) ) );
335 $root = $dom->documentElement;
337 $wrapper = $dom->createElementNS(
'http://www.w3.org/2000/svg',
'g' );
339 while ( $root->firstChild ) {
340 $node = $root->firstChild;
342 if ( !$titleNode && $node->nodeType === XML_ELEMENT_NODE && $node->tagName ===
'title' ) {
346 $wrapper->appendChild( $node );
350 $root->appendChild( $titleNode );
352 $root->appendChild( $wrapper );
353 $wrapper->setAttribute(
'fill', $variantConf[
'color'] );
354 return $dom->saveXML();
368 $dom =
new DOMDocument;
369 $dom->loadXML( $svg );
370 foreach ( $dom->getElementsByTagName(
'path' ) as $node ) {
371 $pathData = $node->getAttribute(
'd' );
374 $pathData = preg_replace(
'/(-?)(\d*\.\d+|\d+)/',
' ${1}0$2 ', $pathData );
376 $pathData = preg_replace(
'/([ -])0(\d)/',
'$1$2', $pathData );
377 $node->setAttribute(
'd', $pathData );
379 return $dom->saveXML();
418 $process = proc_open(
420 [ 0 => [
'pipe',
'r' ], 1 => [
'pipe',
'w' ] ],
424 if ( is_resource( $process ) ) {
425 fwrite( $pipes[0], $svg );
427 $png = stream_get_contents( $pipes[1] );
429 proc_close( $process );
431 return $png ?:
false;
437 $tempFilenameSvg = tempnam(
wfTempDir(),
'ResourceLoaderImage' );
438 $tempFilenamePng = tempnam(
wfTempDir(),
'ResourceLoaderImage' );
440 file_put_contents( $tempFilenameSvg, $svg );
442 $svgReader =
new SVGReader( $tempFilenameSvg );
443 $metadata = $svgReader->getMetadata();
444 if ( !isset( $metadata[
'width'] ) || !isset( $metadata[
'height'] ) ) {
445 unlink( $tempFilenameSvg );
450 $res = $handler->rasterize(
456 unlink( $tempFilenameSvg );
459 if (
$res ===
true ) {
460 $png = file_get_contents( $tempFilenamePng );
461 unlink( $tempFilenamePng );
464 return $png ?:
false;
474 return is_array( $this->descriptor ) && (
475 isset( $this->descriptor[
'ltr'] ) ||
476 isset( $this->descriptor[
'rtl'] ) ||
477 isset( $this->descriptor[
'lang'] ) );