Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 89
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetConfiguration
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 5
1560
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 validateParamsAndArgs
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
182
 execute
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
306
 formatVarDump
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 isAllowedVariable
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
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
26use MediaWiki\Json\FormatJson;
27
28require_once __DIR__ . '/Maintenance.php';
29
30/**
31 * Print serialized output of MediaWiki config vars
32 *
33 * @ingroup Maintenance
34 */
35class GetConfiguration extends Maintenance {
36
37    protected $regex = null;
38
39    protected $settings_list = [];
40
41    /**
42     * List of format output internally supported.
43     * Each item MUST be lower case.
44     */
45    protected static $outFormats = [
46        'json',
47        'php',
48        'serialize',
49        'vardump',
50    ];
51
52    public function __construct() {
53        parent::__construct();
54        $this->addDescription( 'Get serialized MediaWiki site configuration' );
55        $this->addOption( 'regex', 'regex to filter variables with', false, true );
56        $this->addOption( 'iregex', 'same as --regex but case insensitive', false, true );
57        $this->addOption( 'settings', 'Space-separated list of wg* variables', false, true );
58        $this->addOption( 'format', implode( ', ', self::$outFormats ), false, true );
59        $this->addOption(
60            'json-partial-output-on-error',
61            'Use JSON_PARTIAL_OUTPUT_ON_ERROR flag with json_encode(). This allows for partial response to ' .
62            'be output in case of an exception while serializing to JSON. If an error occurs, ' .
63            'the wgGetConfigurationJsonErrorOccurred field is set in the output.'
64        );
65    }
66
67    public function validateParamsAndArgs() {
68        $error_out = false;
69
70        # Get the format and make sure it is set to a valid default value
71        $format = strtolower( $this->getOption( 'format', 'PHP' ) );
72
73        $validFormat = in_array( $format, self::$outFormats );
74        if ( !$validFormat ) {
75            $this->error( "--format set to an unrecognized format" );
76            $error_out = true;
77        }
78
79        if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
80            $this->error( "Can only use either --regex or --iregex" );
81            $error_out = true;
82        }
83        $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' );
84        if ( $this->regex ) {
85            $this->regex = '/' . $this->regex . '/';
86            if ( $this->hasOption( 'iregex' ) ) {
87                # case insensitive regex
88                $this->regex .= 'i';
89            }
90        }
91
92        if ( $this->hasOption( 'settings' ) ) {
93            $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
94            # Values validation
95            foreach ( $this->settings_list as $name ) {
96                if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
97                    $this->error( "Variable '$name' does start with 'wg'." );
98                    $error_out = true;
99                } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
100                    $this->error( "Variable '$name' is not set." );
101                    $error_out = true;
102                } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
103                    $this->error( "Variable '$name' includes non-array, non-scalar, items." );
104                    $error_out = true;
105                }
106            }
107        }
108
109        parent::validateParamsAndArgs();
110
111        if ( $error_out ) {
112            # Force help and quit
113            $this->maybeHelp( true );
114        }
115    }
116
117    public function execute() {
118        // Settings we will display
119        $res = [];
120
121        # Default: dump any wg / wmg variable
122        if ( !$this->regex && !$this->getOption( 'settings' ) ) {
123            $this->regex = '/^wm?g/';
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 ( strtolower( $this->getOption( '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( $res ) {
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    private function isAllowedVariable( $value ) {
186        if ( is_array( $value ) ) {
187            foreach ( $value as $v ) {
188                if ( !$this->isAllowedVariable( $v ) ) {
189                    return false;
190                }
191            }
192
193            return true;
194        } elseif ( is_scalar( $value ) || $value === null ) {
195            return true;
196        }
197
198        return false;
199    }
200}
201
202$maintClass = GetConfiguration::class;
203require_once RUN_MAINTENANCE_IF_MAIN;