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