| 1 |
<?php |
| 2 |
|
| 3 |
use Composer\Semver\VersionParser; |
| 4 |
use MediaWiki\Json\FormatJson; |
| 5 |
use MediaWiki\Registration\ExtensionRegistry; |
| 6 |
|
| 7 |
|
| 8 |
require_once __DIR__ . '/Maintenance.php'; |
| 9 |
|
| 10 |
|
| 11 |
class UpdateExtensionJsonSchema extends Maintenance { |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
parent::__construct(); |
| 15 |
$this->addDescription( 'Updates extension.json files to the latest manifest_version' ); |
| 16 |
$this->addArg( 'path', 'Location to the extension.json or skin.json you wish to convert', |
| 17 |
true ); |
| 18 |
} |
| 19 |
|
| 20 |
public function execute() { |
| 21 |
$filename = $this->getArg( 0 ); |
| 22 |
if ( !is_readable( $filename ) ) { |
| 23 |
$this->fatalError( "Error: Unable to read $filename" ); |
| 24 |
} |
| 25 |
|
| 26 |
$json = FormatJson::decode( file_get_contents( $filename ), true ); |
| 27 |
if ( !is_array( $json ) ) { |
| 28 |
$this->fatalError( "Error: Invalid JSON" ); |
| 29 |
} |
| 30 |
|
| 31 |
if ( !isset( $json['manifest_version'] ) ) { |
| 32 |
$json['manifest_version'] = 1; |
| 33 |
} |
| 34 |
|
| 35 |
if ( $json['manifest_version'] == ExtensionRegistry::MANIFEST_VERSION ) { |
| 36 |
$this->output( "Already at the latest version: {$json['manifest_version']}\n" ); |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
while ( $json['manifest_version'] !== ExtensionRegistry::MANIFEST_VERSION ) { |
| 41 |
$json['manifest_version'] += 1; |
| 42 |
$func = "updateTo{$json['manifest_version']}"; |
| 43 |
$this->$func( $json ); |
| 44 |
} |
| 45 |
|
| 46 |
$this->updateRequiredMwVersion( $json ); |
| 47 |
|
| 48 |
file_put_contents( $filename, FormatJson::encode( $json, "\t", FormatJson::ALL_OK ) . "\n" ); |
| 49 |
$this->output( "Updated to {$json['manifest_version']}...\n" ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected function updateRequiredMwVersion( &$json ) { |
| 56 |
if ( !isset( $json['requires'] ) ) { |
| 57 |
$json['requires'] = []; |
| 58 |
} |
| 59 |
|
| 60 |
$needNewVersion = true; |
| 61 |
|
| 62 |
|
| 63 |
if ( isset( $json['requires'][ExtensionRegistry::MEDIAWIKI_CORE] ) ) { |
| 64 |
$versionParser = new VersionParser(); |
| 65 |
$currentRequired = $versionParser->parseConstraints( |
| 66 |
|
| 67 |
$json['requires'][ExtensionRegistry::MEDIAWIKI_CORE] |
| 68 |
); |
| 69 |
$newRequired = $versionParser->parseConstraints( |
| 70 |
|
| 71 |
str_replace( '>=', '==', ExtensionRegistry::MANIFEST_VERSION_MW_VERSION ) |
| 72 |
); |
| 73 |
if ( !$currentRequired->matches( $newRequired ) ) { |
| 74 |
$needNewVersion = false; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if ( $needNewVersion ) { |
| 79 |
|
| 80 |
|
| 81 |
$json['requires'][ExtensionRegistry::MEDIAWIKI_CORE] = |
| 82 |
ExtensionRegistry::MANIFEST_VERSION_MW_VERSION; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
protected function updateTo2( &$json ) { |
| 87 |
if ( isset( $json['config'] ) ) { |
| 88 |
$config = $json['config']; |
| 89 |
$json['config'] = []; |
| 90 |
if ( isset( $config['_prefix'] ) ) { |
| 91 |
$json = wfArrayInsertAfter( $json, [ |
| 92 |
'config_prefix' => $config['_prefix'] |
| 93 |
], 'config' ); |
| 94 |
unset( $config['_prefix'] ); |
| 95 |
} |
| 96 |
|
| 97 |
foreach ( $config as $name => $value ) { |
| 98 |
if ( $name[0] !== '@' ) { |
| 99 |
$json['config'][$name] = [ 'value' => $value ]; |
| 100 |
if ( isset( $value[ExtensionRegistry::MERGE_STRATEGY] ) ) { |
| 101 |
$json['config'][$name]['merge_strategy'] = $value[ExtensionRegistry::MERGE_STRATEGY]; |
| 102 |
unset( $json['config'][$name]['value'][ExtensionRegistry::MERGE_STRATEGY] ); |
| 103 |
} |
| 104 |
if ( isset( $config["@$name"] ) ) { |
| 105 |
|
| 106 |
$json['config'][$name] = array_merge( |
| 107 |
[ 'description' => $config["@$name"] ], |
| 108 |
$json['config'][$name] |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
$attributes = [ |
| 117 |
'CodeMirrorPluginModules' => [ 'CodeMirror', 'PluginModules' ], |
| 118 |
'CodeMirrorTagModes' => [ 'CodeMirror', 'TagModes' ], |
| 119 |
'EventLoggingSchemas' => [ 'EventLogging', 'Schemas' ], |
| 120 |
'SyntaxHighlightModels' => [ 'SyntaxHighlight', 'Models' ], |
| 121 |
'VisualEditorAvailableContentModels' => [ 'VisualEditor', 'AvailableContentModels' ], |
| 122 |
'VisualEditorAvailableNamespaces' => [ 'VisualEditor', 'AvailableNamespaces' ], |
| 123 |
'VisualEditorPreloadModules' => [ 'VisualEditor', 'PreloadModules' ], |
| 124 |
'VisualEditorPluginModules' => [ 'VisualEditor', 'PluginModules' ], |
| 125 |
]; |
| 126 |
|
| 127 |
foreach ( $attributes as $name => $value ) { |
| 128 |
if ( !isset( $json[$name] ) ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
$json['attributes'][$value[0]][$value[1]] = $json[$name]; |
| 133 |
unset( $json[$name] ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
$maintClass = UpdateExtensionJsonSchema::class; |
| 140 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 141 |
|