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