Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
29.00% |
78 / 269 |
|
17.24% |
5 / 29 |
CRAP | |
0.00% |
0 / 1 |
| SvgHandler | |
29.00% |
78 / 269 |
|
17.24% |
5 / 29 |
3607.44 | |
0.00% |
0 / 1 |
| isEnabled | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
| allowRenderingByUserAgent | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| mustRender | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isVectorized | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isAnimatedImage | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getAvailableLanguages | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| getMatchedLanguage | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
6.03 | |||
| getLanguageFromParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDefaultRenderLanguage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| canAnimateThumbnail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| normaliseParams | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| normaliseParamsInternal | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
| doTransform | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
132 | |||
| rasterize | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
72 | |||
| rasterizeImagickExt | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getThumbType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLongDesc | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getSizeAndMetadata | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| validateMetadata | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| getMetadataType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isFileMetadataValid | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| visibleMetadataFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| formatMetadata | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
56 | |||
| validateParam | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| makeParamString | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| parseParamString | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
| getParamMap | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getScriptParams | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getCommonMetaArray | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
7.03 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Handler for SVG images. |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | * @ingroup Media |
| 8 | */ |
| 9 | |
| 10 | use MediaWiki\Context\IContextSource; |
| 11 | use MediaWiki\FileRepo\File\File; |
| 12 | use MediaWiki\Language\LanguageCode; |
| 13 | use MediaWiki\MainConfigNames; |
| 14 | use MediaWiki\MediaWikiServices; |
| 15 | use MediaWiki\Shell\Shell; |
| 16 | use Wikimedia\AtEase\AtEase; |
| 17 | use Wikimedia\ScopedCallback; |
| 18 | |
| 19 | /** |
| 20 | * Handler for SVG images. |
| 21 | * |
| 22 | * @ingroup Media |
| 23 | */ |
| 24 | class SvgHandler extends ImageHandler { |
| 25 | public const SVG_METADATA_VERSION = 2; |
| 26 | |
| 27 | private const SVG_DEFAULT_RENDER_LANG = 'en'; |
| 28 | |
| 29 | /** @var array A list of metadata tags that can be converted |
| 30 | * to the commonly used exif tags. This allows messages |
| 31 | * to be reused, and consistent tag names for {{#formatmetadata:..}} |
| 32 | */ |
| 33 | private static $metaConversion = [ |
| 34 | 'originalwidth' => 'ImageWidth', |
| 35 | 'originalheight' => 'ImageLength', |
| 36 | 'description' => 'ImageDescription', |
| 37 | 'title' => 'ObjectName', |
| 38 | ]; |
| 39 | |
| 40 | /** @inheritDoc */ |
| 41 | public function isEnabled() { |
| 42 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
| 43 | $svgConverters = $config->get( MainConfigNames::SVGConverters ); |
| 44 | $svgConverter = $config->get( MainConfigNames::SVGConverter ); |
| 45 | if ( $config->get( MainConfigNames::SVGNativeRendering ) === true ) { |
| 46 | return true; |
| 47 | } |
| 48 | if ( !isset( $svgConverters[$svgConverter] ) ) { |
| 49 | wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering." ); |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param File $file |
| 59 | * @return bool |
| 60 | */ |
| 61 | public function allowRenderingByUserAgent( $file ) { |
| 62 | $svgNativeRendering = MediaWikiServices::getInstance() |
| 63 | ->getMainConfig()->get( MainConfigNames::SVGNativeRendering ); |
| 64 | if ( $svgNativeRendering === true ) { |
| 65 | // Don't do any transform for any SVG. |
| 66 | return true; |
| 67 | } |
| 68 | if ( $svgNativeRendering !== 'partial' ) { |
| 69 | // SVG images are always rasterized to PNG |
| 70 | return false; |
| 71 | } |
| 72 | $maxSVGFilesize = MediaWikiServices::getInstance() |
| 73 | ->getMainConfig()->get( MainConfigNames::SVGNativeRenderingSizeLimit ); |
| 74 | // Browsers don't really support SVG translations, so always render them to PNG |
| 75 | // Files bigger than the limit are also rendered as PNG, as big files might be a tax on the user agent |
| 76 | return count( $this->getAvailableLanguages( $file ) ) <= 1 |
| 77 | && $file->getSize() <= $maxSVGFilesize; |
| 78 | } |
| 79 | |
| 80 | /** @inheritDoc */ |
| 81 | public function mustRender( $file ) { |
| 82 | return !$this->allowRenderingByUserAgent( $file ); |
| 83 | } |
| 84 | |
| 85 | /** @inheritDoc */ |
| 86 | public function isVectorized( $file ) { |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param File $file |
| 92 | * @return bool |
| 93 | */ |
| 94 | public function isAnimatedImage( $file ) { |
| 95 | # @todo Detect animated SVGs |
| 96 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 97 | if ( isset( $metadata['animated'] ) ) { |
| 98 | return $metadata['animated']; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Which languages (systemLanguage attribute) is supported. |
| 106 | * |
| 107 | * @note This list is not guaranteed to be exhaustive. |
| 108 | * To avoid OOM errors, we only look at first bit of a file. |
| 109 | * Thus all languages on this list are present in the file, |
| 110 | * but its possible for the file to have a language not on |
| 111 | * this list. |
| 112 | * |
| 113 | * @param File $file |
| 114 | * @return string[] Array of language codes, or empty if no language switching supported. |
| 115 | */ |
| 116 | public function getAvailableLanguages( File $file ) { |
| 117 | $langList = []; |
| 118 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 119 | if ( isset( $metadata['translations'] ) ) { |
| 120 | foreach ( $metadata['translations'] as $lang => $langType ) { |
| 121 | if ( $langType === SVGReader::LANG_FULL_MATCH ) { |
| 122 | $langList[] = strtolower( $lang ); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | return array_unique( $langList ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * SVG's systemLanguage matching rules state: |
| 131 | * 'The `systemLanguage` attribute ... [e]valuates to "true" if one of the languages indicated |
| 132 | * by user preferences exactly equals one of the languages given in the value of this parameter, |
| 133 | * or if one of the languages indicated by user preferences exactly equals a prefix of one of |
| 134 | * the languages given in the value of this parameter such that the first tag character |
| 135 | * following the prefix is "-".' |
| 136 | * |
| 137 | * Return the first element of $svgLanguages that matches $userPreferredLanguage |
| 138 | * |
| 139 | * @see https://www.w3.org/TR/SVG/struct.html#SystemLanguageAttribute |
| 140 | * @param string $userPreferredLanguage |
| 141 | * @param string[] $svgLanguages |
| 142 | * @return string|null |
| 143 | */ |
| 144 | public function getMatchedLanguage( $userPreferredLanguage, array $svgLanguages ) { |
| 145 | // Explicitly requested undetermined language (text without svg systemLanguage attribute) |
| 146 | if ( $userPreferredLanguage === 'und' ) { |
| 147 | return 'und'; |
| 148 | } |
| 149 | foreach ( $svgLanguages as $svgLang ) { |
| 150 | if ( strcasecmp( $svgLang, $userPreferredLanguage ) === 0 ) { |
| 151 | return $svgLang; |
| 152 | } |
| 153 | $trimmedSvgLang = $svgLang; |
| 154 | while ( str_contains( $trimmedSvgLang, '-' ) ) { |
| 155 | $trimmedSvgLang = substr( $trimmedSvgLang, 0, strrpos( $trimmedSvgLang, '-' ) ); |
| 156 | if ( strcasecmp( $trimmedSvgLang, $userPreferredLanguage ) === 0 ) { |
| 157 | return $svgLang; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Determines render language from image parameters |
| 166 | * This is a lowercase IETF language |
| 167 | * |
| 168 | * @param array $params |
| 169 | * @return string |
| 170 | */ |
| 171 | protected function getLanguageFromParams( array $params ) { |
| 172 | return $params['lang'] ?? $params['targetlang'] ?? self::SVG_DEFAULT_RENDER_LANG; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * What language to render file in if none selected |
| 177 | * |
| 178 | * @param File $file Language code |
| 179 | * @return string |
| 180 | */ |
| 181 | public function getDefaultRenderLanguage( File $file ) { |
| 182 | return self::SVG_DEFAULT_RENDER_LANG; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * We do not support making animated svg thumbnails |
| 187 | * @param File $file |
| 188 | * @return bool |
| 189 | */ |
| 190 | public function canAnimateThumbnail( $file ) { |
| 191 | return $this->allowRenderingByUserAgent( $file ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @param File $image |
| 196 | * @param array &$params |
| 197 | * @return bool |
| 198 | */ |
| 199 | public function normaliseParams( $image, &$params ) { |
| 200 | if ( parent::normaliseParams( $image, $params ) ) { |
| 201 | $params = $this->normaliseParamsInternal( $image, $params ); |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Code taken out of normaliseParams() for testability |
| 210 | * |
| 211 | * @since 1.33 |
| 212 | * |
| 213 | * @param File $image |
| 214 | * @param array $params |
| 215 | * @return array Modified $params |
| 216 | */ |
| 217 | protected function normaliseParamsInternal( $image, $params ) { |
| 218 | $svgMaxSize = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SVGMaxSize ); |
| 219 | |
| 220 | # Don't make an image bigger than wgMaxSVGSize on the smaller side |
| 221 | if ( $params['physicalWidth'] <= $params['physicalHeight'] ) { |
| 222 | if ( $params['physicalWidth'] > $svgMaxSize ) { |
| 223 | $srcWidth = $image->getWidth( $params['page'] ); |
| 224 | $srcHeight = $image->getHeight( $params['page'] ); |
| 225 | $params['physicalWidth'] = $svgMaxSize; |
| 226 | $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $svgMaxSize ); |
| 227 | } |
| 228 | } elseif ( $params['physicalHeight'] > $svgMaxSize ) { |
| 229 | $srcWidth = $image->getWidth( $params['page'] ); |
| 230 | $srcHeight = $image->getHeight( $params['page'] ); |
| 231 | $params['physicalWidth'] = File::scaleHeight( $srcHeight, $srcWidth, $svgMaxSize ); |
| 232 | $params['physicalHeight'] = $svgMaxSize; |
| 233 | } |
| 234 | // To prevent the proliferation of thumbnails in languages not present in SVGs, unless |
| 235 | // explicitly forced by user. |
| 236 | if ( isset( $params['targetlang'] ) && !$image->getMatchedLanguage( $params['targetlang'] ) ) { |
| 237 | unset( $params['targetlang'] ); |
| 238 | } |
| 239 | |
| 240 | return $params; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @param File $image |
| 245 | * @param string $dstPath |
| 246 | * @param string $dstUrl |
| 247 | * @param array $params |
| 248 | * @param int $flags |
| 249 | * @return MediaTransformError|ThumbnailImage|TransformParameterError|false |
| 250 | */ |
| 251 | public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { |
| 252 | if ( !$this->normaliseParams( $image, $params ) ) { |
| 253 | return new TransformParameterError( $params ); |
| 254 | } |
| 255 | $clientWidth = $params['width']; |
| 256 | $clientHeight = $params['height']; |
| 257 | $physicalWidth = $params['physicalWidth']; |
| 258 | $physicalHeight = $params['physicalHeight']; |
| 259 | $lang = $this->getLanguageFromParams( $params ); |
| 260 | |
| 261 | if ( $this->allowRenderingByUserAgent( $image ) ) { |
| 262 | // No transformation required for native rendering |
| 263 | return new ThumbnailImage( $image, $image->getURL(), false, $params ); |
| 264 | } |
| 265 | |
| 266 | if ( $flags & self::TRANSFORM_LATER ) { |
| 267 | return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); |
| 268 | } |
| 269 | |
| 270 | $metadata = $this->validateMetadata( $image->getMetadataArray() ); |
| 271 | if ( isset( $metadata['error'] ) ) { |
| 272 | $err = wfMessage( 'svg-long-error', $metadata['error']['message'] ); |
| 273 | |
| 274 | return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err ); |
| 275 | } |
| 276 | |
| 277 | if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) { |
| 278 | return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, |
| 279 | wfMessage( 'thumbnail_dest_directory' ) ); |
| 280 | } |
| 281 | |
| 282 | $srcPath = $image->getLocalRefPath(); |
| 283 | if ( $srcPath === false ) { // Failed to get local copy |
| 284 | wfDebugLog( 'thumbnail', |
| 285 | sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"', |
| 286 | wfHostname(), $image->getName() ) ); |
| 287 | |
| 288 | return new MediaTransformError( 'thumbnail_error', |
| 289 | $params['width'], $params['height'], |
| 290 | wfMessage( 'filemissing' ) |
| 291 | ); |
| 292 | } |
| 293 | |
| 294 | // Make a temp dir with a symlink to the local copy in it. |
| 295 | // This plays well with rsvg-convert policy for external entities. |
| 296 | // https://git.gnome.org/browse/librsvg/commit/?id=f01aded72c38f0e18bc7ff67dee800e380251c8e |
| 297 | $tmpDir = wfTempDir() . '/svg_' . wfRandomString( 24 ); |
| 298 | $lnPath = "$tmpDir/" . basename( $srcPath ); |
| 299 | $ok = mkdir( $tmpDir, 0771 ); |
| 300 | if ( !$ok ) { |
| 301 | wfDebugLog( 'thumbnail', |
| 302 | sprintf( 'Thumbnail failed on %s: could not create temporary directory %s', |
| 303 | wfHostname(), $tmpDir ) ); |
| 304 | return new MediaTransformError( 'thumbnail_error', |
| 305 | $params['width'], $params['height'], |
| 306 | wfMessage( 'thumbnail-temp-create' )->text() |
| 307 | ); |
| 308 | } |
| 309 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 310 | $ok = @symlink( $srcPath, $lnPath ); |
| 311 | /** @noinspection PhpUnusedLocalVariableInspection */ |
| 312 | $cleaner = new ScopedCallback( static function () use ( $tmpDir, $lnPath ) { |
| 313 | AtEase::suppressWarnings(); |
| 314 | unlink( $lnPath ); |
| 315 | rmdir( $tmpDir ); |
| 316 | AtEase::restoreWarnings(); |
| 317 | } ); |
| 318 | if ( !$ok ) { |
| 319 | // Fallback because symlink often fails on Windows |
| 320 | $ok = copy( $srcPath, $lnPath ); |
| 321 | } |
| 322 | if ( !$ok ) { |
| 323 | wfDebugLog( 'thumbnail', |
| 324 | sprintf( 'Thumbnail failed on %s: could not link %s to %s', |
| 325 | wfHostname(), $lnPath, $srcPath ) ); |
| 326 | return new MediaTransformError( 'thumbnail_error', |
| 327 | $params['width'], $params['height'], |
| 328 | wfMessage( 'thumbnail-temp-create' ) |
| 329 | ); |
| 330 | } |
| 331 | |
| 332 | $status = $this->rasterize( $lnPath, $dstPath, $physicalWidth, $physicalHeight, $lang ); |
| 333 | if ( $status === true ) { |
| 334 | return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); |
| 335 | } |
| 336 | |
| 337 | return $status; // MediaTransformError |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Transform an SVG file to PNG |
| 342 | * This function can be called outside of thumbnail contexts |
| 343 | * @param string $srcPath |
| 344 | * @param string $dstPath |
| 345 | * @param int $width |
| 346 | * @param int $height |
| 347 | * @param string|false $lang Language code of the language to render the SVG in |
| 348 | * @return bool|MediaTransformError |
| 349 | */ |
| 350 | public function rasterize( $srcPath, $dstPath, $width, $height, $lang = false ) { |
| 351 | $mainConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 352 | $svgConverters = $mainConfig->get( MainConfigNames::SVGConverters ); |
| 353 | $svgConverter = $mainConfig->get( MainConfigNames::SVGConverter ); |
| 354 | $svgConverterPath = $mainConfig->get( MainConfigNames::SVGConverterPath ); |
| 355 | $err = false; |
| 356 | $retval = ''; |
| 357 | if ( isset( $svgConverters[$svgConverter] ) ) { |
| 358 | if ( is_array( $svgConverters[$svgConverter] ) ) { |
| 359 | // This is a PHP callable |
| 360 | $func = $svgConverters[$svgConverter][0]; |
| 361 | if ( !is_callable( $func ) ) { |
| 362 | throw new UnexpectedValueException( "$func is not callable" ); |
| 363 | } |
| 364 | $err = $func( $srcPath, |
| 365 | $dstPath, |
| 366 | $width, |
| 367 | $height, |
| 368 | $lang, |
| 369 | ...array_slice( $svgConverters[$svgConverter], 1 ) |
| 370 | ); |
| 371 | $retval = (bool)$err; |
| 372 | } else { |
| 373 | // External command |
| 374 | $cmd = strtr( $svgConverters[$svgConverter], [ |
| 375 | '$path/' => $svgConverterPath ? Shell::escape( "$svgConverterPath/" ) : '', |
| 376 | '$width' => (int)$width, |
| 377 | '$height' => (int)$height, |
| 378 | '$input' => Shell::escape( $srcPath ), |
| 379 | '$output' => Shell::escape( $dstPath ), |
| 380 | ] ); |
| 381 | |
| 382 | $env = []; |
| 383 | if ( $lang !== false ) { |
| 384 | $env['LANG'] = $lang; |
| 385 | } |
| 386 | |
| 387 | wfDebug( __METHOD__ . ": $cmd" ); |
| 388 | $err = wfShellExecWithStderr( $cmd, $retval, $env ); |
| 389 | } |
| 390 | } |
| 391 | $removed = $this->removeBadFile( $dstPath, $retval ); |
| 392 | if ( $retval != 0 || $removed ) { |
| 393 | // @phan-suppress-next-next-line PhanPossiblyUndeclaredVariable cmd is set when used |
| 394 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable cmd is set when used |
| 395 | $this->logErrorForExternalProcess( $retval, $err, $cmd ); |
| 396 | return new MediaTransformError( 'thumbnail_error', $width, $height, $err ); |
| 397 | } |
| 398 | |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * @param string $srcPath |
| 404 | * @param string $dstPath |
| 405 | * @param int $width |
| 406 | * @param int $height |
| 407 | * @return string|void |
| 408 | */ |
| 409 | public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) { |
| 410 | $im = new Imagick( $srcPath ); |
| 411 | $im->setBackgroundColor( 'transparent' ); |
| 412 | $im->readImage( $srcPath ); |
| 413 | $im->setImageFormat( 'png' ); |
| 414 | $im->setImageDepth( 8 ); |
| 415 | |
| 416 | if ( !$im->thumbnailImage( (int)$width, (int)$height, /* fit */ false ) ) { |
| 417 | return 'Could not resize image'; |
| 418 | } |
| 419 | if ( !$im->writeImage( $dstPath ) ) { |
| 420 | return "Could not write to $dstPath"; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /** @inheritDoc */ |
| 425 | public function getThumbType( $ext, $mime, $params = null ) { |
| 426 | return [ 'png', 'image/png' ]; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Subtitle for the image. Different from the base |
| 431 | * class so it can be denoted that SVG's have |
| 432 | * a "nominal" resolution, and not a fixed one, |
| 433 | * as well as so animation can be denoted. |
| 434 | * |
| 435 | * @param File $file |
| 436 | * @return string |
| 437 | */ |
| 438 | public function getLongDesc( $file ) { |
| 439 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 440 | if ( isset( $metadata['error'] ) ) { |
| 441 | return wfMessage( 'svg-long-error', $metadata['error']['message'] )->escaped(); |
| 442 | } |
| 443 | |
| 444 | if ( $this->isAnimatedImage( $file ) ) { |
| 445 | $msg = wfMessage( 'svg-long-desc-animated' ); |
| 446 | } else { |
| 447 | $msg = wfMessage( 'svg-long-desc' ); |
| 448 | } |
| 449 | |
| 450 | return $msg |
| 451 | ->numParams( $file->getWidth(), $file->getHeight() ) |
| 452 | ->sizeParams( $file->getSize() ) |
| 453 | ->parse(); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * @param MediaHandlerState $state |
| 458 | * @param string $filename |
| 459 | * @return array |
| 460 | */ |
| 461 | public function getSizeAndMetadata( $state, $filename ) { |
| 462 | $metadata = [ 'version' => self::SVG_METADATA_VERSION ]; |
| 463 | |
| 464 | try { |
| 465 | $svgReader = new SVGReader( $filename ); |
| 466 | $metadata += $svgReader->getMetadata(); |
| 467 | } catch ( InvalidSVGException $e ) { |
| 468 | // File not found, broken, etc. |
| 469 | $metadata['error'] = [ |
| 470 | 'message' => $e->getMessage(), |
| 471 | 'code' => $e->getCode() |
| 472 | ]; |
| 473 | wfDebug( __METHOD__ . ': ' . $e->getMessage() ); |
| 474 | } |
| 475 | |
| 476 | return [ |
| 477 | 'width' => $metadata['width'] ?? 0, |
| 478 | 'height' => $metadata['height'] ?? 0, |
| 479 | 'metadata' => $metadata |
| 480 | ]; |
| 481 | } |
| 482 | |
| 483 | /** @inheritDoc */ |
| 484 | protected function validateMetadata( $unser ) { |
| 485 | if ( isset( $unser['version'] ) && $unser['version'] === self::SVG_METADATA_VERSION ) { |
| 486 | return $unser; |
| 487 | } |
| 488 | |
| 489 | return null; |
| 490 | } |
| 491 | |
| 492 | /** @inheritDoc */ |
| 493 | public function getMetadataType( $image ) { |
| 494 | return 'parsed-svg'; |
| 495 | } |
| 496 | |
| 497 | /** @inheritDoc */ |
| 498 | public function isFileMetadataValid( $image ) { |
| 499 | $meta = $this->validateMetadata( $image->getMetadataArray() ); |
| 500 | if ( !$meta ) { |
| 501 | return self::METADATA_BAD; |
| 502 | } |
| 503 | if ( !isset( $meta['originalWidth'] ) ) { |
| 504 | // Old but compatible |
| 505 | return self::METADATA_COMPATIBLE; |
| 506 | } |
| 507 | |
| 508 | return self::METADATA_GOOD; |
| 509 | } |
| 510 | |
| 511 | /** @inheritDoc */ |
| 512 | protected function visibleMetadataFields() { |
| 513 | return [ 'objectname', 'imagedescription' ]; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * @param File $file |
| 518 | * @param IContextSource|false $context |
| 519 | * @return array[]|false |
| 520 | */ |
| 521 | public function formatMetadata( $file, $context = false ) { |
| 522 | $result = [ |
| 523 | 'visible' => [], |
| 524 | 'collapsed' => [] |
| 525 | ]; |
| 526 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 527 | if ( !$metadata || isset( $metadata['error'] ) ) { |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | /* @todo Add a formatter |
| 532 | $format = new FormatSVG( $metadata ); |
| 533 | $formatted = $format->getFormattedData(); |
| 534 | */ |
| 535 | |
| 536 | // Sort fields into visible and collapsed |
| 537 | $visibleFields = $this->visibleMetadataFields(); |
| 538 | |
| 539 | $showMeta = false; |
| 540 | foreach ( $metadata as $name => $value ) { |
| 541 | $tag = strtolower( $name ); |
| 542 | if ( isset( self::$metaConversion[$tag] ) ) { |
| 543 | $tag = strtolower( self::$metaConversion[$tag] ); |
| 544 | } else { |
| 545 | // Do not output other metadata not in list |
| 546 | continue; |
| 547 | } |
| 548 | $showMeta = true; |
| 549 | self::addMeta( $result, |
| 550 | in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed', |
| 551 | 'exif', |
| 552 | $tag, |
| 553 | $value |
| 554 | ); |
| 555 | } |
| 556 | |
| 557 | return $showMeta ? $result : false; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * @param string $name Parameter name |
| 562 | * @param mixed $value Parameter value |
| 563 | * @return bool Validity |
| 564 | */ |
| 565 | public function validateParam( $name, $value ) { |
| 566 | if ( in_array( $name, [ 'width', 'height' ] ) ) { |
| 567 | // Reject negative heights, widths |
| 568 | return ( $value > 0 ); |
| 569 | } |
| 570 | if ( $name === 'lang' ) { |
| 571 | // Validate $code |
| 572 | if ( $value === '' |
| 573 | || !LanguageCode::isWellFormedLanguageTag( $value ) |
| 574 | ) { |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | return true; |
| 579 | } |
| 580 | |
| 581 | // Only lang, width and height are acceptable keys |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * @param array $params Name=>value pairs of parameters |
| 587 | * @return string|false Filename to use |
| 588 | */ |
| 589 | public function makeParamString( $params ) { |
| 590 | $lang = ''; |
| 591 | $code = $this->getLanguageFromParams( $params ); |
| 592 | if ( $code !== self::SVG_DEFAULT_RENDER_LANG ) { |
| 593 | $lang = 'lang' . strtolower( $code ) . '-'; |
| 594 | } |
| 595 | |
| 596 | if ( isset( $params['physicalWidth'] ) && $params['physicalWidth'] ) { |
| 597 | return "$lang{$params['physicalWidth']}px"; |
| 598 | } |
| 599 | |
| 600 | if ( !isset( $params['width'] ) ) { |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | return "$lang{$params['width']}px"; |
| 605 | } |
| 606 | |
| 607 | /** @inheritDoc */ |
| 608 | public function parseParamString( $str ) { |
| 609 | $m = false; |
| 610 | // Language codes are supposed to be lowercase |
| 611 | if ( preg_match( '/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/', $str, $m ) ) { |
| 612 | if ( LanguageCode::isWellFormedLanguageTag( $m[1] ) ) { |
| 613 | return [ 'width' => array_pop( $m ), 'lang' => $m[1] ]; |
| 614 | } |
| 615 | return [ 'width' => array_pop( $m ), 'lang' => self::SVG_DEFAULT_RENDER_LANG ]; |
| 616 | } |
| 617 | if ( preg_match( '/^(\d+)px$/', $str, $m ) ) { |
| 618 | return [ 'width' => $m[1], 'lang' => self::SVG_DEFAULT_RENDER_LANG ]; |
| 619 | } |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | /** @inheritDoc */ |
| 624 | public function getParamMap() { |
| 625 | return [ 'img_lang' => 'lang', 'img_width' => 'width' ]; |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * @param array $params |
| 630 | * @return array |
| 631 | */ |
| 632 | protected function getScriptParams( $params ) { |
| 633 | $scriptParams = [ 'width' => $params['width'] ]; |
| 634 | if ( isset( $params['lang'] ) ) { |
| 635 | $scriptParams['lang'] = $params['lang']; |
| 636 | } |
| 637 | |
| 638 | return $scriptParams; |
| 639 | } |
| 640 | |
| 641 | /** @inheritDoc */ |
| 642 | public function getCommonMetaArray( File $file ) { |
| 643 | $metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 644 | if ( !$metadata || isset( $metadata['error'] ) ) { |
| 645 | return []; |
| 646 | } |
| 647 | $stdMetadata = []; |
| 648 | foreach ( $metadata as $name => $value ) { |
| 649 | $tag = strtolower( $name ); |
| 650 | if ( $tag === 'originalwidth' || $tag === 'originalheight' ) { |
| 651 | // Skip these. In the exif metadata stuff, it is assumed these |
| 652 | // are measured in px, which is not the case here. |
| 653 | continue; |
| 654 | } |
| 655 | if ( isset( self::$metaConversion[$tag] ) ) { |
| 656 | $tag = self::$metaConversion[$tag]; |
| 657 | $stdMetadata[$tag] = $value; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return $stdMetadata; |
| 662 | } |
| 663 | } |