Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderOOUIImageModule.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 * 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.)
24 *
25 * @since 1.26
26 */
27 class ResourceLoaderOOUIImageModule extends ResourceLoaderImageModule {
28 use ResourceLoaderOOUIModule;
29
30 protected function loadFromDefinition() {
31 if ( $this->definition === null ) {
32 // Do nothing if definition was already processed
33 return;
34 }
35
36 $themes = self::getSkinThemeMap();
37
38 // For backwards-compatibility, allow missing 'themeImages'
39 $module = $this->definition['themeImages'] ?? '';
40
41 $definition = [];
42 foreach ( $themes as $skin => $theme ) {
43 $data = $this->loadOOUIDefinition( $theme, $module );
44
45 if ( !$data ) {
46 // If there's no file for this module of this theme, that's okay, it will just use the defaults
47 continue;
48 }
49
50 // Convert into a definition compatible with the parent vanilla ResourceLoaderImageModule
51 foreach ( $data as $key => $value ) {
52 switch ( $key ) {
53 // Images and color variants are defined per-theme, here converted to per-skin
54 case 'images':
55 case 'variants':
56 $definition[$key][$skin] = $data[$key];
57 break;
58
59 // Other options must be identical for each theme (or only defined in the default one)
60 default:
61 if ( !isset( $definition[$key] ) ) {
62 $definition[$key] = $data[$key];
63 } elseif ( $definition[$key] !== $data[$key] ) {
64 throw new Exception(
65 "Mismatched OOUI theme images definition: " .
66 "key '$key' of theme '$theme' for module '$module' " .
67 "does not match other themes"
68 );
69 }
70 break;
71 }
72 }
73 }
74
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';
80 }
81
82 // Fields from module definition silently override keys from JSON files
83 $this->definition += $definition;
84
85 parent::loadFromDefinition();
86 }
87
88 /**
89 * Load the module definition from the JSON file(s) for the given theme and module.
90 *
91 * @since 1.34
92 * @param string $theme
93 * @param string $module
94 * @return array
95 */
96 protected function loadOOUIDefinition( $theme, $module ) {
97 // Find the path to the JSON file which contains the actual image definitions for this theme
98 if ( $module ) {
99 $dataPath = $this->getThemeImagesPath( $theme, $module );
100 if ( !$dataPath ) {
101 return [];
102 }
103 } else {
104 // Backwards-compatibility for things that probably shouldn't have used this class...
105 $dataPath =
106 $this->definition['rootPath'] . '/' .
107 strtolower( $theme ) . '/' .
108 $this->definition['name'] . '.json';
109 }
110
111 return $this->readJSONFile( $dataPath );
112 }
113
114 /**
115 * Read JSON from a file, and transform all paths in it to be relative to the module's base path.
116 *
117 * @since 1.34
118 * @param string $dataPath Path relative to the module's base bath
119 * @return array|false
120 */
121 protected function readJSONFile( $dataPath ) {
122 $localDataPath = $this->getLocalPath( $dataPath );
123
124 if ( !file_exists( $localDataPath ) ) {
125 return false;
126 }
127
128 $data = json_decode( file_get_contents( $localDataPath ), true );
129
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()
138 );
139 } else {
140 $path = dirname( $dataPath ) . '/' . $path;
141 }
142 };
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 );
148 }
149 } );
150
151 return $data;
152 }
153 }