30require_once __DIR__ .
'/Maintenance.php';
50 private const OUT_FORMATS = [
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 );
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.'
75 # Get the format and make sure it is set to a valid default value
76 $format = strtolower( $this->
getOption(
'format',
'PHP' ) );
78 $validFormat = in_array( $format, self::OUT_FORMATS );
79 if ( !$validFormat ) {
80 $this->
error(
"--format set to an unrecognized format" );
85 $this->
error(
"Can only use either --regex or --iregex" );
90 $this->regex =
'/' . $this->regex .
'/';
92 # case insensitive regex
98 $this->settings_list = explode(
' ', $this->
getOption(
'settings' ) );
100 foreach ( $this->settings_list as $name ) {
101 if ( !preg_match(
'/^wg[A-Z]/', $name ) ) {
102 $this->
error(
"Variable '$name' does start with 'wg'." );
104 } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
105 $this->
error(
"Variable '$name' is not set." );
107 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
108 $this->
error(
"Variable '$name' includes non-array, non-scalar, items." );
114 parent::validateParamsAndArgs();
117 # Force help and quit
126 # Default: dump any wg / wmg variable
127 if ( !$this->regex && !$this->
getOption(
'settings' ) ) {
128 $this->regex =
'/^wm?g/';
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;
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];
149 switch ( strtolower( $this->
getOption(
'format' ) ) ) {
152 $out = serialize( $res );
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 );
165 $this->
fatalError(
"Invalid serialization format given." );
167 if ( !is_string( $out ) ) {
168 $this->
fatalError(
"Failed to serialize the requested settings." );
172 $this->
output( $out .
"\n" );
178 foreach ( $res as $key => $value ) {
179 # intercept var_dump() output
183 # grab var_dump() output and discard it from the output buffer
184 $ret .= trim( ob_get_clean() ) .
";\n";
187 return trim( $ret,
"\n" );
190 private function isAllowedVariable( $value ) {
191 if ( is_array( $value ) ) {
192 foreach ( $value as $v ) {
193 if ( !$this->isAllowedVariable( $v ) ) {
199 } elseif ( is_scalar( $value ) || $value ===
null ) {
209require_once RUN_MAINTENANCE_IF_MAIN;
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...
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.