Don't check namespace in SpecialWantedtemplates
[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 // Put this at the bottom
122 $out['manifest_version'] = ExtensionRegistry::MANIFEST_VERSION;
123 $type = $this->hasOption( 'skin' ) ? 'skin' : 'extension';
124 $fname = "{$this->dir}/$type.json";
125 $prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK );
126 file_put_contents( $fname, $prettyJSON . "\n" );
127 $this->output( "Wrote output to $fname.\n" );
128 if ( $this->hasWarning ) {
129 $this->output( "Found warnings! Please resolve the warnings and rerun this script.\n" );
130 }
131 }
132
133 protected function handleExtensionFunctions( $realName, $value ) {
134 foreach ( $value as $func ) {
135 if ( $func instanceof Closure ) {
136 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
137 }
138 }
139
140 $this->json[$realName] = $value;
141 }
142
143 protected function handleMessagesDirs( $realName, $value ) {
144 foreach ( $value as $key => $dirs ) {
145 foreach ( (array)$dirs as $dir ) {
146 $this->json[$realName][$key][] = $this->stripPath( $dir, $this->dir );
147 }
148 }
149 }
150
151 protected function handleExtensionMessagesFiles( $realName, $value, $vars ) {
152 foreach ( $value as $key => $file ) {
153 $strippedFile = $this->stripPath( $file, $this->dir );
154 if ( isset( $vars['wgMessagesDirs'][$key] ) ) {
155 $this->output(
156 "Note: Ignoring PHP shim $strippedFile. " .
157 "If your extension no longer supports versions of MediaWiki " .
158 "older than 1.23.0, you can safely delete it.\n"
159 );
160 } else {
161 $this->json[$realName][$key] = $strippedFile;
162 }
163 }
164 }
165
166 private function stripPath( $val, $dir ) {
167 if ( $val === $dir ) {
168 $val = '';
169 } elseif ( strpos( $val, $dir ) === 0 ) {
170 // +1 is for the trailing / that won't be in $this->dir
171 $val = substr( $val, strlen( $dir ) + 1 );
172 }
173
174 return $val;
175 }
176
177 protected function removeAbsolutePath( $realName, $value ) {
178 $out = array();
179 foreach ( $value as $key => $val ) {
180 $out[$key] = $this->stripPath( $val, $this->dir );
181 }
182 $this->json[$realName] = $out;
183 }
184
185 protected function handleCredits( $realName, $value) {
186 $keys = array_keys( $value );
187 $this->json['type'] = $keys[0];
188 $values = array_values( $value );
189 foreach ( $values[0][0] as $name => $val ) {
190 if ( $name !== 'path' ) {
191 $this->json[$name] = $val;
192 }
193 }
194 }
195
196 public function handleHooks( $realName, $value ) {
197 foreach ( $value as $hookName => $handlers ) {
198 foreach ( $handlers as $func ) {
199 if ( $func instanceof Closure ) {
200 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
201 }
202 }
203 }
204 $this->json[$realName] = $value;
205 }
206
207 protected function handleResourceModules( $realName, $value ) {
208 $defaults = array();
209 $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath';
210 foreach ( $value as $name => $data ) {
211 if ( isset( $data['localBasePath'] ) ) {
212 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
213 if ( !$defaults ) {
214 $defaults['localBasePath'] = $data['localBasePath'];
215 unset( $data['localBasePath'] );
216 if ( isset( $data[$remote] ) ) {
217 $defaults[$remote] = $data[$remote];
218 unset( $data[$remote] );
219 }
220 } else {
221 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
222 unset( $data['localBasePath'] );
223 }
224 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
225 && $data[$remote] === $defaults[$remote]
226 ) {
227 unset( $data[$remote] );
228 }
229 }
230 }
231
232
233 $this->json[$realName][$name] = $data;
234 }
235 if ( $defaults ) {
236 $this->json['ResourceFileModulePaths'] = $defaults;
237 }
238 }
239 }
240
241 $maintClass = 'ConvertExtensionToRegistration';
242 require_once RUN_MAINTENANCE_IF_MAIN;