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.
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.
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
22 * Convenience methods for dealing with OOUI themes and their relations to MW skins.
24 * @ingroup ResourceLoader
27 trait ResourceLoaderOOUIModule
{
28 protected static $knownScriptsModules = [ 'core' ];
29 protected static $knownStylesModules = [ 'core', 'widgets', 'toolbars', 'windows' ];
30 protected static $knownImagesModules = [
33 'icons-accessibility',
36 'icons-editing-advanced',
37 'icons-editing-citation',
40 'icons-editing-styling',
51 // Note that keys must be lowercase, values TitleCase.
52 protected static $builtinSkinThemeMap = [
53 'default' => 'WikimediaUI',
56 // Note that keys must be TitleCase.
57 protected static $builtinThemePaths = [
59 'scripts' => 'resources/lib/ooui/oojs-ui-wikimediaui.js',
60 'styles' => 'resources/lib/ooui/oojs-ui-{module}-wikimediaui.css',
61 'images' => 'resources/lib/ooui/themes/wikimediaui/{module}.json',
64 'scripts' => 'resources/lib/ooui/oojs-ui-apex.js',
65 'styles' => 'resources/lib/ooui/oojs-ui-{module}-apex.css',
66 'images' => 'resources/lib/ooui/themes/apex/{module}.json',
71 * Return a map of skin names (in lowercase) to OOUI theme names, defining which theme a given
76 public static function getSkinThemeMap() {
77 $themeMap = self
::$builtinSkinThemeMap;
78 $themeMap +
= ExtensionRegistry
::getInstance()->getAttribute( 'SkinOOUIThemes' );
83 * Return a map of theme names to lists of paths from which a given theme should be loaded.
85 * Keys are theme names, values are associative arrays. Keys of the inner array are 'scripts',
86 * 'styles', or 'images', and values are paths. Paths may be strings or ResourceLoaderFilePaths.
88 * Additionally, the string '{module}' in paths represents the name of the module to load.
92 protected static function getThemePaths() {
93 $themePaths = self
::$builtinThemePaths;
94 $themePaths +
= ExtensionRegistry
::getInstance()->getAttribute( 'OOUIThemePaths' );
96 list( $defaultLocalBasePath, $defaultRemoteBasePath ) =
97 ResourceLoaderFileModule
::extractBasePaths();
99 // Allow custom themes' paths to be relative to the skin/extension that defines them,
100 // like with ResourceModuleSkinStyles
101 foreach ( $themePaths as $theme => &$paths ) {
102 list( $localBasePath, $remoteBasePath ) =
103 ResourceLoaderFileModule
::extractBasePaths( $paths );
104 if ( $localBasePath !== $defaultLocalBasePath ||
$remoteBasePath !== $defaultRemoteBasePath ) {
105 foreach ( $paths as &$path ) {
106 $path = new ResourceLoaderFilePath( $path, $localBasePath, $remoteBasePath );
115 * Return a path to load given module of given theme from.
117 * The file at this path may not exist. This should be handled by the caller (throwing an error or
118 * falling back to default theme).
120 * @param string $theme OOUI theme name, for example 'WikimediaUI' or 'Apex'
121 * @param string $kind Kind of the module: 'scripts', 'styles', or 'images'
122 * @param string $module Module name, for valid values see $knownScriptsModules,
123 * $knownStylesModules, $knownImagesModules
124 * @return string|ResourceLoaderFilePath
126 protected function getThemePath( $theme, $kind, $module ) {
127 $paths = self
::getThemePaths();
128 $path = $paths[$theme][$kind];
129 if ( $path instanceof ResourceLoaderFilePath
) {
130 $path = new ResourceLoaderFilePath(
131 str_replace( '{module}', $module, $path->getPath() ),
132 $path->getLocalBasePath(),
133 $path->getRemoteBasePath()
136 $path = str_replace( '{module}', $module, $path );
142 * @param string $theme See getThemePath()
143 * @param string $module See getThemePath()
144 * @return string|ResourceLoaderFilePath
146 protected function getThemeScriptsPath( $theme, $module ) {
147 if ( !in_array( $module, self
::$knownScriptsModules ) ) {
148 throw new InvalidArgumentException( "Invalid OOUI scripts module '$module'" );
150 return $this->getThemePath( $theme, 'scripts', $module );
154 * @param string $theme See getThemePath()
155 * @param string $module See getThemePath()
156 * @return string|ResourceLoaderFilePath
158 protected function getThemeStylesPath( $theme, $module ) {
159 if ( !in_array( $module, self
::$knownStylesModules ) ) {
160 throw new InvalidArgumentException( "Invalid OOUI styles module '$module'" );
162 return $this->getThemePath( $theme, 'styles', $module );
166 * @param string $theme See getThemePath()
167 * @param string $module See getThemePath()
168 * @return string|ResourceLoaderFilePath
170 protected function getThemeImagesPath( $theme, $module ) {
171 if ( !in_array( $module, self
::$knownImagesModules ) ) {
172 throw new InvalidArgumentException( "Invalid OOUI images module '$module'" );
174 return $this->getThemePath( $theme, 'images', $module );