Merge "Export mw.Message's string formatter as mw.format"
[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' => 'removeAbsolutePath',
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 public function execute() {
25 require $this->getArg( 0 );
26 // Try not to create any local variables before this line
27 $vars = get_defined_vars();
28 unset( $vars['this'] );
29 $this->dir = dirname( realpath( $this->getArg( 0 ) ) );
30 $this->json = array();
31 $processor = new ReflectionClass( 'ExtensionProcessor' );
32 $settings = $processor->getProperty( 'globalSettings' );
33 $settings->setAccessible( true );
34 $globalSettings = $settings->getValue();
35 foreach ( $vars as $name => $value ) {
36 $realName = substr( $name, 2 ); // Strip 'wg'
37 if ( isset( $this->custom[$realName] ) ) {
38 call_user_func_array( array( $this, $this->custom[$realName] ), array( $realName, $value ) );
39 } elseif ( in_array( $realName, $globalSettings ) ) {
40 $this->json[$realName] = $value;
41 } elseif ( strpos( $name, 'wg' ) === 0 ) {
42 // Most likely a config setting
43 $this->json['config'][$realName] = $value;
44 }
45 }
46
47 $fname = "{$this->dir}/extension.json";
48 $prettyJSON = FormatJson::encode( $this->json, "\t" );
49 file_put_contents( $fname, $prettyJSON . "\n" );
50 $this->output( "Wrote output to $fname.\n" );
51 }
52
53 protected function handleExtensionFunctions( $realName, $value ) {
54 foreach ( $value as $func ) {
55 if ( $func instanceof Closure ) {
56 $this->error( "Error: Closures cannot be converted to JSON. Please move your extension function somewhere else.", 1 );
57 }
58 }
59
60 $this->json[$realName] = $value;
61 }
62
63 private function stripPath( $val, $dir ) {
64 if ( strpos( $val, $dir ) === 0 ) {
65 // +1 is for the trailing / that won't be in $this->dir
66 $val = substr( $val, strlen( $dir ) + 1 );
67 }
68
69 return $val;
70 }
71
72 protected function removeAbsolutePath( $realName, $value ) {
73 $out = array();
74 foreach ( $value as $key => $val ) {
75 $out[$key] = $this->stripPath( $val, $this->dir );
76 }
77 $this->json[$realName] = $out;
78 }
79
80 protected function handleCredits( $realName, $value) {
81 $keys = array_keys( $value );
82 $this->json['type'] = $keys[0];
83 $values = array_values( $value );
84 foreach ( $values[0][0] as $name => $val ) {
85 if ( $name !== 'path' ) {
86 $this->json[$name] = $val;
87 }
88 }
89 }
90
91 public function handleHooks( $realName, $value ) {
92 foreach ( $value as $hookName => $handlers ) {
93 foreach ( $handlers as $func ) {
94 if ( $func instanceof Closure ) {
95 $this->error( "Error: Closures cannot be converted to JSON. Please move the handler for $hookName somewhere else.", 1 );
96 }
97 }
98 }
99 $this->json[$realName] = $value;
100 }
101
102 protected function handleResourceModules( $realName, $value ) {
103 foreach ( $value as $name => $data ) {
104 if ( isset( $data['localBasePath'] ) ) {
105 $data['localBasePath'] = $this->stripPath( $data['localBasePath'], $this->dir );
106 }
107 $this->json[$realName][$name] = $data;
108 }
109 }
110 }
111
112 $maintClass = 'ConvertExtensionToRegistration';
113 require_once RUN_MAINTENANCE_IF_MAIN;