MediaWiki REL1_39
OOUIImageModule.php
Go to the documentation of this file.
1<?php
22
23use Exception;
24
32 use OOUIModule;
33
34 protected function loadFromDefinition() {
35 if ( $this->definition === null ) {
36 // Do nothing if definition was already processed
37 return;
38 }
39
40 $themes = self::getSkinThemeMap();
41
42 // For backwards-compatibility, allow missing 'themeImages'
43 $module = $this->definition['themeImages'] ?? '';
44
45 $definition = [];
46 foreach ( $themes as $skin => $theme ) {
47 $data = $this->loadOOUIDefinition( $theme, $module );
48
49 if ( !$data ) {
50 // If there's no file for this module of this theme, that's okay, it will just use the defaults
51 continue;
52 }
53
54 // Convert into a definition compatible with the parent vanilla ImageModule
55 foreach ( $data as $key => $value ) {
56 switch ( $key ) {
57 // Images and color variants are defined per-theme, here converted to per-skin
58 case 'images':
59 case 'variants':
60 $definition[$key][$skin] = $value;
61 break;
62
63 // Other options must be identical for each theme (or only defined in the default one)
64 default:
65 if ( !isset( $definition[$key] ) ) {
66 $definition[$key] = $value;
67 } elseif ( $definition[$key] !== $value ) {
68 throw new Exception(
69 "Mismatched OOUI theme images definition: " .
70 "key '$key' of theme '$theme' for module '$module' " .
71 "does not match other themes"
72 );
73 }
74 break;
75 }
76 }
77 }
78
79 // Extra selectors to allow using the same icons for old-style MediaWiki UI code
80 if ( substr( $module, 0, 5 ) === 'icons' ) {
81 $definition['selectorWithoutVariant'] = '.oo-ui-icon-{name}, .mw-ui-icon-{name}:before';
82 $definition['selectorWithVariant'] = '.oo-ui-image-{variant}.oo-ui-icon-{name}, ' .
83 '.mw-ui-icon-{name}-{variant}:before';
84 }
85
86 // Fields from module definition silently override keys from JSON files
87 $this->definition += $definition;
88
89 parent::loadFromDefinition();
90 }
91
101 protected function loadOOUIDefinition( $theme, $module ) {
102 // Find the path to the JSON file which contains the actual image definitions for this theme
103 if ( $module ) {
104 $dataPath = $this->getThemeImagesPath( $theme, $module );
105 if ( !$dataPath ) {
106 return [];
107 }
108 } else {
109 // Backwards-compatibility for things that probably shouldn't have used this class...
110 $dataPath =
111 $this->definition['rootPath'] . '/' .
112 strtolower( $theme ) . '/' .
113 $this->definition['name'] . '.json';
114 }
115
116 return $this->readJSONFile( $dataPath );
117 }
118
126 protected function readJSONFile( $dataPath ) {
127 $localDataPath = $this->getLocalPath( $dataPath );
128
129 if ( !file_exists( $localDataPath ) ) {
130 return false;
131 }
132
133 $data = json_decode( file_get_contents( $localDataPath ), true );
134
135 // Expand the paths to images (since they are relative to the JSON file that defines them, not
136 // our base directory)
137 $fixPath = static function ( &$path ) use ( $dataPath ) {
138 if ( $dataPath instanceof FilePath ) {
139 $path = new FilePath(
140 dirname( $dataPath->getPath() ) . '/' . $path,
141 $dataPath->getLocalBasePath(),
142 $dataPath->getRemoteBasePath()
143 );
144 } else {
145 $path = dirname( $dataPath ) . '/' . $path;
146 }
147 };
148 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
149 array_walk( $data['images'], static function ( &$value ) use ( $fixPath ) {
150 if ( is_string( $value['file'] ) ) {
151 $fixPath( $value['file'] );
152 } elseif ( is_array( $value['file'] ) ) {
153 array_walk_recursive( $value['file'], $fixPath );
154 }
155 } );
156
157 return $data;
158 }
159}
160
162class_alias( OOUIImageModule::class, 'ResourceLoaderOOUIImageModule' );
A path to a bundled file (such as JavaScript or CSS), along with a remote and local base path.
Definition FilePath.php:34
Module for generated and embedded images.
Loads the module definition from JSON files in the format that OOUI uses, converting it to the format...
loadOOUIDefinition( $theme, $module)
Load the module definition from the JSON file(s) for the given theme and module.
readJSONFile( $dataPath)
Read JSON from a file, and transform all paths in it to be relative to the module's base path.
loadFromDefinition()
Parse definition and external JSON data, if referenced.
trait OOUIModule
Convenience methods for dealing with OOUI themes and their relations to MW skins.
static getSkinThemeMap()
Return a map of skin names (in lowercase) to OOUI theme names, defining which theme a given skin shou...
getThemeImagesPath( $theme, $module)