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