| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Context\IContextSource; |
| 25 |
use MediaWiki\Language\LanguageCode; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\MediaWikiServices; |
| 28 |
use MediaWiki\Shell\Shell; |
| 29 |
use Wikimedia\AtEase\AtEase; |
| 30 |
use Wikimedia\ScopedCallback; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class SvgHandler extends ImageHandler { |
| 38 |
public const SVG_METADATA_VERSION = 2; |
| 39 |
|
| 40 |
private const SVG_DEFAULT_RENDER_LANG = 'en'; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private static $metaConversion = [ |
| 47 |
'originalwidth' => 'ImageWidth', |
| 48 |
'originalheight' => 'ImageLength', |
| 49 |
'description' => 'ImageDescription', |
| 50 |
'title' => 'ObjectName', |
| 51 |
]; |
| 52 |
|
| 53 |
public function isEnabled() { |
| 54 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 55 |
$svgConverters = $config->get( MainConfigNames::SVGConverters ); |
| 56 |
$svgConverter = $config->get( MainConfigNames::SVGConverter ); |
| 57 |
if ( $config->get( MainConfigNames::SVGNativeRendering ) === true ) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
if ( !isset( $svgConverters[$svgConverter] ) ) { |
| 61 |
wfDebug( "\$wgSVGConverter is invalid, disabling SVG rendering." ); |
| 62 |
|
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
public function allowRenderingByUserAgent( $file ) { |
| 70 |
$svgNativeRendering = MediaWikiServices::getInstance() |
| 71 |
->getMainConfig()->get( MainConfigNames::SVGNativeRendering ); |
| 72 |
if ( $svgNativeRendering === true ) { |
| 73 |
|
| 74 |
return true; |
| 75 |
} |
| 76 |
if ( $svgNativeRendering !== 'partial' ) { |
| 77 |
|
| 78 |
return false; |
| 79 |
} |
| 80 |
$maxSVGFilesize = MediaWikiServices::getInstance() |
| 81 |
->getMainConfig()->get( MainConfigNames::SVGNativeRenderingSizeLimit ); |
| 82 |
|
| 83 |
|
| 84 |
return count( $this->getAvailableLanguages( $file ) ) <= 1 |
| 85 |
&& $file->getSize() <= $maxSVGFilesize; |
| 86 |
} |
| 87 |
|
| 88 |
public function mustRender( $file ) { |
| 89 |
return !$this->allowRenderingByUserAgent( $file ); |
| 90 |
} |
| 91 |
|
| 92 |
public function isVectorized( $file ) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function isAnimatedImage( $file ) { |
| 101 |
|
| 102 |
$metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 103 |
if ( isset( $metadata['animated'] ) ) { |
| 104 |
return $metadata['animated']; |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function getAvailableLanguages( File $file ) { |
| 123 |
$langList = []; |
| 124 |
$metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 125 |
if ( isset( $metadata['translations'] ) ) { |
| 126 |
foreach ( $metadata['translations'] as $lang => $langType ) { |
| 127 |
if ( $langType === SVGReader::LANG_FULL_MATCH ) { |
| 128 |
$langList[] = strtolower( $lang ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
return array_unique( $langList ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function getMatchedLanguage( $userPreferredLanguage, array $svgLanguages ) { |
| 151 |
|
| 152 |
if ( $userPreferredLanguage === 'und' ) { |
| 153 |
return 'und'; |
| 154 |
} |
| 155 |
foreach ( $svgLanguages as $svgLang ) { |
| 156 |
if ( strcasecmp( $svgLang, $userPreferredLanguage ) === 0 ) { |
| 157 |
return $svgLang; |
| 158 |
} |
| 159 |
$trimmedSvgLang = $svgLang; |
| 160 |
while ( strpos( $trimmedSvgLang, '-' ) !== false ) { |
| 161 |
$trimmedSvgLang = substr( $trimmedSvgLang, 0, strrpos( $trimmedSvgLang, '-' ) ); |
| 162 |
if ( strcasecmp( $trimmedSvgLang, $userPreferredLanguage ) === 0 ) { |
| 163 |
return $svgLang; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
return null; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
protected function getLanguageFromParams( array $params ) { |
| 178 |
return $params['lang'] ?? $params['targetlang'] ?? self::SVG_DEFAULT_RENDER_LANG; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public function getDefaultRenderLanguage( File $file ) { |
| 188 |
return self::SVG_DEFAULT_RENDER_LANG; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function canAnimateThumbnail( $file ) { |
| 197 |
return $this->allowRenderingByUserAgent( $file ); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function normaliseParams( $image, &$params ) { |
| 206 |
if ( parent::normaliseParams( $image, $params ) ) { |
| 207 |
$params = $this->normaliseParamsInternal( $image, $params ); |
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
protected function normaliseParamsInternal( $image, $params ) { |
| 224 |
$svgMaxSize = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SVGMaxSize ); |
| 225 |
|
| 226 |
$srcWidth = $image->getWidth( $params['page'] ); |
| 227 |
$srcHeight = $image->getHeight( $params['page'] ); |
| 228 |
$params['physicalWidth'] = $this->getSteppedThumbWidth( |
| 229 |
$image, $params['physicalWidth'], $srcWidth, $srcHeight |
| 230 |
); |
| 231 |
$params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $params['physicalWidth'] ); |
| 232 |
|
| 233 |
|
| 234 |
if ( $params['physicalWidth'] <= $params['physicalHeight'] ) { |
| 235 |
if ( $params['physicalWidth'] > $svgMaxSize ) { |
| 236 |
$params['physicalWidth'] = $svgMaxSize; |
| 237 |
$params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $svgMaxSize ); |
| 238 |
} |
| 239 |
} elseif ( $params['physicalHeight'] > $svgMaxSize ) { |
| 240 |
$params['physicalWidth'] = File::scaleHeight( $srcHeight, $srcWidth, $svgMaxSize ); |
| 241 |
$params['physicalHeight'] = $svgMaxSize; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
if ( isset( $params['targetlang'] ) && !$image->getMatchedLanguage( $params['targetlang'] ) ) { |
| 246 |
unset( $params['targetlang'] ); |
| 247 |
} |
| 248 |
|
| 249 |
return $params; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { |
| 261 |
if ( !$this->normaliseParams( $image, $params ) ) { |
| 262 |
return new TransformParameterError( $params ); |
| 263 |
} |
| 264 |
$clientWidth = $params['width']; |
| 265 |
$clientHeight = $params['height']; |
| 266 |
$physicalWidth = $params['physicalWidth']; |
| 267 |
$physicalHeight = $params['physicalHeight']; |
| 268 |
$lang = $this->getLanguageFromParams( $params ); |
| 269 |
|
| 270 |
if ( $this->allowRenderingByUserAgent( $image ) ) { |
| 271 |
|
| 272 |
return new ThumbnailImage( $image, $image->getURL(), false, $params ); |
| 273 |
} |
| 274 |
|
| 275 |
if ( $flags & self::TRANSFORM_LATER ) { |
| 276 |
return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); |
| 277 |
} |
| 278 |
|
| 279 |
$metadata = $this->validateMetadata( $image->getMetadataArray() ); |
| 280 |
if ( isset( $metadata['error'] ) ) { |
| 281 |
$err = wfMessage( 'svg-long-error', $metadata['error']['message'] ); |
| 282 |
|
| 283 |
return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, $err ); |
| 284 |
} |
| 285 |
|
| 286 |
if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) { |
| 287 |
return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, |
| 288 |
wfMessage( 'thumbnail_dest_directory' ) ); |
| 289 |
} |
| 290 |
|
| 291 |
$srcPath = $image->getLocalRefPath(); |
| 292 |
if ( $srcPath === false ) { |
| 293 |
wfDebugLog( 'thumbnail', |
| 294 |
sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"', |
| 295 |
wfHostname(), $image->getName() ) ); |
| 296 |
|
| 297 |
return new MediaTransformError( 'thumbnail_error', |
| 298 |
$params['width'], $params['height'], |
| 299 |
wfMessage( 'filemissing' ) |
| 300 |
); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
$tmpDir = wfTempDir() . '/svg_' . wfRandomString( 24 ); |
| 307 |
$lnPath = "$tmpDir/" . basename( $srcPath ); |
| 308 |
$ok = mkdir( $tmpDir, 0771 ); |
| 309 |
if ( !$ok ) { |
| 310 |
wfDebugLog( 'thumbnail', |
| 311 |
sprintf( 'Thumbnail failed on %s: could not create temporary directory %s', |
| 312 |
wfHostname(), $tmpDir ) ); |
| 313 |
return new MediaTransformError( 'thumbnail_error', |
| 314 |
$params['width'], $params['height'], |
| 315 |
wfMessage( 'thumbnail-temp-create' )->text() |
| 316 |
); |
| 317 |
} |
| 318 |
|
| 319 |
$ok = @symlink( $srcPath, $lnPath ); |
| 320 |
|
| 321 |
$cleaner = new ScopedCallback( static function () use ( $tmpDir, $lnPath ) { |
| 322 |
AtEase::suppressWarnings(); |
| 323 |
unlink( $lnPath ); |
| 324 |
rmdir( $tmpDir ); |
| 325 |
AtEase::restoreWarnings(); |
| 326 |
} ); |
| 327 |
if ( !$ok ) { |
| 328 |
|
| 329 |
$ok = copy( $srcPath, $lnPath ); |
| 330 |
} |
| 331 |
if ( !$ok ) { |
| 332 |
wfDebugLog( 'thumbnail', |
| 333 |
sprintf( 'Thumbnail failed on %s: could not link %s to %s', |
| 334 |
wfHostname(), $lnPath, $srcPath ) ); |
| 335 |
return new MediaTransformError( 'thumbnail_error', |
| 336 |
$params['width'], $params['height'], |
| 337 |
wfMessage( 'thumbnail-temp-create' ) |
| 338 |
); |
| 339 |
} |
| 340 |
|
| 341 |
$status = $this->rasterize( $lnPath, $dstPath, $physicalWidth, $physicalHeight, $lang ); |
| 342 |
if ( $status === true ) { |
| 343 |
return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); |
| 344 |
} |
| 345 |
|
| 346 |
return $status; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public function rasterize( $srcPath, $dstPath, $width, $height, $lang = false ) { |
| 360 |
$mainConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 361 |
$svgConverters = $mainConfig->get( MainConfigNames::SVGConverters ); |
| 362 |
$svgConverter = $mainConfig->get( MainConfigNames::SVGConverter ); |
| 363 |
$svgConverterPath = $mainConfig->get( MainConfigNames::SVGConverterPath ); |
| 364 |
$err = false; |
| 365 |
$retval = ''; |
| 366 |
if ( isset( $svgConverters[$svgConverter] ) ) { |
| 367 |
if ( is_array( $svgConverters[$svgConverter] ) ) { |
| 368 |
|
| 369 |
$func = $svgConverters[$svgConverter][0]; |
| 370 |
if ( !is_callable( $func ) ) { |
| 371 |
throw new UnexpectedValueException( "$func is not callable" ); |
| 372 |
} |
| 373 |
$err = $func( $srcPath, |
| 374 |
$dstPath, |
| 375 |
$width, |
| 376 |
$height, |
| 377 |
$lang, |
| 378 |
...array_slice( $svgConverters[$svgConverter], 1 ) |
| 379 |
); |
| 380 |
$retval = (bool)$err; |
| 381 |
} else { |
| 382 |
|
| 383 |
$cmd = strtr( $svgConverters[$svgConverter], [ |
| 384 |
'$path/' => $svgConverterPath ? Shell::escape( "$svgConverterPath/" ) : '', |
| 385 |
'$width' => (int)$width, |
| 386 |
'$height' => (int)$height, |
| 387 |
'$input' => Shell::escape( $srcPath ), |
| 388 |
'$output' => Shell::escape( $dstPath ), |
| 389 |
] ); |
| 390 |
|
| 391 |
$env = []; |
| 392 |
if ( $lang !== false ) { |
| 393 |
$env['LANG'] = $lang; |
| 394 |
} |
| 395 |
|
| 396 |
wfDebug( __METHOD__ . ": $cmd" ); |
| 397 |
$err = wfShellExecWithStderr( $cmd, $retval, $env ); |
| 398 |
} |
| 399 |
} |
| 400 |
$removed = $this->removeBadFile( $dstPath, $retval ); |
| 401 |
if ( $retval != 0 || $removed ) { |
| 402 |
|
| 403 |
|
| 404 |
$this->logErrorForExternalProcess( $retval, $err, $cmd ); |
| 405 |
return new MediaTransformError( 'thumbnail_error', $width, $height, $err ); |
| 406 |
} |
| 407 |
|
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
public static function rasterizeImagickExt( $srcPath, $dstPath, $width, $height ) { |
| 412 |
$im = new Imagick( $srcPath ); |
| 413 |
$im->setBackgroundColor( 'transparent' ); |
| 414 |
$im->readImage( $srcPath ); |
| 415 |
$im->setImageFormat( 'png' ); |
| 416 |
$im->setImageDepth( 8 ); |
| 417 |
|
| 418 |
if ( !$im->thumbnailImage( (int)$width, (int)$height, false ) ) { |
| 419 |
return 'Could not resize image'; |
| 420 |
} |
| 421 |
if ( !$im->writeImage( $dstPath ) ) { |
| 422 |
return "Could not write to $dstPath"; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
public function getThumbType( $ext, $mime, $params = null ) { |
| 427 |
return [ 'png', 'image/png' ]; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public function getLongDesc( $file ) { |
| 440 |
$metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 441 |
if ( isset( $metadata['error'] ) ) { |
| 442 |
return wfMessage( 'svg-long-error', $metadata['error']['message'] )->text(); |
| 443 |
} |
| 444 |
|
| 445 |
if ( $this->isAnimatedImage( $file ) ) { |
| 446 |
$msg = wfMessage( 'svg-long-desc-animated' ); |
| 447 |
} else { |
| 448 |
$msg = wfMessage( 'svg-long-desc' ); |
| 449 |
} |
| 450 |
|
| 451 |
return $msg->numParams( $file->getWidth(), $file->getHeight() )->sizeParams( $file->getSize() )->parse(); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
public function getSizeAndMetadata( $state, $filename ) { |
| 460 |
$metadata = [ 'version' => self::SVG_METADATA_VERSION ]; |
| 461 |
|
| 462 |
try { |
| 463 |
$svgReader = new SVGReader( $filename ); |
| 464 |
$metadata += $svgReader->getMetadata(); |
| 465 |
} catch ( InvalidSVGException $e ) { |
| 466 |
|
| 467 |
$metadata['error'] = [ |
| 468 |
'message' => $e->getMessage(), |
| 469 |
'code' => $e->getCode() |
| 470 |
]; |
| 471 |
wfDebug( __METHOD__ . ': ' . $e->getMessage() ); |
| 472 |
} |
| 473 |
|
| 474 |
return [ |
| 475 |
'width' => $metadata['width'] ?? 0, |
| 476 |
'height' => $metadata['height'] ?? 0, |
| 477 |
'metadata' => $metadata |
| 478 |
]; |
| 479 |
} |
| 480 |
|
| 481 |
protected function validateMetadata( $unser ) { |
| 482 |
if ( isset( $unser['version'] ) && $unser['version'] === self::SVG_METADATA_VERSION ) { |
| 483 |
return $unser; |
| 484 |
} |
| 485 |
|
| 486 |
return null; |
| 487 |
} |
| 488 |
|
| 489 |
public function getMetadataType( $image ) { |
| 490 |
return 'parsed-svg'; |
| 491 |
} |
| 492 |
|
| 493 |
public function isFileMetadataValid( $image ) { |
| 494 |
$meta = $this->validateMetadata( $image->getMetadataArray() ); |
| 495 |
if ( !$meta ) { |
| 496 |
return self::METADATA_BAD; |
| 497 |
} |
| 498 |
if ( !isset( $meta['originalWidth'] ) ) { |
| 499 |
|
| 500 |
return self::METADATA_COMPATIBLE; |
| 501 |
} |
| 502 |
|
| 503 |
return self::METADATA_GOOD; |
| 504 |
} |
| 505 |
|
| 506 |
protected function visibleMetadataFields() { |
| 507 |
return [ 'objectname', 'imagedescription' ]; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public function formatMetadata( $file, $context = false ) { |
| 516 |
$result = [ |
| 517 |
'visible' => [], |
| 518 |
'collapsed' => [] |
| 519 |
]; |
| 520 |
$metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 521 |
if ( !$metadata || isset( $metadata['error'] ) ) { |
| 522 |
return false; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
$visibleFields = $this->visibleMetadataFields(); |
| 532 |
|
| 533 |
$showMeta = false; |
| 534 |
foreach ( $metadata as $name => $value ) { |
| 535 |
$tag = strtolower( $name ); |
| 536 |
if ( isset( self::$metaConversion[$tag] ) ) { |
| 537 |
$tag = strtolower( self::$metaConversion[$tag] ); |
| 538 |
} else { |
| 539 |
|
| 540 |
continue; |
| 541 |
} |
| 542 |
$showMeta = true; |
| 543 |
self::addMeta( $result, |
| 544 |
in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed', |
| 545 |
'exif', |
| 546 |
$tag, |
| 547 |
$value |
| 548 |
); |
| 549 |
} |
| 550 |
|
| 551 |
return $showMeta ? $result : false; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
public function validateParam( $name, $value ) { |
| 560 |
if ( in_array( $name, [ 'width', 'height' ] ) ) { |
| 561 |
|
| 562 |
return ( $value > 0 ); |
| 563 |
} |
| 564 |
if ( $name === 'lang' ) { |
| 565 |
|
| 566 |
if ( $value === '' |
| 567 |
|| !LanguageCode::isWellFormedLanguageTag( $value ) |
| 568 |
) { |
| 569 |
return false; |
| 570 |
} |
| 571 |
|
| 572 |
return true; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
return false; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public function makeParamString( $params ) { |
| 584 |
$lang = ''; |
| 585 |
$code = $this->getLanguageFromParams( $params ); |
| 586 |
if ( $code !== self::SVG_DEFAULT_RENDER_LANG ) { |
| 587 |
$lang = 'lang' . strtolower( $code ) . '-'; |
| 588 |
} |
| 589 |
|
| 590 |
if ( isset( $params['physicalWidth'] ) && $params['physicalWidth'] ) { |
| 591 |
return "$lang{$params['physicalWidth']}px"; |
| 592 |
} |
| 593 |
|
| 594 |
if ( !isset( $params['width'] ) ) { |
| 595 |
return false; |
| 596 |
} |
| 597 |
|
| 598 |
return "$lang{$params['width']}px"; |
| 599 |
} |
| 600 |
|
| 601 |
public function parseParamString( $str ) { |
| 602 |
$m = false; |
| 603 |
|
| 604 |
if ( preg_match( '/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/', $str, $m ) ) { |
| 605 |
if ( LanguageCode::isWellFormedLanguageTag( $m[1] ) ) { |
| 606 |
return [ 'width' => array_pop( $m ), 'lang' => $m[1] ]; |
| 607 |
} |
| 608 |
return [ 'width' => array_pop( $m ), 'lang' => self::SVG_DEFAULT_RENDER_LANG ]; |
| 609 |
} |
| 610 |
if ( preg_match( '/^(\d+)px$/', $str, $m ) ) { |
| 611 |
return [ 'width' => $m[1], 'lang' => self::SVG_DEFAULT_RENDER_LANG ]; |
| 612 |
} |
| 613 |
return false; |
| 614 |
} |
| 615 |
|
| 616 |
public function getParamMap() { |
| 617 |
return [ 'img_lang' => 'lang', 'img_width' => 'width' ]; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
protected function getScriptParams( $params ) { |
| 625 |
$scriptParams = [ 'width' => $params['width'] ]; |
| 626 |
if ( isset( $params['lang'] ) ) { |
| 627 |
$scriptParams['lang'] = $params['lang']; |
| 628 |
} |
| 629 |
|
| 630 |
return $scriptParams; |
| 631 |
} |
| 632 |
|
| 633 |
public function getCommonMetaArray( File $file ) { |
| 634 |
$metadata = $this->validateMetadata( $file->getMetadataArray() ); |
| 635 |
if ( !$metadata || isset( $metadata['error'] ) ) { |
| 636 |
return []; |
| 637 |
} |
| 638 |
$stdMetadata = []; |
| 639 |
foreach ( $metadata as $name => $value ) { |
| 640 |
$tag = strtolower( $name ); |
| 641 |
if ( $tag === 'originalwidth' || $tag === 'originalheight' ) { |
| 642 |
|
| 643 |
|
| 644 |
continue; |
| 645 |
} |
| 646 |
if ( isset( self::$metaConversion[$tag] ) ) { |
| 647 |
$tag = self::$metaConversion[$tag]; |
| 648 |
$stdMetadata[$tag] = $value; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
return $stdMetadata; |
| 653 |
} |
| 654 |
} |