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