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';
41 protected function getAllGlobals() {
42 $processor = new ReflectionClass( 'ExtensionProcessor' );
43 $settings = $processor->getProperty( 'globalSettings' );
44 $settings->setAccessible( true );
45 return $settings->getValue();
48 public function execute() {
49 // Extensions will do stuff like $wgResourceModules += array(...) which is a
50 // fatal unless an array is already set. So set an empty value.
51 foreach ( array_merge( $this->getAllGlobals(), array_keys( $this->custom
) ) as $var ) {
56 require $this->getArg( 0 );
57 // Try not to create any local variables before this line
58 $vars = get_defined_vars();
59 unset( $vars['this'] );
60 $this->dir
= dirname( realpath( $this->getArg( 0 ) ) );
61 $this->json
= array();
62 $globalSettings = $this->getAllGlobals();
63 foreach ( $vars as $name => $value ) {
64 // If an empty array, assume it's the default we set, so skip it
65 if ( is_array( $value ) && count( $value ) === 0 ) {
68 $realName = substr( $name, 2 ); // Strip 'wg'
69 if ( isset( $this->custom
[$realName] ) ) {
70 call_user_func_array( array( $this, $this->custom
[$realName] ), array( $realName, $value ) );
71 } elseif ( in_array( $realName, $globalSettings ) ) {
72 $this->json
[$realName] = $value;
73 } elseif ( strpos( $name, 'wg' ) === 0 ) {
74 // Most likely a config setting
75 $this->json
['config'][$realName] = $value;
79 // Move some keys to the top
81 foreach ( $this->promote
as $key ) {
82 if ( isset( $this->json
[$key] ) ) {
83 $out[$key] = $this->json
[$key];
84 unset( $this->json
[$key] );
89 $fname = "{$this->dir}/extension.json";
90 $prettyJSON = FormatJson
::encode( $out, "\t", FormatJson
::ALL_OK
);
91 file_put_contents( $fname, $prettyJSON . "\n" );
92 $this->output( "Wrote output to $fname.\n" );
95 protected function handleExtensionFunctions( $realName, $value ) {
96 foreach ( $value as $func ) {
97 if ( $func instanceof Closure
) {
98 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
102 $this->json
[$realName] = $value;
105 protected function handleMessagesDirs( $realName, $value ) {
106 foreach ( $value as $key => $dirs ) {
107 foreach ( (array)$dirs as $dir ) {
108 $this->json
[$realName][$key][] = $this->stripPath( $dir, $this->dir
);
113 private function stripPath( $val, $dir ) {
114 if ( $val === $dir ) {
116 } elseif ( strpos( $val, $dir ) === 0 ) {
117 // +1 is for the trailing / that won't be in $this->dir
118 $val = substr( $val, strlen( $dir ) +
1 );
124 protected function removeAbsolutePath( $realName, $value ) {
126 foreach ( $value as $key => $val ) {
127 $out[$key] = $this->stripPath( $val, $this->dir
);
129 $this->json
[$realName] = $out;
132 protected function handleCredits( $realName, $value) {
133 $keys = array_keys( $value );
134 $this->json
['type'] = $keys[0];
135 $values = array_values( $value );
136 foreach ( $values[0][0] as $name => $val ) {
137 if ( $name !== 'path' ) {
138 $this->json
[$name] = $val;
143 public function handleHooks( $realName, $value ) {
144 foreach ( $value as $hookName => $handlers ) {
145 foreach ( $handlers as $func ) {
146 if ( $func instanceof Closure
) {
147 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
151 $this->json
[$realName] = $value;
154 protected function handleResourceModules( $realName, $value ) {
155 foreach ( $value as $name => $data ) {
156 if ( isset( $data['localBasePath'] ) ) {
157 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir
);
159 $this->json
[$realName][$name] = $data;
164 $maintClass = 'ConvertExtensionToRegistration';
165 require_once RUN_MAINTENANCE_IF_MAIN
;