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