MediaWiki REL1_40
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 } else {
106 // Backwards-compatibility for things that probably shouldn't have used this class...
107 $dataPath =
108 $this->definition['rootPath'] . '/' .
109 strtolower( $theme ) . '/' .
110 $this->definition['name'] . '.json';
111 }
112
113 return $this->readJSONFile( $dataPath );
114 }
115
123 protected function readJSONFile( $dataPath ) {
124 $localDataPath = $this->getLocalPath( $dataPath );
125
126 if ( !file_exists( $localDataPath ) ) {
127 return false;
128 }
129
130 $data = json_decode( file_get_contents( $localDataPath ), true );
131
132 // Expand the paths to images (since they are relative to the JSON file that defines them, not
133 // our base directory)
134 $fixPath = static function ( &$path ) use ( $dataPath ) {
135 if ( $dataPath instanceof FilePath ) {
136 $path = new FilePath(
137 dirname( $dataPath->getPath() ) . '/' . $path,
138 $dataPath->getLocalBasePath(),
139 $dataPath->getRemoteBasePath()
140 );
141 } else {
142 $path = dirname( $dataPath ) . '/' . $path;
143 }
144 };
145 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
146 array_walk( $data['images'], static function ( &$value ) use ( $fixPath ) {
147 if ( is_string( $value['file'] ) ) {
148 $fixPath( $value['file'] );
149 } elseif ( is_array( $value['file'] ) ) {
150 array_walk_recursive( $value['file'], $fixPath );
151 }
152 } );
153
154 return $data;
155 }
156}
157
159class_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)