23use InvalidArgumentException;
24use Wikimedia\Minify\CSSMin;
34 private $useMaskImage;
44 protected $origin = self::ORIGIN_CORE_SITEWIDE;
121 $this->useMaskImage = $options[
'useMaskImage'] ??
false;
122 $this->localBasePath = static::extractLocalBasePath( $options,
$localBasePath );
124 $this->definition = $options;
131 if ( $this->definition ===
null ) {
136 $this->definition =
null;
138 if ( isset( $options[
'data'] ) ) {
140 $data = json_decode( file_get_contents( $dataPath ),
true );
141 $options = array_merge( $data, $options );
151 $prefix = isset( $options[
'prefix'] ) && $options[
'prefix'];
152 $selector = isset( $options[
'selector'] ) && $options[
'selector'];
154 && $options[
'selectorWithoutVariant'];
156 && $options[
'selectorWithVariant'];
159 throw new InvalidArgumentException(
160 "Given 'selectorWithoutVariant' but no 'selectorWithVariant'."
164 throw new InvalidArgumentException(
165 "Given 'selectorWithVariant' but no 'selectorWithoutVariant'."
169 throw new InvalidArgumentException(
170 "Incompatible 'selector' and 'selectorWithVariant'+'selectorWithoutVariant' given."
174 throw new InvalidArgumentException(
175 "None of 'prefix', 'selector' or 'selectorWithVariant'+'selectorWithoutVariant' given."
179 foreach ( $options as $member => $option ) {
183 if ( !is_array( $option ) ) {
184 throw new InvalidArgumentException(
185 "Invalid list error. '$option' given, array expected."
188 if ( !isset( $option[
'default'] ) ) {
190 $option = [
'default' => $option ];
192 foreach ( $option as $data ) {
193 if ( !is_array( $data ) ) {
194 throw new InvalidArgumentException(
195 "Invalid list error. '$data' given, array expected."
199 $this->{$member} = $option;
203 $this->{$member} = (bool)$option;
207 case 'selectorWithoutVariant':
208 case 'selectorWithVariant':
209 $this->{$member} = (string)$option;
213 $this->selectorWithoutVariant = $this->selectorWithVariant = (string)$option;
257 $skin = $context->getSkin();
258 if ( $this->imageObjects ===
null ) {
259 $this->loadFromDefinition();
260 $this->imageObjects = [];
262 if ( !isset( $this->imageObjects[$skin] ) ) {
263 $this->imageObjects[$skin] = [];
264 if ( !isset( $this->images[$skin] ) ) {
265 $this->images[$skin] = $this->images[
'default'] ?? [];
267 foreach ( $this->images[$skin] as $name => $options ) {
268 $fileDescriptor = is_array( $options ) ? $options[
'file'] : $options;
270 $allowedVariants = array_merge(
271 ( is_array( $options ) && isset( $options[
'variants'] ) ) ? $options[
'variants'] : [],
272 $this->getGlobalVariants( $context )
274 if ( isset( $this->variants[$skin] ) ) {
275 $variantConfig = array_intersect_key(
276 $this->variants[$skin],
277 array_fill_keys( $allowedVariants,
true )
287 $this->localBasePath,
291 $this->imageObjects[$skin][$image->getName()] = $image;
295 return $this->imageObjects[$skin];
305 $skin = $context->getSkin();
306 if ( $this->globalVariants ===
null ) {
307 $this->loadFromDefinition();
308 $this->globalVariants = [];
310 if ( !isset( $this->globalVariants[$skin] ) ) {
311 $this->globalVariants[$skin] = [];
312 if ( !isset( $this->variants[$skin] ) ) {
313 $this->variants[$skin] = $this->variants[
'default'] ?? [];
315 foreach ( $this->variants[$skin] as $name => $config ) {
316 if ( $config[
'global'] ??
false ) {
317 $this->globalVariants[$skin][] = $name;
322 return $this->globalVariants[$skin];
330 $this->loadFromDefinition();
335 $selectors = $this->getSelectors();
337 foreach ( $this->getImages( $context ) as $name => $image ) {
338 $declarations = $this->getStyleDeclarations( $context, $image, $script );
340 $selectors[
'selectorWithoutVariant'],
342 '{prefix}' => $this->getPrefix(),
347 $rules[] =
"$selector {\n\t$declarations\n}";
349 foreach ( $image->getVariants() as $variant ) {
350 $declarations = $this->getStyleDeclarations( $context, $image, $script, $variant );
352 $selectors[
'selectorWithVariant'],
354 '{prefix}' => $this->getPrefix(),
356 '{variant}' => $variant,
359 $rules[] =
"$selector {\n\t$declarations\n}";
363 $style = implode(
"\n", $rules );
365 return [
'all' => $style ];
379 private function getStyleDeclarations(
385 $imageDataUri = $this->useDataURI ? $image->getDataUri( $context, $variant,
'original' ) :
false;
386 $primaryUrl = $imageDataUri ?: $image->getUrl( $context, $script, $variant,
'original' );
387 $declarations = $this->getCssDeclarations(
390 return implode(
"\n\t", $declarations );
400 $primaryUrl = CSSMin::buildUrlValue( $primary );
401 if ( $this->supportsMaskImage() ) {
403 "--webkit-mask-image: $primaryUrl;",
404 "mask-image: $primaryUrl;",
408 "background-image: $primaryUrl;",
416 return $this->useMaskImage;
433 $this->loadFromDefinition();
434 $summary = parent::getDefinitionSummary( $context );
442 'selectorWithoutVariant',
443 'selectorWithVariant',
445 $options[$member] = $this->{$member};
449 'options' => $options,
450 'fileHashes' => $this->getFileHashes( $context ),
460 private function getFileHashes(
Context $context ) {
461 $this->loadFromDefinition();
463 foreach ( $this->getImages( $context ) as $image ) {
464 $files[] = $image->getPath( $context );
466 $files = array_values( array_unique( $files ) );
467 return array_map( [ __CLASS__,
'safeFileHash' ], $files );
476 return $path->getLocalPath();
479 return "{$this->localBasePath}/$path";
493 if ( array_key_exists(
'localBasePath', $options ) ) {
494 $localBasePath = (string)$options[
'localBasePath'];
497 return $localBasePath ??
$IP;
504 return self::LOAD_STYLES;
if(!defined( 'MEDIAWIKI')) if(ini_get('mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Context object that contains information about the state of a specific ResourceLoader web request.