Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
OOUIIconPackModule
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getIcons
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 loadOOUIDefinition
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
 extractLocalBasePath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\ResourceLoader;
8
9use InvalidArgumentException;
10
11/**
12 * Allows loading arbitrary sets of OOUI icons.
13 *
14 * @ingroup ResourceLoader
15 * @since 1.34
16 */
17class OOUIIconPackModule extends OOUIImageModule {
18
19    public function __construct( array $options = [], ?string $localBasePath = null ) {
20        parent::__construct( $options, $localBasePath );
21
22        if ( !isset( $this->definition['icons'] ) || !$this->definition['icons'] ) {
23            throw new InvalidArgumentException( "Parameter 'icons' must be given." );
24        }
25
26        // A few things check for the "icons" prefix on this value, so specify it even though
27        // we don't use it for actually loading the data, like in the other modules.
28        $this->definition['themeImages'] = 'icons';
29    }
30
31    private function getIcons(): array {
32        // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Checked in the constructor
33        return $this->definition['icons'];
34    }
35
36    /** @inheritDoc */
37    protected function loadOOUIDefinition( $theme, $unused ): array {
38        // This is shared between instances of this class, so we only have to load the JSON files once
39        static $data = [];
40
41        if ( !isset( $data[$theme] ) ) {
42            $data[$theme] = [];
43            // Load and merge the JSON data for all "icons-foo" modules
44            foreach ( self::KNOWN_IMAGES_MODULES as $module ) {
45                if ( str_starts_with( $module, 'icons' ) ) {
46                    $moreData = $this->readJSONFile( $this->getThemeImagesPath( $theme, $module ) );
47                    if ( $moreData ) {
48                        $data[$theme] = array_replace_recursive( $data[$theme], $moreData );
49                    }
50                }
51            }
52        }
53
54        $definition = $data[$theme];
55
56        // Filter out the data for all other icons, leaving only the ones we want for this module
57        $iconsNames = $this->getIcons();
58        foreach ( $definition['images'] as $iconName => $_ ) {
59            if ( !in_array( $iconName, $iconsNames ) ) {
60                unset( $definition['images'][$iconName] );
61            }
62        }
63
64        return $definition;
65    }
66
67    /** @inheritDoc */
68    public static function extractLocalBasePath( array $options, $localBasePath = null ) {
69        global $IP;
70        // Ignore any 'localBasePath' present in $options, this always refers to files in MediaWiki core
71        return $localBasePath ?? $IP;
72    }
73}