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