| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\ResourceLoader; |
| 22 |
|
| 23 |
use DOMDocument; |
| 24 |
use InvalidArgumentException; |
| 25 |
use InvalidSVGException; |
| 26 |
use MediaWiki\Languages\LanguageFallback; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\MediaWikiServices; |
| 29 |
use MediaWiki\Shell\Shell; |
| 30 |
use RuntimeException; |
| 31 |
use SvgHandler; |
| 32 |
use SVGReader; |
| 33 |
use Wikimedia\FileBackend\FileBackend; |
| 34 |
use Wikimedia\Minify\CSSMin; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class Image { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private const FILE_TYPES = [ |
| 47 |
'svg' => 'image/svg+xml', |
| 48 |
'png' => 'image/png', |
| 49 |
'gif' => 'image/gif', |
| 50 |
'jpg' => 'image/jpg', |
| 51 |
]; |
| 52 |
|
| 53 |
|
| 54 |
private $name; |
| 55 |
|
| 56 |
private $module; |
| 57 |
|
| 58 |
private $descriptor; |
| 59 |
|
| 60 |
private $basePath; |
| 61 |
|
| 62 |
private $variants; |
| 63 |
|
| 64 |
private $defaultColor; |
| 65 |
|
| 66 |
private $extension; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function __construct( $name, $module, $descriptor, $basePath, array $variants, |
| 78 |
$defaultColor = null |
| 79 |
) { |
| 80 |
$this->name = $name; |
| 81 |
$this->module = $module; |
| 82 |
$this->descriptor = $descriptor; |
| 83 |
$this->basePath = $basePath; |
| 84 |
$this->variants = $variants; |
| 85 |
$this->defaultColor = $defaultColor; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
] |
| 90 |
if ( is_array( $this->descriptor ) && isset( $this->descriptor['lang'] ) ) { |
| 91 |
foreach ( $this->descriptor['lang'] as $langList => $_ ) { |
| 92 |
if ( strpos( $langList, ',' ) !== false ) { |
| 93 |
$this->descriptor['lang'] += array_fill_keys( |
| 94 |
explode( ',', $langList ), |
| 95 |
$this->descriptor['lang'][$langList] |
| 96 |
); |
| 97 |
unset( $this->descriptor['lang'][$langList] ); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if ( is_array( $this->descriptor ) ) { |
| 103 |
unset( $this->descriptor['deprecated'] ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
$extensions = []; |
| 108 |
$descriptor = is_array( $this->descriptor ) ? $this->descriptor : [ $this->descriptor ]; |
| 109 |
array_walk_recursive( $descriptor, function ( $path ) use ( &$extensions ) { |
| 110 |
$extensions[] = pathinfo( $this->getLocalPath( $path ), PATHINFO_EXTENSION ); |
| 111 |
} ); |
| 112 |
$extensions = array_unique( $extensions ); |
| 113 |
if ( count( $extensions ) !== 1 ) { |
| 114 |
throw new InvalidArgumentException( |
| 115 |
"File type for different image files of '$name' not the same in module '$module'" |
| 116 |
); |
| 117 |
} |
| 118 |
$ext = $extensions[0]; |
| 119 |
if ( !isset( self::FILE_TYPES[$ext] ) ) { |
| 120 |
throw new InvalidArgumentException( |
| 121 |
"Invalid file type for image files of '$name' (valid: svg, png, gif, jpg) in module '$module'" |
| 122 |
); |
| 123 |
} |
| 124 |
$this->extension = $ext; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function getName() { |
| 133 |
return $this->name; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function getModule() { |
| 142 |
return $this->module; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function getVariants(): array { |
| 151 |
return array_keys( $this->variants ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
protected function getLangFallbacks( string $lang ): array { |
| 160 |
return MediaWikiServices::getInstance() |
| 161 |
->getLanguageFallback() |
| 162 |
->getAll( $lang, LanguageFallback::STRICT ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function getPath( Context $context ) { |
| 172 |
$desc = $this->descriptor; |
| 173 |
if ( !is_array( $desc ) ) { |
| 174 |
return $this->getLocalPath( $desc ); |
| 175 |
} |
| 176 |
if ( isset( $desc['lang'] ) ) { |
| 177 |
$contextLang = $context->getLanguage(); |
| 178 |
if ( isset( $desc['lang'][$contextLang] ) ) { |
| 179 |
return $this->getLocalPath( $desc['lang'][$contextLang] ); |
| 180 |
} |
| 181 |
$fallbacks = $this->getLangFallbacks( $contextLang ); |
| 182 |
foreach ( $fallbacks as $lang ) { |
| 183 |
if ( isset( $desc['lang'][$lang] ) ) { |
| 184 |
return $this->getLocalPath( $desc['lang'][$lang] ); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
if ( isset( $desc[$context->getDirection()] ) ) { |
| 189 |
return $this->getLocalPath( $desc[$context->getDirection()] ); |
| 190 |
} |
| 191 |
if ( isset( $desc['default'] ) ) { |
| 192 |
return $this->getLocalPath( $desc['default'] ); |
| 193 |
} |
| 194 |
throw new RuntimeException( 'No matching path found' ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
protected function getLocalPath( $path ) { |
| 202 |
if ( $path instanceof FilePath ) { |
| 203 |
return $path->getLocalPath(); |
| 204 |
} |
| 205 |
|
| 206 |
return "{$this->basePath}/$path"; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function getExtension( $format = 'original' ) { |
| 216 |
if ( $format === 'rasterized' && $this->extension === 'svg' ) { |
| 217 |
return 'png'; |
| 218 |
} |
| 219 |
return $this->extension; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public function getMimeType( $format = 'original' ) { |
| 229 |
$ext = $this->getExtension( $format ); |
| 230 |
return self::FILE_TYPES[$ext]; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function getUrl( Context $context, $script, $variant, $format ) { |
| 243 |
$query = [ |
| 244 |
'modules' => $this->getModule(), |
| 245 |
'image' => $this->getName(), |
| 246 |
'variant' => $variant, |
| 247 |
'format' => $format, |
| 248 |
|
| 249 |
|
| 250 |
'lang' => $context->getLanguage(), |
| 251 |
'skin' => $context->getSkin(), |
| 252 |
'version' => $context->getResourceLoader()->makeVersionQuery( $context, [ $this->getModule() ] ), |
| 253 |
]; |
| 254 |
|
| 255 |
return wfAppendQuery( $script, $query ); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public function getDataUri( Context $context, $variant, $format ) { |
| 267 |
$type = $this->getMimeType( $format ); |
| 268 |
$contents = $this->getImageData( $context, $variant, $format ); |
| 269 |
return CSSMin::encodeStringAsDataURI( $contents, $type ); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public function getImageData( Context $context, $variant = false, $format = false ) { |
| 288 |
if ( $variant === false ) { |
| 289 |
$variant = $context->getVariant(); |
| 290 |
} |
| 291 |
if ( $format === false ) { |
| 292 |
$format = $context->getFormat(); |
| 293 |
} |
| 294 |
|
| 295 |
$path = $this->getPath( $context ); |
| 296 |
if ( !file_exists( $path ) ) { |
| 297 |
throw new RuntimeException( "File '$path' does not exist" ); |
| 298 |
} |
| 299 |
|
| 300 |
if ( $this->getExtension() !== 'svg' ) { |
| 301 |
return file_get_contents( $path ); |
| 302 |
} |
| 303 |
|
| 304 |
if ( $variant && isset( $this->variants[$variant] ) ) { |
| 305 |
$data = $this->variantize( $this->variants[$variant], $context ); |
| 306 |
} else { |
| 307 |
$defaultColor = $this->defaultColor; |
| 308 |
$data = $defaultColor ? |
| 309 |
$this->variantize( [ 'color' => $defaultColor ], $context ) : |
| 310 |
file_get_contents( $path ); |
| 311 |
} |
| 312 |
|
| 313 |
if ( $format === 'rasterized' ) { |
| 314 |
$data = $this->rasterize( $data ); |
| 315 |
if ( !$data ) { |
| 316 |
$logger = $context->getResourceLoader()->getLogger(); |
| 317 |
$logger->error( __METHOD__ . " failed to rasterize for $path" ); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return $data; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public function sendResponseHeaders( Context $context ): void { |
| 333 |
$format = $context->getFormat(); |
| 334 |
$mime = $this->getMimeType( $format ); |
| 335 |
$filename = $this->getName() . '.' . $this->getExtension( $format ); |
| 336 |
|
| 337 |
header( 'Content-Type: ' . $mime ); |
| 338 |
header( 'Content-Disposition: ' . |
| 339 |
FileBackend::makeContentDisposition( 'inline', $filename ) ); |
| 340 |
header( 'Access-Control-Allow-Origin: *' ); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
protected function variantize( array $variantConf, Context $context ) { |
| 351 |
$dom = new DOMDocument; |
| 352 |
$dom->loadXML( file_get_contents( $this->getPath( $context ) ) ); |
| 353 |
$root = $dom->documentElement; |
| 354 |
$titleNode = null; |
| 355 |
$wrapper = $dom->createElementNS( 'http://www.w3.org/2000/svg', 'g' ); |
| 356 |
|
| 357 |
while ( $root->firstChild ) { |
| 358 |
$node = $root->firstChild; |
| 359 |
'@phan-var \DOMElement $node'; |
| 360 |
if ( !$titleNode && $node->nodeType === XML_ELEMENT_NODE && $node->tagName === 'title' ) { |
| 361 |
|
| 362 |
$titleNode = $node; |
| 363 |
} |
| 364 |
$wrapper->appendChild( $node ); |
| 365 |
} |
| 366 |
if ( $titleNode ) { |
| 367 |
|
| 368 |
$root->appendChild( $titleNode ); |
| 369 |
} |
| 370 |
$root->appendChild( $wrapper ); |
| 371 |
$wrapper->setAttribute( 'fill', $variantConf['color'] ); |
| 372 |
return $dom->saveXML(); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
protected function massageSvgPathdata( $svg ) { |
| 386 |
$dom = new DOMDocument; |
| 387 |
$dom->loadXML( $svg ); |
| 388 |
foreach ( $dom->getElementsByTagName( 'path' ) as $node ) { |
| 389 |
$pathData = $node->getAttribute( 'd' ); |
| 390 |
|
| 391 |
|
| 392 |
$pathData = preg_replace( '/(-?)(\d*\.\d+|\d+)/', ' ${1}0$2 ', $pathData ); |
| 393 |
|
| 394 |
$pathData = preg_replace( '/([ -])0(\d)/', '$1$2', $pathData ); |
| 395 |
$node->setAttribute( 'd', $pathData ); |
| 396 |
} |
| 397 |
return $dom->saveXML(); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
protected function rasterize( $svg ) { |
| 407 |
$svgConverter = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SVGConverter ); |
| 408 |
$svgConverterPath = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SVGConverterPath ); |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
$svg = $this->massageSvgPathdata( $svg ); |
| 426 |
|
| 427 |
|
| 428 |
if ( strpos( $svgConverter, 'rsvg' ) === 0 ) { |
| 429 |
$command = 'rsvg-convert'; |
| 430 |
if ( $svgConverterPath ) { |
| 431 |
$command = Shell::escape( "{$svgConverterPath}/" ) . $command; |
| 432 |
} |
| 433 |
|
| 434 |
$process = proc_open( |
| 435 |
$command, |
| 436 |
[ 0 => [ 'pipe', 'r' ], 1 => [ 'pipe', 'w' ] ], |
| 437 |
$pipes |
| 438 |
); |
| 439 |
|
| 440 |
if ( $process ) { |
| 441 |
fwrite( $pipes[0], $svg ); |
| 442 |
fclose( $pipes[0] ); |
| 443 |
$png = stream_get_contents( $pipes[1] ); |
| 444 |
fclose( $pipes[1] ); |
| 445 |
proc_close( $process ); |
| 446 |
|
| 447 |
return $png ?: false; |
| 448 |
} |
| 449 |
return false; |
| 450 |
|
| 451 |
} |
| 452 |
|
| 453 |
$tempFilenameSvg = tempnam( wfTempDir(), 'ResourceLoaderImage' ); |
| 454 |
$tempFilenamePng = tempnam( wfTempDir(), 'ResourceLoaderImage' ); |
| 455 |
|
| 456 |
file_put_contents( $tempFilenameSvg, $svg ); |
| 457 |
|
| 458 |
try { |
| 459 |
$svgReader = new SVGReader( $tempFilenameSvg ); |
| 460 |
} catch ( InvalidSVGException $e ) { |
| 461 |
|
| 462 |
throw new RuntimeException( 'Invalid SVG', 0, $e ); |
| 463 |
} |
| 464 |
$metadata = $svgReader->getMetadata(); |
| 465 |
if ( !isset( $metadata['width'] ) || !isset( $metadata['height'] ) ) { |
| 466 |
unlink( $tempFilenameSvg ); |
| 467 |
return false; |
| 468 |
} |
| 469 |
|
| 470 |
$handler = new SvgHandler; |
| 471 |
$res = $handler->rasterize( |
| 472 |
$tempFilenameSvg, |
| 473 |
$tempFilenamePng, |
| 474 |
$metadata['width'], |
| 475 |
$metadata['height'] |
| 476 |
); |
| 477 |
unlink( $tempFilenameSvg ); |
| 478 |
|
| 479 |
if ( $res === true ) { |
| 480 |
$png = file_get_contents( $tempFilenamePng ); |
| 481 |
unlink( $tempFilenamePng ); |
| 482 |
return $png; |
| 483 |
} |
| 484 |
|
| 485 |
return false; |
| 486 |
} |
| 487 |
} |