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