MediaWiki REL1_35
SvgHandler Class Reference

Handler for SVG images. More...

Inheritance diagram for SvgHandler:
Collaboration diagram for SvgHandler:

Public Member Functions

 canAnimateThumbnail ( $file)
 We do not support making animated svg thumbnails.
 
 doTransform ( $image, $dstPath, $dstUrl, $params, $flags=0)
 
 formatMetadata ( $file, $context=false)
 
 getAvailableLanguages (File $file)
 Which languages (systemLanguage attribute) is supported.
 
 getCommonMetaArray (File $file)
 Get an array of standard (FormatMetadata type) metadata values.
 
 getDefaultRenderLanguage (File $file)
 What language to render file in if none selected.
 
 getImageSize ( $file, $path, $metadata=false)
 
 getLongDesc ( $file)
 Subtitle for the image.
 
 getMatchedLanguage ( $userPreferredLanguage, array $svgLanguages)
 SVG's systemLanguage matching rules state: 'The systemLanguage attribute ... [e]valuates to "true" if one of the languages indicated by user preferences exactly equals one of the languages given in the value of this parameter, or if one of the languages indicated by user preferences exactly equals a prefix of one of the languages given in the value of this parameter such that the first tag character following the prefix is "-".
 
 getMetadata ( $file, $filename)
 
 getMetadataType ( $image)
 Get a string describing the type of metadata, for display purposes.
 
 getParamMap ()
 Get an associative array mapping magic word IDs to parameter names.Will be used by the parser to identify parameters. Stable to override
Returns
string[]
Stable to override
 
 getThumbType ( $ext, $mime, $params=null)
 Get the thumbnail extension and MIME type for a given source MIME type.
 
 isAnimatedImage ( $file)
 
 isEnabled ()
 False if the handler is disabled for all files Stable to override.
 
 isMetadataValid ( $image, $metadata)
 Check if the metadata string is valid for this handler.
 
 isVectorized ( $file)
 The material is vectorized and thus scaling is lossless.
 
 makeParamString ( $params)
 
 mustRender ( $file)
 True if handled types cannot be displayed directly in a browser but can be rendered.
 
 normaliseParams ( $image, &$params)
 
 parseParamString ( $str)
 Parse a param string made with makeParamString back into an array.
Parameters
string$strThe parameter string without file name (e.g. 122px)
Returns
array|bool Array of parameters or false on failure.
Stable to override Stable to override
 
 rasterize ( $srcPath, $dstPath, $width, $height, $lang=false)
 Transform an SVG file to PNG This function can be called outside of thumbnail contexts.
 
 validateParam ( $name, $value)
 
- Public Member Functions inherited from ImageHandler
 canRender ( $file)
 True if the handled types can be transformed.Stable to override
Parameters
File$file
Returns
bool
Stable to override
 
 getDimensionsString ( $file)
 Shown in file history box on image description page.Stable to override
Parameters
File$file
Returns
string Dimensions
Stable to override
 
 getImageArea ( $image)
 Function that returns the number of pixels to be thumbnailed.
 
 getImageSize ( $image, $path)
 Get an image size array like that returned by getimagesize(), or false if it can't be determined.This function is used for determining the width, height and bitdepth directly from an image. The results are stored in the database in the img_width, img_height, img_bits fields.
Note
If this is a multipage file, return the width and height of the first page.
Parameters
File | FSFile$imageThe image object, or false if there isn't one. Warning, FSFile::getPropsFromPath might pass an FSFile instead of File (!)
string$pathThe filename
Returns
array|false Follow the format of PHP getimagesize() internal function. See https://www.php.net/getimagesize. MediaWiki will only ever use the first two array keys (the width and height), and the 'bits' associative key. All other array keys are ignored. Returning a 'bits' key is optional as not all formats have a notion of "bitdepth". Returns false on failure.
Stable to override
 
 getScriptedTransform ( $image, $script, $params)
 Get a MediaTransformOutput object representing an alternate of the transformed output which will call an intermediary thumbnail assist script.Used when the repository has a thumbnailScriptUrl option configured.Return false to fall back to the regular getTransform().Stable to override
Parameters
File$image
string$script
array$params
Returns
bool|ThumbnailImage
Stable to override
 
 getShortDesc ( $file)
 Short description.Shown on Special:Search results.Stable to override
Parameters
File$file
Returns
string
Stable to override
 
 sanitizeParamsForBucketing ( $params)
 Returns a normalised params array for which parameters have been cleaned up for bucketing purposes Stable to override.
Parameters
array$params
Returns
array
Stable to override
 
- Public Member Functions inherited from MediaHandler
 canRotate ()
 True if the handler can rotate the media.
 
 convertMetadataVersion ( $metadata, $version=1)
 Convert metadata version.
 
 filterThumbnailPurgeList (&$files, $options)
 Remove files from the purge list.
 
 getContentHeaders ( $metadata)
 Get useful response headers for GET/HEAD requests for a file with the given metadata Stable to override.
 
 getEntireText (File $file)
 Get the text of the entire document.
 
 getLength ( $file)
 If its an audio file, return the length of the file.
 
 getPageDimensions (File $image, $page)
 Get an associative array of page dimensions Currently "width" and "height" are understood, but this might be expanded in the future.
 
 getPageText (File $image, $page)
 Generic getter for text layer.
 
 getRotation ( $file)
 On supporting image formats, try to read out the low-level orientation of the file and return the angle that the file needs to be rotated to be viewed.
 
 getTransform ( $image, $dstPath, $dstUrl, $params)
 Get a MediaTransformOutput object representing the transformed output.
 
 getWarningConfig ( $file)
 Gets configuration for the file warning message.
 
 isExpensiveToThumbnail ( $file)
 True if creating thumbnails from the file is large or otherwise resource-intensive.
 
 isMultiPage ( $file)
 True if the type has multi-page capabilities.
 
 pageCount (File $file)
 Page count for a multi-page document, false if unsupported or unknown.
 
 parserTransformHook ( $parser, $file)
 Modify the parser object post-transform.
 
 removeBadFile ( $dstPath, $retval=0)
 Check for zero-sized thumbnails.
 
 supportsBucketing ()
 Returns whether or not this handler supports the chained generation of thumbnails according to buckets Stable to override.
 
 verifyUpload ( $fileName)
 File validation hook called on upload.
 

Static Public Member Functions

static rasterizeImagickExt ( $srcPath, $dstPath, $width, $height)
 
- Static Public Member Functions inherited from MediaHandler
static fitBoxWidth ( $boxWidth, $boxHeight, $maxHeight)
 Calculate the largest thumbnail width for a given original file size such that the thumbnail's height is at most $maxHeight.
 
static getGeneralLongDesc ( $file)
 Used instead of getLongDesc if there is no handler registered for file.
 
static getGeneralShortDesc ( $file)
 Used instead of getShortDesc if there is no handler registered for file.
 
static getHandler ( $type)
 Get a MediaHandler for a given MIME type from the instance cache.
 
static getMetadataVersion ()
 Get metadata version.
 
static getPageRangesByDimensions ( $pagesByDimensions)
 Converts a dimensions array about a potentially multipage document from an exhaustive list of ordered page numbers to a list of page ranges.
 

Public Attributes

const SVG_METADATA_VERSION = 2
 
- Public Attributes inherited from MediaHandler
const METADATA_BAD = false
 
const METADATA_COMPATIBLE = 2
 
const METADATA_GOOD = true
 
const TRANSFORM_LATER = 1
 

Protected Member Functions

 getLanguageFromParams (array $params)
 Determines render language from image parameters.
 
 getScriptParams ( $params)
 
 normaliseParamsInternal ( $image, $params)
 Code taken out of normaliseParams() for testability.
 
 unpackMetadata ( $metadata)
 
 visibleMetadataFields ()
 Get a list of metadata items which should be displayed when the metadata table is collapsed.
 
- Protected Member Functions inherited from MediaHandler
 formatMetadataHelper ( $metadataArray, $context=false)
 sorts the visible/invisible field.
 
 logErrorForExternalProcess ( $retval, $err, $cmd)
 Log an error that occurred in an external process.
 

Static Private Attributes

static array $metaConversion
 A list of metadata tags that can be converted to the commonly used exif tags.
 

Additional Inherited Members

- Static Protected Member Functions inherited from MediaHandler
static addMeta (&$array, $visibility, $type, $id, $value, $param=false)
 This is used to generate an array element for each metadata value That array is then used to generate the table of metadata values on the image page.
 

Detailed Description

Handler for SVG images.

Definition at line 33 of file SvgHandler.php.

Member Function Documentation

◆ canAnimateThumbnail()

SvgHandler::canAnimateThumbnail (   $file)

We do not support making animated svg thumbnails.

Parameters
File$file
Returns
bool

Reimplemented from MediaHandler.

Definition at line 167 of file SvgHandler.php.

◆ doTransform()

SvgHandler::doTransform (   $image,
  $dstPath,
  $dstUrl,
  $params,
  $flags = 0 
)
Parameters
File$image
string$dstPath
string$dstUrl
array$params
int$flags
Returns
bool|MediaTransformError|ThumbnailImage|TransformParameterError

Reimplemented from MediaHandler.

Definition at line 228 of file SvgHandler.php.

References $lang, getLanguageFromParams(), normaliseParams(), rasterize(), unpackMetadata(), wfDebugLog(), wfHostname(), wfMessage(), wfMkdirParents(), wfRandomString(), and wfTempDir().

◆ formatMetadata()

SvgHandler::formatMetadata (   $file,
  $context = false 
)
Parameters
File$file
bool | IContextSource$contextContext to use (optional)
Returns
array|bool

Reimplemented from MediaHandler.

Definition at line 497 of file SvgHandler.php.

References $file, MediaHandler\addMeta(), unpackMetadata(), and visibleMetadataFields().

◆ getAvailableLanguages()

SvgHandler::getAvailableLanguages ( File  $file)

Which languages (systemLanguage attribute) is supported.

Note
This list is not guaranteed to be exhaustive. To avoid OOM errors, we only look at first bit of a file. Thus all languages on this list are present in the file, but its possible for the file to have a language not on this list.
Parameters
File$file
Returns
array Array of language codes, or empty if no language switching supported.

Reimplemented from MediaHandler.

Definition at line 95 of file SvgHandler.php.

References $file, $lang, SVGReader\LANG_FULL_MATCH, and unpackMetadata().

◆ getCommonMetaArray()

SvgHandler::getCommonMetaArray ( File  $file)

Get an array of standard (FormatMetadata type) metadata values.

The returned data is largely the same as that from getMetadata(), but formatted in a standard, stable, handler-independent way. The idea being that some values like ImageDescription or Artist are universal and should be retrievable in a handler generic way.

The specific properties are the type of properties that can be handled by the FormatMetadata class. These values are exposed to the user via the filemetadata parser function.

Details of the response format of this function can be found at https://www.mediawiki.org/wiki/Manual:File_metadata_handling tl/dr: the response is an associative array of properties keyed by name, but the value can be complex. You probably want to call one of the FormatMetadata::flatten* functions on the property values before using them, or call FormatMetadata::getFormattedData() on the full response array, which transforms all values into prettified, human-readable text.

Subclasses overriding this function must return a value which is a valid API response fragment (all associative array keys are valid XML tagnames).

Note, if the file simply has no metadata, but the handler supports this interface, it should return an empty array, not false.

Stable to override

Parameters
File$file
Returns
array|bool False if interface not supported
Since
1.23

Reimplemented from MediaHandler.

Definition at line 610 of file SvgHandler.php.

References $file, and unpackMetadata().

◆ getDefaultRenderLanguage()

SvgHandler::getDefaultRenderLanguage ( File  $file)

What language to render file in if none selected.

Parameters
File$fileLanguage code
Returns
string

Reimplemented from MediaHandler.

Definition at line 158 of file SvgHandler.php.

◆ getImageSize()

SvgHandler::getImageSize (   $file,
  $path,
  $metadata = false 
)
Parameters
File | FSFile$file
string$pathUnused
bool | array$metadata
Returns
array|false

Definition at line 387 of file SvgHandler.php.

References $file, and unpackMetadata().

◆ getLanguageFromParams()

SvgHandler::getLanguageFromParams ( array  $params)
protected

Determines render language from image parameters.

Parameters
array$params
Returns
string

Definition at line 148 of file SvgHandler.php.

Referenced by doTransform(), and makeParamString().

◆ getLongDesc()

SvgHandler::getLongDesc (   $file)

Subtitle for the image.

Different from the base class so it can be denoted that SVG's have a "nominal" resolution, and not a fixed one, as well as so animation can be denoted.

Parameters
File$file
Returns
string

Reimplemented from ImageHandler.

Definition at line 414 of file SvgHandler.php.

References $file, $wgLang, isAnimatedImage(), unpackMetadata(), and wfMessage().

◆ getMatchedLanguage()

SvgHandler::getMatchedLanguage (   $userPreferredLanguage,
array  $svgLanguages 
)

SVG's systemLanguage matching rules state: 'The systemLanguage attribute ... [e]valuates to "true" if one of the languages indicated by user preferences exactly equals one of the languages given in the value of this parameter, or if one of the languages indicated by user preferences exactly equals a prefix of one of the languages given in the value of this parameter such that the first tag character following the prefix is "-".

'

Return the first element of $svgLanguages that matches $userPreferredLanguage

See also
https://www.w3.org/TR/SVG/struct.html#SystemLanguageAttribute
Parameters
string$userPreferredLanguage
array$svgLanguages
Returns
string|null

Reimplemented from MediaHandler.

Definition at line 126 of file SvgHandler.php.

◆ getMetadata()

SvgHandler::getMetadata (   $file,
  $filename 
)
Parameters
File | FSFile$file
string$filename
Returns
string Serialised metadata

Reimplemented from MediaHandler.

Definition at line 440 of file SvgHandler.php.

References serialize(), SVG_METADATA_VERSION, and wfDebug().

◆ getMetadataType()

SvgHandler::getMetadataType (   $image)

Get a string describing the type of metadata, for display purposes.

Stable to override

Note
This method is currently unused.
Parameters
File$image
Returns
string

Reimplemented from MediaHandler.

Definition at line 469 of file SvgHandler.php.

◆ getParamMap()

SvgHandler::getParamMap ( )

Get an associative array mapping magic word IDs to parameter names.Will be used by the parser to identify parameters. Stable to override

Returns
string[]
Stable to override

Returns
string[]

Reimplemented from ImageHandler.

Definition at line 593 of file SvgHandler.php.

◆ getScriptParams()

SvgHandler::getScriptParams (   $params)
protected
Parameters
array$params
Returns
array

Reimplemented from ImageHandler.

Definition at line 601 of file SvgHandler.php.

◆ getThumbType()

SvgHandler::getThumbType (   $ext,
  $mime,
  $params = null 
)

Get the thumbnail extension and MIME type for a given source MIME type.

Stable to override

Parameters
string$extExtension of original file
string$mimeMIME type of original file
array | null$paramsHandler specific rendering parameters
Returns
array Thumbnail extension and MIME type

Reimplemented from MediaHandler.

Definition at line 401 of file SvgHandler.php.

◆ isAnimatedImage()

SvgHandler::isAnimatedImage (   $file)
Parameters
File$file
Returns
bool

Reimplemented from MediaHandler.

Definition at line 70 of file SvgHandler.php.

References $file, and unpackMetadata().

Referenced by getLongDesc().

◆ isEnabled()

SvgHandler::isEnabled ( )

False if the handler is disabled for all files Stable to override.

Returns
bool

Reimplemented from MediaHandler.

Definition at line 47 of file SvgHandler.php.

References $wgSVGConverter, $wgSVGConverters, and wfDebug().

◆ isMetadataValid()

SvgHandler::isMetadataValid (   $image,
  $metadata 
)

Check if the metadata string is valid for this handler.

If it returns MediaHandler::METADATA_BAD (or false), Image will reload the metadata from the file and update the database. MediaHandler::METADATA_GOOD for if the metadata is a-ok, MediaHandler::METADATA_COMPATIBLE if metadata is old but backwards compatible (which may or may not trigger a metadata reload).

Note
Returning self::METADATA_BAD will trigger a metadata reload from file on page view. Always returning this from a broken file, or suddenly triggering as bad metadata for a large number of files can cause performance problems.

Stable to override

Parameters
File$image
string$metadataThe metadata in serialized form
Returns
bool|int

Reimplemented from MediaHandler.

Definition at line 473 of file SvgHandler.php.

References MediaHandler\METADATA_BAD, MediaHandler\METADATA_COMPATIBLE, MediaHandler\METADATA_GOOD, and unpackMetadata().

◆ isVectorized()

SvgHandler::isVectorized (   $file)

The material is vectorized and thus scaling is lossless.

Stable to override

Parameters
File$file
Returns
bool

Reimplemented from MediaHandler.

Definition at line 62 of file SvgHandler.php.

◆ makeParamString()

SvgHandler::makeParamString (   $params)
Parameters
array$paramsName=>value pairs of parameters
Returns
string Filename to use

Reimplemented from ImageHandler.

Definition at line 569 of file SvgHandler.php.

References $lang, and getLanguageFromParams().

◆ mustRender()

SvgHandler::mustRender (   $file)

True if handled types cannot be displayed directly in a browser but can be rendered.

Stable to override

Parameters
File$file
Returns
bool

Reimplemented from MediaHandler.

Definition at line 58 of file SvgHandler.php.

◆ normaliseParams()

SvgHandler::normaliseParams (   $image,
$params 
)
Parameters
File$image
array&$params
Returns
bool

Reimplemented from ImageHandler.

Definition at line 176 of file SvgHandler.php.

References normaliseParamsInternal().

Referenced by doTransform().

◆ normaliseParamsInternal()

SvgHandler::normaliseParamsInternal (   $image,
  $params 
)
protected

Code taken out of normaliseParams() for testability.

Since
1.33
Parameters
File$image
array$params
Returns
array Modified $params

Definition at line 194 of file SvgHandler.php.

References $wgSVGMaxSize.

Referenced by normaliseParams().

◆ parseParamString()

SvgHandler::parseParamString (   $str)

Parse a param string made with makeParamString back into an array.

Parameters
string$strThe parameter string without file name (e.g. 122px)
Returns
array|bool Array of parameters or false on failure.
Stable to override Stable to override

Reimplemented from ImageHandler.

Definition at line 582 of file SvgHandler.php.

◆ rasterize()

SvgHandler::rasterize (   $srcPath,
  $dstPath,
  $width,
  $height,
  $lang = false 
)

Transform an SVG file to PNG This function can be called outside of thumbnail contexts.

Parameters
string$srcPath
string$dstPath
string$width
string$height
bool | string$langLanguage code of the language to render the SVG in
Exceptions
MWException
Returns
bool|MediaTransformError

Definition at line 318 of file SvgHandler.php.

References $lang, $wgSVGConverter, $wgSVGConverterPath, $wgSVGConverters, MediaHandler\logErrorForExternalProcess(), MediaHandler\removeBadFile(), wfDebug(), and wfShellExecWithStderr().

Referenced by doTransform(), and ResourceLoaderImage\rasterize().

◆ rasterizeImagickExt()

static SvgHandler::rasterizeImagickExt (   $srcPath,
  $dstPath,
  $width,
  $height 
)
static

Definition at line 367 of file SvgHandler.php.

◆ unpackMetadata()

SvgHandler::unpackMetadata (   $metadata)
protected

◆ validateParam()

SvgHandler::validateParam (   $name,
  $value 
)
Parameters
string$nameParameter name
mixed$valueParameter value
Returns
bool Validity

Reimplemented from ImageHandler.

Definition at line 545 of file SvgHandler.php.

◆ visibleMetadataFields()

SvgHandler::visibleMetadataFields ( )
protected

Get a list of metadata items which should be displayed when the metadata table is collapsed.

Stable to override

Returns
array Array of strings

Reimplemented from MediaHandler.

Definition at line 486 of file SvgHandler.php.

Referenced by formatMetadata().

Member Data Documentation

◆ $metaConversion

array SvgHandler::$metaConversion
staticprivate
Initial value:
= [
'originalwidth' => 'ImageWidth',
'originalheight' => 'ImageLength',
'description' => 'ImageDescription',
'title' => 'ObjectName',
]

A list of metadata tags that can be converted to the commonly used exif tags.

This allows messages to be reused, and consistent tag names for {{#formatmetadata:..}}

Definition at line 40 of file SvgHandler.php.

◆ SVG_METADATA_VERSION

const SvgHandler::SVG_METADATA_VERSION = 2

Definition at line 34 of file SvgHandler.php.

Referenced by getMetadata().


The documentation for this class was generated from the following file: