10use InvalidArgumentException;
11use Wikimedia\Minify\CSSMin;
21 private $useMaskImage;
32 protected $origin = self::ORIGIN_CORE_SITEWIDE;
112 $this->useMaskImage = $options[
'useMaskImage'] ??
false;
113 $this->localBasePath = static::extractLocalBasePath( $options,
$localBasePath );
115 $this->definition = $options;
122 if ( $this->definition ===
null ) {
127 $this->definition =
null;
129 if ( isset( $options[
'data'] ) ) {
131 $data = json_decode( file_get_contents( $dataPath ),
true );
132 $options = array_merge( $data, $options );
142 $prefix = isset( $options[
'prefix'] ) && $options[
'prefix'];
143 $selector = isset( $options[
'selector'] ) && $options[
'selector'];
145 && $options[
'selectorWithoutVariant'];
147 && $options[
'selectorWithVariant'];
150 throw new InvalidArgumentException(
151 "Given 'selectorWithoutVariant' but no 'selectorWithVariant'."
155 throw new InvalidArgumentException(
156 "Given 'selectorWithVariant' but no 'selectorWithoutVariant'."
160 throw new InvalidArgumentException(
161 "Incompatible 'selector' and 'selectorWithVariant'+'selectorWithoutVariant' given."
165 throw new InvalidArgumentException(
166 "None of 'prefix', 'selector' or 'selectorWithVariant'+'selectorWithoutVariant' given."
170 foreach ( $options as $member => $option ) {
174 if ( !is_array( $option ) ) {
175 throw new InvalidArgumentException(
176 "Invalid list error. '$option' given, array expected."
179 if ( !isset( $option[
'default'] ) ) {
181 $option = [
'default' => $option ];
183 foreach ( $option as $data ) {
184 if ( !is_array( $data ) ) {
185 throw new InvalidArgumentException(
186 "Invalid list error. '$data' given, array expected."
190 $this->{$member} = $option;
194 $this->{$member} = (bool)$option;
198 case 'selectorWithoutVariant':
199 case 'selectorWithVariant':
200 $this->{$member} = (string)$option;
204 $this->selectorWithoutVariant = $this->selectorWithVariant = (string)$option;
248 $skin = $context->getSkin();
249 if ( $this->imageObjects ===
null ) {
250 $this->loadFromDefinition();
251 $this->imageObjects = [];
253 if ( !isset( $this->imageObjects[$skin] ) ) {
254 $this->imageObjects[$skin] = [];
255 if ( !isset( $this->images[$skin] ) ) {
256 $this->images[$skin] = $this->images[
'default'] ?? [];
258 foreach ( $this->images[$skin] as $name => $options ) {
259 $fileDescriptor = is_array( $options ) ? $options[
'file'] : $options;
261 $allowedVariants = array_merge(
262 ( is_array( $options ) && isset( $options[
'variants'] ) ) ? $options[
'variants'] : [],
263 $this->getGlobalVariants( $context )
265 if ( isset( $this->variants[$skin] ) ) {
266 $variantConfig = array_intersect_key(
267 $this->variants[$skin],
268 array_fill_keys( $allowedVariants,
true )
278 $this->localBasePath,
282 $this->imageObjects[$skin][$image->getName()] = $image;
286 return $this->imageObjects[$skin];
296 $skin = $context->getSkin();
297 if ( $this->globalVariants ===
null ) {
298 $this->loadFromDefinition();
299 $this->globalVariants = [];
301 if ( !isset( $this->globalVariants[$skin] ) ) {
302 $this->globalVariants[$skin] = [];
303 if ( !isset( $this->variants[$skin] ) ) {
304 $this->variants[$skin] = $this->variants[
'default'] ?? [];
306 foreach ( $this->variants[$skin] as $name => $config ) {
307 if ( $config[
'global'] ??
false ) {
308 $this->globalVariants[$skin][] = $name;
313 return $this->globalVariants[$skin];
317 $this->loadFromDefinition();
323 $this->getHookRunner()->onResourceLoaderModifyEmbeddedSourceUrls( $sources );
324 if ( array_keys( $sources ) !== array_keys( $oldSources ) ) {
325 throw new DomainException(
'ResourceLoaderModifyEmbeddedSourceUrls hook must not add or remove sources' );
327 $script = $sources[ $this->getSource() ];
329 $selectors = $this->getSelectors();
331 foreach ( $this->getImages( $context ) as $name => $image ) {
332 $declarations = $this->getStyleDeclarations( $context, $image, $script );
334 $selectors[
'selectorWithoutVariant'],
336 '{prefix}' => $this->getPrefix(),
341 $rules[] =
"$selector {\n\t$declarations\n}";
343 foreach ( $image->getVariants() as $variant ) {
344 $declarations = $this->getStyleDeclarations( $context, $image, $script, $variant );
346 $selectors[
'selectorWithVariant'],
348 '{prefix}' => $this->getPrefix(),
350 '{variant}' => $variant,
353 $rules[] =
"$selector {\n\t$declarations\n}";
357 $style = implode(
"\n", $rules );
359 return [
'all' => $style ];
373 private function getStyleDeclarations(
379 $imageDataUri = $this->useDataURI ? $image->getDataUri( $context, $variant,
'original' ) :
false;
380 $primaryUrl = $imageDataUri ?: $image->getUrl( $context, $script, $variant,
'original' );
381 $declarations = $this->getCssDeclarations(
384 return implode(
"\n\t", $declarations );
394 $primaryUrl = CSSMin::buildUrlValue( $primary );
395 if ( $this->supportsMaskImage() ) {
397 "-webkit-mask-image: $primaryUrl;",
398 "mask-image: $primaryUrl;",
402 "background-image: $primaryUrl;",
410 return $this->useMaskImage;
427 $this->loadFromDefinition();
428 $summary = parent::getDefinitionSummary( $context );
436 'selectorWithoutVariant',
437 'selectorWithVariant',
439 $options[$member] = $this->{$member};
443 'options' => $options,
444 'fileHashes' => $this->getFileHashes( $context ),
454 private function getFileHashes(
Context $context ) {
455 $this->loadFromDefinition();
457 foreach ( $this->getImages( $context ) as $image ) {
458 $files[] = $image->getPath( $context );
460 $files = array_values( array_unique( $files ) );
461 return array_map( self::safeFileHash( ... ), $files );
470 return $path->getLocalPath();
473 return "{$this->localBasePath}/$path";
487 if ( array_key_exists(
'localBasePath', $options ) ) {
488 $localBasePath = (string)$options[
'localBasePath'];
491 return $localBasePath ??
$IP;
498 return self::LOAD_STYLES;
if(!defined('MEDIAWIKI')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Context object that contains information about the state of a specific ResourceLoader web request.