| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
use MediaWiki\Json\FormatJson; |
| 27 |
|
| 28 |
|
| 29 |
require_once __DIR__ . '/Maintenance.php'; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class GetConfiguration extends Maintenance { |
| 38 |
|
| 39 |
|
| 40 |
protected $regex = null; |
| 41 |
|
| 42 |
|
| 43 |
protected $format = null; |
| 44 |
|
| 45 |
|
| 46 |
protected $settings_list = []; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private const OUT_FORMATS = [ |
| 53 |
'json', |
| 54 |
'php', |
| 55 |
'serialize', |
| 56 |
'vardump', |
| 57 |
]; |
| 58 |
|
| 59 |
public function __construct() { |
| 60 |
parent::__construct(); |
| 61 |
$this->addDescription( 'Get serialized MediaWiki site configuration' ); |
| 62 |
$this->addOption( 'regex', 'regex to filter variables with', false, true ); |
| 63 |
$this->addOption( 'iregex', 'same as --regex but case insensitive', false, true ); |
| 64 |
$this->addOption( 'settings', 'Space-separated list of wg* variables', false, true ); |
| 65 |
$this->addOption( 'format', implode( ', ', self::OUT_FORMATS ), false, true ); |
| 66 |
$this->addOption( |
| 67 |
'json-partial-output-on-error', |
| 68 |
'Use JSON_PARTIAL_OUTPUT_ON_ERROR flag with json_encode(). This allows for partial response to ' . |
| 69 |
'be output in case of an exception while serializing to JSON. If an error occurs, ' . |
| 70 |
'the wgGetConfigurationJsonErrorOccurred field is set in the output.' |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
public function validateParamsAndArgs() { |
| 75 |
$error_out = false; |
| 76 |
|
| 77 |
|
| 78 |
$this->format = strtolower( $this->getOption( 'format', 'PHP' ) ); |
| 79 |
|
| 80 |
$validFormat = in_array( $this->format, self::OUT_FORMATS ); |
| 81 |
if ( !$validFormat ) { |
| 82 |
$this->error( "--format set to an unrecognized format" ); |
| 83 |
$error_out = true; |
| 84 |
} |
| 85 |
|
| 86 |
if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) { |
| 87 |
$this->error( "Can only use either --regex or --iregex" ); |
| 88 |
$error_out = true; |
| 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 |
|
| 95 |
$this->regex .= 'i'; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if ( $this->hasOption( 'settings' ) ) { |
| 100 |
$this->settings_list = explode( ' ', $this->getOption( 'settings' ) ); |
| 101 |
|
| 102 |
foreach ( $this->settings_list as $name ) { |
| 103 |
if ( !preg_match( '/^wg[A-Z]/', $name ) ) { |
| 104 |
$this->error( "Variable '$name' does start with 'wg'." ); |
| 105 |
$error_out = true; |
| 106 |
} elseif ( !array_key_exists( $name, $GLOBALS ) ) { |
| 107 |
$this->error( "Variable '$name' is not set." ); |
| 108 |
$error_out = true; |
| 109 |
} elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) { |
| 110 |
$this->error( "Variable '$name' includes non-array, non-scalar, items." ); |
| 111 |
$error_out = true; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
parent::validateParamsAndArgs(); |
| 117 |
|
| 118 |
if ( $error_out ) { |
| 119 |
|
| 120 |
$this->maybeHelp( true ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public function execute() { |
| 125 |
|
| 126 |
$res = []; |
| 127 |
|
| 128 |
|
| 129 |
if ( !$this->regex && !$this->getOption( 'settings' ) ) { |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
$this->regex = '/^wm?g(?!User|Out|Request|Hooks).*$/'; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
if ( $this->regex ) { |
| 141 |
foreach ( $GLOBALS as $name => $value ) { |
| 142 |
if ( preg_match( $this->regex, $name ) ) { |
| 143 |
$res[$name] = $value; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
if ( $this->settings_list ) { |
| 150 |
foreach ( $this->settings_list as $name ) { |
| 151 |
$res[$name] = $GLOBALS[$name]; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
ksort( $res ); |
| 156 |
|
| 157 |
switch ( $this->format ) { |
| 158 |
case 'serialize': |
| 159 |
case 'php': |
| 160 |
$out = serialize( $res ); |
| 161 |
break; |
| 162 |
case 'vardump': |
| 163 |
$out = $this->formatVarDump( $res ); |
| 164 |
break; |
| 165 |
case 'json': |
| 166 |
$out = FormatJson::encode( $res ); |
| 167 |
if ( !$out && $this->getOption( 'json-partial-output-on-error' ) ) { |
| 168 |
$res['wgGetConfigurationJsonErrorOccurred'] = true; |
| 169 |
$out = json_encode( $res, JSON_PARTIAL_OUTPUT_ON_ERROR ); |
| 170 |
} |
| 171 |
break; |
| 172 |
default: |
| 173 |
$this->fatalError( "Invalid serialization format given." ); |
| 174 |
} |
| 175 |
if ( !is_string( $out ) ) { |
| 176 |
$this->fatalError( "Failed to serialize the requested settings." ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( $out ) { |
| 180 |
$this->output( $out . "\n" ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
protected function formatVarDump( $res ) { |
| 185 |
$ret = ''; |
| 186 |
foreach ( $res as $key => $value ) { |
| 187 |
|
| 188 |
ob_start(); |
| 189 |
print "\${$key} = "; |
| 190 |
var_dump( $value ); |
| 191 |
|
| 192 |
$ret .= trim( ob_get_clean() ) . ";\n"; |
| 193 |
} |
| 194 |
|
| 195 |
return trim( $ret, "\n" ); |
| 196 |
} |
| 197 |
|
| 198 |
private function isAllowedVariable( $value ) { |
| 199 |
if ( is_array( $value ) ) { |
| 200 |
foreach ( $value as $v ) { |
| 201 |
if ( !$this->isAllowedVariable( $v ) ) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return true; |
| 207 |
} elseif ( is_scalar( $value ) || $value === null ) { |
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
return false; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
$maintClass = GetConfiguration::class; |
| 217 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 218 |
|