Merge "Move up devunt's name to Developers"
[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 return;
30 }
31
32 // Core default themes
33 $themes = [ 'default' => 'mediawiki' ];
34 $themes += ExtensionRegistry::getInstance()->getAttribute( 'SkinOOUIThemes' );
35
36 $name = $this->definition['name'];
37 $rootPath = $this->definition['rootPath'];
38
39 $definition = [];
40 foreach ( $themes as $skin => $theme ) {
41 // TODO Allow extensions to specify this path somehow
42 $dataPath = $this->localBasePath . '/' . $rootPath . '/' . $theme . '/' . $name . '.json';
43
44 if ( file_exists( $dataPath ) ) {
45 $data = json_decode( file_get_contents( $dataPath ), true );
46 $fixPath = function ( &$path ) use ( $rootPath, $theme ) {
47 // TODO Allow extensions to specify this path somehow
48 $path = $rootPath . '/' . $theme . '/' . $path;
49 };
50 array_walk( $data['images'], function ( &$value ) use ( $fixPath ) {
51 if ( is_string( $value['file'] ) ) {
52 $fixPath( $value['file'] );
53 } elseif ( is_array( $value['file'] ) ) {
54 array_walk_recursive( $value['file'], $fixPath );
55 }
56 } );
57 } else {
58 $data = [];
59 }
60
61 foreach ( $data as $key => $value ) {
62 switch ( $key ) {
63 case 'images':
64 case 'variants':
65 $definition[$key][$skin] = $data[$key];
66 break;
67
68 default:
69 if ( !isset( $definition[$key] ) ) {
70 $definition[$key] = $data[$key];
71 } elseif ( $definition[$key] !== $data[$key] ) {
72 throw new Exception(
73 "Mismatched OOUI theme definitions are not supported: trying to load $key of $theme theme"
74 );
75 }
76 break;
77 }
78 }
79 }
80
81 // Fields from definition silently override keys from JSON files
82 $this->definition += $definition;
83
84 parent::loadFromDefinition();
85 }
86 }