Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| VisualEditorFileModule | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
380 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
380 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\VisualEditor; |
| 4 | |
| 5 | use MediaWiki\Config\ConfigException; |
| 6 | use MediaWiki\MediaWikiServices; |
| 7 | use MediaWiki\ResourceLoader\FileModule; |
| 8 | |
| 9 | /** |
| 10 | * ResourceLoader module for VisualEditor lib/ve modules, reading file lists from modules.json. |
| 11 | */ |
| 12 | class VisualEditorFileModule extends FileModule { |
| 13 | |
| 14 | public function __construct( |
| 15 | array $options = [], |
| 16 | ?string $localBasePath = null, |
| 17 | ?string $remoteBasePath = null |
| 18 | ) { |
| 19 | if ( !isset( $options['veModules'] ) || !is_array( $options['veModules'] ) ) { |
| 20 | parent::__construct( $options, $localBasePath, $remoteBasePath ); |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | $jsonPath = __DIR__ . '/../lib/ve/build/modules.json'; |
| 25 | $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache(); |
| 26 | $cacheKey = $cache->makeKey( |
| 27 | 'visualeditor-modules-json', |
| 28 | $jsonPath, |
| 29 | filemtime( $jsonPath ) |
| 30 | ); |
| 31 | $veModulesJson = $cache->getWithSetCallback( |
| 32 | $cacheKey, |
| 33 | $cache::TTL_DAY, |
| 34 | static function () use ( $jsonPath ) { |
| 35 | return json_decode( file_get_contents( $jsonPath ), true ); |
| 36 | } |
| 37 | ); |
| 38 | |
| 39 | // Prepend scripts/styles from $options['veModules'] to $options['scripts']/$options['styles'] |
| 40 | $scripts = []; |
| 41 | $debugScripts = []; |
| 42 | $styles = []; |
| 43 | foreach ( $options['veModules'] as $veModuleName ) { |
| 44 | if ( !isset( $veModulesJson[$veModuleName] ) ) { |
| 45 | throw new ConfigException( 'veModule not found: ' . $veModuleName ); |
| 46 | } |
| 47 | $module = $veModulesJson[$veModuleName]; |
| 48 | if ( isset( $module['scripts'] ) ) { |
| 49 | foreach ( $module['scripts'] as $item ) { |
| 50 | $path = is_array( $item ) ? $item['file'] : $item; |
| 51 | $fullPath = 'lib/ve/' . $path; |
| 52 | if ( is_array( $item ) && !empty( $item['debug'] ) ) { |
| 53 | $debugScripts[] = $fullPath; |
| 54 | } else { |
| 55 | $scripts[] = $fullPath; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | if ( isset( $module['styles'] ) ) { |
| 60 | foreach ( $module['styles'] as $item ) { |
| 61 | $path = is_array( $item ) ? $item['file'] : $item; |
| 62 | $styles[] = 'lib/ve/' . $path; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | if ( isset( $options['scripts'] ) && is_array( $options['scripts'] ) ) { |
| 67 | $options['scripts'] = array_merge( $scripts, $options['scripts'] ); |
| 68 | } else { |
| 69 | $options['scripts'] = $scripts; |
| 70 | } |
| 71 | if ( isset( $options['debugScripts'] ) && is_array( $options['debugScripts'] ) ) { |
| 72 | $options['debugScripts'] = array_merge( $debugScripts, $options['debugScripts'] ); |
| 73 | } else { |
| 74 | $options['debugScripts'] = $debugScripts; |
| 75 | } |
| 76 | if ( isset( $options['styles'] ) && is_array( $options['styles'] ) ) { |
| 77 | $options['styles'] = array_merge( $styles, $options['styles'] ); |
| 78 | } else { |
| 79 | $options['styles'] = $styles; |
| 80 | } |
| 81 | |
| 82 | parent::__construct( $options, $localBasePath, $remoteBasePath ); |
| 83 | } |
| 84 | } |