Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / getConfiguration.php
blob22fcc8b3b8ec83bce84a7ed975e7b0b24ded0798
1 <?php
2 /**
3 * Print serialized output of MediaWiki config vars.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
22 * @author Tim Starling
23 * @author Antoine Musso <hashar@free.fr>
26 use MediaWiki\Json\FormatJson;
27 use MediaWiki\Maintenance\Maintenance;
29 // @codeCoverageIgnoreStart
30 require_once __DIR__ . '/Maintenance.php';
31 // @codeCoverageIgnoreEnd
33 /**
34 * Print serialized output of MediaWiki config vars
36 * @ingroup Maintenance
38 class GetConfiguration extends Maintenance {
40 /** @var string|null */
41 protected $regex = null;
43 /** @var array */
44 protected $settings_list = [];
46 /**
47 * List of format output internally supported.
48 * Each item MUST be lower case.
50 private const OUT_FORMATS = [
51 'json',
52 'php',
53 'serialize',
54 'vardump',
57 public function __construct() {
58 parent::__construct();
59 $this->addDescription( 'Get serialized MediaWiki site configuration' );
60 $this->addOption( 'regex', 'regex to filter variables with', false, true );
61 $this->addOption( 'iregex', 'same as --regex but case insensitive', false, true );
62 $this->addOption( 'settings', 'Space-separated list of wg* variables', false, true );
63 $this->addOption( 'format', implode( ', ', self::OUT_FORMATS ), false, true );
64 $this->addOption(
65 'json-partial-output-on-error',
66 'Use JSON_PARTIAL_OUTPUT_ON_ERROR flag with json_encode(). This allows for partial response to ' .
67 'be output in case of an exception while serializing to JSON. If an error occurs, ' .
68 'the wgGetConfigurationJsonErrorOccurred field is set in the output.'
72 public function validateParamsAndArgs() {
73 $error_out = false;
75 # Get the format and make sure it is set to a valid default value
76 $format = strtolower( $this->getOption( 'format', 'PHP' ) );
78 $validFormat = in_array( $format, self::OUT_FORMATS );
79 if ( !$validFormat ) {
80 $this->error( "--format set to an unrecognized format" );
81 $error_out = true;
84 if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
85 $this->error( "Can only use either --regex or --iregex" );
86 $error_out = true;
88 $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' );
89 if ( $this->regex ) {
90 $this->regex = '/' . $this->regex . '/';
91 if ( $this->hasOption( 'iregex' ) ) {
92 # case insensitive regex
93 $this->regex .= 'i';
97 if ( $this->hasOption( 'settings' ) ) {
98 $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
99 # Values validation
100 foreach ( $this->settings_list as $name ) {
101 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
102 $this->error( "Variable '$name' does start with 'wg'." );
103 $error_out = true;
104 } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
105 $this->error( "Variable '$name' is not set." );
106 $error_out = true;
107 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
108 $this->error( "Variable '$name' includes non-array, non-scalar, items." );
109 $error_out = true;
114 parent::validateParamsAndArgs();
116 if ( $error_out ) {
117 # Force help and quit
118 $this->maybeHelp( true );
122 public function execute() {
123 // Settings we will display
124 $res = [];
126 # Default: dump any wg / wmg variable
127 if ( !$this->regex && !$this->getOption( 'settings' ) ) {
128 $this->regex = '/^wm?g/';
131 # Filter out globals based on the regex
132 if ( $this->regex ) {
133 foreach ( $GLOBALS as $name => $value ) {
134 if ( preg_match( $this->regex, $name ) ) {
135 $res[$name] = $value;
140 # Explicitly dumps a list of provided global names
141 if ( $this->settings_list ) {
142 foreach ( $this->settings_list as $name ) {
143 $res[$name] = $GLOBALS[$name];
147 ksort( $res );
149 switch ( strtolower( $this->getOption( 'format' ) ) ) {
150 case 'serialize':
151 case 'php':
152 $out = serialize( $res );
153 break;
154 case 'vardump':
155 $out = $this->formatVarDump( $res );
156 break;
157 case 'json':
158 $out = FormatJson::encode( $res );
159 if ( !$out && $this->getOption( 'json-partial-output-on-error' ) ) {
160 $res['wgGetConfigurationJsonErrorOccurred'] = true;
161 $out = json_encode( $res, JSON_PARTIAL_OUTPUT_ON_ERROR );
163 break;
164 default:
165 $this->fatalError( "Invalid serialization format given." );
167 if ( !is_string( $out ) ) {
168 $this->fatalError( "Failed to serialize the requested settings." );
171 if ( $out ) {
172 $this->output( $out . "\n" );
176 protected function formatVarDump( $res ) {
177 $ret = '';
178 foreach ( $res as $key => $value ) {
179 # intercept var_dump() output
180 ob_start();
181 print "\${$key} = ";
182 var_dump( $value );
183 # grab var_dump() output and discard it from the output buffer
184 $ret .= trim( ob_get_clean() ) . ";\n";
187 return trim( $ret, "\n" );
190 private function isAllowedVariable( $value ) {
191 if ( is_array( $value ) ) {
192 foreach ( $value as $v ) {
193 if ( !$this->isAllowedVariable( $v ) ) {
194 return false;
198 return true;
199 } elseif ( is_scalar( $value ) || $value === null ) {
200 return true;
203 return false;
207 // @codeCoverageIgnoreStart
208 $maintClass = GetConfiguration::class;
209 require_once RUN_MAINTENANCE_IF_MAIN;
210 // @codeCoverageIgnoreEnd