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