23use InvalidArgumentException;
24use Wikimedia\Minify\CSSMin;
42 protected $origin = self::ORIGIN_CORE_SITEWIDE;
117 $this->localBasePath = static::extractLocalBasePath( $options,
$localBasePath );
119 $this->definition = $options;
126 if ( $this->definition ===
null ) {
131 $this->definition =
null;
133 if ( isset( $options[
'data'] ) ) {
135 $data = json_decode( file_get_contents( $dataPath ),
true );
136 $options = array_merge( $data, $options );
146 $prefix = isset( $options[
'prefix'] ) && $options[
'prefix'];
147 $selector = isset( $options[
'selector'] ) && $options[
'selector'];
149 && $options[
'selectorWithoutVariant'];
151 && $options[
'selectorWithVariant'];
154 throw new InvalidArgumentException(
155 "Given 'selectorWithoutVariant' but no 'selectorWithVariant'."
159 throw new InvalidArgumentException(
160 "Given 'selectorWithVariant' but no 'selectorWithoutVariant'."
164 throw new InvalidArgumentException(
165 "Incompatible 'selector' and 'selectorWithVariant'+'selectorWithoutVariant' given."
169 throw new InvalidArgumentException(
170 "None of 'prefix', 'selector' or 'selectorWithVariant'+'selectorWithoutVariant' given."
174 foreach ( $options as $member => $option ) {
178 if ( !is_array( $option ) ) {
179 throw new InvalidArgumentException(
180 "Invalid list error. '$option' given, array expected."
183 if ( !isset( $option[
'default'] ) ) {
185 $option = [
'default' => $option ];
187 foreach ( $option as $skin => $data ) {
188 if ( !is_array( $data ) ) {
189 throw new InvalidArgumentException(
190 "Invalid list error. '$data' given, array expected."
194 $this->{$member} = $option;
198 $this->{$member} = (bool)$option;
202 case 'selectorWithoutVariant':
203 case 'selectorWithVariant':
204 $this->{$member} = (string)$option;
208 $this->selectorWithoutVariant = $this->selectorWithVariant = (string)$option;
252 $skin = $context->getSkin();
253 if ( $this->imageObjects ===
null ) {
254 $this->loadFromDefinition();
255 $this->imageObjects = [];
257 if ( !isset( $this->imageObjects[$skin] ) ) {
258 $this->imageObjects[$skin] = [];
259 if ( !isset( $this->images[$skin] ) ) {
260 $this->images[$skin] = $this->images[
'default'] ?? [];
262 foreach ( $this->images[$skin] as $name => $options ) {
263 $fileDescriptor = is_array( $options ) ? $options[
'file'] : $options;
265 $allowedVariants = array_merge(
266 ( is_array( $options ) && isset( $options[
'variants'] ) ) ? $options[
'variants'] : [],
267 $this->getGlobalVariants( $context )
269 if ( isset( $this->variants[$skin] ) ) {
270 $variantConfig = array_intersect_key(
271 $this->variants[$skin],
272 array_fill_keys( $allowedVariants,
true )
282 $this->localBasePath,
286 $this->imageObjects[$skin][$image->getName()] = $image;
290 return $this->imageObjects[$skin];
300 $skin = $context->getSkin();
301 if ( $this->globalVariants ===
null ) {
302 $this->loadFromDefinition();
303 $this->globalVariants = [];
305 if ( !isset( $this->globalVariants[$skin] ) ) {
306 $this->globalVariants[$skin] = [];
307 if ( !isset( $this->variants[$skin] ) ) {
308 $this->variants[$skin] = $this->variants[
'default'] ?? [];
310 foreach ( $this->variants[$skin] as $name => $config ) {
311 if ( $config[
'global'] ??
false ) {
312 $this->globalVariants[$skin][] = $name;
317 return $this->globalVariants[$skin];
325 $this->loadFromDefinition();
330 $selectors = $this->getSelectors();
332 foreach ( $this->getImages( $context ) as $name => $image ) {
333 $declarations = $this->getStyleDeclarations( $context, $image, $script );
335 $selectors[
'selectorWithoutVariant'],
337 '{prefix}' => $this->getPrefix(),
342 $rules[] =
"$selector {\n\t$declarations\n}";
344 foreach ( $image->getVariants() as $variant ) {
345 $declarations = $this->getStyleDeclarations( $context, $image, $script, $variant );
347 $selectors[
'selectorWithVariant'],
349 '{prefix}' => $this->getPrefix(),
351 '{variant}' => $variant,
354 $rules[] =
"$selector {\n\t$declarations\n}";
358 $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(
383 $image->getUrl( $context, $script, $variant,
'rasterized' )
385 return implode(
"\n\t", $declarations );
401 $primaryUrl = CSSMin::buildUrlValue( $primary );
402 $fallbackUrl = CSSMin::buildUrlValue(
$fallback );
404 "background-image: $fallbackUrl;",
405 "background-image: linear-gradient(transparent, transparent), $primaryUrl;",
423 $this->loadFromDefinition();
424 $summary = parent::getDefinitionSummary( $context );
432 'selectorWithoutVariant',
433 'selectorWithVariant',
435 $options[$member] = $this->{$member};
439 'options' => $options,
440 'fileHashes' => $this->getFileHashes( $context ),
450 private function getFileHashes(
Context $context ) {
451 $this->loadFromDefinition();
453 foreach ( $this->getImages( $context ) as $name => $image ) {
454 $files[] = $image->getPath( $context );
456 $files = array_values( array_unique( $files ) );
457 return array_map( [ __CLASS__,
'safeFileHash' ], $files );
466 return $path->getLocalPath();
469 return "{$this->localBasePath}/$path";
483 if ( $localBasePath ===
null ) {
484 $localBasePath =
$IP;
487 if ( array_key_exists(
'localBasePath', $options ) ) {
488 $localBasePath = (string)$options[
'localBasePath'];
491 return $localBasePath;
498 return self::LOAD_STYLES;
503class_alias( ImageModule::class,
'ResourceLoaderImageModule' );
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.