2014-11-11 19:50:44 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
namespace MediaWiki\ResourceLoader;
|
|
|
|
|
|
|
|
|
|
use DOMDocument;
|
|
|
|
|
use FileBackend;
|
|
|
|
|
use InvalidArgumentException;
|
2019-10-18 23:48:10 +00:00
|
|
|
use MediaWiki\Languages\LanguageFallback;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-04-07 11:36:22 +00:00
|
|
|
use MediaWiki\Shell\Shell;
|
2022-06-21 23:25:17 +00:00
|
|
|
use RuntimeException;
|
2022-05-06 09:09:56 +00:00
|
|
|
use SvgHandler;
|
|
|
|
|
use SVGReader;
|
2021-02-18 04:38:29 +00:00
|
|
|
use Wikimedia\Minify\CSSMin;
|
2019-04-07 11:36:22 +00:00
|
|
|
|
2014-11-11 19:50:44 +00:00
|
|
|
/**
|
2022-05-06 09:09:56 +00:00
|
|
|
* Class encapsulating an image used in an ImageModule.
|
2014-11-11 19:50:44 +00:00
|
|
|
*
|
2019-09-14 04:32:54 +00:00
|
|
|
* @ingroup ResourceLoader
|
2014-11-11 19:50:44 +00:00
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
class Image {
|
2014-11-11 19:50:44 +00:00
|
|
|
/**
|
|
|
|
|
* Map of allowed file extensions to their MIME types.
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2020-11-25 04:14:36 +00:00
|
|
|
private const FILE_TYPES = [
|
2014-11-11 19:50:44 +00:00
|
|
|
'svg' => 'image/svg+xml',
|
|
|
|
|
'png' => 'image/png',
|
|
|
|
|
'gif' => 'image/gif',
|
|
|
|
|
'jpg' => 'image/jpg',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-11-11 19:50:44 +00:00
|
|
|
|
2019-04-15 16:45:26 +00:00
|
|
|
/** @var string */
|
|
|
|
|
private $name;
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $module;
|
|
|
|
|
/** @var string|array */
|
|
|
|
|
private $descriptor;
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $basePath;
|
|
|
|
|
/** @var array */
|
|
|
|
|
private $variants;
|
|
|
|
|
/** @var string|null */
|
|
|
|
|
private $defaultColor;
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $extension;
|
|
|
|
|
|
2014-11-11 19:50:44 +00:00
|
|
|
/**
|
2022-05-06 09:09:56 +00:00
|
|
|
* @param string $name Self-name of the image as known to ImageModule.
|
resourceloader: Add version to ResourceLoaderImage urls for long-cache
The code previously here did not work well as it merely forwarded the
hash from the current web request. This had numerous issues:
1. It was often null because requests for stylesheets do not cary
a version hash.
2. When requested by JavaScript, the version hash would be a
combination-hash of many unrelated modules, thus when requested as
part of different batches, it would produce different urls which
is not ideal.
The impact of this is minimal currently because we basically never use
these urls, as SVGs are almost always embedded instead of ref'ed by url.
PNG urls are only generated for non-JS modules and then only used in older
browsers not supporting SVG. And, even after all that, for the edge case
of an SVG being ref'ed by url, they would be stored in LocalStorage by
mw.loader with the name+version of the module the image belonged to, not
the version hash of the batch request it came with.
Which means that, yes, localstorage key for "somemodule+someversion" would
have different values for different users, based on which batch the value
came with, because the image urls were using the version hash of the batch
request from ResourceLoaderContext. This is weird, but didn't cause bugs
or inefficiencies because the user would never be exposed to the other
possible urls for that image because we always check LocalStorage first.
It did cause fragmentation server-side in Varnish, though.
This is all fixed now by always including a version, and setting it to
the version of the module. This means there is no more Varnish fragmentation
for these. And it means that browsers are now allowed to cache the images
served from these urls for 30+ days (immutable) instead of only 5min,
which is what happened when they didn't have a version parameter (or set to
null).
Bug: T233343
Change-Id: I4af7fda03698ed4c288d154e7787fb2f3cbbe6c5
2019-09-26 16:26:52 +00:00
|
|
|
* @param string $module Self-name of the module containing this image.
|
|
|
|
|
* Used to find the image in the registry e.g. through a load.php url.
|
2014-11-11 19:50:44 +00:00
|
|
|
* @param string|array $descriptor Path to image file, or array structure containing paths
|
|
|
|
|
* @param string $basePath Directory to which paths in descriptor refer
|
|
|
|
|
* @param array $variants
|
2018-06-25 22:55:19 +00:00
|
|
|
* @param string|null $defaultColor of the base variant
|
2014-11-11 19:50:44 +00:00
|
|
|
*/
|
2019-10-05 07:38:41 +00:00
|
|
|
public function __construct( $name, $module, $descriptor, $basePath, array $variants,
|
2018-06-25 22:55:19 +00:00
|
|
|
$defaultColor = null
|
|
|
|
|
) {
|
2014-11-11 19:50:44 +00:00
|
|
|
$this->name = $name;
|
|
|
|
|
$this->module = $module;
|
|
|
|
|
$this->descriptor = $descriptor;
|
|
|
|
|
$this->basePath = $basePath;
|
|
|
|
|
$this->variants = $variants;
|
2018-06-25 22:55:19 +00:00
|
|
|
$this->defaultColor = $defaultColor;
|
2014-11-11 19:50:44 +00:00
|
|
|
|
2015-04-02 17:54:07 +00:00
|
|
|
// Expand shorthands:
|
2016-08-13 01:10:40 +00:00
|
|
|
// [ "en,de,fr" => "foo.svg" ]
|
|
|
|
|
// → [ "en" => "foo.svg", "de" => "foo.svg", "fr" => "foo.svg" ]
|
2015-04-02 17:54:07 +00:00
|
|
|
if ( is_array( $this->descriptor ) && isset( $this->descriptor['lang'] ) ) {
|
|
|
|
|
foreach ( array_keys( $this->descriptor['lang'] ) as $langList ) {
|
|
|
|
|
if ( strpos( $langList, ',' ) !== false ) {
|
|
|
|
|
$this->descriptor['lang'] += array_fill_keys(
|
|
|
|
|
explode( ',', $langList ),
|
2015-06-17 20:01:00 +00:00
|
|
|
$this->descriptor['lang'][$langList]
|
2015-04-02 17:54:07 +00:00
|
|
|
);
|
2015-06-17 20:01:00 +00:00
|
|
|
unset( $this->descriptor['lang'][$langList] );
|
2015-04-02 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-29 18:44:22 +00:00
|
|
|
// Remove 'deprecated' key
|
|
|
|
|
if ( is_array( $this->descriptor ) ) {
|
2019-04-06 12:48:39 +00:00
|
|
|
unset( $this->descriptor['deprecated'] );
|
2017-03-29 18:44:22 +00:00
|
|
|
}
|
2015-04-02 17:54:07 +00:00
|
|
|
|
2014-11-11 19:50:44 +00:00
|
|
|
// Ensure that all files have common extension.
|
2016-02-17 09:09:32 +00:00
|
|
|
$extensions = [];
|
2017-03-17 02:14:05 +00:00
|
|
|
$descriptor = is_array( $this->descriptor ) ? $this->descriptor : [ $this->descriptor ];
|
2014-11-11 19:50:44 +00:00
|
|
|
array_walk_recursive( $descriptor, function ( $path ) use ( &$extensions ) {
|
2017-03-17 02:14:05 +00:00
|
|
|
$extensions[] = pathinfo( $this->getLocalPath( $path ), PATHINFO_EXTENSION );
|
2014-11-11 19:50:44 +00:00
|
|
|
} );
|
|
|
|
|
$extensions = array_unique( $extensions );
|
|
|
|
|
if ( count( $extensions ) !== 1 ) {
|
2015-06-15 06:35:58 +00:00
|
|
|
throw new InvalidArgumentException(
|
2017-03-29 18:44:22 +00:00
|
|
|
"File type for different image files of '$name' not the same in module '$module'"
|
2015-06-15 06:35:58 +00:00
|
|
|
);
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
$ext = $extensions[0];
|
2020-11-25 04:14:36 +00:00
|
|
|
if ( !isset( self::FILE_TYPES[$ext] ) ) {
|
2015-06-15 06:35:58 +00:00
|
|
|
throw new InvalidArgumentException(
|
2017-03-29 18:44:22 +00:00
|
|
|
"Invalid file type for image files of '$name' (valid: svg, png, gif, jpg) in module '$module'"
|
2015-06-15 06:35:58 +00:00
|
|
|
);
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
$this->extension = $ext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get name of this image.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getName() {
|
|
|
|
|
return $this->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get name of the module this image belongs to.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getModule() {
|
|
|
|
|
return $this->module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the list of variants this image can be converted to.
|
|
|
|
|
*
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function getVariants(): array {
|
2014-11-11 19:50:44 +00:00
|
|
|
return array_keys( $this->variants );
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 23:48:10 +00:00
|
|
|
/**
|
2022-01-09 17:44:44 +00:00
|
|
|
* @internal For unit testing override
|
2019-10-18 23:48:10 +00:00
|
|
|
* @param string $lang
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function getLangFallbacks( string $lang ): array {
|
2019-10-18 23:48:10 +00:00
|
|
|
return MediaWikiServices::getInstance()
|
|
|
|
|
->getLanguageFallback()
|
|
|
|
|
->getAll( $lang, LanguageFallback::STRICT );
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-11 19:50:44 +00:00
|
|
|
/**
|
|
|
|
|
* Get the path to image file for given context.
|
|
|
|
|
*
|
2022-05-06 09:09:56 +00:00
|
|
|
* @param Context $context Any context
|
2014-11-11 19:50:44 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
public function getPath( Context $context ) {
|
2014-11-11 19:50:44 +00:00
|
|
|
$desc = $this->descriptor;
|
2017-03-17 02:14:05 +00:00
|
|
|
if ( !is_array( $desc ) ) {
|
|
|
|
|
return $this->getLocalPath( $desc );
|
2018-03-30 19:56:59 +00:00
|
|
|
}
|
|
|
|
|
if ( isset( $desc['lang'] ) ) {
|
|
|
|
|
$contextLang = $context->getLanguage();
|
|
|
|
|
if ( isset( $desc['lang'][$contextLang] ) ) {
|
2017-03-17 02:14:05 +00:00
|
|
|
return $this->getLocalPath( $desc['lang'][$contextLang] );
|
2018-03-30 19:56:59 +00:00
|
|
|
}
|
2019-10-18 23:48:10 +00:00
|
|
|
$fallbacks = $this->getLangFallbacks( $contextLang );
|
2018-03-30 19:56:59 +00:00
|
|
|
foreach ( $fallbacks as $lang ) {
|
2018-04-03 21:08:52 +00:00
|
|
|
if ( isset( $desc['lang'][$lang] ) ) {
|
2017-03-17 02:14:05 +00:00
|
|
|
return $this->getLocalPath( $desc['lang'][$lang] );
|
2018-03-30 19:56:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $desc[$context->getDirection()] ) ) {
|
2017-03-17 02:14:05 +00:00
|
|
|
return $this->getLocalPath( $desc[$context->getDirection()] );
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
2019-06-16 09:20:11 +00:00
|
|
|
if ( isset( $desc['default'] ) ) {
|
2017-03-17 02:14:05 +00:00
|
|
|
return $this->getLocalPath( $desc['default'] );
|
2019-06-16 09:20:11 +00:00
|
|
|
}
|
2022-06-21 23:25:17 +00:00
|
|
|
throw new RuntimeException( 'No matching path found' );
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
2017-03-17 02:14:05 +00:00
|
|
|
/**
|
2022-05-06 09:09:56 +00:00
|
|
|
* @param string|FilePath $path
|
2017-03-17 02:14:05 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getLocalPath( $path ) {
|
2022-05-06 09:09:56 +00:00
|
|
|
if ( $path instanceof FilePath ) {
|
2017-03-17 02:14:05 +00:00
|
|
|
return $path->getLocalPath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "{$this->basePath}/$path";
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-11 19:50:44 +00:00
|
|
|
/**
|
|
|
|
|
* Get the extension of the image.
|
|
|
|
|
*
|
2021-11-02 14:29:42 +00:00
|
|
|
* @param string|null $format Format to get the extension for, 'original' or 'rasterized'
|
2014-11-11 19:50:44 +00:00
|
|
|
* @return string Extension without leading dot, e.g. 'png'
|
|
|
|
|
*/
|
|
|
|
|
public function getExtension( $format = 'original' ) {
|
|
|
|
|
if ( $format === 'rasterized' && $this->extension === 'svg' ) {
|
|
|
|
|
return 'png';
|
|
|
|
|
}
|
2017-07-01 00:31:56 +00:00
|
|
|
return $this->extension;
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the MIME type of the image.
|
|
|
|
|
*
|
2021-11-02 14:29:42 +00:00
|
|
|
* @param string|null $format Format to get the MIME type for, 'original' or 'rasterized'
|
2014-11-11 19:50:44 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getMimeType( $format = 'original' ) {
|
|
|
|
|
$ext = $this->getExtension( $format );
|
2020-11-25 04:14:36 +00:00
|
|
|
return self::FILE_TYPES[$ext];
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the load.php URL that will produce this image.
|
|
|
|
|
*
|
2022-05-06 09:09:56 +00:00
|
|
|
* @param Context $context Any context
|
2014-11-11 19:50:44 +00:00
|
|
|
* @param string $script URL to load.php
|
|
|
|
|
* @param string|null $variant Variant to get the URL for
|
|
|
|
|
* @param string $format Format to get the URL for, 'original' or 'rasterized'
|
resourceloader: Add version to ResourceLoaderImage urls for long-cache
The code previously here did not work well as it merely forwarded the
hash from the current web request. This had numerous issues:
1. It was often null because requests for stylesheets do not cary
a version hash.
2. When requested by JavaScript, the version hash would be a
combination-hash of many unrelated modules, thus when requested as
part of different batches, it would produce different urls which
is not ideal.
The impact of this is minimal currently because we basically never use
these urls, as SVGs are almost always embedded instead of ref'ed by url.
PNG urls are only generated for non-JS modules and then only used in older
browsers not supporting SVG. And, even after all that, for the edge case
of an SVG being ref'ed by url, they would be stored in LocalStorage by
mw.loader with the name+version of the module the image belonged to, not
the version hash of the batch request it came with.
Which means that, yes, localstorage key for "somemodule+someversion" would
have different values for different users, based on which batch the value
came with, because the image urls were using the version hash of the batch
request from ResourceLoaderContext. This is weird, but didn't cause bugs
or inefficiencies because the user would never be exposed to the other
possible urls for that image because we always check LocalStorage first.
It did cause fragmentation server-side in Varnish, though.
This is all fixed now by always including a version, and setting it to
the version of the module. This means there is no more Varnish fragmentation
for these. And it means that browsers are now allowed to cache the images
served from these urls for 30+ days (immutable) instead of only 5min,
which is what happened when they didn't have a version parameter (or set to
null).
Bug: T233343
Change-Id: I4af7fda03698ed4c288d154e7787fb2f3cbbe6c5
2019-09-26 16:26:52 +00:00
|
|
|
* @return string URL
|
2014-11-11 19:50:44 +00:00
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
public function getUrl( Context $context, $script, $variant, $format ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$query = [
|
2014-11-11 19:50:44 +00:00
|
|
|
'modules' => $this->getModule(),
|
|
|
|
|
'image' => $this->getName(),
|
|
|
|
|
'variant' => $variant,
|
|
|
|
|
'format' => $format,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2019-06-27 12:28:17 +00:00
|
|
|
if ( $this->varyOnLanguage() ) {
|
|
|
|
|
$query['lang'] = $context->getLanguage();
|
|
|
|
|
}
|
|
|
|
|
// The following parameters are at the end to keep the original order of the parameters.
|
|
|
|
|
$query['skin'] = $context->getSkin();
|
resourceloader: Add version to ResourceLoaderImage urls for long-cache
The code previously here did not work well as it merely forwarded the
hash from the current web request. This had numerous issues:
1. It was often null because requests for stylesheets do not cary
a version hash.
2. When requested by JavaScript, the version hash would be a
combination-hash of many unrelated modules, thus when requested as
part of different batches, it would produce different urls which
is not ideal.
The impact of this is minimal currently because we basically never use
these urls, as SVGs are almost always embedded instead of ref'ed by url.
PNG urls are only generated for non-JS modules and then only used in older
browsers not supporting SVG. And, even after all that, for the edge case
of an SVG being ref'ed by url, they would be stored in LocalStorage by
mw.loader with the name+version of the module the image belonged to, not
the version hash of the batch request it came with.
Which means that, yes, localstorage key for "somemodule+someversion" would
have different values for different users, based on which batch the value
came with, because the image urls were using the version hash of the batch
request from ResourceLoaderContext. This is weird, but didn't cause bugs
or inefficiencies because the user would never be exposed to the other
possible urls for that image because we always check LocalStorage first.
It did cause fragmentation server-side in Varnish, though.
This is all fixed now by always including a version, and setting it to
the version of the module. This means there is no more Varnish fragmentation
for these. And it means that browsers are now allowed to cache the images
served from these urls for 30+ days (immutable) instead of only 5min,
which is what happened when they didn't have a version parameter (or set to
null).
Bug: T233343
Change-Id: I4af7fda03698ed4c288d154e7787fb2f3cbbe6c5
2019-09-26 16:26:52 +00:00
|
|
|
$rl = $context->getResourceLoader();
|
|
|
|
|
$query['version'] = $rl->makeVersionQuery( $context, [ $this->getModule() ] );
|
2014-11-11 19:50:44 +00:00
|
|
|
|
2016-04-01 23:22:29 +00:00
|
|
|
return wfAppendQuery( $script, $query );
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the data: URI that will produce this image.
|
|
|
|
|
*
|
2022-05-06 09:09:56 +00:00
|
|
|
* @param Context $context Any context
|
2014-11-11 19:50:44 +00:00
|
|
|
* @param string|null $variant Variant to get the URI for
|
|
|
|
|
* @param string $format Format to get the URI for, 'original' or 'rasterized'
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
public function getDataUri( Context $context, $variant, $format ) {
|
2014-11-11 19:50:44 +00:00
|
|
|
$type = $this->getMimeType( $format );
|
|
|
|
|
$contents = $this->getImageData( $context, $variant, $format );
|
|
|
|
|
return CSSMin::encodeStringAsDataURI( $contents, $type );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get actual image data for this image. This can be saved to a file or sent to the browser to
|
|
|
|
|
* produce the converted image.
|
|
|
|
|
*
|
|
|
|
|
* Call getExtension() or getMimeType() with the same $format argument to learn what file type the
|
|
|
|
|
* returned data uses.
|
|
|
|
|
*
|
2022-05-06 09:09:56 +00:00
|
|
|
* @param Context $context Image context, or any context if $variant and $format
|
2014-11-11 19:50:44 +00:00
|
|
|
* given.
|
2021-11-02 14:29:42 +00:00
|
|
|
* @param string|null|false $variant Variant to get the data for. Optional; if given, overrides the data
|
2014-11-11 19:50:44 +00:00
|
|
|
* from $context.
|
2021-11-02 14:29:42 +00:00
|
|
|
* @param string|false $format Format to get the data for, 'original' or 'rasterized'. Optional; if
|
2014-11-11 19:50:44 +00:00
|
|
|
* given, overrides the data from $context.
|
|
|
|
|
* @return string|false Possibly binary image data, or false on failure
|
|
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
public function getImageData( Context $context, $variant = false, $format = false ) {
|
2014-11-11 19:50:44 +00:00
|
|
|
if ( $variant === false ) {
|
|
|
|
|
$variant = $context->getVariant();
|
|
|
|
|
}
|
|
|
|
|
if ( $format === false ) {
|
|
|
|
|
$format = $context->getFormat();
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 19:19:38 +00:00
|
|
|
$path = $this->getPath( $context );
|
2015-03-30 17:08:55 +00:00
|
|
|
if ( !file_exists( $path ) ) {
|
2022-06-21 23:25:17 +00:00
|
|
|
throw new RuntimeException( "File '$path' does not exist" );
|
2015-03-30 17:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-11 19:50:44 +00:00
|
|
|
if ( $this->getExtension() !== 'svg' ) {
|
2015-02-18 19:19:38 +00:00
|
|
|
return file_get_contents( $path );
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $variant && isset( $this->variants[$variant] ) ) {
|
|
|
|
|
$data = $this->variantize( $this->variants[$variant], $context );
|
|
|
|
|
} else {
|
2018-06-25 22:55:19 +00:00
|
|
|
$defaultColor = $this->defaultColor;
|
|
|
|
|
$data = $defaultColor ?
|
|
|
|
|
$this->variantize( [ 'color' => $defaultColor ], $context ) :
|
|
|
|
|
file_get_contents( $path );
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $format === 'rasterized' ) {
|
|
|
|
|
$data = $this->rasterize( $data );
|
2015-02-18 19:19:38 +00:00
|
|
|
if ( !$data ) {
|
2015-03-30 17:09:24 +00:00
|
|
|
wfDebugLog( 'ResourceLoaderImage', __METHOD__ . " failed to rasterize for $path" );
|
2015-02-18 19:19:38 +00:00
|
|
|
}
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send response headers (using the header() function) that are necessary to correctly serve the
|
|
|
|
|
* image data for this image, as returned by getImageData().
|
|
|
|
|
*
|
|
|
|
|
* Note that the headers are independent of the language or image variant.
|
|
|
|
|
*
|
2022-05-06 09:09:56 +00:00
|
|
|
* @param Context $context Image context
|
2014-11-11 19:50:44 +00:00
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
public function sendResponseHeaders( Context $context ): void {
|
2014-11-11 19:50:44 +00:00
|
|
|
$format = $context->getFormat();
|
|
|
|
|
$mime = $this->getMimeType( $format );
|
|
|
|
|
$filename = $this->getName() . '.' . $this->getExtension( $format );
|
|
|
|
|
|
|
|
|
|
header( 'Content-Type: ' . $mime );
|
|
|
|
|
header( 'Content-Disposition: ' .
|
|
|
|
|
FileBackend::makeContentDisposition( 'inline', $filename ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert this image, which is assumed to be SVG, to given variant.
|
|
|
|
|
*
|
|
|
|
|
* @param array $variantConf Array with a 'color' key, its value will be used as fill color
|
2022-05-06 09:09:56 +00:00
|
|
|
* @param Context $context Image context
|
2014-11-11 19:50:44 +00:00
|
|
|
* @return string New SVG file data
|
|
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
protected function variantize( array $variantConf, Context $context ) {
|
2018-12-01 09:28:18 +00:00
|
|
|
$dom = new DOMDocument;
|
2016-03-19 00:08:06 +00:00
|
|
|
$dom->loadXML( file_get_contents( $this->getPath( $context ) ) );
|
2014-11-11 19:50:44 +00:00
|
|
|
$root = $dom->documentElement;
|
2019-01-15 19:28:30 +00:00
|
|
|
$titleNode = null;
|
2019-01-15 19:43:35 +00:00
|
|
|
$wrapper = $dom->createElementNS( 'http://www.w3.org/2000/svg', 'g' );
|
2019-01-15 19:28:30 +00:00
|
|
|
// Reattach all direct children of the `<svg>` root node to the `<g>` wrapper
|
2014-11-11 19:50:44 +00:00
|
|
|
while ( $root->firstChild ) {
|
2019-01-15 19:28:30 +00:00
|
|
|
$node = $root->firstChild;
|
2019-09-07 13:06:50 +00:00
|
|
|
// @phan-suppress-next-line PhanUndeclaredProperty False positive
|
2019-01-15 19:28:30 +00:00
|
|
|
if ( !$titleNode && $node->nodeType === XML_ELEMENT_NODE && $node->tagName === 'title' ) {
|
|
|
|
|
// Remember the first encountered `<title>` node
|
|
|
|
|
$titleNode = $node;
|
|
|
|
|
}
|
|
|
|
|
$wrapper->appendChild( $node );
|
|
|
|
|
}
|
|
|
|
|
if ( $titleNode ) {
|
|
|
|
|
// Reattach the `<title>` node to the `<svg>` root node rather than the `<g>` wrapper
|
|
|
|
|
$root->appendChild( $titleNode );
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
$root->appendChild( $wrapper );
|
|
|
|
|
$wrapper->setAttribute( 'fill', $variantConf['color'] );
|
2016-03-19 00:08:06 +00:00
|
|
|
return $dom->saveXML();
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-30 17:09:24 +00:00
|
|
|
* Massage the SVG image data for converters which don't understand some path data syntax.
|
2014-11-11 19:50:44 +00:00
|
|
|
*
|
|
|
|
|
* This is necessary for rsvg and ImageMagick when compiled with rsvg support.
|
|
|
|
|
* Upstream bug is https://bugzilla.gnome.org/show_bug.cgi?id=620923, fixed 2014-11-10, so
|
|
|
|
|
* this will be needed for a while. (T76852)
|
|
|
|
|
*
|
|
|
|
|
* @param string $svg SVG image data
|
|
|
|
|
* @return string Massaged SVG image data
|
|
|
|
|
*/
|
|
|
|
|
protected function massageSvgPathdata( $svg ) {
|
2018-12-01 09:28:18 +00:00
|
|
|
$dom = new DOMDocument;
|
2016-03-19 00:08:06 +00:00
|
|
|
$dom->loadXML( $svg );
|
2014-11-11 19:50:44 +00:00
|
|
|
foreach ( $dom->getElementsByTagName( 'path' ) as $node ) {
|
|
|
|
|
$pathData = $node->getAttribute( 'd' );
|
|
|
|
|
// Make sure there is at least one space between numbers, and that leading zero is not omitted.
|
|
|
|
|
// rsvg has issues with syntax like "M-1-2" and "M.445.483" and especially "M-.445-.483".
|
|
|
|
|
$pathData = preg_replace( '/(-?)(\d*\.\d+|\d+)/', ' ${1}0$2 ', $pathData );
|
|
|
|
|
// Strip unnecessary leading zeroes for prettiness, not strictly necessary
|
|
|
|
|
$pathData = preg_replace( '/([ -])0(\d)/', '$1$2', $pathData );
|
|
|
|
|
$node->setAttribute( 'd', $pathData );
|
|
|
|
|
}
|
2016-03-19 00:08:06 +00:00
|
|
|
return $dom->saveXML();
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert passed image data, which is assumed to be SVG, to PNG.
|
|
|
|
|
*
|
|
|
|
|
* @param string $svg SVG image data
|
|
|
|
|
* @return string|bool PNG image data, or false on failure
|
|
|
|
|
*/
|
|
|
|
|
protected function rasterize( $svg ) {
|
2022-04-26 15:48:03 +00:00
|
|
|
$svgConverter = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SVGConverter );
|
|
|
|
|
$svgConverterPath = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SVGConverterPath );
|
2020-06-14 18:40:02 +00:00
|
|
|
// This code should be factored out to a separate method on SvgHandler, or perhaps a separate
|
|
|
|
|
// class, with a separate set of configuration settings.
|
|
|
|
|
//
|
|
|
|
|
// This is a distinct use case from regular SVG rasterization:
|
2021-11-19 23:19:42 +00:00
|
|
|
// * We can skip many checks (as the images come from a trusted source,
|
2020-06-14 18:40:02 +00:00
|
|
|
// rather than from the user).
|
|
|
|
|
// * We need to provide extra options to some converters to achieve acceptable quality for very
|
|
|
|
|
// small images, which might cause performance issues in the general case.
|
|
|
|
|
// * We want to directly pass image data to the converter, rather than a file path.
|
|
|
|
|
//
|
|
|
|
|
// See https://phabricator.wikimedia.org/T76473#801446 for examples of what happens with the
|
|
|
|
|
// default settings.
|
|
|
|
|
//
|
|
|
|
|
// For now, we special-case rsvg (used in WMF production) and do a messy workaround for other
|
|
|
|
|
// converters.
|
|
|
|
|
|
2014-11-11 19:50:44 +00:00
|
|
|
$svg = $this->massageSvgPathdata( $svg );
|
|
|
|
|
|
2015-02-18 19:19:38 +00:00
|
|
|
// Sometimes this might be 'rsvg-secure'. Long as it's rsvg.
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( strpos( $svgConverter, 'rsvg' ) === 0 ) {
|
2015-02-05 18:14:34 +00:00
|
|
|
$command = 'rsvg-convert';
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( $svgConverterPath ) {
|
|
|
|
|
$command = Shell::escape( "{$svgConverterPath}/" ) . $command;
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$process = proc_open(
|
|
|
|
|
$command,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 0 => [ 'pipe', 'r' ], 1 => [ 'pipe', 'w' ] ],
|
2014-11-11 19:50:44 +00:00
|
|
|
$pipes
|
|
|
|
|
);
|
|
|
|
|
|
2022-01-25 01:53:25 +00:00
|
|
|
if ( $process ) {
|
2014-11-11 19:50:44 +00:00
|
|
|
fwrite( $pipes[0], $svg );
|
|
|
|
|
fclose( $pipes[0] );
|
|
|
|
|
$png = stream_get_contents( $pipes[1] );
|
|
|
|
|
fclose( $pipes[1] );
|
|
|
|
|
proc_close( $process );
|
|
|
|
|
|
|
|
|
|
return $png ?: false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-06-22 01:14:25 +00:00
|
|
|
}
|
|
|
|
|
// Write input to and read output from a temporary file
|
|
|
|
|
$tempFilenameSvg = tempnam( wfTempDir(), 'ResourceLoaderImage' );
|
|
|
|
|
$tempFilenamePng = tempnam( wfTempDir(), 'ResourceLoaderImage' );
|
2014-11-11 19:50:44 +00:00
|
|
|
|
2022-06-22 01:14:25 +00:00
|
|
|
file_put_contents( $tempFilenameSvg, $svg );
|
2014-11-11 19:50:44 +00:00
|
|
|
|
2022-06-22 01:14:25 +00:00
|
|
|
$svgReader = new SVGReader( $tempFilenameSvg );
|
|
|
|
|
$metadata = $svgReader->getMetadata();
|
|
|
|
|
if ( !isset( $metadata['width'] ) || !isset( $metadata['height'] ) ) {
|
2014-11-11 19:50:44 +00:00
|
|
|
unlink( $tempFilenameSvg );
|
2022-06-22 01:14:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-02-18 19:19:38 +00:00
|
|
|
|
2022-06-22 01:14:25 +00:00
|
|
|
$handler = new SvgHandler;
|
|
|
|
|
$res = $handler->rasterize(
|
|
|
|
|
$tempFilenameSvg,
|
|
|
|
|
$tempFilenamePng,
|
|
|
|
|
$metadata['width'],
|
|
|
|
|
$metadata['height']
|
|
|
|
|
);
|
|
|
|
|
unlink( $tempFilenameSvg );
|
|
|
|
|
|
|
|
|
|
if ( $res === true ) {
|
|
|
|
|
$png = file_get_contents( $tempFilenamePng );
|
|
|
|
|
unlink( $tempFilenamePng );
|
|
|
|
|
return $png;
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
2022-06-22 01:14:25 +00:00
|
|
|
|
|
|
|
|
return false;
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
2019-06-27 12:28:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the image depends on the language.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function varyOnLanguage() {
|
|
|
|
|
return is_array( $this->descriptor ) && (
|
|
|
|
|
isset( $this->descriptor['ltr'] ) ||
|
|
|
|
|
isset( $this->descriptor['rtl'] ) ||
|
|
|
|
|
isset( $this->descriptor['lang'] ) );
|
|
|
|
|
}
|
2014-11-11 19:50:44 +00:00
|
|
|
}
|
2022-05-06 09:09:56 +00:00
|
|
|
|
|
|
|
|
/** @deprecated since 1.39 */
|
|
|
|
|
class_alias( Image::class, 'ResourceLoaderImage' );
|