3 require_once __DIR__
. '/Maintenance.php';
5 class ConvertExtensionToRegistration
extends Maintenance
{
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',
19 public function __construct() {
20 parent
::__construct();
21 $this->mDescription
= 'Converts extension entry points to the new JSON registration format';
24 protected function getAllGlobals() {
25 $processor = new ReflectionClass( 'ExtensionProcessor' );
26 $settings = $processor->getProperty( 'globalSettings' );
27 $settings->setAccessible( true );
28 return $settings->getValue();
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 ) {
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 ) {
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;
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" );
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 );
75 $this->json
[$realName] = $value;
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
);
86 private function stripPath( $val, $dir ) {
87 if ( $val === $dir ) {
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 );
97 protected function removeAbsolutePath( $realName, $value ) {
99 foreach ( $value as $key => $val ) {
100 $out[$key] = $this->stripPath( $val, $this->dir
);
102 $this->json
[$realName] = $out;
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;
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 );
124 $this->json
[$realName] = $value;
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
);
132 $this->json
[$realName][$name] = $data;
137 $maintClass = 'ConvertExtensionToRegistration';
138 require_once RUN_MAINTENANCE_IF_MAIN
;