wiki.techinc.nl/maintenance/updateExtensionJsonSchema.php
Dreamy Jazz e7393b3cc7 Exclude boilerplate maintenance code from code coverage reports
Why:
* Maintenance scripts in core have bolierplate code that is
  added before and after the class to allow directly running
  the maintenance script.
* Running the maintenance script directly has been deprecated
  since 1.40, so this boilerplate code is only to support a now
  deprecated method of running maintenance scripts.
* This code cannot also be marked as covered, due to PHPUnit
  not recognising code coverage for files.
* Therefore, it is best to ignore this boilerplate code in code
  coverage reports as it cannot be marked as covered and also
  is for deprecated code.

What:
* Wrap the boilerplate code (requiring Maintenance.php and then
  later defining the maintenance script class and running if the
  maintenance script was called directly) with @codeCoverageIgnore
  comments.
* Some files use a different boilerplate code, however, these
  should also be marked as ignored for coverage for the same
  reason that coverage is not properly reported for files.

Bug: T371167
Change-Id: I32f5c6362dfb354149a48ce9c28da9a7fc494f7c
2024-08-27 13:22:29 +01:00

141 lines
4.5 KiB
PHP

<?php
use Composer\Semver\VersionParser;
use MediaWiki\Json\FormatJson;
use MediaWiki\Registration\ExtensionRegistry;
// @codeCoverageIgnoreStart
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd
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] );
}
}
}
// @codeCoverageIgnoreStart
$maintClass = UpdateExtensionJsonSchema::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd