convertExtensionToRegistration: Move 'namemsg' to top as well
[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 }
40
41 protected function getAllGlobals() {
42 $processor = new ReflectionClass( 'ExtensionProcessor' );
43 $settings = $processor->getProperty( 'globalSettings' );
44 $settings->setAccessible( true );
45 return $settings->getValue();
46 }
47
48 public function execute() {
49 // Extensions will do stuff like $wgResourceModules += array(...) which is a
50 // fatal unless an array is already set. So set an empty value.
51 foreach ( array_merge( $this->getAllGlobals(), array_keys( $this->custom ) ) as $var ) {
52 $var = 'wg' . $var;
53 $$var = array();
54 }
55 unset( $var );
56 require $this->getArg( 0 );
57 // Try not to create any local variables before this line
58 $vars = get_defined_vars();
59 unset( $vars['this'] );
60 $this->dir = dirname( realpath( $this->getArg( 0 ) ) );
61 $this->json = array();
62 $globalSettings = $this->getAllGlobals();
63 foreach ( $vars as $name => $value ) {
64 // If an empty array, assume it's the default we set, so skip it
65 if ( is_array( $value ) && count( $value ) === 0 ) {
66 continue;
67 }
68 $realName = substr( $name, 2 ); // Strip 'wg'
69 if ( isset( $this->custom[$realName] ) ) {
70 call_user_func_array( array( $this, $this->custom[$realName] ), array( $realName, $value ) );
71 } elseif ( in_array( $realName, $globalSettings ) ) {
72 $this->json[$realName] = $value;
73 } elseif ( strpos( $name, 'wg' ) === 0 ) {
74 // Most likely a config setting
75 $this->json['config'][$realName] = $value;
76 }
77 }
78
79 // Move some keys to the top
80 $out = array();
81 foreach ( $this->promote as $key ) {
82 if ( isset( $this->json[$key] ) ) {
83 $out[$key] = $this->json[$key];
84 unset( $this->json[$key] );
85 }
86 }
87 $out += $this->json;
88
89 $fname = "{$this->dir}/extension.json";
90 $prettyJSON = FormatJson::encode( $out, "\t", FormatJson::ALL_OK );
91 file_put_contents( $fname, $prettyJSON . "\n" );
92 $this->output( "Wrote output to $fname.\n" );
93 }
94
95 protected function handleExtensionFunctions( $realName, $value ) {
96 foreach ( $value as $func ) {
97 if ( $func instanceof Closure ) {
98 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
99 }
100 }
101
102 $this->json[$realName] = $value;
103 }
104
105 protected function handleMessagesDirs( $realName, $value ) {
106 foreach ( $value as $key => $dirs ) {
107 foreach ( (array)$dirs as $dir ) {
108 $this->json[$realName][$key][] = $this->stripPath( $dir, $this->dir );
109 }
110 }
111 }
112
113 private function stripPath( $val, $dir ) {
114 if ( $val === $dir ) {
115 $val = '';
116 } elseif ( strpos( $val, $dir ) === 0 ) {
117 // +1 is for the trailing / that won't be in $this->dir
118 $val = substr( $val, strlen( $dir ) + 1 );
119 }
120
121 return $val;
122 }
123
124 protected function removeAbsolutePath( $realName, $value ) {
125 $out = array();
126 foreach ( $value as $key => $val ) {
127 $out[$key] = $this->stripPath( $val, $this->dir );
128 }
129 $this->json[$realName] = $out;
130 }
131
132 protected function handleCredits( $realName, $value) {
133 $keys = array_keys( $value );
134 $this->json['type'] = $keys[0];
135 $values = array_values( $value );
136 foreach ( $values[0][0] as $name => $val ) {
137 if ( $name !== 'path' ) {
138 $this->json[$name] = $val;
139 }
140 }
141 }
142
143 public function handleHooks( $realName, $value ) {
144 foreach ( $value as $hookName => $handlers ) {
145 foreach ( $handlers as $func ) {
146 if ( $func instanceof Closure ) {
147 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
148 }
149 }
150 }
151 $this->json[$realName] = $value;
152 }
153
154 protected function handleResourceModules( $realName, $value ) {
155 foreach ( $value as $name => $data ) {
156 if ( isset( $data['localBasePath'] ) ) {
157 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
158 }
159 $this->json[$realName][$name] = $data;
160 }
161 }
162 }
163
164 $maintClass = 'ConvertExtensionToRegistration';
165 require_once RUN_MAINTENANCE_IF_MAIN;