MediaWiki REL1_34
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] = $data[$key];
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] = $data[$key];
63 } elseif ( $definition[$key] !== $data[$key] ) {
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
96 protected function loadOOUIDefinition( $theme, $module ) {
97 // Find the path to the JSON file which contains the actual image definitions for this theme
98 if ( $module ) {
99 $dataPath = $this->getThemeImagesPath( $theme, $module );
100 if ( !$dataPath ) {
101 return [];
102 }
103 } else {
104 // Backwards-compatibility for things that probably shouldn't have used this class...
105 $dataPath =
106 $this->definition['rootPath'] . '/' .
107 strtolower( $theme ) . '/' .
108 $this->definition['name'] . '.json';
109 }
110
111 return $this->readJSONFile( $dataPath );
112 }
113
121 protected function readJSONFile( $dataPath ) {
122 $localDataPath = $this->getLocalPath( $dataPath );
123
124 if ( !file_exists( $localDataPath ) ) {
125 return false;
126 }
127
128 $data = json_decode( file_get_contents( $localDataPath ), true );
129
130 // Expand the paths to images (since they are relative to the JSON file that defines them, not
131 // our base directory)
132 $fixPath = function ( &$path ) use ( $dataPath ) {
133 if ( $dataPath instanceof ResourceLoaderFilePath ) {
135 dirname( $dataPath->getPath() ) . '/' . $path,
136 $dataPath->getLocalBasePath(),
137 $dataPath->getRemoteBasePath()
138 );
139 } else {
140 $path = dirname( $dataPath ) . '/' . $path;
141 }
142 };
143 array_walk( $data['images'], function ( &$value ) use ( $fixPath ) {
144 if ( is_string( $value['file'] ) ) {
145 $fixPath( $value['file'] );
146 } elseif ( is_array( $value['file'] ) ) {
147 array_walk_recursive( $value['file'], $fixPath );
148 }
149 } );
150
151 return $data;
152 }
153}
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.