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    public function __construct( array $options = [], ?string $localBasePath = null ) {
19        parent::__construct( $options, $localBasePath );
20
21        if ( !isset( $this->definition['icons'] ) || !$this->definition['icons'] ) {
22            throw new InvalidArgumentException( "Parameter 'icons' must be given." );
23        }
24
25        // A few things check for the "icons" prefix on this value, so specify it even though
26        // we don't use it for actually loading the data, like in the other modules.
27        $this->definition['themeImages'] = 'icons';
28    }
29
30    private function getIcons(): array {
31        // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Checked in the constructor
32        return $this->definition['icons'];
33    }
34
35    /** @inheritDoc */
36    protected function loadOOUIDefinition( $theme, $unused ): array {
37        // This is shared between instances of this class, so we only have to load the JSON files once
38        static $data = [];
39
40        if ( !isset( $data[$theme] ) ) {
41            $data[$theme] = [];
42            // Load and merge the JSON data for all "icons-foo" modules
43            foreach ( self::$knownImagesModules as $module ) {
44                if ( str_starts_with( $module, 'icons' ) ) {
45                    $moreData = $this->readJSONFile( $this->getThemeImagesPath( $theme, $module ) );
46                    if ( $moreData ) {
47                        $data[$theme] = array_replace_recursive( $data[$theme], $moreData );
48                    }
49                }
50            }
51        }
52
53        $definition = $data[$theme];
54
55        // Filter out the data for all other icons, leaving only the ones we want for this module
56        $iconsNames = $this->getIcons();
57        foreach ( $definition['images'] as $iconName => $_ ) {
58            if ( !in_array( $iconName, $iconsNames ) ) {
59                unset( $definition['images'][$iconName] );
60            }
61        }
62
63        return $definition;
64    }
65
66    /** @inheritDoc */
67    public static function extractLocalBasePath( array $options, $localBasePath = null ) {
68        global $IP;
69        // Ignore any 'localBasePath' present in $options, this always refers to files in MediaWiki core
70        return $localBasePath ?? $IP;
71    }
72}