3 require_once __DIR__
. '/Maintenance.php';
5 class ConvertExtensionToRegistration
extends Maintenance
{
7 protected $custom = array(
8 'MessagesDirs' => 'handleMessagesDirs',
9 'ExtensionMessagesFiles' => 'handleExtensionMessagesFiles',
10 'AutoloadClasses' => 'removeAbsolutePath',
11 'ExtensionCredits' => 'handleCredits',
12 'ResourceModules' => 'handleResourceModules',
13 'ResourceModuleSkinStyles' => 'handleResourceModules',
14 'Hooks' => 'handleHooks',
15 'ExtensionFunctions' => 'handleExtensionFunctions',
19 * Things that were formerly globals and should still be converted
23 protected $formerGlobals = array(
28 * Keys that should be put at the top of the generated JSON file (T86608)
32 protected $promote = array(
46 public function __construct() {
47 parent
::__construct();
48 $this->mDescription
= 'Converts extension entry points to the new JSON registration format';
49 $this->addArg( 'path', 'Location to the PHP entry point you wish to convert', /* $required = */ true );
50 $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
53 protected function getAllGlobals() {
54 $processor = new ReflectionClass( 'ExtensionProcessor' );
55 $settings = $processor->getProperty( 'globalSettings' );
56 $settings->setAccessible( true );
57 return $settings->getValue() +
$this->formerGlobals
;
60 public function execute() {
61 // Extensions will do stuff like $wgResourceModules += array(...) which is a
62 // fatal unless an array is already set. So set an empty value.
63 foreach ( array_merge( $this->getAllGlobals(), array_keys( $this->custom
) ) as $var ) {
68 require $this->getArg( 0 );
69 // Try not to create any local variables before this line
70 $vars = get_defined_vars();
71 unset( $vars['this'] );
72 $this->dir
= dirname( realpath( $this->getArg( 0 ) ) );
73 $this->json
= array();
74 $globalSettings = $this->getAllGlobals();
75 foreach ( $vars as $name => $value ) {
76 // If an empty array, assume it's the default we set, so skip it
77 if ( is_array( $value ) && count( $value ) === 0 ) {
80 $realName = substr( $name, 2 ); // Strip 'wg'
81 if ( isset( $this->custom
[$realName] ) ) {
82 call_user_func_array( array( $this, $this->custom
[$realName] ), array( $realName, $value, $vars ) );
83 } elseif ( in_array( $realName, $globalSettings ) ) {
84 $this->json
[$realName] = $value;
85 } elseif ( strpos( $name, 'wg' ) === 0 ) {
86 // Most likely a config setting
87 $this->json
['config'][$realName] = $value;
91 // Move some keys to the top
93 foreach ( $this->promote
as $key ) {
94 if ( isset( $this->json
[$key] ) ) {
95 $out[$key] = $this->json
[$key];
96 unset( $this->json
[$key] );
101 $type = $this->hasOption( 'skin' ) ?
'skin' : 'extension';
102 $fname = "{$this->dir}/$type.json";
103 $prettyJSON = FormatJson
::encode( $out, "\t", FormatJson
::ALL_OK
);
104 file_put_contents( $fname, $prettyJSON . "\n" );
105 $this->output( "Wrote output to $fname.\n" );
108 protected function handleExtensionFunctions( $realName, $value ) {
109 foreach ( $value as $func ) {
110 if ( $func instanceof Closure
) {
111 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
115 $this->json
[$realName] = $value;
118 protected function handleMessagesDirs( $realName, $value ) {
119 foreach ( $value as $key => $dirs ) {
120 foreach ( (array)$dirs as $dir ) {
121 $this->json
[$realName][$key][] = $this->stripPath( $dir, $this->dir
);
126 protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
127 foreach ( $value as $key => $file ) {
128 $strippedFile = $this->stripPath( $file, $this->dir
);
129 if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
131 "Note: Ignoring PHP shim $strippedFile. " .
132 "If your extension no longer supports versions of MediaWiki " .
133 "older than 1.23.0, you can safely delete it.\n"
136 $this->json
[$realName][$key] = $strippedFile;
141 private function stripPath( $val, $dir ) {
142 if ( $val === $dir ) {
144 } elseif ( strpos( $val, $dir ) === 0 ) {
145 // +1 is for the trailing / that won't be in $this->dir
146 $val = substr( $val, strlen( $dir ) +
1 );
152 protected function removeAbsolutePath( $realName, $value ) {
154 foreach ( $value as $key => $val ) {
155 $out[$key] = $this->stripPath( $val, $this->dir
);
157 $this->json
[$realName] = $out;
160 protected function handleCredits( $realName, $value) {
161 $keys = array_keys( $value );
162 $this->json
['type'] = $keys[0];
163 $values = array_values( $value );
164 foreach ( $values[0][0] as $name => $val ) {
165 if ( $name !== 'path' ) {
166 $this->json
[$name] = $val;
171 public function handleHooks( $realName, $value ) {
172 foreach ( $value as $hookName => $handlers ) {
173 foreach ( $handlers as $func ) {
174 if ( $func instanceof Closure
) {
175 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
179 $this->json
[$realName] = $value;
182 protected function handleResourceModules( $realName, $value ) {
184 $remote = $this->hasOption( 'skin' ) ?
'remoteSkinPath' : 'remoteExtPath';
185 foreach ( $value as $name => $data ) {
186 if ( isset( $data['localBasePath'] ) ) {
187 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir
);
189 $defaults['localBasePath'] = $data['localBasePath'];
190 unset( $data['localBasePath'] );
191 if ( isset( $data[$remote] ) ) {
192 $defaults[$remote] = $data[$remote];
193 unset( $data[$remote] );
196 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
197 unset( $data['localBasePath'] );
199 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
200 && $data[$remote] === $defaults[$remote]
202 unset( $data[$remote] );
208 $this->json
[$realName][$name] = $data;
211 $this->json
['ResourceFileModulePaths'] = $defaults;
216 $maintClass = 'ConvertExtensionToRegistration';
217 require_once RUN_MAINTENANCE_IF_MAIN
;