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