Merge "mergeMessageFileList: Support reading extension/skin.json files"
[lhc/web/wiklou.git] / maintenance / convertExtensionToRegistration.php
1 <?php
2
3 require_once __DIR__ . '/Maintenance.php';
4
5 class ConvertExtensionToRegistration extends Maintenance {
6
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',
16 );
17
18 /**
19 * Keys that should be put at the top of the generated JSON file (T86608)
20 *
21 * @var array
22 */
23 protected $promote = array(
24 'name',
25 'version',
26 'author',
27 'url',
28 'description',
29 'descriptionmsg',
30 'namemsg',
31 'license-name',
32 'type',
33 );
34
35 private $json, $dir;
36
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 );
42 }
43
44 protected function getAllGlobals() {
45 $processor = new ReflectionClass( 'ExtensionProcessor' );
46 $settings = $processor->getProperty( 'globalSettings' );
47 $settings->setAccessible( true );
48 return $settings->getValue();
49 }
50
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 ) {
55 $var = 'wg' . $var;
56 $$var = array();
57 }
58 unset( $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 ) {
69 continue;
70 }
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;
79 }
80 }
81
82 // Move some keys to the top
83 $out = array();
84 foreach ( $this->promote as $key ) {
85 if ( isset( $this->json[$key] ) ) {
86 $out[$key] = $this->json[$key];
87 unset( $this->json[$key] );
88 }
89 }
90 $out += $this->json;
91
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" );
97 }
98
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 );
103 }
104 }
105
106 $this->json[$realName] = $value;
107 }
108
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 );
113 }
114 }
115 }
116
117 private function stripPath( $val, $dir ) {
118 if ( $val === $dir ) {
119 $val = '';
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 );
123 }
124
125 return $val;
126 }
127
128 protected function removeAbsolutePath( $realName, $value ) {
129 $out = array();
130 foreach ( $value as $key => $val ) {
131 $out[$key] = $this->stripPath( $val, $this->dir );
132 }
133 $this->json[$realName] = $out;
134 }
135
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;
143 }
144 }
145 }
146
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 );
152 }
153 }
154 }
155 $this->json[$realName] = $value;
156 }
157
158 protected function handleResourceModules( $realName, $value ) {
159 $defaults = array();
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 );
164 if ( !$defaults ) {
165 $defaults['localBasePath'] = $data['localBasePath'];
166 unset( $data['localBasePath'] );
167 if ( isset( $data[$remote] ) ) {
168 $defaults[$remote] = $data[$remote];
169 unset( $data[$remote] );
170 }
171 } else {
172 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
173 unset( $data['localBasePath'] );
174 }
175 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
176 && $data[$remote] === $defaults[$remote]
177 ) {
178 unset( $data[$remote] );
179 }
180 }
181 }
182
183
184 $this->json[$realName][$name] = $data;
185 }
186 if ( $defaults ) {
187 $this->json['ResourceFileModulePaths'] = $defaults;
188 }
189 }
190 }
191
192 $maintClass = 'ConvertExtensionToRegistration';
193 require_once RUN_MAINTENANCE_IF_MAIN;