| 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 DomainException; |
| 24 |
use InvalidArgumentException; |
| 25 |
use Wikimedia\Minify\CSSMin; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class ImageModule extends Module { |
| 34 |
|
| 35 |
private $useMaskImage; |
| 36 |
|
| 37 |
protected $definition; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected $localBasePath = ''; |
| 44 |
|
| 45 |
|
| 46 |
protected $origin = self::ORIGIN_CORE_SITEWIDE; |
| 47 |
|
| 48 |
|
| 49 |
protected $imageObjects = null; |
| 50 |
|
| 51 |
protected $images = []; |
| 52 |
|
| 53 |
protected $defaultColor = null; |
| 54 |
|
| 55 |
protected $useDataURI = true; |
| 56 |
|
| 57 |
protected $globalVariants = null; |
| 58 |
|
| 59 |
protected $variants = []; |
| 60 |
|
| 61 |
protected $prefix = null; |
| 62 |
|
| 63 |
protected $selectorWithoutVariant = '.{prefix}-{name}'; |
| 64 |
|
| 65 |
protected $selectorWithVariant = '.{prefix}-{name}-{variant}'; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function __construct( array $options = [], $localBasePath = null ) { |
| 126 |
$this->useMaskImage = $options['useMaskImage'] ?? false; |
| 127 |
$this->localBasePath = static::extractLocalBasePath( $options, $localBasePath ); |
| 128 |
|
| 129 |
$this->definition = $options; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
protected function loadFromDefinition() { |
| 136 |
if ( $this->definition === null ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
$options = $this->definition; |
| 141 |
$this->definition = null; |
| 142 |
|
| 143 |
if ( isset( $options['data'] ) ) { |
| 144 |
$dataPath = $this->getLocalPath( $options['data'] ); |
| 145 |
$data = json_decode( file_get_contents( $dataPath ), true ); |
| 146 |
$options = array_merge( $data, $options ); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
$prefix = isset( $options['prefix'] ) && $options['prefix']; |
| 157 |
$selector = isset( $options['selector'] ) && $options['selector']; |
| 158 |
$selectorWithoutVariant = isset( $options['selectorWithoutVariant'] ) |
| 159 |
&& $options['selectorWithoutVariant']; |
| 160 |
$selectorWithVariant = isset( $options['selectorWithVariant'] ) |
| 161 |
&& $options['selectorWithVariant']; |
| 162 |
|
| 163 |
if ( $selectorWithoutVariant && !$selectorWithVariant ) { |
| 164 |
throw new InvalidArgumentException( |
| 165 |
"Given 'selectorWithoutVariant' but no 'selectorWithVariant'." |
| 166 |
); |
| 167 |
} |
| 168 |
if ( $selectorWithVariant && !$selectorWithoutVariant ) { |
| 169 |
throw new InvalidArgumentException( |
| 170 |
"Given 'selectorWithVariant' but no 'selectorWithoutVariant'." |
| 171 |
); |
| 172 |
} |
| 173 |
if ( $selector && $selectorWithVariant ) { |
| 174 |
throw new InvalidArgumentException( |
| 175 |
"Incompatible 'selector' and 'selectorWithVariant'+'selectorWithoutVariant' given." |
| 176 |
); |
| 177 |
} |
| 178 |
if ( !$prefix && !$selector && !$selectorWithVariant ) { |
| 179 |
throw new InvalidArgumentException( |
| 180 |
"None of 'prefix', 'selector' or 'selectorWithVariant'+'selectorWithoutVariant' given." |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
foreach ( $options as $member => $option ) { |
| 185 |
switch ( $member ) { |
| 186 |
case 'images': |
| 187 |
case 'variants': |
| 188 |
if ( !is_array( $option ) ) { |
| 189 |
throw new InvalidArgumentException( |
| 190 |
"Invalid list error. '$option' given, array expected." |
| 191 |
); |
| 192 |
} |
| 193 |
if ( !isset( $option['default'] ) ) { |
| 194 |
|
| 195 |
$option = [ 'default' => $option ]; |
| 196 |
} |
| 197 |
foreach ( $option as $data ) { |
| 198 |
if ( !is_array( $data ) ) { |
| 199 |
throw new InvalidArgumentException( |
| 200 |
"Invalid list error. '$data' given, array expected." |
| 201 |
); |
| 202 |
} |
| 203 |
} |
| 204 |
$this->{$member} = $option; |
| 205 |
break; |
| 206 |
|
| 207 |
case 'useDataURI': |
| 208 |
$this->{$member} = (bool)$option; |
| 209 |
break; |
| 210 |
case 'defaultColor': |
| 211 |
case 'prefix': |
| 212 |
case 'selectorWithoutVariant': |
| 213 |
case 'selectorWithVariant': |
| 214 |
$this->{$member} = (string)$option; |
| 215 |
break; |
| 216 |
|
| 217 |
case 'selector': |
| 218 |
$this->selectorWithoutVariant = $this->selectorWithVariant = (string)$option; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function getPrefix() { |
| 228 |
$this->loadFromDefinition(); |
| 229 |
return $this->prefix; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public function getSelectors() { |
| 237 |
$this->loadFromDefinition(); |
| 238 |
return [ |
| 239 |
'selectorWithoutVariant' => $this->selectorWithoutVariant, |
| 240 |
'selectorWithVariant' => $this->selectorWithVariant, |
| 241 |
]; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public function getImage( $name, Context $context ): ?Image { |
| 251 |
$this->loadFromDefinition(); |
| 252 |
$images = $this->getImages( $context ); |
| 253 |
return $images[$name] ?? null; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function getImages( Context $context ): array { |
| 262 |
$skin = $context->getSkin(); |
| 263 |
if ( $this->imageObjects === null ) { |
| 264 |
$this->loadFromDefinition(); |
| 265 |
$this->imageObjects = []; |
| 266 |
} |
| 267 |
if ( !isset( $this->imageObjects[$skin] ) ) { |
| 268 |
$this->imageObjects[$skin] = []; |
| 269 |
if ( !isset( $this->images[$skin] ) ) { |
| 270 |
$this->images[$skin] = $this->images['default'] ?? []; |
| 271 |
} |
| 272 |
foreach ( $this->images[$skin] as $name => $options ) { |
| 273 |
$fileDescriptor = is_array( $options ) ? $options['file'] : $options; |
| 274 |
|
| 275 |
$allowedVariants = array_merge( |
| 276 |
( is_array( $options ) && isset( $options['variants'] ) ) ? $options['variants'] : [], |
| 277 |
$this->getGlobalVariants( $context ) |
| 278 |
); |
| 279 |
if ( isset( $this->variants[$skin] ) ) { |
| 280 |
$variantConfig = array_intersect_key( |
| 281 |
$this->variants[$skin], |
| 282 |
array_fill_keys( $allowedVariants, true ) |
| 283 |
); |
| 284 |
} else { |
| 285 |
$variantConfig = []; |
| 286 |
} |
| 287 |
|
| 288 |
$image = new Image( |
| 289 |
$name, |
| 290 |
$this->getName(), |
| 291 |
$fileDescriptor, |
| 292 |
$this->localBasePath, |
| 293 |
$variantConfig, |
| 294 |
$this->defaultColor |
| 295 |
); |
| 296 |
$this->imageObjects[$skin][$image->getName()] = $image; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
return $this->imageObjects[$skin]; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public function getGlobalVariants( Context $context ): array { |
| 310 |
$skin = $context->getSkin(); |
| 311 |
if ( $this->globalVariants === null ) { |
| 312 |
$this->loadFromDefinition(); |
| 313 |
$this->globalVariants = []; |
| 314 |
} |
| 315 |
if ( !isset( $this->globalVariants[$skin] ) ) { |
| 316 |
$this->globalVariants[$skin] = []; |
| 317 |
if ( !isset( $this->variants[$skin] ) ) { |
| 318 |
$this->variants[$skin] = $this->variants['default'] ?? []; |
| 319 |
} |
| 320 |
foreach ( $this->variants[$skin] as $name => $config ) { |
| 321 |
if ( $config['global'] ?? false ) { |
| 322 |
$this->globalVariants[$skin][] = $name; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
return $this->globalVariants[$skin]; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public function getStyles( Context $context ): array { |
| 335 |
$this->loadFromDefinition(); |
| 336 |
|
| 337 |
|
| 338 |
$rules = []; |
| 339 |
|
| 340 |
$sources = $oldSources = $context->getResourceLoader()->getSources(); |
| 341 |
$this->getHookRunner()->onResourceLoaderModifyEmbeddedSourceUrls( $sources ); |
| 342 |
if ( array_keys( $sources ) !== array_keys( $oldSources ) ) { |
| 343 |
throw new DomainException( 'ResourceLoaderModifyEmbeddedSourceUrls hook must not add or remove sources' ); |
| 344 |
} |
| 345 |
$script = $sources[ $this->getSource() ]; |
| 346 |
|
| 347 |
$selectors = $this->getSelectors(); |
| 348 |
|
| 349 |
foreach ( $this->getImages( $context ) as $name => $image ) { |
| 350 |
$declarations = $this->getStyleDeclarations( $context, $image, $script ); |
| 351 |
$selector = strtr( |
| 352 |
$selectors['selectorWithoutVariant'], |
| 353 |
[ |
| 354 |
'{prefix}' => $this->getPrefix(), |
| 355 |
'{name}' => $name, |
| 356 |
'{variant}' => '', |
| 357 |
] |
| 358 |
); |
| 359 |
$rules[] = "$selector {\n\t$declarations\n}"; |
| 360 |
|
| 361 |
foreach ( $image->getVariants() as $variant ) { |
| 362 |
$declarations = $this->getStyleDeclarations( $context, $image, $script, $variant ); |
| 363 |
$selector = strtr( |
| 364 |
$selectors['selectorWithVariant'], |
| 365 |
[ |
| 366 |
'{prefix}' => $this->getPrefix(), |
| 367 |
'{name}' => $name, |
| 368 |
'{variant}' => $variant, |
| 369 |
] |
| 370 |
); |
| 371 |
$rules[] = "$selector {\n\t$declarations\n}"; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
$style = implode( "\n", $rules ); |
| 376 |
|
| 377 |
return [ 'all' => $style ]; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
private function getStyleDeclarations( |
| 392 |
Context $context, |
| 393 |
Image $image, |
| 394 |
$script, |
| 395 |
$variant = null |
| 396 |
) { |
| 397 |
$imageDataUri = $this->useDataURI ? $image->getDataUri( $context, $variant, 'original' ) : false; |
| 398 |
$primaryUrl = $imageDataUri ?: $image->getUrl( $context, $script, $variant, 'original' ); |
| 399 |
$declarations = $this->getCssDeclarations( |
| 400 |
$primaryUrl |
| 401 |
); |
| 402 |
return implode( "\n\t", $declarations ); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
protected function getCssDeclarations( $primary ): array { |
| 412 |
$primaryUrl = CSSMin::buildUrlValue( $primary ); |
| 413 |
if ( $this->supportsMaskImage() ) { |
| 414 |
return [ |
| 415 |
"-webkit-mask-image: $primaryUrl;", |
| 416 |
"mask-image: $primaryUrl;", |
| 417 |
]; |
| 418 |
} |
| 419 |
return [ |
| 420 |
"background-image: $primaryUrl;", |
| 421 |
]; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public function supportsMaskImage() { |
| 428 |
return $this->useMaskImage; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
public function supportsURLLoading() { |
| 435 |
return false; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public function getDefinitionSummary( Context $context ) { |
| 445 |
$this->loadFromDefinition(); |
| 446 |
$summary = parent::getDefinitionSummary( $context ); |
| 447 |
|
| 448 |
$options = []; |
| 449 |
foreach ( [ |
| 450 |
'localBasePath', |
| 451 |
'images', |
| 452 |
'variants', |
| 453 |
'prefix', |
| 454 |
'selectorWithoutVariant', |
| 455 |
'selectorWithVariant', |
| 456 |
] as $member ) { |
| 457 |
$options[$member] = $this->{$member}; |
| 458 |
} |
| 459 |
|
| 460 |
$summary[] = [ |
| 461 |
'options' => $options, |
| 462 |
'fileHashes' => $this->getFileHashes( $context ), |
| 463 |
]; |
| 464 |
return $summary; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
private function getFileHashes( Context $context ) { |
| 473 |
$this->loadFromDefinition(); |
| 474 |
$files = []; |
| 475 |
foreach ( $this->getImages( $context ) as $image ) { |
| 476 |
$files[] = $image->getPath( $context ); |
| 477 |
} |
| 478 |
$files = array_values( array_unique( $files ) ); |
| 479 |
return array_map( [ __CLASS__, 'safeFileHash' ], $files ); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
protected function getLocalPath( $path ) { |
| 487 |
if ( $path instanceof FilePath ) { |
| 488 |
return $path->getLocalPath(); |
| 489 |
} |
| 490 |
|
| 491 |
return "{$this->localBasePath}/$path"; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
public static function extractLocalBasePath( array $options, $localBasePath = null ) { |
| 503 |
global $IP; |
| 504 |
|
| 505 |
if ( array_key_exists( 'localBasePath', $options ) ) { |
| 506 |
$localBasePath = (string)$options['localBasePath']; |
| 507 |
} |
| 508 |
|
| 509 |
return $localBasePath ?? $IP; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public function getType() { |
| 516 |
return self::LOAD_STYLES; |
| 517 |
} |
| 518 |
} |