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 'Hooks' => 'handleHooks',
14 'ExtensionFunctions' => 'handleExtensionFunctions',
18 * Keys that should be put at the top of the generated JSON file (T86608)
22 protected $promote = array(
36 public function __construct() {
37 parent
::__construct();
38 $this->mDescription
= 'Converts extension entry points to the new JSON registration format';
39 $this->addArg( 'path', 'Location to the PHP entry point you wish to convert', /* $required = */ true );
40 $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
43 protected function getAllGlobals() {
44 $processor = new ReflectionClass( 'ExtensionProcessor' );
45 $settings = $processor->getProperty( 'globalSettings' );
46 $settings->setAccessible( true );
47 return $settings->getValue();
50 public function execute() {
51 // Extensions will do stuff like $wgResourceModules += array(...) which is a
52 // fatal unless an array is already set. So set an empty value.
53 foreach ( array_merge( $this->getAllGlobals(), array_keys( $this->custom
) ) as $var ) {
58 require $this->getArg( 0 );
59 // Try not to create any local variables before this line
60 $vars = get_defined_vars();
61 unset( $vars['this'] );
62 $this->dir
= dirname( realpath( $this->getArg( 0 ) ) );
63 $this->json
= array();
64 $globalSettings = $this->getAllGlobals();
65 foreach ( $vars as $name => $value ) {
66 // If an empty array, assume it's the default we set, so skip it
67 if ( is_array( $value ) && count( $value ) === 0 ) {
70 $realName = substr( $name, 2 ); // Strip 'wg'
71 if ( isset( $this->custom
[$realName] ) ) {
72 call_user_func_array( array( $this, $this->custom
[$realName] ), array( $realName, $value ) );
73 } elseif ( in_array( $realName, $globalSettings ) ) {
74 $this->json
[$realName] = $value;
75 } elseif ( strpos( $name, 'wg' ) === 0 ) {
76 // Most likely a config setting
77 $this->json
['config'][$realName] = $value;
81 // Move some keys to the top
83 foreach ( $this->promote
as $key ) {
84 if ( isset( $this->json
[$key] ) ) {
85 $out[$key] = $this->json
[$key];
86 unset( $this->json
[$key] );
91 $type = $this->hasOption( 'skin' ) ?
'skin' : 'extension';
92 $fname = "{$this->dir}/$type.json";
93 $prettyJSON = FormatJson
::encode( $out, "\t", FormatJson
::ALL_OK
);
94 file_put_contents( $fname, $prettyJSON . "\n" );
95 $this->output( "Wrote output to $fname.\n" );
98 protected function handleExtensionFunctions( $realName, $value ) {
99 foreach ( $value as $func ) {
100 if ( $func instanceof Closure
) {
101 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
105 $this->json
[$realName] = $value;
108 protected function handleMessagesDirs( $realName, $value ) {
109 foreach ( $value as $key => $dirs ) {
110 foreach ( (array)$dirs as $dir ) {
111 $this->json
[$realName][$key][] = $this->stripPath( $dir, $this->dir
);
116 private function stripPath( $val, $dir ) {
117 if ( $val === $dir ) {
119 } elseif ( strpos( $val, $dir ) === 0 ) {
120 // +1 is for the trailing / that won't be in $this->dir
121 $val = substr( $val, strlen( $dir ) +
1 );
127 protected function removeAbsolutePath( $realName, $value ) {
129 foreach ( $value as $key => $val ) {
130 $out[$key] = $this->stripPath( $val, $this->dir
);
132 $this->json
[$realName] = $out;
135 protected function handleCredits( $realName, $value) {
136 $keys = array_keys( $value );
137 $this->json
['type'] = $keys[0];
138 $values = array_values( $value );
139 foreach ( $values[0][0] as $name => $val ) {
140 if ( $name !== 'path' ) {
141 $this->json
[$name] = $val;
146 public function handleHooks( $realName, $value ) {
147 foreach ( $value as $hookName => $handlers ) {
148 foreach ( $handlers as $func ) {
149 if ( $func instanceof Closure
) {
150 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
154 $this->json
[$realName] = $value;
157 protected function handleResourceModules( $realName, $value ) {
159 $remote = $this->hasOption( 'skin' ) ?
'remoteSkinPath' : 'remoteExtPath';
160 foreach ( $value as $name => $data ) {
161 if ( isset( $data['localBasePath'] ) ) {
162 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir
);
164 $defaults['localBasePath'] = $data['localBasePath'];
165 unset( $data['localBasePath'] );
166 if ( isset( $data[$remote] ) ) {
167 $defaults[$remote] = $data[$remote];
168 unset( $data[$remote] );
171 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
172 unset( $data['localBasePath'] );
174 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
175 && $data[$remote] === $defaults[$remote]
177 unset( $data[$remote] );
183 $this->json
[$realName][$name] = $data;
186 $this->json
['ResourceFileModulePaths'] = $defaults;
191 $maintClass = 'ConvertExtensionToRegistration';
192 require_once RUN_MAINTENANCE_IF_MAIN
;