MediaWiki
master
Toggle main menu visibility
OOUIImageModule.php
Go to the documentation of this file.
1
<?php
6
7
namespace
MediaWiki\ResourceLoader;
8
9
use LogicException;
10
17
class
OOUIImageModule
extends
ImageModule
{
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 = $dataPath instanceof
FilePath
?
121
static
function
( &
$path
) use ( $dataPath ) {
122
$path
=
new
FilePath
(
123
dirname( $dataPath->getPath() ) .
'/'
.
$path
,
124
$dataPath->getLocalBasePath(),
125
$dataPath->getRemoteBasePath()
126
);
127
} :
128
static
function
( &
$path
) use ( $dataPath ) {
129
$path
= dirname( $dataPath ) .
'/'
.
$path
;
130
};
131
132
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
133
array_walk( $data[
'images'
],
static
function
( &$value ) use ( $fixPath ) {
134
if
( is_string( $value[
'file'
] ) ) {
135
$fixPath( $value[
'file'
] );
136
} elseif ( is_array( $value[
'file'
] ) ) {
137
array_walk_recursive( $value[
'file'
], $fixPath );
138
}
139
} );
140
141
return
$data;
142
}
143
}
$path
$path
Definition
NoLocalSettings.php:14
MediaWiki\ResourceLoader\FilePath
A path to a bundled file (such as JavaScript or CSS), along with a remote and local base path.
Definition
FilePath.php:20
MediaWiki\ResourceLoader\ImageModule
Module for generated and embedded images.
Definition
ImageModule.php:19
MediaWiki\ResourceLoader\ImageModule\getLocalPath
getLocalPath( $path)
Definition
ImageModule.php:464
MediaWiki\ResourceLoader\ImageModule\$definition
array null $definition
Definition
ImageModule.php:23
MediaWiki\ResourceLoader\OOUIImageModule
Loads the module definition from JSON files in the format that OOUI uses, converting it to the format...
Definition
OOUIImageModule.php:17
MediaWiki\ResourceLoader\OOUIImageModule\loadOOUIDefinition
loadOOUIDefinition( $theme, $module)
Load the module definition from the JSON file(s) for the given theme and module.
Definition
OOUIImageModule.php:87
MediaWiki\ResourceLoader\OOUIImageModule\readJSONFile
readJSONFile( $dataPath)
Read JSON from a file, and transform all paths in it to be relative to the module's base path.
Definition
OOUIImageModule.php:109
MediaWiki\ResourceLoader\OOUIImageModule\loadFromDefinition
loadFromDefinition()
Parse definition and external JSON data, if referenced.
Definition
OOUIImageModule.php:20
MediaWiki\ResourceLoader\OOUIModule
trait OOUIModule
Convenience methods for dealing with OOUI themes and their relations to MW skins.
Definition
OOUIModule.php:18
MediaWiki\ResourceLoader\getThemeImagesPath
getThemeImagesPath( $theme, $module)
Definition
OOUIModule.php:174
includes
ResourceLoader
OOUIImageModule.php
Generated on
for MediaWiki by
1.18.0