Update OOjs UI to v0.22.1
[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 * Secret special sauce.
23 *
24 * @since 1.26
25 */
26 class ResourceLoaderOOUIImageModule extends ResourceLoaderImageModule {
27 protected function loadFromDefinition() {
28 if ( $this->definition === null ) {
29 // Do nothing if definition was already processed
30 return;
31 }
32
33 // Core default themes
34 $themes = [ 'default' => 'wikimediaui' ];
35 $themes += ExtensionRegistry::getInstance()->getAttribute( 'SkinOOUIThemes' );
36
37 $name = $this->definition['name'];
38 $rootPath = $this->definition['rootPath'];
39
40 $definition = [];
41 foreach ( $themes as $skin => $theme ) {
42 // Find the path to the JSON file which contains the actual image definitions for this theme
43 // TODO Allow extensions to specify this path somehow
44 $dataPath = $rootPath . '/' . strtolower( $theme ) . '/' . $name . '.json';
45 $localDataPath = $this->localBasePath . '/' . $dataPath;
46
47 // If there's no file for this module of this theme, that's okay, it will just use the defaults
48 if ( !file_exists( $localDataPath ) ) {
49 continue;
50 }
51 $data = json_decode( file_get_contents( $localDataPath ), true );
52
53 // Expand the paths to images (since they are relative to the JSON file that defines them, not
54 // our base directory)
55 $fixPath = function ( &$path ) use ( $dataPath ) {
56 $path = dirname( $dataPath ) . '/' . $path;
57 };
58 array_walk( $data['images'], function ( &$value ) use ( $fixPath ) {
59 if ( is_string( $value['file'] ) ) {
60 $fixPath( $value['file'] );
61 } elseif ( is_array( $value['file'] ) ) {
62 array_walk_recursive( $value['file'], $fixPath );
63 }
64 } );
65
66 // Convert into a definition compatible with the parent vanilla ResourceLoaderImageModule
67 foreach ( $data as $key => $value ) {
68 switch ( $key ) {
69 // Images and color variants are defined per-theme, here converted to per-skin
70 case 'images':
71 case 'variants':
72 $definition[$key][$skin] = $data[$key];
73 break;
74
75 // Other options must be identical for each theme (or only defined in the default one)
76 default:
77 if ( !isset( $definition[$key] ) ) {
78 $definition[$key] = $data[$key];
79 } elseif ( $definition[$key] !== $data[$key] ) {
80 throw new Exception(
81 "Mismatched OOUI theme images definition: " .
82 "key '$key' of theme '$theme' " .
83 "does not match other themes"
84 );
85 }
86 break;
87 }
88 }
89 }
90
91 // Fields from module definition silently override keys from JSON files
92 $this->definition += $definition;
93
94 parent::loadFromDefinition();
95 }
96 }