MediaWiki  master
getConfiguration.php
Go to the documentation of this file.
1 <?php
26 require_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 
82  parent::validateParamsAndArgs();
83 
84  if ( $error_out ) {
85  # Force help and quit
86  $this->maybeHelp( true );
87  }
88  }
89 
93  public function finalSetup() {
94  parent::finalSetup();
95 
96  $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' );
97  if ( $this->regex ) {
98  $this->regex = '/' . $this->regex . '/';
99  if ( $this->hasOption( 'iregex' ) ) {
100  # case insensitive regex
101  $this->regex .= 'i';
102  }
103  }
104 
105  if ( $this->hasOption( 'settings' ) ) {
106  $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
107  # Values validation
108  foreach ( $this->settings_list as $name ) {
109  if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
110  throw new MWException( "Variable '$name' does start with 'wg'." );
111  } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
112  throw new MWException( "Variable '$name' is not set." );
113  } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
114  throw new MWException( "Variable '$name' includes non-array, non-scalar, items." );
115  }
116  }
117  }
118  }
119 
120  public function execute() {
121  // Settings we will display
122  $res = [];
123 
124  # Default: dump any wg / wmg variable
125  if ( !$this->regex && !$this->getOption( 'settings' ) ) {
126  $this->regex = '/^wm?g/';
127  }
128 
129  # Filter out globals based on the regex
130  if ( $this->regex ) {
131  foreach ( $GLOBALS as $name => $value ) {
132  if ( preg_match( $this->regex, $name ) ) {
133  $res[$name] = $value;
134  }
135  }
136  }
137 
138  # Explicitly dumps a list of provided global names
139  if ( $this->settings_list ) {
140  foreach ( $this->settings_list as $name ) {
141  $res[$name] = $GLOBALS[$name];
142  }
143  }
144 
145  ksort( $res );
146 
147  switch ( strtolower( $this->getOption( 'format' ) ) ) {
148  case 'serialize':
149  case 'php':
150  $out = serialize( $res );
151  break;
152  case 'vardump':
153  $out = $this->formatVarDump( $res );
154  break;
155  case 'json':
156  $out = FormatJson::encode( $res );
157  if ( !$out && $this->getOption( 'json-partial-output-on-error' ) ) {
158  $res['wgGetConfigurationJsonErrorOccurred'] = true;
159  $out = json_encode( $res, JSON_PARTIAL_OUTPUT_ON_ERROR );
160  }
161  break;
162  default:
163  throw new MWException( "Invalid serialization format given." );
164  }
165  if ( !is_string( $out ) ) {
166  throw new MWException( "Failed to serialize the requested settings." );
167  }
168 
169  if ( $out ) {
170  $this->output( $out . "\n" );
171  }
172  }
173 
174  protected function formatVarDump( $res ) {
175  $ret = '';
176  foreach ( $res as $key => $value ) {
177  # intercept var_dump() output
178  ob_start();
179  print "\${$key} = ";
180  var_dump( $value );
181  # grab var_dump() output and discard it from the output buffer
182  $ret .= trim( ob_get_clean() ) . ";\n";
183  }
184 
185  return trim( $ret, "\n" );
186  }
187 
188  private function isAllowedVariable( $value ) {
189  if ( is_array( $value ) ) {
190  foreach ( $value as $k => $v ) {
191  if ( !$this->isAllowedVariable( $v ) ) {
192  return false;
193  }
194  }
195 
196  return true;
197  } elseif ( is_scalar( $value ) || $value === null ) {
198  return true;
199  }
200 
201  return false;
202  }
203 }
204 
205 $maintClass = GetConfiguration::class;
206 require_once RUN_MAINTENANCE_IF_MAIN;
GetConfiguration\$regex
$regex
Definition: getConfiguration.php:35
GetConfiguration\execute
execute()
Do the actual work.
Definition: getConfiguration.php:120
Maintenance\maybeHelp
maybeHelp( $force=false)
Maybe show the help.
Definition: Maintenance.php:1009
$maintClass
$maintClass
Definition: getConfiguration.php:205
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
GetConfiguration\__construct
__construct()
Default constructor.
Definition: getConfiguration.php:50
GetConfiguration\finalSetup
finalSetup()
finalSetup() since we need MWException
Definition: getConfiguration.php:93
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$res
$res
Definition: testCompression.php:57
serialize
serialize()
Definition: ApiMessageTrait.php:138
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:96
MWException
MediaWiki exception.
Definition: MWException.php:29
GetConfiguration
Print serialized output of MediaWiki config vars.
Definition: getConfiguration.php:33
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
GetConfiguration\$outFormats
static $outFormats
List of format output internally supported.
Definition: getConfiguration.php:43
GetConfiguration\formatVarDump
formatVarDump( $res)
Definition: getConfiguration.php:174
GetConfiguration\validateParamsAndArgs
validateParamsAndArgs()
Run some validation checks on the params, etc.
Definition: getConfiguration.php:65
GetConfiguration\isAllowedVariable
isAllowedVariable( $value)
Definition: getConfiguration.php:188
GetConfiguration\$settings_list
$settings_list
Definition: getConfiguration.php:37
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:464
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271