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