Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderOOUIModule.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * Convenience methods for dealing with OOUI themes and their relations to MW skins.
23 *
24 * @since 1.30
25 */
26 trait ResourceLoaderOOUIModule {
27 protected static $knownScriptsModules = [ 'core' ];
28 protected static $knownStylesModules = [ 'core', 'widgets', 'toolbars', 'windows' ];
29 protected static $knownImagesModules = [
30 'indicators', 'textures',
31 // Extra icons
32 'icons-accessibility',
33 'icons-alerts',
34 'icons-content',
35 'icons-editing-advanced',
36 'icons-editing-core',
37 'icons-editing-list',
38 'icons-editing-styling',
39 'icons-interactions',
40 'icons-layout',
41 'icons-location',
42 'icons-media',
43 'icons-moderation',
44 'icons-movement',
45 'icons-user',
46 'icons-wikimedia',
47 ];
48
49 // Note that keys must be lowercase, values TitleCase.
50 protected static $builtinSkinThemeMap = [
51 'default' => 'WikimediaUI',
52 ];
53
54 // Note that keys must be TitleCase.
55 protected static $builtinThemePaths = [
56 'WikimediaUI' => [
57 'scripts' => 'resources/lib/oojs-ui/oojs-ui-wikimediaui.js',
58 'styles' => 'resources/lib/oojs-ui/oojs-ui-{module}-wikimediaui.css',
59 'images' => 'resources/lib/oojs-ui/themes/wikimediaui/{module}.json',
60 ],
61 'Apex' => [
62 'scripts' => 'resources/lib/oojs-ui/oojs-ui-apex.js',
63 'styles' => 'resources/lib/oojs-ui/oojs-ui-{module}-apex.css',
64 'images' => 'resources/lib/oojs-ui/themes/apex/{module}.json',
65 ],
66 ];
67
68 /**
69 * Return a map of skin names (in lowercase) to OOUI theme names, defining which theme a given
70 * skin should use.
71 *
72 * @return array
73 */
74 public static function getSkinThemeMap() {
75 $themeMap = self::$builtinSkinThemeMap;
76 $themeMap += ExtensionRegistry::getInstance()->getAttribute( 'SkinOOUIThemes' );
77 return $themeMap;
78 }
79
80 /**
81 * Return a map of theme names to lists of paths from which a given theme should be loaded.
82 *
83 * Keys are theme names, values are associative arrays. Keys of the inner array are 'scripts',
84 * 'styles', or 'images', and values are string paths.
85 *
86 * Additionally, the string '{module}' in paths represents the name of the module to load.
87 *
88 * @return array
89 */
90 protected static function getThemePaths() {
91 $themePaths = self::$builtinThemePaths;
92 return $themePaths;
93 }
94
95 /**
96 * Return a path to load given module of given theme from.
97 *
98 * @param string $theme OOUI theme name, for example 'WikimediaUI' or 'Apex'
99 * @param string $kind Kind of the module: 'scripts', 'styles', or 'images'
100 * @param string $module Module name, for valid values see $knownScriptsModules,
101 * $knownStylesModules, $knownImagesModules
102 * @return string
103 */
104 protected function getThemePath( $theme, $kind, $module ) {
105 $paths = self::getThemePaths();
106 $path = $paths[ $theme ][ $kind ];
107 $path = str_replace( '{module}', $module, $path );
108 return $path;
109 }
110
111 /**
112 * @param string $theme See getThemePath()
113 * @param string $module See getThemePath()
114 * @return string
115 */
116 protected function getThemeScriptsPath( $theme, $module ) {
117 if ( !in_array( $module, self::$knownScriptsModules ) ) {
118 throw new InvalidArgumentException( "Invalid OOUI scripts module '$module'" );
119 }
120 return $this->getThemePath( $theme, 'scripts', $module );
121 }
122
123 /**
124 * @param string $theme See getThemePath()
125 * @param string $module See getThemePath()
126 * @return string
127 */
128 protected function getThemeStylesPath( $theme, $module ) {
129 if ( !in_array( $module, self::$knownStylesModules ) ) {
130 throw new InvalidArgumentException( "Invalid OOUI styles module '$module'" );
131 }
132 return $this->getThemePath( $theme, 'styles', $module );
133 }
134
135 /**
136 * @param string $theme See getThemePath()
137 * @param string $module See getThemePath()
138 * @return string
139 */
140 protected function getThemeImagesPath( $theme, $module ) {
141 if ( !in_array( $module, self::$knownImagesModules ) ) {
142 throw new InvalidArgumentException( "Invalid OOUI images module '$module'" );
143 }
144 return $this->getThemePath( $theme, 'images', $module );
145 }
146 }