MediaWiki master
getConfiguration.php
Go to the documentation of this file.
1<?php
27
28require_once __DIR__ . '/Maintenance.php';
29
36
37 protected $regex = null;
38
39 protected $settings_list = [];
40
45 protected static $outFormats = [
46 'json',
47 'php',
48 'serialize',
49 'vardump',
50 ];
51
52 public function __construct() {
53 parent::__construct();
54 $this->addDescription( 'Get serialized MediaWiki site configuration' );
55 $this->addOption( 'regex', 'regex to filter variables with', false, true );
56 $this->addOption( 'iregex', 'same as --regex but case insensitive', false, true );
57 $this->addOption( 'settings', 'Space-separated list of wg* variables', false, true );
58 $this->addOption( 'format', implode( ', ', self::$outFormats ), false, true );
59 $this->addOption(
60 'json-partial-output-on-error',
61 'Use JSON_PARTIAL_OUTPUT_ON_ERROR flag with json_encode(). This allows for partial response to ' .
62 'be output in case of an exception while serializing to JSON. If an error occurs, ' .
63 'the wgGetConfigurationJsonErrorOccurred field is set in the output.'
64 );
65 }
66
67 public function validateParamsAndArgs() {
68 $error_out = false;
69
70 # Get the format and make sure it is set to a valid default value
71 $format = strtolower( $this->getOption( 'format', 'PHP' ) );
72
73 $validFormat = in_array( $format, self::$outFormats );
74 if ( !$validFormat ) {
75 $this->error( "--format set to an unrecognized format" );
76 $error_out = true;
77 }
78
79 if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
80 $this->error( "Can only use either --regex or --iregex" );
81 $error_out = true;
82 }
83 $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' );
84 if ( $this->regex ) {
85 $this->regex = '/' . $this->regex . '/';
86 if ( $this->hasOption( 'iregex' ) ) {
87 # case insensitive regex
88 $this->regex .= 'i';
89 }
90 }
91
92 if ( $this->hasOption( 'settings' ) ) {
93 $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
94 # Values validation
95 foreach ( $this->settings_list as $name ) {
96 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
97 $this->error( "Variable '$name' does start with 'wg'." );
98 $error_out = true;
99 } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
100 $this->error( "Variable '$name' is not set." );
101 $error_out = true;
102 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
103 $this->error( "Variable '$name' includes non-array, non-scalar, items." );
104 $error_out = true;
105 }
106 }
107 }
108
109 parent::validateParamsAndArgs();
110
111 if ( $error_out ) {
112 # Force help and quit
113 $this->maybeHelp( true );
114 }
115 }
116
117 public function execute() {
118 // Settings we will display
119 $res = [];
120
121 # Default: dump any wg / wmg variable
122 if ( !$this->regex && !$this->getOption( 'settings' ) ) {
123 $this->regex = '/^wm?g/';
124 }
125
126 # Filter out globals based on the regex
127 if ( $this->regex ) {
128 foreach ( $GLOBALS as $name => $value ) {
129 if ( preg_match( $this->regex, $name ) ) {
130 $res[$name] = $value;
131 }
132 }
133 }
134
135 # Explicitly dumps a list of provided global names
136 if ( $this->settings_list ) {
137 foreach ( $this->settings_list as $name ) {
138 $res[$name] = $GLOBALS[$name];
139 }
140 }
141
142 ksort( $res );
143
144 switch ( strtolower( $this->getOption( 'format' ) ) ) {
145 case 'serialize':
146 case 'php':
147 $out = serialize( $res );
148 break;
149 case 'vardump':
150 $out = $this->formatVarDump( $res );
151 break;
152 case 'json':
153 $out = FormatJson::encode( $res );
154 if ( !$out && $this->getOption( 'json-partial-output-on-error' ) ) {
155 $res['wgGetConfigurationJsonErrorOccurred'] = true;
156 $out = json_encode( $res, JSON_PARTIAL_OUTPUT_ON_ERROR );
157 }
158 break;
159 default:
160 $this->fatalError( "Invalid serialization format given." );
161 }
162 if ( !is_string( $out ) ) {
163 $this->fatalError( "Failed to serialize the requested settings." );
164 }
165
166 if ( $out ) {
167 $this->output( $out . "\n" );
168 }
169 }
170
171 protected function formatVarDump( $res ) {
172 $ret = '';
173 foreach ( $res as $key => $value ) {
174 # intercept var_dump() output
175 ob_start();
176 print "\${$key} = ";
177 var_dump( $value );
178 # grab var_dump() output and discard it from the output buffer
179 $ret .= trim( ob_get_clean() ) . ";\n";
180 }
181
182 return trim( $ret, "\n" );
183 }
184
185 private function isAllowedVariable( $value ) {
186 if ( is_array( $value ) ) {
187 foreach ( $value as $v ) {
188 if ( !$this->isAllowedVariable( $v ) ) {
189 return false;
190 }
191 }
192
193 return true;
194 } elseif ( is_scalar( $value ) || $value === null ) {
195 return true;
196 }
197
198 return false;
199 }
200}
201
202$maintClass = GetConfiguration::class;
203require_once RUN_MAINTENANCE_IF_MAIN;
Print serialized output of MediaWiki config vars.
execute()
Do the actual work.
static $outFormats
List of format output internally supported.
__construct()
Default constructor.
validateParamsAndArgs()
Run some validation checks on the params, etc.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
maybeHelp( $force=false)
Maybe show the help.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
JSON formatter wrapper class.