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