16require_once __DIR__ .
'/Maintenance.php';
39 private const OUT_FORMATS = [
47 parent::__construct();
48 $this->
addDescription(
'Get serialized MediaWiki site configuration' );
49 $this->
addOption(
'regex',
'regex to filter variables with',
false,
true );
50 $this->
addOption(
'iregex',
'same as --regex but case insensitive',
false,
true );
51 $this->
addOption(
'settings',
'Space-separated list of wg* variables',
false,
true );
52 $this->
addOption(
'format', implode(
', ', self::OUT_FORMATS ),
false,
true );
54 'json-partial-output-on-error',
55 'Use JSON_PARTIAL_OUTPUT_ON_ERROR flag with json_encode(). This allows for partial response to ' .
56 'be output in case of an exception while serializing to JSON. If an error occurs, ' .
57 'the wgGetConfigurationJsonErrorOccurred field is set in the output.'
64 # Get the format and make sure it is set to a valid default value
65 $this->format = strtolower( $this->
getOption(
'format',
'PHP' ) );
67 $validFormat = in_array( $this->format, self::OUT_FORMATS );
68 if ( !$validFormat ) {
69 $this->
error(
"--format set to an unrecognized format" );
74 $this->
error(
"Can only use either --regex or --iregex" );
79 $this->regex =
'/' . $this->regex .
'/';
81 # case insensitive regex
87 $this->settings_list = explode(
' ', $this->
getOption(
'settings' ) );
89 foreach ( $this->settings_list as $name ) {
90 if ( !preg_match(
'/^wg[A-Z]/', $name ) ) {
91 $this->
error(
"Variable '$name' does start with 'wg'." );
93 } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
94 $this->
error(
"Variable '$name' is not set." );
96 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
97 $this->
error(
"Variable '$name' includes non-array, non-scalar, items." );
103 parent::validateParamsAndArgs();
106 # Force help and quit
115 # Default: dump any wg / wmg variable
116 if ( !$this->regex && !$this->
getOption(
'settings' ) ) {
123 $this->regex =
'/^wm?g(?!User|Out|Request|Hooks).*$/';
126 # Filter out globals based on the regex
127 if ( $this->regex ) {
128 foreach ( $GLOBALS as $name => $value ) {
129 if ( preg_match( $this->regex, $name ) ) {
130 $res[$name] = $value;
135 # Explicitly dumps a list of provided global names
136 if ( $this->settings_list ) {
137 foreach ( $this->settings_list as $name ) {
138 $res[$name] = $GLOBALS[$name];
144 switch ( $this->format ) {
147 $out = serialize( $res );
153 $out = FormatJson::encode( $res );
154 if ( !$out && $this->
getOption(
'json-partial-output-on-error' ) ) {
155 $res[
'wgGetConfigurationJsonErrorOccurred'] =
true;
156 $out = json_encode( $res, JSON_PARTIAL_OUTPUT_ON_ERROR );
160 $this->
fatalError(
"Invalid serialization format given." );
162 if ( !is_string( $out ) ) {
163 $this->
fatalError(
"Failed to serialize the requested settings." );
167 $this->
output( $out .
"\n" );
173 foreach ( $res as $key => $value ) {
174 # intercept var_dump() output
178 # grab var_dump() output and discard it from the output buffer
179 $ret .= trim( ob_get_clean() ) .
";\n";
182 return trim( $ret,
"\n" );
188 private function isAllowedVariable( $value ): bool {
189 if ( is_array( $value ) ) {
190 foreach ( $value as $v ) {
191 if ( !$this->isAllowedVariable( $v ) ) {
197 } elseif ( is_scalar( $value ) || $value ===
null ) {
207require_once RUN_MAINTENANCE_IF_MAIN;
if(!defined('MW_SETUP_CALLBACK'))
Print serialized output of MediaWiki config vars.
execute()
Do the actual work.
formatVarDump(array $res)
__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.