MediaWiki  1.34.0
updateExtensionJsonSchema.php
Go to the documentation of this file.
1 <?php
2 
3 require_once __DIR__ . '/Maintenance.php';
4 
6 
7  public function __construct() {
8  parent::__construct();
9  $this->addDescription( 'Updates extension.json files to the latest manifest_version' );
10  $this->addArg( 'path', 'Location to the extension.json or skin.json you wish to convert',
11  /* $required = */ true );
12  }
13 
14  public function execute() {
15  $filename = $this->getArg( 0 );
16  if ( !is_readable( $filename ) ) {
17  $this->fatalError( "Error: Unable to read $filename" );
18  }
19 
20  $json = FormatJson::decode( file_get_contents( $filename ), true );
21  if ( !is_array( $json ) ) {
22  $this->fatalError( "Error: Invalid JSON" );
23  }
24 
25  if ( !isset( $json['manifest_version'] ) ) {
26  $json['manifest_version'] = 1;
27  }
28 
29  if ( $json['manifest_version'] == ExtensionRegistry::MANIFEST_VERSION ) {
30  $this->output( "Already at the latest version: {$json['manifest_version']}\n" );
31  return;
32  }
33 
34  while ( $json['manifest_version'] !== ExtensionRegistry::MANIFEST_VERSION ) {
35  $json['manifest_version'] += 1;
36  $func = "updateTo{$json['manifest_version']}";
37  // @phan-suppress-next-line PhanUndeclaredMethod
38  $this->$func( $json );
39  }
40 
41  file_put_contents( $filename, FormatJson::encode( $json, "\t", FormatJson::ALL_OK ) . "\n" );
42  $this->output( "Updated to {$json['manifest_version']}...\n" );
43  }
44 
45  protected function updateTo2( &$json ) {
46  if ( isset( $json['config'] ) ) {
47  $config = $json['config'];
48  $json['config'] = [];
49  if ( isset( $config['_prefix'] ) ) {
50  $json = wfArrayInsertAfter( $json, [
51  'config_prefix' => $config['_prefix']
52  ], 'config' );
53  unset( $config['_prefix'] );
54  }
55 
56  foreach ( $config as $name => $value ) {
57  if ( $name[0] !== '@' ) {
58  $json['config'][$name] = [ 'value' => $value ];
59  if ( isset( $value[ExtensionRegistry::MERGE_STRATEGY] ) ) {
60  $json['config'][$name]['merge_strategy'] = $value[ExtensionRegistry::MERGE_STRATEGY];
61  unset( $value[ExtensionRegistry::MERGE_STRATEGY] );
62  }
63  if ( isset( $config["@$name"] ) ) {
64  // Put 'description' first for better human-legibility.
65  $json['config'][$name] = array_merge(
66  [ 'description' => $config["@$name"] ],
67  $json['config'][$name]
68  );
69  }
70  }
71  }
72  }
73  }
74 }
75 
76 $maintClass = UpdateExtensionJsonSchema::class;
77 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
wfArrayInsertAfter
wfArrayInsertAfter(array $array, array $insert, $after)
Insert array into another array after the specified KEY
Definition: GlobalFunctions.php:207
ExtensionRegistry\MANIFEST_VERSION
const MANIFEST_VERSION
Version of the highest supported manifest version Note: Update MANIFEST_VERSION_MW_VERSION when chang...
Definition: ExtensionRegistry.php:29
UpdateExtensionJsonSchema\updateTo2
updateTo2(&$json)
Definition: updateExtensionJsonSchema.php:45
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
FormatJson\ALL_OK
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition: FormatJson.php:55
ExtensionRegistry\MERGE_STRATEGY
const MERGE_STRATEGY
Special key that defines the merge strategy.
Definition: ExtensionRegistry.php:52
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:174
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
UpdateExtensionJsonSchema
Definition: updateExtensionJsonSchema.php:5
UpdateExtensionJsonSchema\__construct
__construct()
Default constructor.
Definition: updateExtensionJsonSchema.php:7
Maintenance\$config
Config $config
Accessible via getConfig()
Definition: Maintenance.php:170
$maintClass
$maintClass
Definition: updateExtensionJsonSchema.php:76
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:319
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
UpdateExtensionJsonSchema\execute
execute()
Do the actual work.
Definition: updateExtensionJsonSchema.php:14
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:371