MediaWiki REL1_35
ResourceLoaderOOUIImageModule.php
Go to the documentation of this file.
1<?php
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] = $value;
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] = $value;
63 } elseif ( $definition[$key] !== $value ) {
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
97 protected function loadOOUIDefinition( $theme, $module ) {
98 // Find the path to the JSON file which contains the actual image definitions for this theme
99 if ( $module ) {
100 $dataPath = $this->getThemeImagesPath( $theme, $module );
101 if ( !$dataPath ) {
102 return [];
103 }
104 } else {
105 // Backwards-compatibility for things that probably shouldn't have used this class...
106 $dataPath =
107 $this->definition['rootPath'] . '/' .
108 strtolower( $theme ) . '/' .
109 $this->definition['name'] . '.json';
110 }
111
112 return $this->readJSONFile( $dataPath );
113 }
114
122 protected function readJSONFile( $dataPath ) {
123 $localDataPath = $this->getLocalPath( $dataPath );
124
125 if ( !file_exists( $localDataPath ) ) {
126 return false;
127 }
128
129 $data = json_decode( file_get_contents( $localDataPath ), true );
130
131 // Expand the paths to images (since they are relative to the JSON file that defines them, not
132 // our base directory)
133 $fixPath = function ( &$path ) use ( $dataPath ) {
134 if ( $dataPath instanceof ResourceLoaderFilePath ) {
136 dirname( $dataPath->getPath() ) . '/' . $path,
137 $dataPath->getLocalBasePath(),
138 $dataPath->getRemoteBasePath()
139 );
140 } else {
141 $path = dirname( $dataPath ) . '/' . $path;
142 }
143 };
144 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
145 array_walk( $data['images'], function ( &$value ) use ( $fixPath ) {
146 if ( is_string( $value['file'] ) ) {
147 $fixPath( $value['file'] );
148 } elseif ( is_array( $value['file'] ) ) {
149 array_walk_recursive( $value['file'], $fixPath );
150 }
151 } );
152
153 return $data;
154 }
155}
static getSkinThemeMap()
Return a map of skin names (in lowercase) to OOUI theme names, defining which theme a given skin shou...
getThemeImagesPath( $theme, $module)
An object to represent a path to a JavaScript/CSS file, along with a remote and local base path,...
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.
loadFromDefinition()
Parse definition and external JSON data, if referenced.
readJSONFile( $dataPath)
Read JSON from a file, and transform all paths in it to be relative to the module's base path.
trait ResourceLoaderOOUIModule
Convenience methods for dealing with OOUI themes and their relations to MW skins.