Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
9.68% covered (danger)
9.68%
3 / 31
16.67% covered (danger)
16.67%
1 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
OOUIModule
9.68% covered (danger)
9.68%
3 / 31
16.67% covered (danger)
16.67%
1 / 6
158.43
0.00% covered (danger)
0.00%
0 / 1
 getSkinThemeMap
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getThemePaths
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 getThemePath
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getThemeScriptsPath
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getThemeStylesPath
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getThemeImagesPath
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\ResourceLoader;
8
9use InvalidArgumentException;
10use MediaWiki\Registration\ExtensionRegistry;
11
12/**
13 * Convenience methods for dealing with OOUI themes and their relations to MW skins.
14 *
15 * @ingroup ResourceLoader
16 * @internal
17 */
18trait OOUIModule {
19
20    /** @var string[] */
21    private const array KNOWN_SCRIPTS_MODULES = [
22        'core',
23    ];
24
25    /** @var string[] */
26    private const array KNOWN_STYLES_MODULES = [
27        'core',
28        'toolbars',
29        'widgets',
30        'windows',
31    ];
32
33    /** @var string[] */
34    protected const array KNOWN_IMAGES_MODULES = [
35        'indicators',
36        // Extra icons
37        'icons-accessibility',
38        'icons-alerts',
39        'icons-content',
40        'icons-editing-advanced',
41        'icons-editing-citation',
42        'icons-editing-core',
43        'icons-editing-functions',
44        'icons-editing-list',
45        'icons-editing-styling',
46        'icons-interactions',
47        'icons-layout',
48        'icons-location',
49        'icons-media',
50        'icons-moderation',
51        'icons-movement',
52        'icons-user',
53        'icons-wikimedia',
54    ];
55
56    /** @var array<string,string> Note that keys must be lowercase, values TitleCase. */
57    private const array BUILTIN_SKIN_THEME_MAP = [
58        'default' => 'WikimediaUI',
59    ];
60
61    /** @var array<string,array<string,string>> Note that keys must be TitleCase. */
62    private const array BUILTIN_THEME_PATHS = [
63        'WikimediaUI' => [
64            'scripts' => 'resources/lib/ooui/oojs-ui-wikimediaui.js',
65            'styles' => 'resources/lib/ooui/oojs-ui-{module}-wikimediaui.css',
66            'images' => 'resources/lib/ooui/themes/wikimediaui/{module}.json',
67        ],
68        'Apex' => [
69            'scripts' => 'resources/lib/ooui/oojs-ui-apex.js',
70            'styles' => 'resources/lib/ooui/oojs-ui-{module}-apex.css',
71            'images' => 'resources/lib/ooui/themes/apex/{module}.json',
72        ],
73    ];
74
75    /**
76     * Return a map of skin names (in lowercase) to OOUI theme names, defining which theme a given
77     * skin should use.
78     *
79     * @return string[]
80     */
81    public static function getSkinThemeMap() {
82        $themeMap = self::BUILTIN_SKIN_THEME_MAP;
83        $themeMap += ExtensionRegistry::getInstance()->getAttribute( 'SkinOOUIThemes' );
84        return $themeMap;
85    }
86
87    /**
88     * Return a map of theme names to lists of paths from which a given theme should be loaded.
89     *
90     * Keys are theme names, values are associative arrays. Keys of the inner array are 'scripts',
91     * 'styles', or 'images', and values are paths. Paths may be strings or FilePaths.
92     *
93     * Additionally, the string '{module}' in paths represents the name of the module to load.
94     *
95     * @return array<string,array<string,string|FilePath>>
96     */
97    protected static function getThemePaths() {
98        $themePaths = self::BUILTIN_THEME_PATHS;
99        $themePaths += ExtensionRegistry::getInstance()->getAttribute( 'OOUIThemePaths' );
100
101        [ $defaultLocalBasePath, $defaultRemoteBasePath ] =
102            FileModule::extractBasePaths();
103
104        // Allow custom themes' paths to be relative to the skin/extension that defines them,
105        // like with ResourceModuleSkinStyles
106        foreach ( $themePaths as &$paths ) {
107            [ $localBasePath, $remoteBasePath ] =
108                FileModule::extractBasePaths( $paths );
109            if ( $localBasePath !== $defaultLocalBasePath || $remoteBasePath !== $defaultRemoteBasePath ) {
110                foreach ( $paths as &$path ) {
111                    $path = new FilePath( $path, $localBasePath, $remoteBasePath );
112                }
113            }
114        }
115
116        return $themePaths;
117    }
118
119    /**
120     * Return a path to load given module of given theme from.
121     *
122     * The file at this path may not exist. This should be handled by the caller (throwing an error or
123     * falling back to default theme).
124     *
125     * @param string $theme OOUI theme name, for example 'WikimediaUI' or 'Apex'
126     * @param string $kind Kind of the module: 'scripts', 'styles', or 'images'
127     * @param string $module Module name, for valid values see {@link KNOWN_SCRIPTS_MODULES},
128     *     {@link KNOWN_STYLES_MODULES}, {@link KNOWN_IMAGES_MODULES}
129     * @return string|FilePath
130     */
131    protected function getThemePath( $theme, $kind, $module ) {
132        $path = self::getThemePaths()[$theme][$kind];
133
134        if ( $path instanceof FilePath ) {
135            return new FilePath(
136                str_replace( '{module}', $module, $path->getPath() ),
137                $path->getLocalBasePath(),
138                $path->getRemoteBasePath()
139            );
140        }
141
142        return str_replace( '{module}', $module, $path );
143    }
144
145    /**
146     * @param string $theme See getThemePath()
147     * @param string $module See getThemePath()
148     * @return string|FilePath
149     */
150    protected function getThemeScriptsPath( $theme, $module ) {
151        if ( !in_array( $module, self::KNOWN_SCRIPTS_MODULES ) ) {
152            throw new InvalidArgumentException( "Invalid OOUI scripts module '$module'" );
153        }
154        return $this->getThemePath( $theme, 'scripts', $module );
155    }
156
157    /**
158     * @param string $theme See getThemePath()
159     * @param string $module See getThemePath()
160     * @return string|FilePath
161     */
162    protected function getThemeStylesPath( $theme, $module ) {
163        if ( !in_array( $module, self::KNOWN_STYLES_MODULES ) ) {
164            throw new InvalidArgumentException( "Invalid OOUI styles module '$module'" );
165        }
166        return $this->getThemePath( $theme, 'styles', $module );
167    }
168
169    /**
170     * @param string $theme See getThemePath()
171     * @param string $module See getThemePath()
172     * @return string|FilePath
173     */
174    protected function getThemeImagesPath( $theme, $module ) {
175        if ( !in_array( $module, self::KNOWN_IMAGES_MODULES ) ) {
176            throw new InvalidArgumentException( "Invalid OOUI images module '$module'" );
177        }
178        return $this->getThemePath( $theme, 'images', $module );
179    }
180}