Debian Codegraph @

maintenance/getConfiguration.php

raw symbols SCIP
1 <?php
2 /**
3 * Print serialized output of MediaWiki config vars.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Tim Starling
23 * @author Antoine Musso <[email protected]>
24 */
25
26 use MediaWiki\Json\FormatJson;
27
28 // @codeCoverageIgnoreStart
29 require_once __DIR__ . '/Maintenance.php';
30 // @codeCoverageIgnoreEnd
31
32 /**
33 * Print serialized output of MediaWiki config vars
34 *
35 * @ingroup Maintenance
36 */
37 class GetConfiguration extends Maintenance {
38
39 /** @var string|null */
40 protected $regex = null;
41
42 /** @var string|null */
43 protected $format = null;
44
45 /** @var array */
46 protected $settings_list = [];
47
48 /**
49 * List of format output internally supported.
50 * Each item MUST be lower case.
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 # Get the format and make sure it is set to a valid default value
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 # 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 $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 # Force help and quit
120 $this->maybeHelp( true );
121 }
122 }
123
124 public function execute() {
125 // Settings we will display
126 $res = [];
127
128 # Default: dump any wg / wmg variable
129 if ( !$this->regex && !$this->getOption( 'settings' ) ) {
130 // Avoid fatal "Exception: Serialization of Closure is not allowed"
131 //
132 // * Exclude legacy singletons that are not configuration but
133 // non-serializable objects, such as $wgUser.
134 // * Exclude config arrays such as wgHooks which may contain closures
135 // via LocalSettings.php.
136 $this->regex = '/^wm?g(?!User|Out|Request|Hooks).*$/';
137 }
138
139 # Filter out globals based on the regex
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 # Explicitly dumps a list of provided global names
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 # intercept var_dump() output
188 ob_start();
189 print "\${$key} = ";
190 var_dump( $value );
191 # grab var_dump() output and discard it from the output buffer
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 // @codeCoverageIgnoreStart
216 $maintClass = GetConfiguration::class;
217 require_once RUN_MAINTENANCE_IF_MAIN;
218 // @codeCoverageIgnoreEnd