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 * Loads the module definition from JSON files in the format that OOUI uses, converting it to the
23 * format we use. (Previously known as secret special sauce.)
27 class ResourceLoaderOOUIImageModule
extends ResourceLoaderImageModule
{
28 use ResourceLoaderOOUIModule
;
30 protected function loadFromDefinition() {
31 if ( $this->definition
=== null ) {
32 // Do nothing if definition was already processed
36 $themes = self
::getSkinThemeMap();
38 // For backwards-compatibility, allow missing 'themeImages'
39 $module = $this->definition
['themeImages'] ??
'';
42 foreach ( $themes as $skin => $theme ) {
43 $data = $this->loadOOUIDefinition( $theme, $module );
46 // If there's no file for this module of this theme, that's okay, it will just use the defaults
50 // Convert into a definition compatible with the parent vanilla ResourceLoaderImageModule
51 foreach ( $data as $key => $value ) {
53 // Images and color variants are defined per-theme, here converted to per-skin
56 $definition[$key][$skin] = $data[$key];
59 // Other options must be identical for each theme (or only defined in the default one)
61 if ( !isset( $definition[$key] ) ) {
62 $definition[$key] = $data[$key];
63 } elseif ( $definition[$key] !== $data[$key] ) {
65 "Mismatched OOUI theme images definition: " .
66 "key '$key' of theme '$theme' for module '$module' " .
67 "does not match other themes"
75 // Extra selectors to allow using the same icons for old-style MediaWiki UI code
76 if ( substr( $module, 0, 5 ) === 'icons' ) {
77 $definition['selectorWithoutVariant'] = '.oo-ui-icon-{name}, .mw-ui-icon-{name}:before';
78 $definition['selectorWithVariant'] = '.oo-ui-image-{variant}.oo-ui-icon-{name}, ' .
79 '.mw-ui-icon-{name}-{variant}:before';
82 // Fields from module definition silently override keys from JSON files
83 $this->definition +
= $definition;
85 parent
::loadFromDefinition();
89 * Load the module definition from the JSON file(s) for the given theme and module.
92 * @param string $theme
93 * @param string $module
96 protected function loadOOUIDefinition( $theme, $module ) {
97 // Find the path to the JSON file which contains the actual image definitions for this theme
99 $dataPath = $this->getThemeImagesPath( $theme, $module );
104 // Backwards-compatibility for things that probably shouldn't have used this class...
106 $this->definition
['rootPath'] . '/' .
107 strtolower( $theme ) . '/' .
108 $this->definition
['name'] . '.json';
111 return $this->readJSONFile( $dataPath );
115 * Read JSON from a file, and transform all paths in it to be relative to the module's base path.
118 * @param string $dataPath Path relative to the module's base bath
119 * @return array|false
121 protected function readJSONFile( $dataPath ) {
122 $localDataPath = $this->getLocalPath( $dataPath );
124 if ( !file_exists( $localDataPath ) ) {
128 $data = json_decode( file_get_contents( $localDataPath ), true );
130 // Expand the paths to images (since they are relative to the JSON file that defines them, not
131 // our base directory)
132 $fixPath = function ( &$path ) use ( $dataPath ) {
133 if ( $dataPath instanceof ResourceLoaderFilePath
) {
134 $path = new ResourceLoaderFilePath(
135 dirname( $dataPath->getPath() ) . '/' . $path,
136 $dataPath->getLocalBasePath(),
137 $dataPath->getRemoteBasePath()
140 $path = dirname( $dataPath ) . '/' . $path;
143 array_walk( $data['images'], function ( &$value ) use ( $fixPath ) {
144 if ( is_string( $value['file'] ) ) {
145 $fixPath( $value['file'] );
146 } elseif ( is_array( $value['file'] ) ) {
147 array_walk_recursive( $value['file'], $fixPath );