| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\ResourceLoader; |
| 22 |
|
| 23 |
use LogicException; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class OOUIImageModule extends ImageModule { |
| 32 |
use OOUIModule; |
| 33 |
|
| 34 |
protected function loadFromDefinition() { |
| 35 |
if ( $this->definition === null ) { |
| 36 |
|
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
$themes = self::getSkinThemeMap(); |
| 41 |
|
| 42 |
|
| 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 |
|
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
foreach ( $data as $key => $value ) { |
| 56 |
switch ( $key ) { |
| 57 |
|
| 58 |
case 'images': |
| 59 |
case 'variants': |
| 60 |
$definition[$key][$skin] = $value; |
| 61 |
break; |
| 62 |
|
| 63 |
|
| 64 |
default: |
| 65 |
if ( !isset( $definition[$key] ) ) { |
| 66 |
$definition[$key] = $value; |
| 67 |
} elseif ( $definition[$key] !== $value ) { |
| 68 |
throw new LogicException( |
| 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 |
|
| 80 |
if ( str_starts_with( $module, '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 |
|
| 87 |
$this->definition += $definition; |
| 88 |
|
| 89 |
parent::loadFromDefinition(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
protected function loadOOUIDefinition( $theme, $module ) { |
| 102 |
|
| 103 |
if ( $module ) { |
| 104 |
$dataPath = $this->getThemeImagesPath( $theme, $module ); |
| 105 |
} else { |
| 106 |
|
| 107 |
$dataPath = |
| 108 |
$this->definition['rootPath'] . '/' . |
| 109 |
strtolower( $theme ) . '/' . |
| 110 |
$this->definition['name'] . '.json'; |
| 111 |
} |
| 112 |
|
| 113 |
return $this->readJSONFile( $dataPath ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
|
| 133 |
|
| 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 |
|
| 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 |
} |