registration: Don't ignore empty array config settings when converting
[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' => 'handleExtensionMessagesFiles',
10 'AutoloadClasses' => 'removeAbsolutePath',
11 'ExtensionCredits' => 'handleCredits',
12 'ResourceModules' => 'handleResourceModules',
13 'ResourceModuleSkinStyles' => 'handleResourceModules',
14 'Hooks' => 'handleHooks',
15 'ExtensionFunctions' => 'handleExtensionFunctions',
16 'ParserTestFiles' => 'removeAbsolutePath',
17 );
18
19 /**
20 * Things that were formerly globals and should still be converted
21 *
22 * @var array
23 */
24 protected $formerGlobals = array(
25 'TrackingCategories',
26 );
27
28 /**
29 * Keys that should be put at the top of the generated JSON file (T86608)
30 *
31 * @var array
32 */
33 protected $promote = array(
34 'name',
35 'version',
36 'author',
37 'url',
38 'description',
39 'descriptionmsg',
40 'namemsg',
41 'license-name',
42 'type',
43 );
44
45 private $json, $dir;
46
47 public function __construct() {
48 parent::__construct();
49 $this->mDescription = 'Converts extension entry points to the new JSON registration format';
50 $this->addArg( 'path', 'Location to the PHP entry point you wish to convert', /* $required = */ true );
51 $this->addOption( 'skin', 'Whether to write to skin.json', false, false );
52 }
53
54 protected function getAllGlobals() {
55 $processor = new ReflectionClass( 'ExtensionProcessor' );
56 $settings = $processor->getProperty( 'globalSettings' );
57 $settings->setAccessible( true );
58 return $settings->getValue() + $this->formerGlobals;
59 }
60
61 public function execute() {
62 // Extensions will do stuff like $wgResourceModules += array(...) which is a
63 // fatal unless an array is already set. So set an empty value.
64 // And use the weird $__settings name to avoid any conflicts
65 // with real poorly named settings.
66 $__settings = array_merge( $this->getAllGlobals(), array_keys( $this->custom ) );
67 foreach ( $__settings as $var ) {
68 $var = 'wg' . $var;
69 $$var = array();
70 }
71 unset( $var );
72 require $this->getArg( 0 );
73 // Try not to create any local variables before this line
74 $vars = get_defined_vars();
75 unset( $vars['this'] );
76 unset( $vars['__settings'] );
77 $this->dir = dirname( realpath( $this->getArg( 0 ) ) );
78 $this->json = array();
79 $globalSettings = $this->getAllGlobals();
80 foreach ( $vars as $name => $value ) {
81 $realName = substr( $name, 2 ); // Strip 'wg'
82
83 // If it's an empty array that we likely set, skip it
84 if ( is_array( $value ) && count( $value ) === 0 && in_array( $realName, $__settings ) ) {
85 continue;
86 }
87
88 if ( isset( $this->custom[$realName] ) ) {
89 call_user_func_array( array( $this, $this->custom[$realName] ), array( $realName, $value, $vars ) );
90 } elseif ( in_array( $realName, $globalSettings ) ) {
91 $this->json[$realName] = $value;
92 } elseif ( strpos( $name, 'wg' ) === 0 ) {
93 // Most likely a config setting
94 $this->json['config'][$realName] = $value;
95 }
96 }
97
98 // Move some keys to the top
99 $out = array();
100 foreach ( $this->promote as $key ) {
101 if ( isset( $this->json[$key] ) ) {
102 $out[$key] = $this->json[$key];
103 unset( $this->json[$key] );
104 }
105 }
106 $out += $this->json;
107
108 $type = $this->hasOption( 'skin' ) ? 'skin' : 'extension';
109 $fname = "{$this->dir}/$type.json";
110 $prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK );
111 file_put_contents( $fname, $prettyJSON . "\n" );
112 $this->output( "Wrote output to $fname.\n" );
113 }
114
115 protected function handleExtensionFunctions( $realName, $value ) {
116 foreach ( $value as $func ) {
117 if ( $func instanceof Closure ) {
118 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
119 }
120 }
121
122 $this->json[$realName] = $value;
123 }
124
125 protected function handleMessagesDirs( $realName, $value ) {
126 foreach ( $value as $key => $dirs ) {
127 foreach ( (array)$dirs as $dir ) {
128 $this->json[$realName][$key][] = $this->stripPath( $dir, $this->dir );
129 }
130 }
131 }
132
133 protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
134 foreach ( $value as $key => $file ) {
135 $strippedFile = $this->stripPath( $file, $this->dir );
136 if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
137 $this->output(
138 "Note: Ignoring PHP shim $strippedFile. " .
139 "If your extension no longer supports versions of MediaWiki " .
140 "older than 1.23.0, you can safely delete it.\n"
141 );
142 } else {
143 $this->json[$realName][$key] = $strippedFile;
144 }
145 }
146 }
147
148 private function stripPath( $val, $dir ) {
149 if ( $val === $dir ) {
150 $val = '';
151 } elseif ( strpos( $val, $dir ) === 0 ) {
152 // +1 is for the trailing / that won't be in $this->dir
153 $val = substr( $val, strlen( $dir ) + 1 );
154 }
155
156 return $val;
157 }
158
159 protected function removeAbsolutePath( $realName, $value ) {
160 $out = array();
161 foreach ( $value as $key => $val ) {
162 $out[$key] = $this->stripPath( $val, $this->dir );
163 }
164 $this->json[$realName] = $out;
165 }
166
167 protected function handleCredits( $realName, $value) {
168 $keys = array_keys( $value );
169 $this->json['type'] = $keys[0];
170 $values = array_values( $value );
171 foreach ( $values[0][0] as $name => $val ) {
172 if ( $name !== 'path' ) {
173 $this->json[$name] = $val;
174 }
175 }
176 }
177
178 public function handleHooks( $realName, $value ) {
179 foreach ( $value as $hookName => $handlers ) {
180 foreach ( $handlers as $func ) {
181 if ( $func instanceof Closure ) {
182 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
183 }
184 }
185 }
186 $this->json[$realName] = $value;
187 }
188
189 protected function handleResourceModules( $realName, $value ) {
190 $defaults = array();
191 $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath';
192 foreach ( $value as $name => $data ) {
193 if ( isset( $data['localBasePath'] ) ) {
194 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
195 if ( !$defaults ) {
196 $defaults['localBasePath'] = $data['localBasePath'];
197 unset( $data['localBasePath'] );
198 if ( isset( $data[$remote] ) ) {
199 $defaults[$remote] = $data[$remote];
200 unset( $data[$remote] );
201 }
202 } else {
203 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
204 unset( $data['localBasePath'] );
205 }
206 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
207 && $data[$remote] === $defaults[$remote]
208 ) {
209 unset( $data[$remote] );
210 }
211 }
212 }
213
214
215 $this->json[$realName][$name] = $data;
216 }
217 if ( $defaults ) {
218 $this->json['ResourceFileModulePaths'] = $defaults;
219 }
220 }
221 }
222
223 $maintClass = 'ConvertExtensionToRegistration';
224 require_once RUN_MAINTENANCE_IF_MAIN;