Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.84% |
85 / 86 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| GetConfiguration | |
98.84% |
85 / 86 |
|
80.00% |
4 / 5 |
39 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| validateParamsAndArgs | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
13 | |||
| execute | |
96.67% |
29 / 30 |
|
0.00% |
0 / 1 |
17 | |||
| formatVarDump | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| isAllowedVariable | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Print serialized output of MediaWiki config vars. |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | * @ingroup Maintenance |
| 8 | * @author Tim Starling |
| 9 | * @author Antoine Musso <hashar@free.fr> |
| 10 | */ |
| 11 | |
| 12 | use MediaWiki\Json\FormatJson; |
| 13 | use MediaWiki\Maintenance\Maintenance; |
| 14 | |
| 15 | // @codeCoverageIgnoreStart |
| 16 | require_once __DIR__ . '/Maintenance.php'; |
| 17 | // @codeCoverageIgnoreEnd |
| 18 | |
| 19 | /** |
| 20 | * Print serialized output of MediaWiki config vars |
| 21 | * |
| 22 | * @ingroup Maintenance |
| 23 | */ |
| 24 | class GetConfiguration extends Maintenance { |
| 25 | |
| 26 | /** @var string|null */ |
| 27 | protected $regex = null; |
| 28 | |
| 29 | /** @var string|null */ |
| 30 | protected $format = null; |
| 31 | |
| 32 | /** @var array */ |
| 33 | protected $settings_list = []; |
| 34 | |
| 35 | /** |
| 36 | * List of format output internally supported. |
| 37 | * Each item MUST be lower case. |
| 38 | */ |
| 39 | private const OUT_FORMATS = [ |
| 40 | 'json', |
| 41 | 'php', |
| 42 | 'serialize', |
| 43 | 'vardump', |
| 44 | ]; |
| 45 | |
| 46 | public function __construct() { |
| 47 | parent::__construct(); |
| 48 | $this->addDescription( 'Get serialized MediaWiki site configuration' ); |
| 49 | $this->addOption( 'regex', 'regex to filter variables with', false, true ); |
| 50 | $this->addOption( 'iregex', 'same as --regex but case insensitive', false, true ); |
| 51 | $this->addOption( 'settings', 'Space-separated list of wg* variables', false, true ); |
| 52 | $this->addOption( 'format', implode( ', ', self::OUT_FORMATS ), false, true ); |
| 53 | $this->addOption( |
| 54 | 'json-partial-output-on-error', |
| 55 | 'Use JSON_PARTIAL_OUTPUT_ON_ERROR flag with json_encode(). This allows for partial response to ' . |
| 56 | 'be output in case of an exception while serializing to JSON. If an error occurs, ' . |
| 57 | 'the wgGetConfigurationJsonErrorOccurred field is set in the output.' |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | public function validateParamsAndArgs() { |
| 62 | $error_out = false; |
| 63 | |
| 64 | # Get the format and make sure it is set to a valid default value |
| 65 | $this->format = strtolower( $this->getOption( 'format', 'PHP' ) ); |
| 66 | |
| 67 | $validFormat = in_array( $this->format, self::OUT_FORMATS ); |
| 68 | if ( !$validFormat ) { |
| 69 | $this->error( "--format set to an unrecognized format" ); |
| 70 | $error_out = true; |
| 71 | } |
| 72 | |
| 73 | if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) { |
| 74 | $this->error( "Can only use either --regex or --iregex" ); |
| 75 | $error_out = true; |
| 76 | } |
| 77 | $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' ); |
| 78 | if ( $this->regex ) { |
| 79 | $this->regex = '/' . $this->regex . '/'; |
| 80 | if ( $this->hasOption( 'iregex' ) ) { |
| 81 | # case insensitive regex |
| 82 | $this->regex .= 'i'; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if ( $this->hasOption( 'settings' ) ) { |
| 87 | $this->settings_list = explode( ' ', $this->getOption( 'settings' ) ); |
| 88 | # Values validation |
| 89 | foreach ( $this->settings_list as $name ) { |
| 90 | if ( !preg_match( '/^wg[A-Z]/', $name ) ) { |
| 91 | $this->error( "Variable '$name' does start with 'wg'." ); |
| 92 | $error_out = true; |
| 93 | } elseif ( !array_key_exists( $name, $GLOBALS ) ) { |
| 94 | $this->error( "Variable '$name' is not set." ); |
| 95 | $error_out = true; |
| 96 | } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) { |
| 97 | $this->error( "Variable '$name' includes non-array, non-scalar, items." ); |
| 98 | $error_out = true; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | parent::validateParamsAndArgs(); |
| 104 | |
| 105 | if ( $error_out ) { |
| 106 | # Force help and quit |
| 107 | $this->maybeHelp( true ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public function execute() { |
| 112 | // Settings we will display |
| 113 | $res = []; |
| 114 | |
| 115 | # Default: dump any wg / wmg variable |
| 116 | if ( !$this->regex && !$this->getOption( 'settings' ) ) { |
| 117 | // Avoid fatal "Exception: Serialization of Closure is not allowed" |
| 118 | // |
| 119 | // * Exclude legacy singletons that are not configuration but |
| 120 | // non-serializable objects, such as $wgOut. |
| 121 | // * Exclude config arrays such as wgHooks which may contain closures |
| 122 | // via LocalSettings.php. |
| 123 | $this->regex = '/^wm?g(?!Out|Request|Hooks).*$/'; |
| 124 | } |
| 125 | |
| 126 | # Filter out globals based on the regex |
| 127 | if ( $this->regex ) { |
| 128 | foreach ( $GLOBALS as $name => $value ) { |
| 129 | if ( preg_match( $this->regex, $name ) ) { |
| 130 | $res[$name] = $value; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | # Explicitly dumps a list of provided global names |
| 136 | if ( $this->settings_list ) { |
| 137 | foreach ( $this->settings_list as $name ) { |
| 138 | $res[$name] = $GLOBALS[$name]; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | ksort( $res ); |
| 143 | |
| 144 | switch ( $this->format ) { |
| 145 | case 'serialize': |
| 146 | case 'php': |
| 147 | $out = serialize( $res ); |
| 148 | break; |
| 149 | case 'vardump': |
| 150 | $out = $this->formatVarDump( $res ); |
| 151 | break; |
| 152 | case 'json': |
| 153 | $out = FormatJson::encode( $res ); |
| 154 | if ( !$out && $this->getOption( 'json-partial-output-on-error' ) ) { |
| 155 | $res['wgGetConfigurationJsonErrorOccurred'] = true; |
| 156 | $out = json_encode( $res, JSON_PARTIAL_OUTPUT_ON_ERROR ); |
| 157 | } |
| 158 | break; |
| 159 | default: |
| 160 | $this->fatalError( "Invalid serialization format given." ); |
| 161 | } |
| 162 | if ( !is_string( $out ) ) { |
| 163 | $this->fatalError( "Failed to serialize the requested settings." ); |
| 164 | } |
| 165 | |
| 166 | if ( $out ) { |
| 167 | $this->output( $out . "\n" ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | protected function formatVarDump( array $res ): string { |
| 172 | $ret = ''; |
| 173 | foreach ( $res as $key => $value ) { |
| 174 | # intercept var_dump() output |
| 175 | ob_start(); |
| 176 | print "\${$key} = "; |
| 177 | var_dump( $value ); |
| 178 | # grab var_dump() output and discard it from the output buffer |
| 179 | $ret .= trim( ob_get_clean() ) . ";\n"; |
| 180 | } |
| 181 | |
| 182 | return trim( $ret, "\n" ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @param mixed $value |
| 187 | */ |
| 188 | private function isAllowedVariable( $value ): bool { |
| 189 | if ( is_array( $value ) ) { |
| 190 | foreach ( $value as $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 | // @codeCoverageIgnoreStart |
| 206 | $maintClass = GetConfiguration::class; |
| 207 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 208 | // @codeCoverageIgnoreEnd |