class ConvertExtensionToRegistration extends Maintenance {
protected $custom = array(
- 'MessagesDirs' => 'removeAbsolutePath',
+ 'MessagesDirs' => 'handleMessagesDirs',
'ExtensionMessagesFiles' => 'removeAbsolutePath',
'AutoloadClasses' => 'removeAbsolutePath',
'ExtensionCredits' => 'handleCredits',
'ExtensionFunctions' => 'handleExtensionFunctions',
);
+ /**
+ * Keys that should be put at the top of the generated JSON file (T86608)
+ *
+ * @var array
+ */
+ protected $promote = array(
+ 'name',
+ 'version',
+ 'author',
+ 'url',
+ 'description',
+ 'descriptionmsg',
+ 'namemsg',
+ 'license-name',
+ 'type',
+ );
+
private $json, $dir;
public function __construct() {
parent::__construct();
$this->mDescription = 'Converts extension entry points to the new JSON registration format';
+ $this->addArg( 'path', 'Location to the PHP entry point you wish to convert', /* $required = */ true );
+ $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
+ }
+
+ protected function getAllGlobals() {
+ $processor = new ReflectionClass( 'ExtensionProcessor' );
+ $settings = $processor->getProperty( 'globalSettings' );
+ $settings->setAccessible( true );
+ return $settings->getValue();
}
public function execute() {
+ // Extensions will do stuff like $wgResourceModules += array(...) which is a
+ // fatal unless an array is already set. So set an empty value.
+ foreach ( array_merge( $this->getAllGlobals(), array_keys( $this->custom ) ) as $var ) {
+ $var = 'wg' . $var;
+ $$var = array();
+ }
+ unset( $var );
require $this->getArg( 0 );
// Try not to create any local variables before this line
$vars = get_defined_vars();
unset( $vars['this'] );
$this->dir = dirname( realpath( $this->getArg( 0 ) ) );
$this->json = array();
- $processor = new ReflectionClass( 'ExtensionProcessor' );
- $settings = $processor->getProperty( 'globalSettings' );
- $settings->setAccessible( true );
- $globalSettings = $settings->getValue();
+ $globalSettings = $this->getAllGlobals();
foreach ( $vars as $name => $value ) {
+ // If an empty array, assume it's the default we set, so skip it
+ if ( is_array( $value ) && count( $value ) === 0 ) {
+ continue;
+ }
$realName = substr( $name, 2 ); // Strip 'wg'
if ( isset( $this->custom[$realName] ) ) {
call_user_func_array( array( $this, $this->custom[$realName] ), array( $realName, $value ) );
}
}
- $fname = "{$this->dir}/extension.json";
- $prettyJSON = FormatJson::encode( $this->json, "\t" );
+ // Move some keys to the top
+ $out = array();
+ foreach ( $this->promote as $key ) {
+ if ( isset( $this->json[$key] ) ) {
+ $out[$key] = $this->json[$key];
+ unset( $this->json[$key] );
+ }
+ }
+ $out += $this->json;
+
+ $type = $this->hasOption( 'skin' ) ? 'skin' : 'extension';
+ $fname = "{$this->dir}/$type.json";
+ $prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK );
file_put_contents( $fname, $prettyJSON . "\n" );
$this->output( "Wrote output to $fname.\n" );
}
$this->json[$realName] = $value;
}
+ protected function handleMessagesDirs( $realName, $value ) {
+ foreach ( $value as $key => $dirs ) {
+ foreach ( (array)$dirs as $dir ) {
+ $this->json[$realName][$key][] = $this->stripPath( $dir, $this->dir );
+ }
+ }
+ }
+
private function stripPath( $val, $dir ) {
- if ( strpos( $val, $dir ) === 0 ) {
+ if ( $val === $dir ) {
+ $val = '';
+ } elseif ( strpos( $val, $dir ) === 0 ) {
// +1 is for the trailing / that won't be in $this->dir
$val = substr( $val, strlen( $dir ) + 1 );
}
}
protected function handleResourceModules( $realName, $value ) {
+ $defaults = array();
+ $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath';
foreach ( $value as $name => $data ) {
if ( isset( $data['localBasePath'] ) ) {
$data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
+ if ( !$defaults ) {
+ $defaults['localBasePath'] = $data['localBasePath'];
+ unset( $data['localBasePath'] );
+ if ( isset( $data[$remote] ) ) {
+ $defaults[$remote] = $data[$remote];
+ unset( $data[$remote] );
+ }
+ } else {
+ if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
+ unset( $data['localBasePath'] );
+ }
+ if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
+ && $data[$remote] === $defaults[$remote]
+ ) {
+ unset( $data[$remote] );
+ }
+ }
}
+
+
$this->json[$realName][$name] = $data;
}
+ if ( $defaults ) {
+ $this->json['ResourceFileModulePaths'] = $defaults;
+ }
}
}