Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.84% covered (success)
98.84%
85 / 86
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetConfiguration
98.84% covered (success)
98.84%
85 / 86
80.00% covered (warning)
80.00%
4 / 5
39
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 validateParamsAndArgs
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
13
 execute
96.67% covered (success)
96.67%
29 / 30
0.00% covered (danger)
0.00%
0 / 1
17
 formatVarDump
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 isAllowedVariable
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
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
26use MediaWiki\Json\FormatJson;
27use MediaWiki\Maintenance\Maintenance;
28
29// @codeCoverageIgnoreStart
30require_once __DIR__ . '/Maintenance.php';
31// @codeCoverageIgnoreEnd
32
33/**
34 * Print serialized output of MediaWiki config vars
35 *
36 * @ingroup Maintenance
37 */
38class GetConfiguration extends Maintenance {
39
40    /** @var string|null */
41    protected $regex = null;
42
43    /** @var array */
44    protected $settings_list = [];
45
46    /**
47     * List of format output internally supported.
48     * Each item MUST be lower case.
49     */
50    private const OUT_FORMATS = [
51        'json',
52        'php',
53        'serialize',
54        'vardump',
55    ];
56
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.'
69        );
70    }
71
72    public function validateParamsAndArgs() {
73        $error_out = false;
74
75        # Get the format and make sure it is set to a valid default value
76        $format = strtolower( $this->getOption( 'format', 'PHP' ) );
77
78        $validFormat = in_array( $format, self::OUT_FORMATS );
79        if ( !$validFormat ) {
80            $this->error( "--format set to an unrecognized format" );
81            $error_out = true;
82        }
83
84        if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
85            $this->error( "Can only use either --regex or --iregex" );
86            $error_out = true;
87        }
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';
94            }
95        }
96
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;
110                }
111            }
112        }
113
114        parent::validateParamsAndArgs();
115
116        if ( $error_out ) {
117            # Force help and quit
118            $this->maybeHelp( true );
119        }
120    }
121
122    public function execute() {
123        // Settings we will display
124        $res = [];
125
126        # Default: dump any wg / wmg variable
127        if ( !$this->regex && !$this->getOption( 'settings' ) ) {
128            $this->regex = '/^wm?g/';
129        }
130
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;
136                }
137            }
138        }
139
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];
144            }
145        }
146
147        ksort( $res );
148
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 );
162                }
163                break;
164            default:
165                $this->fatalError( "Invalid serialization format given." );
166        }
167        if ( !is_string( $out ) ) {
168            $this->fatalError( "Failed to serialize the requested settings." );
169        }
170
171        if ( $out ) {
172            $this->output( $out . "\n" );
173        }
174    }
175
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";
185        }
186
187        return trim( $ret, "\n" );
188    }
189
190    private function isAllowedVariable( $value ) {
191        if ( is_array( $value ) ) {
192            foreach ( $value as $v ) {
193                if ( !$this->isAllowedVariable( $v ) ) {
194                    return false;
195                }
196            }
197
198            return true;
199        } elseif ( is_scalar( $value ) || $value === null ) {
200            return true;
201        }
202
203        return false;
204    }
205}
206
207// @codeCoverageIgnoreStart
208$maintClass = GetConfiguration::class;
209require_once RUN_MAINTENANCE_IF_MAIN;
210// @codeCoverageIgnoreEnd