28 require_once __DIR__ .
'/Maintenance.php';
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 );
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.'
70 # Get the format and make sure it is set to a valid default value
71 $format = strtolower( $this->
getOption(
'format',
'PHP' ) );
73 $validFormat = in_array( $format, self::$outFormats );
74 if ( !$validFormat ) {
75 $this->
error(
"--format set to an unrecognized format" );
80 $this->
error(
"Can only use either --regex or --iregex" );
84 parent::validateParamsAndArgs();
98 parent::finalSetup( $settingsBuilder );
101 if ( $this->regex ) {
102 $this->regex =
'/' . $this->regex .
'/';
104 # case insensitive regex
110 $this->settings_list = explode(
' ', $this->
getOption(
'settings' ) );
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." );
128 # Default: dump any wg / wmg variable
129 if ( !$this->regex && !$this->
getOption(
'settings' ) ) {
130 $this->regex =
'/^wm?g/';
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;
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];
151 switch ( strtolower( $this->
getOption(
'format' ) ) ) {
161 if ( !$out && $this->
getOption(
'json-partial-output-on-error' ) ) {
162 $res[
'wgGetConfigurationJsonErrorOccurred'] =
true;
163 $out = json_encode(
$res, JSON_PARTIAL_OUTPUT_ON_ERROR );
167 throw new MWException(
"Invalid serialization format given." );
169 if ( !is_string( $out ) ) {
170 throw new MWException(
"Failed to serialize the requested settings." );
174 $this->
output( $out .
"\n" );
180 foreach (
$res as $key => $value ) {
181 # intercept var_dump() output
185 # grab var_dump() output and discard it from the output buffer
186 $ret .= trim( ob_get_clean() ) .
";\n";
189 return trim( $ret,
"\n" );
192 private function isAllowedVariable( $value ) {
193 if ( is_array( $value ) ) {
194 foreach ( $value as $k => $v ) {
195 if ( !$this->isAllowedVariable( $v ) ) {
201 } elseif ( is_scalar( $value ) || $value ===
null ) {
210 require_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.
finalSetup(SettingsBuilder $settingsBuilder=null)
finalSetup() since we need MWException
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.