MediaWiki master
OOUIImageModule.php
Go to the documentation of this file.
1<?php
8
9use LogicException;
10
18 use OOUIModule;
19
20 protected function loadFromDefinition() {
21 if ( $this->definition === null ) {
22 // Do nothing if definition was already processed
23 return;
24 }
25
26 $themes = self::getSkinThemeMap();
27
28 // For backwards-compatibility, allow missing 'themeImages'
29 $module = $this->definition['themeImages'] ?? '';
30
31 $definition = [];
32 foreach ( $themes as $skin => $theme ) {
33 $data = $this->loadOOUIDefinition( $theme, $module );
34
35 if ( !$data ) {
36 // If there's no file for this module of this theme, that's okay, it will just use the defaults
37 continue;
38 }
39
40 // Convert into a definition compatible with the parent vanilla ImageModule
41 foreach ( $data as $key => $value ) {
42 switch ( $key ) {
43 // Images and color variants are defined per-theme, here converted to per-skin
44 case 'images':
45 case 'variants':
46 $definition[$key][$skin] = $value;
47 break;
48
49 // Other options must be identical for each theme (or only defined in the default one)
50 default:
51 if ( !isset( $definition[$key] ) ) {
52 $definition[$key] = $value;
53 } elseif ( $definition[$key] !== $value ) {
54 throw new LogicException(
55 "Mismatched OOUI theme images definition: " .
56 "key '$key' of theme '$theme' for module '$module' " .
57 "does not match other themes"
58 );
59 }
60 break;
61 }
62 }
63 }
64
65 // Extra selectors to allow using the same icons for old-style MediaWiki UI code
66 if ( str_starts_with( $module, 'icons' ) ) {
67 $definition['selectorWithoutVariant'] = '.oo-ui-icon-{name}, .mw-ui-icon-{name}:before';
68 $definition['selectorWithVariant'] = '.oo-ui-image-{variant}.oo-ui-icon-{name}, ' .
69 '.mw-ui-icon-{name}-{variant}:before';
70 }
71
72 // Fields from module definition silently override keys from JSON files
73 $this->definition += $definition;
74
75 parent::loadFromDefinition();
76 }
77
87 protected function loadOOUIDefinition( $theme, $module ) {
88 // Find the path to the JSON file which contains the actual image definitions for this theme
89 if ( $module ) {
90 $dataPath = $this->getThemeImagesPath( $theme, $module );
91 } else {
92 // Backwards-compatibility for things that probably shouldn't have used this class...
93 $dataPath =
94 $this->definition['rootPath'] . '/' .
95 strtolower( $theme ) . '/' .
96 $this->definition['name'] . '.json';
97 }
98
99 return $this->readJSONFile( $dataPath );
100 }
101
109 protected function readJSONFile( $dataPath ) {
110 $localDataPath = $this->getLocalPath( $dataPath );
111
112 if ( !file_exists( $localDataPath ) ) {
113 return false;
114 }
115
116 $data = json_decode( file_get_contents( $localDataPath ), true );
117
118 // Expand the paths to images (since they are relative to the JSON file that defines them, not
119 // our base directory)
120 $fixPath = static function ( &$path ) use ( $dataPath ) {
121 if ( $dataPath instanceof FilePath ) {
122 $path = new FilePath(
123 dirname( $dataPath->getPath() ) . '/' . $path,
124 $dataPath->getLocalBasePath(),
125 $dataPath->getRemoteBasePath()
126 );
127 } else {
128 $path = dirname( $dataPath ) . '/' . $path;
129 }
130 };
131 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
132 array_walk( $data['images'], static function ( &$value ) use ( $fixPath ) {
133 if ( is_string( $value['file'] ) ) {
134 $fixPath( $value['file'] );
135 } elseif ( is_array( $value['file'] ) ) {
136 array_walk_recursive( $value['file'], $fixPath );
137 }
138 } );
139
140 return $data;
141 }
142}
A path to a bundled file (such as JavaScript or CSS), along with a remote and local base path.
Definition FilePath.php:20
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)