3 require_once __DIR__
. '/Maintenance.php';
5 class ConvertExtensionToRegistration
extends Maintenance
{
7 protected $custom = array(
8 'MessagesDirs' => 'handleMessagesDirs',
9 'ExtensionMessagesFiles' => 'removeAbsolutePath',
10 'AutoloadClasses' => 'removeAbsolutePath',
11 'ExtensionCredits' => 'handleCredits',
12 'ResourceModules' => 'handleResourceModules',
13 'ResourceModuleSkinStyles' => 'handleResourceModules',
14 'Hooks' => 'handleHooks',
15 'ExtensionFunctions' => 'handleExtensionFunctions',
19 * Keys that should be put at the top of the generated JSON file (T86608)
23 protected $promote = array(
37 public function __construct() {
38 parent
::__construct();
39 $this->mDescription
= 'Converts extension entry points to the new JSON registration format';
40 $this->addArg( 'path', 'Location to the PHP entry point you wish to convert', /* $required = */ true );
41 $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
44 protected function getAllGlobals() {
45 $processor = new ReflectionClass( 'ExtensionProcessor' );
46 $settings = $processor->getProperty( 'globalSettings' );
47 $settings->setAccessible( true );
48 return $settings->getValue();
51 public function execute() {
52 // Extensions will do stuff like $wgResourceModules += array(...) which is a
53 // fatal unless an array is already set. So set an empty value.
54 foreach ( array_merge( $this->getAllGlobals(), array_keys( $this->custom
) ) as $var ) {
59 require $this->getArg( 0 );
60 // Try not to create any local variables before this line
61 $vars = get_defined_vars();
62 unset( $vars['this'] );
63 $this->dir
= dirname( realpath( $this->getArg( 0 ) ) );
64 $this->json
= array();
65 $globalSettings = $this->getAllGlobals();
66 foreach ( $vars as $name => $value ) {
67 // If an empty array, assume it's the default we set, so skip it
68 if ( is_array( $value ) && count( $value ) === 0 ) {
71 $realName = substr( $name, 2 ); // Strip 'wg'
72 if ( isset( $this->custom
[$realName] ) ) {
73 call_user_func_array( array( $this, $this->custom
[$realName] ), array( $realName, $value ) );
74 } elseif ( in_array( $realName, $globalSettings ) ) {
75 $this->json
[$realName] = $value;
76 } elseif ( strpos( $name, 'wg' ) === 0 ) {
77 // Most likely a config setting
78 $this->json
['config'][$realName] = $value;
82 // Move some keys to the top
84 foreach ( $this->promote
as $key ) {
85 if ( isset( $this->json
[$key] ) ) {
86 $out[$key] = $this->json
[$key];
87 unset( $this->json
[$key] );
92 $type = $this->hasOption( 'skin' ) ?
'skin' : 'extension';
93 $fname = "{$this->dir}/$type.json";
94 $prettyJSON = FormatJson
::encode( $out, "\t", FormatJson
::ALL_OK
);
95 file_put_contents( $fname, $prettyJSON . "\n" );
96 $this->output( "Wrote output to $fname.\n" );
99 protected function handleExtensionFunctions( $realName, $value ) {
100 foreach ( $value as $func ) {
101 if ( $func instanceof Closure
) {
102 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
106 $this->json
[$realName] = $value;
109 protected function handleMessagesDirs( $realName, $value ) {
110 foreach ( $value as $key => $dirs ) {
111 foreach ( (array)$dirs as $dir ) {
112 $this->json
[$realName][$key][] = $this->stripPath( $dir, $this->dir
);
117 private function stripPath( $val, $dir ) {
118 if ( $val === $dir ) {
120 } elseif ( strpos( $val, $dir ) === 0 ) {
121 // +1 is for the trailing / that won't be in $this->dir
122 $val = substr( $val, strlen( $dir ) +
1 );
128 protected function removeAbsolutePath( $realName, $value ) {
130 foreach ( $value as $key => $val ) {
131 $out[$key] = $this->stripPath( $val, $this->dir
);
133 $this->json
[$realName] = $out;
136 protected function handleCredits( $realName, $value) {
137 $keys = array_keys( $value );
138 $this->json
['type'] = $keys[0];
139 $values = array_values( $value );
140 foreach ( $values[0][0] as $name => $val ) {
141 if ( $name !== 'path' ) {
142 $this->json
[$name] = $val;
147 public function handleHooks( $realName, $value ) {
148 foreach ( $value as $hookName => $handlers ) {
149 foreach ( $handlers as $func ) {
150 if ( $func instanceof Closure
) {
151 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
155 $this->json
[$realName] = $value;
158 protected function handleResourceModules( $realName, $value ) {
160 $remote = $this->hasOption( 'skin' ) ?
'remoteSkinPath' : 'remoteExtPath';
161 foreach ( $value as $name => $data ) {
162 if ( isset( $data['localBasePath'] ) ) {
163 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir
);
165 $defaults['localBasePath'] = $data['localBasePath'];
166 unset( $data['localBasePath'] );
167 if ( isset( $data[$remote] ) ) {
168 $defaults[$remote] = $data[$remote];
169 unset( $data[$remote] );
172 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
173 unset( $data['localBasePath'] );
175 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
176 && $data[$remote] === $defaults[$remote]
178 unset( $data[$remote] );
184 $this->json
[$realName][$name] = $data;
187 $this->json
['ResourceFileModulePaths'] = $defaults;
192 $maintClass = 'ConvertExtensionToRegistration';
193 require_once RUN_MAINTENANCE_IF_MAIN
;