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