MediaWiki  1.23.2
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 = array();
38 
43  protected static $outFormats = array(
44  'json',
45  'php',
46  'serialize',
47  'vardump',
48  );
49 
50  public function __construct() {
51  parent::__construct();
52  $this->mDescription = "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', join( ', ', self::$outFormats ), false, true );
57  }
58 
59  protected 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", 0 );
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  $this->regex .= 'i'; # case insensitive regex
95  }
96  }
97 
98  if ( $this->hasOption( 'settings' ) ) {
99  $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
100  # Values validation
101  foreach ( $this->settings_list as $name ) {
102  if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
103  throw new MWException( "Variable '$name' does start with 'wg'." );
104  } elseif ( !isset( $GLOBALS[$name] ) ) {
105  throw new MWException( "Variable '$name' is not set." );
106  } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
107  throw new MWException( "Variable '$name' includes non-array, non-scalar, items." );
108  }
109  }
110  }
111  }
112 
113  public function execute() {
114  // Settings we will display
115  $res = array();
116 
117  # Sane default: dump any wg / wmg variable
118  if ( ! $this->regex && ! $this->getOption( 'settings' ) ) {
119  $this->regex = '/^wm?g/';
120  }
121 
122  # Filter out globals based on the regex
123  if ( $this->regex ) {
124  $res = array();
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':
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  ob_start(); # intercept var_dump() output
169  print "\${$key} = ";
170  var_dump( $value );
171  # grab var_dump() output and discard it from the output buffer
172  $ret .= trim( ob_get_clean() ) . ";\n";
173  }
174 
175  return trim( $ret, "\n" );
176  }
177 
178  private function isAllowedVariable( $value ) {
179  if ( is_array( $value ) ) {
180  foreach ( $value as $k => $v ) {
181  if ( !$this->isAllowedVariable( $v ) ) {
182  return false;
183  }
184  }
185  return true;
186  } elseif ( is_scalar( $value ) ) {
187  return true;
188  }
189  return false;
190  }
191 }
192 
193 $maintClass = "GetConfiguration";
194 require_once RUN_MAINTENANCE_IF_MAIN;
GetConfiguration\$regex
$regex
Definition: getConfiguration.php:35
GetConfiguration\execute
execute()
Do the actual work.
Definition: getConfiguration.php:113
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Maintenance\maybeHelp
maybeHelp( $force=false)
Maybe show the help.
Definition: Maintenance.php:710
$maintClass
$maintClass
Definition: getConfiguration.php:193
GetConfiguration\__construct
__construct()
Default constructor.
Definition: getConfiguration.php:50
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1530
GetConfiguration\finalSetup
finalSetup()
finalSetup() since we need MWException
Definition: getConfiguration.php:87
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:104
MWException
MediaWiki exception.
Definition: MWException.php:26
$out
$out
Definition: UtfNormalGenerate.php:167
GetConfiguration
Print serialized output of MediaWiki config vars.
Definition: getConfiguration.php:33
GetConfiguration\$outFormats
static $outFormats
List of format output internally supported.
Definition: getConfiguration.php:43
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
GetConfiguration\formatVarDump
formatVarDump( $res)
Definition: getConfiguration.php:165
GetConfiguration\validateParamsAndArgs
validateParamsAndArgs()
Run some validation checks on the params, etc.
Definition: getConfiguration.php:59
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$value
$value
Definition: styleTest.css.php:45
GetConfiguration\isAllowedVariable
isAllowedVariable( $value)
Definition: getConfiguration.php:178
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:191
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
$res
$res
Definition: database.txt:21
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6