MediaWiki master
updateExtensionJsonSchema.php
Go to the documentation of this file.
1<?php
2
3use Composer\Semver\VersionParser;
6
7// @codeCoverageIgnoreStart
8require_once __DIR__ . '/Maintenance.php';
9// @codeCoverageIgnoreEnd
10
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 /* $required = */ 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
55 protected function updateRequiredMwVersion( &$json ) {
56 if ( !isset( $json['requires'] ) ) {
57 $json['requires'] = [];
58 }
59
60 $needNewVersion = true;
61
62 // When version is set, parse it and compare against requirement for new manifest
63 if ( isset( $json['requires'][ExtensionRegistry::MEDIAWIKI_CORE] ) ) {
64 $versionParser = new VersionParser();
65 $currentRequired = $versionParser->parseConstraints(
66 // @phan-suppress-next-line PhanTypeInvalidDimOffset,PhanTypeMismatchArgument isset check exists
67 $json['requires'][ExtensionRegistry::MEDIAWIKI_CORE]
68 );
69 $newRequired = $versionParser->parseConstraints(
70 // The match works only when using an equal comparision
71 str_replace( '>=', '==', ExtensionRegistry::MANIFEST_VERSION_MW_VERSION )
72 );
73 if ( !$currentRequired->matches( $newRequired ) ) {
74 $needNewVersion = false;
75 }
76 }
77
78 if ( $needNewVersion ) {
79 // Set or update a requirement on the MediaWiki version
80 // that the current MANIFEST_VERSION was introduced in.
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 // Put 'description' first for better human-legibility.
106 $json['config'][$name] = array_merge(
107 [ 'description' => $config["@$name"] ],
108 $json['config'][$name]
109 );
110 }
111 }
112 }
113 }
114
115 // Re-maps top level keys under attributes
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// @codeCoverageIgnoreStart
139$maintClass = UpdateExtensionJsonSchema::class;
140require_once RUN_MAINTENANCE_IF_MAIN;
141// @codeCoverageIgnoreEnd
wfArrayInsertAfter(array $array, array $insert, $after)
Insert an array into another array after the specified key.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
JSON formatter wrapper class.
Load JSON files, and uses a Processor to extract information.