MediaWiki REL1_39
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
84 parent::validateParamsAndArgs();
85
86 if ( $error_out ) {
87 # Force help and quit
88 $this->maybeHelp( true );
89 }
90 }
91
97 public function finalSetup( SettingsBuilder $settingsBuilder = null ) {
98 parent::finalSetup( $settingsBuilder );
99
100 $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' );
101 if ( $this->regex ) {
102 $this->regex = '/' . $this->regex . '/';
103 if ( $this->hasOption( 'iregex' ) ) {
104 # case insensitive regex
105 $this->regex .= 'i';
106 }
107 }
108
109 if ( $this->hasOption( 'settings' ) ) {
110 $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
111 # Values validation
112 foreach ( $this->settings_list as $name ) {
113 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
114 throw new MWException( "Variable '$name' does start with 'wg'." );
115 } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
116 throw new MWException( "Variable '$name' is not set." );
117 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
118 throw new MWException( "Variable '$name' includes non-array, non-scalar, items." );
119 }
120 }
121 }
122 }
123
124 public function execute() {
125 // Settings we will display
126 $res = [];
127
128 # Default: dump any wg / wmg variable
129 if ( !$this->regex && !$this->getOption( 'settings' ) ) {
130 $this->regex = '/^wm?g/';
131 }
132
133 # Filter out globals based on the regex
134 if ( $this->regex ) {
135 foreach ( $GLOBALS as $name => $value ) {
136 if ( preg_match( $this->regex, $name ) ) {
137 $res[$name] = $value;
138 }
139 }
140 }
141
142 # Explicitly dumps a list of provided global names
143 if ( $this->settings_list ) {
144 foreach ( $this->settings_list as $name ) {
145 $res[$name] = $GLOBALS[$name];
146 }
147 }
148
149 ksort( $res );
150
151 switch ( strtolower( $this->getOption( 'format' ) ) ) {
152 case 'serialize':
153 case 'php':
154 $out = serialize( $res );
155 break;
156 case 'vardump':
157 $out = $this->formatVarDump( $res );
158 break;
159 case 'json':
160 $out = FormatJson::encode( $res );
161 if ( !$out && $this->getOption( 'json-partial-output-on-error' ) ) {
162 $res['wgGetConfigurationJsonErrorOccurred'] = true;
163 $out = json_encode( $res, JSON_PARTIAL_OUTPUT_ON_ERROR );
164 }
165 break;
166 default:
167 throw new MWException( "Invalid serialization format given." );
168 }
169 if ( !is_string( $out ) ) {
170 throw new MWException( "Failed to serialize the requested settings." );
171 }
172
173 if ( $out ) {
174 $this->output( $out . "\n" );
175 }
176 }
177
178 protected function formatVarDump( $res ) {
179 $ret = '';
180 foreach ( $res as $key => $value ) {
181 # intercept var_dump() output
182 ob_start();
183 print "\${$key} = ";
184 var_dump( $value );
185 # grab var_dump() output and discard it from the output buffer
186 $ret .= trim( ob_get_clean() ) . ";\n";
187 }
188
189 return trim( $ret, "\n" );
190 }
191
192 private function isAllowedVariable( $value ) {
193 if ( is_array( $value ) ) {
194 foreach ( $value as $k => $v ) {
195 if ( !$this->isAllowedVariable( $v ) ) {
196 return false;
197 }
198 }
199
200 return true;
201 } elseif ( is_scalar( $value ) || $value === null ) {
202 return true;
203 }
204
205 return false;
206 }
207}
208
209$maintClass = GetConfiguration::class;
210require_once RUN_MAINTENANCE_IF_MAIN;
serialize()
Print serialized output of MediaWiki config vars.
execute()
Do the actual work.
static $outFormats
List of format output internally supported.
__construct()
Default constructor.
finalSetup(SettingsBuilder $settingsBuilder=null)
finalSetup() since we need MWException
validateParamsAndArgs()
Run some validation checks on the params, etc.
MediaWiki exception.
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.
Utility for loading settings files.
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:69