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