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