27 use Wikimedia\AtEase\AtEase;
28 use Wikimedia\ScopedCallback;
38 private const SVG_DEFAULT_RENDER_LANG =
'en';
44 private static $metaConversion = [
45 'originalwidth' =>
'ImageWidth',
46 'originalheight' =>
'ImageLength',
47 'description' =>
'ImageDescription',
48 'title' =>
'ObjectName',
52 $config = MediaWikiServices::getInstance()->getMainConfig();
53 $svgConverters = $config->get( MainConfigNames::SVGConverters );
54 $svgConverter = $config->get( MainConfigNames::SVGConverter );
55 if ( $config->get( MainConfigNames::SVGNativeRendering ) ===
true ) {
58 if ( !isset( $svgConverters[$svgConverter] ) ) {
59 wfDebug(
"\$wgSVGConverter is invalid, disabling SVG rendering." );
68 $svgNativeRendering = MediaWikiServices::getInstance()
69 ->getMainConfig()->get( MainConfigNames::SVGNativeRendering );
70 if ( $svgNativeRendering ===
true ) {
74 if ( $svgNativeRendering !==
'partial' ) {
78 $maxSVGFilesize = MediaWikiServices::getInstance()
79 ->getMainConfig()->get( MainConfigNames::SVGNativeRenderingSizeLimit );
83 &&
$file->getSize() <= $maxSVGFilesize;
99 # @todo Detect animated SVGs
101 if ( isset( $metadata[
'animated'] ) ) {
102 return $metadata[
'animated'];
123 if ( isset( $metadata[
'translations'] ) ) {
124 foreach ( $metadata[
'translations'] as $lang => $langType ) {
126 $langList[] = strtolower( $lang );
130 return array_unique( $langList );
150 if ( $userPreferredLanguage ===
'und' ) {
153 foreach ( $svgLanguages as $svgLang ) {
154 if ( strcasecmp( $svgLang, $userPreferredLanguage ) === 0 ) {
157 $trimmedSvgLang = $svgLang;
158 while ( strpos( $trimmedSvgLang,
'-' ) !==
false ) {
159 $trimmedSvgLang = substr( $trimmedSvgLang, 0, strrpos( $trimmedSvgLang,
'-' ) );
160 if ( strcasecmp( $trimmedSvgLang, $userPreferredLanguage ) === 0 ) {
176 return $params[
'lang'] ?? $params[
'targetlang'] ?? self::SVG_DEFAULT_RENDER_LANG;
186 return self::SVG_DEFAULT_RENDER_LANG;
204 if ( parent::normaliseParams( $image, $params ) ) {
222 $svgMaxSize = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SVGMaxSize );
224 # Don't make an image bigger than wgMaxSVGSize on the smaller side
225 if ( $params[
'physicalWidth'] <= $params[
'physicalHeight'] ) {
226 if ( $params[
'physicalWidth'] > $svgMaxSize ) {
227 $srcWidth = $image->getWidth( $params[
'page'] );
228 $srcHeight = $image->getHeight( $params[
'page'] );
229 $params[
'physicalWidth'] = $svgMaxSize;
230 $params[
'physicalHeight'] =
File::scaleHeight( $srcWidth, $srcHeight, $svgMaxSize );
232 } elseif ( $params[
'physicalHeight'] > $svgMaxSize ) {
233 $srcWidth = $image->getWidth( $params[
'page'] );
234 $srcHeight = $image->getHeight( $params[
'page'] );
235 $params[
'physicalWidth'] =
File::scaleHeight( $srcHeight, $srcWidth, $svgMaxSize );
236 $params[
'physicalHeight'] = $svgMaxSize;
240 if ( isset( $params[
'targetlang'] ) && !$image->getMatchedLanguage( $params[
'targetlang'] ) ) {
241 unset( $params[
'targetlang'] );
255 public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
259 $clientWidth = $params[
'width'];
260 $clientHeight = $params[
'height'];
261 $physicalWidth = $params[
'physicalWidth'];
262 $physicalHeight = $params[
'physicalHeight'];
267 return new ThumbnailImage( $image, $image->getURL(),
false, $params );
270 if ( $flags & self::TRANSFORM_LATER ) {
275 if ( isset( $metadata[
'error'] ) ) {
276 $err =
wfMessage(
'svg-long-error', $metadata[
'error'][
'message'] );
283 wfMessage(
'thumbnail_dest_directory' ) );
286 $srcPath = $image->getLocalRefPath();
287 if ( $srcPath ===
false ) {
289 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
293 $params[
'width'], $params[
'height'],
302 $lnPath =
"$tmpDir/" . basename( $srcPath );
303 $ok = mkdir( $tmpDir, 0771 );
306 sprintf(
'Thumbnail failed on %s: could not create temporary directory %s',
309 $params[
'width'], $params[
'height'],
310 wfMessage(
'thumbnail-temp-create' )->text()
313 $ok = symlink( $srcPath, $lnPath );
315 $cleaner =
new ScopedCallback(
static function () use ( $tmpDir, $lnPath ) {
316 AtEase::suppressWarnings();
319 AtEase::restoreWarnings();
323 $ok = copy( $srcPath, $lnPath );
327 sprintf(
'Thumbnail failed on %s: could not link %s to %s',
330 $params[
'width'], $params[
'height'],
335 $status = $this->
rasterize( $lnPath, $dstPath, $physicalWidth, $physicalHeight, $lang );
336 if ( $status ===
true ) {
353 public function rasterize( $srcPath, $dstPath, $width, $height, $lang =
false ) {
354 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
355 $svgConverters = $mainConfig->get( MainConfigNames::SVGConverters );
356 $svgConverter = $mainConfig->get( MainConfigNames::SVGConverter );
357 $svgConverterPath = $mainConfig->get( MainConfigNames::SVGConverterPath );
360 if ( isset( $svgConverters[$svgConverter] ) ) {
361 if ( is_array( $svgConverters[$svgConverter] ) ) {
363 $func = $svgConverters[$svgConverter][0];
364 if ( !is_callable( $func ) ) {
365 throw new UnexpectedValueException(
"$func is not callable" );
367 $err = $func( $srcPath,
372 ...array_slice( $svgConverters[$svgConverter], 1 )
374 $retval = (bool)$err;
377 $path = $svgConverterPath ? Shell::escape(
"{$svgConverterPath}/" ) :
'';
378 $cmd = preg_replace_callback(
'/\$(path\/|width|height|input|output)/',
379 static function ( $m ) use (
$path, $width, $height, $srcPath, $dstPath ) {
382 '$width' => intval( $width ),
383 '$height' => intval( $height ),
384 '$input' => Shell::escape( $srcPath ),
385 '$output' => Shell::escape( $dstPath ),
388 $svgConverters[$svgConverter]
392 if ( $lang !==
false ) {
393 $env[
'LANG'] = $lang;
396 wfDebug( __METHOD__ .
": $cmd" );
401 if ( $retval != 0 || $removed ) {
412 $im =
new Imagick( $srcPath );
413 $im->setBackgroundColor(
'transparent' );
414 $im->readImage( $srcPath );
415 $im->setImageFormat(
'png' );
416 $im->setImageDepth( 8 );
418 if ( !$im->thumbnailImage( (
int)$width, (
int)$height,
false ) ) {
419 return 'Could not resize image';
421 if ( !$im->writeImage( $dstPath ) ) {
422 return "Could not write to $dstPath";
427 return [
'png',
'image/png' ];
441 if ( isset( $metadata[
'error'] ) ) {
442 return wfMessage(
'svg-long-error', $metadata[
'error'][
'message'] )->text();
446 $msg =
wfMessage(
'svg-long-desc-animated' );
451 return $msg->numParams(
$file->getWidth(),
$file->getHeight() )->sizeParams(
$file->getSize() )->parse();
464 $metadata += $svgReader->getMetadata();
467 $metadata[
'error'] = [
468 'message' => $e->getMessage(),
469 'code' => $e->getCode()
471 wfDebug( __METHOD__ .
': ' . $e->getMessage() );
475 'width' => $metadata[
'width'] ?? 0,
476 'height' => $metadata[
'height'] ?? 0,
477 'metadata' => $metadata
482 if ( isset( $unser[
'version'] ) && $unser[
'version'] === self::SVG_METADATA_VERSION ) {
498 if ( !isset( $meta[
'originalWidth'] ) ) {
507 return [
'objectname',
'imagedescription' ];
521 if ( !$metadata || isset( $metadata[
'error'] ) ) {
534 foreach ( $metadata as $name => $value ) {
535 $tag = strtolower( $name );
536 if ( isset( self::$metaConversion[$tag] ) ) {
537 $tag = strtolower( self::$metaConversion[$tag] );
544 in_array( $tag, $visibleFields ) ?
'visible' :
'collapsed',
551 return $showMeta ? $result :
false;
560 if ( in_array( $name, [
'width',
'height' ] ) ) {
562 return ( $value > 0 );
564 if ( $name ===
'lang' ) {
586 if ( $code !== self::SVG_DEFAULT_RENDER_LANG ) {
587 $lang =
'lang' . strtolower( $code ) .
'-';
589 if ( !isset( $params[
'width'] ) ) {
593 return "$lang{$params['width']}px";
599 if ( preg_match(
'/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/', $str, $m ) ) {
601 return [
'width' => array_pop( $m ),
'lang' => $m[1] ];
603 return [
'width' => array_pop( $m ),
'lang' => self::SVG_DEFAULT_RENDER_LANG ];
605 if ( preg_match(
'/^(\d+)px$/', $str, $m ) ) {
606 return [
'width' => $m[1],
'lang' => self::SVG_DEFAULT_RENDER_LANG ];
612 return [
'img_lang' =>
'lang',
'img_width' =>
'width' ];
620 $scriptParams = [
'width' => $params[
'width'] ];
621 if ( isset( $params[
'lang'] ) ) {
622 $scriptParams[
'lang'] = $params[
'lang'];
625 return $scriptParams;
630 if ( !$metadata || isset( $metadata[
'error'] ) ) {
634 foreach ( $metadata as $name => $value ) {
635 $tag = strtolower( $name );
636 if ( $tag ===
'originalwidth' || $tag ===
'originalheight' ) {
641 if ( isset( self::$metaConversion[$tag] ) ) {
642 $tag = self::$metaConversion[$tag];
643 $stdMetadata[$tag] = $value;
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTempDir()
Tries to get the system directory for temporary files.
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
wfHostname()
Get host name of the current machine, for use in error reporting.
wfShellExecWithStderr( $cmd, &$retval=null, $environ=[], $limits=[])
Execute a shell command, returning both stdout and stderr.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Implements some public methods and some protected utility functions which are required by multiple ch...
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
static scaleHeight( $srcWidth, $srcHeight, $dstWidth)
Calculate the height of a thumbnail using the source and destination width.
Media handler abstract base class for images.
static isWellFormedLanguageTag(string $code, bool $lenient=false)
Returns true if a language code string is a well-formed language tag according to RFC 5646.
A class containing constants representing the names of configuration variables.
validateParam( $name, $value)
isVectorized( $file)
The material is vectorized and thus scaling is lossless.
normaliseParams( $image, &$params)
formatMetadata( $file, $context=false)
parseParamString( $str)
Parse a param string made with makeParamString back into an array.The parameter string without file n...
mustRender( $file)
True if handled types cannot be displayed directly in a browser but can be rendered.
getScriptParams( $params)
makeParamString( $params)
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.
doTransform( $image, $dstPath, $dstUrl, $params, $flags=0)
validateMetadata( $unser)
getLanguageFromParams(array $params)
Determines render language from image parameters This is a lowercase IETF language.
getAvailableLanguages(File $file)
Which languages (systemLanguage attribute) is supported.
getLongDesc( $file)
Subtitle for the image.
normaliseParamsInternal( $image, $params)
Code taken out of normaliseParams() for testability.
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
getThumbType( $ext, $mime, $params=null)
Get the thumbnail extension and MIME type for a given source MIME type.
getDefaultRenderLanguage(File $file)
What language to render file in if none selected.
rasterize( $srcPath, $dstPath, $width, $height, $lang=false)
Transform an SVG file to PNG This function can be called outside of thumbnail contexts.
getSizeAndMetadata( $state, $filename)
isEnabled()
False if the handler is disabled for all files.
canAnimateThumbnail( $file)
We do not support making animated svg thumbnails.
visibleMetadataFields()
Get a list of metadata items which should be displayed when the metadata table is collapsed.
static rasterizeImagickExt( $srcPath, $dstPath, $width, $height)
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.
getMatchedLanguage( $userPreferredLanguage, array $svgLanguages)
SVG's systemLanguage matching rules state: 'The systemLanguage attribute ...
const SVG_METADATA_VERSION
allowRenderingByUserAgent( $file)
getParamMap()
Get an associative array mapping magic word IDs to parameter names.Will be used by the parser to iden...
Media transform output for images.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
if(!is_readable( $file)) $ext