Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateExtensionJsonSchema
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 4
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
 updateRequiredMwVersion
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 updateTo2
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3use Composer\Semver\VersionParser;
4use MediaWiki\Json\FormatJson;
5
6require_once __DIR__ . '/Maintenance.php';
7
8class UpdateExtensionJsonSchema extends Maintenance {
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
49    /**
50     * @param array &$json
51     */
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;