Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 89 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
GetConfiguration | |
0.00% |
0 / 86 |
|
0.00% |
0 / 5 |
1560 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
validateParamsAndArgs | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
182 | |||
execute | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
306 | |||
formatVarDump | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
isAllowedVariable | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
42 |
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 <hashar@free.fr> |
24 | */ |
25 | |
26 | require_once __DIR__ . '/Maintenance.php'; |
27 | |
28 | /** |
29 | * Print serialized output of MediaWiki config vars |
30 | * |
31 | * @ingroup Maintenance |
32 | */ |
33 | class GetConfiguration extends Maintenance { |
34 | |
35 | protected $regex = null; |
36 | |
37 | protected $settings_list = []; |
38 | |
39 | /** |
40 | * List of format output internally supported. |
41 | * Each item MUST be lower case. |
42 | */ |
43 | protected static $outFormats = [ |
44 | 'json', |
45 | 'php', |
46 | 'serialize', |
47 | 'vardump', |
48 | ]; |
49 | |
50 | public function __construct() { |
51 | parent::__construct(); |
52 | $this->addDescription( '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', implode( ', ', self::$outFormats ), false, true ); |
57 | $this->addOption( |
58 | 'json-partial-output-on-error', |
59 | 'Use JSON_PARTIAL_OUTPUT_ON_ERROR flag with json_encode(). This allows for partial response to ' . |
60 | 'be output in case of an exception while serializing to JSON. If an error occurs, ' . |
61 | 'the wgGetConfigurationJsonErrorOccurred field is set in the output.' |
62 | ); |
63 | } |
64 | |
65 | public function validateParamsAndArgs() { |
66 | $error_out = false; |
67 | |
68 | # Get the format and make sure it is set to a valid default value |
69 | $format = strtolower( $this->getOption( 'format', 'PHP' ) ); |
70 | |
71 | $validFormat = in_array( $format, self::$outFormats ); |
72 | if ( !$validFormat ) { |
73 | $this->error( "--format set to an unrecognized format" ); |
74 | $error_out = true; |
75 | } |
76 | |
77 | if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) { |
78 | $this->error( "Can only use either --regex or --iregex" ); |
79 | $error_out = true; |
80 | } |
81 | $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' ); |
82 | if ( $this->regex ) { |
83 | $this->regex = '/' . $this->regex . '/'; |
84 | if ( $this->hasOption( 'iregex' ) ) { |
85 | # case insensitive regex |
86 | $this->regex .= 'i'; |
87 | } |
88 | } |
89 | |
90 | if ( $this->hasOption( 'settings' ) ) { |
91 | $this->settings_list = explode( ' ', $this->getOption( 'settings' ) ); |
92 | # Values validation |
93 | foreach ( $this->settings_list as $name ) { |
94 | if ( !preg_match( '/^wg[A-Z]/', $name ) ) { |
95 | $this->error( "Variable '$name' does start with 'wg'." ); |
96 | $error_out = true; |
97 | } elseif ( !array_key_exists( $name, $GLOBALS ) ) { |
98 | $this->error( "Variable '$name' is not set." ); |
99 | $error_out = true; |
100 | } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) { |
101 | $this->error( "Variable '$name' includes non-array, non-scalar, items." ); |
102 | $error_out = true; |
103 | } |
104 | } |
105 | } |
106 | |
107 | parent::validateParamsAndArgs(); |
108 | |
109 | if ( $error_out ) { |
110 | # Force help and quit |
111 | $this->maybeHelp( true ); |
112 | } |
113 | } |
114 | |
115 | public function execute() { |
116 | // Settings we will display |
117 | $res = []; |
118 | |
119 | # Default: dump any wg / wmg variable |
120 | if ( !$this->regex && !$this->getOption( 'settings' ) ) { |
121 | $this->regex = '/^wm?g/'; |
122 | } |
123 | |
124 | # Filter out globals based on the regex |
125 | if ( $this->regex ) { |
126 | foreach ( $GLOBALS as $name => $value ) { |
127 | if ( preg_match( $this->regex, $name ) ) { |
128 | $res[$name] = $value; |
129 | } |
130 | } |
131 | } |
132 | |
133 | # Explicitly dumps a list of provided global names |
134 | if ( $this->settings_list ) { |
135 | foreach ( $this->settings_list as $name ) { |
136 | $res[$name] = $GLOBALS[$name]; |
137 | } |
138 | } |
139 | |
140 | ksort( $res ); |
141 | |
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': |
151 | $out = FormatJson::encode( $res ); |
152 | if ( !$out && $this->getOption( 'json-partial-output-on-error' ) ) { |
153 | $res['wgGetConfigurationJsonErrorOccurred'] = true; |
154 | $out = json_encode( $res, JSON_PARTIAL_OUTPUT_ON_ERROR ); |
155 | } |
156 | break; |
157 | default: |
158 | $this->fatalError( "Invalid serialization format given." ); |
159 | } |
160 | if ( !is_string( $out ) ) { |
161 | $this->fatalError( "Failed to serialize the requested settings." ); |
162 | } |
163 | |
164 | if ( $out ) { |
165 | $this->output( $out . "\n" ); |
166 | } |
167 | } |
168 | |
169 | protected function formatVarDump( $res ) { |
170 | $ret = ''; |
171 | foreach ( $res as $key => $value ) { |
172 | # intercept var_dump() output |
173 | ob_start(); |
174 | print "\${$key} = "; |
175 | var_dump( $value ); |
176 | # grab var_dump() output and discard it from the output buffer |
177 | $ret .= trim( ob_get_clean() ) . ";\n"; |
178 | } |
179 | |
180 | return trim( $ret, "\n" ); |
181 | } |
182 | |
183 | private function isAllowedVariable( $value ) { |
184 | if ( is_array( $value ) ) { |
185 | foreach ( $value as $v ) { |
186 | if ( !$this->isAllowedVariable( $v ) ) { |
187 | return false; |
188 | } |
189 | } |
190 | |
191 | return true; |
192 | } elseif ( is_scalar( $value ) || $value === null ) { |
193 | return true; |
194 | } |
195 | |
196 | return false; |
197 | } |
198 | } |
199 | |
200 | $maintClass = GetConfiguration::class; |
201 | require_once RUN_MAINTENANCE_IF_MAIN; |