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