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  }
58 
59  public function validateParamsAndArgs() {
60  $error_out = false;
61 
62  # Get the format and make sure it is set to a valid default value
63  $format = strtolower( $this->getOption( 'format', 'PHP' ) );
64 
65  $validFormat = in_array( $format, self::$outFormats );
66  if ( !$validFormat ) {
67  $this->error( "--format set to an unrecognized format" );
68  $error_out = true;
69  }
70 
71  if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
72  $this->error( "Can only use either --regex or --iregex" );
73  $error_out = true;
74  }
75 
76  parent::validateParamsAndArgs();
77 
78  if ( $error_out ) {
79  # Force help and quit
80  $this->maybeHelp( true );
81  }
82  }
83 
87  public function finalSetup() {
88  parent::finalSetup();
89 
90  $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' );
91  if ( $this->regex ) {
92  $this->regex = '/' . $this->regex . '/';
93  if ( $this->hasOption( 'iregex' ) ) {
94  # case insensitive regex
95  $this->regex .= 'i';
96  }
97  }
98 
99  if ( $this->hasOption( 'settings' ) ) {
100  $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
101  # Values validation
102  foreach ( $this->settings_list as $name ) {
103  if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
104  throw new MWException( "Variable '$name' does start with 'wg'." );
105  } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
106  throw new MWException( "Variable '$name' is not set." );
107  } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
108  throw new MWException( "Variable '$name' includes non-array, non-scalar, items." );
109  }
110  }
111  }
112  }
113 
114  public function execute() {
115  // Settings we will display
116  $res = [];
117 
118  # Sane default: dump any wg / wmg variable
119  if ( !$this->regex && !$this->getOption( 'settings' ) ) {
120  $this->regex = '/^wm?g/';
121  }
122 
123  # Filter out globals based on the regex
124  if ( $this->regex ) {
125  foreach ( $GLOBALS as $name => $value ) {
126  if ( preg_match( $this->regex, $name ) ) {
127  $res[$name] = $value;
128  }
129  }
130  }
131 
132  # Explicitly dumps a list of provided global names
133  if ( $this->settings_list ) {
134  foreach ( $this->settings_list as $name ) {
135  $res[$name] = $GLOBALS[$name];
136  }
137  }
138 
139  ksort( $res );
140 
141  $out = null;
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  break;
153  default:
154  throw new MWException( "Invalid serialization format given." );
155  }
156  if ( !is_string( $out ) ) {
157  throw new MWException( "Failed to serialize the requested settings." );
158  }
159 
160  if ( $out ) {
161  $this->output( $out . "\n" );
162  }
163  }
164 
165  protected function formatVarDump( $res ) {
166  $ret = '';
167  foreach ( $res as $key => $value ) {
168  # intercept var_dump() output
169  ob_start();
170  print "\${$key} = ";
171  var_dump( $value );
172  # grab var_dump() output and discard it from the output buffer
173  $ret .= trim( ob_get_clean() ) . ";\n";
174  }
175 
176  return trim( $ret, "\n" );
177  }
178 
179  private function isAllowedVariable( $value ) {
180  if ( is_array( $value ) ) {
181  foreach ( $value as $k => $v ) {
182  if ( !$this->isAllowedVariable( $v ) ) {
183  return false;
184  }
185  }
186 
187  return true;
188  } elseif ( is_scalar( $value ) || $value === null ) {
189  return true;
190  }
191 
192  return false;
193  }
194 }
195 
196 $maintClass = GetConfiguration::class;
197 require_once RUN_MAINTENANCE_IF_MAIN;
GetConfiguration\$regex
$regex
Definition: getConfiguration.php:35
GetConfiguration\execute
execute()
Do the actual work.
Definition: getConfiguration.php:114
Maintenance\maybeHelp
maybeHelp( $force=false)
Maybe show the help.
Definition: Maintenance.php:1009
$maintClass
$maintClass
Definition: getConfiguration.php:196
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:87
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:165
GetConfiguration\validateParamsAndArgs
validateParamsAndArgs()
Run some validation checks on the params, etc.
Definition: getConfiguration.php:59
GetConfiguration\isAllowedVariable
isAllowedVariable( $value)
Definition: getConfiguration.php:179
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