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