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