Use CSS columns instead of tables in Special:SpecialPages
[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' => 'removeAbsolutePath',
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 ) );
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 private function stripPath( $val, $dir ) {
127 if ( $val === $dir ) {
128 $val = '';
129 } elseif ( strpos( $val, $dir ) === 0 ) {
130 // +1 is for the trailing / that won't be in $this->dir
131 $val = substr( $val, strlen( $dir ) + 1 );
132 }
133
134 return $val;
135 }
136
137 protected function removeAbsolutePath( $realName, $value ) {
138 $out = array();
139 foreach ( $value as $key => $val ) {
140 $out[$key] = $this->stripPath( $val, $this->dir );
141 }
142 $this->json[$realName] = $out;
143 }
144
145 protected function handleCredits( $realName, $value) {
146 $keys = array_keys( $value );
147 $this->json['type'] = $keys[0];
148 $values = array_values( $value );
149 foreach ( $values[0][0] as $name => $val ) {
150 if ( $name !== 'path' ) {
151 $this->json[$name] = $val;
152 }
153 }
154 }
155
156 public function handleHooks( $realName, $value ) {
157 foreach ( $value as $hookName => $handlers ) {
158 foreach ( $handlers as $func ) {
159 if ( $func instanceof Closure ) {
160 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
161 }
162 }
163 }
164 $this->json[$realName] = $value;
165 }
166
167 protected function handleResourceModules( $realName, $value ) {
168 $defaults = array();
169 $remote = $this->hasOption( 'skin' ) ? 'remoteSkinPath' : 'remoteExtPath';
170 foreach ( $value as $name => $data ) {
171 if ( isset( $data['localBasePath'] ) ) {
172 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
173 if ( !$defaults ) {
174 $defaults['localBasePath'] = $data['localBasePath'];
175 unset( $data['localBasePath'] );
176 if ( isset( $data[$remote] ) ) {
177 $defaults[$remote] = $data[$remote];
178 unset( $data[$remote] );
179 }
180 } else {
181 if ( $data['localBasePath'] === $defaults['localBasePath'] ) {
182 unset( $data['localBasePath'] );
183 }
184 if ( isset( $data[$remote] ) && isset( $defaults[$remote] )
185 && $data[$remote] === $defaults[$remote]
186 ) {
187 unset( $data[$remote] );
188 }
189 }
190 }
191
192
193 $this->json[$realName][$name] = $data;
194 }
195 if ( $defaults ) {
196 $this->json['ResourceFileModulePaths'] = $defaults;
197 }
198 }
199 }
200
201 $maintClass = 'ConvertExtensionToRegistration';
202 require_once RUN_MAINTENANCE_IF_MAIN;