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
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
34 * Print serialized output of MediaWiki config vars
36 * @ingroup Maintenance
38 class GetConfiguration
extends Maintenance
{
40 /** @var string|null */
41 protected $regex = null;
44 protected $settings_list = [];
47 * List of format output internally supported.
48 * Each item MUST be lower case.
50 private const OUT_FORMATS
= [
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 );
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() {
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" );
84 if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
85 $this->error( "Can only use either --regex or --iregex" );
88 $this->regex
= $this->getOption( 'regex' ) ?
: $this->getOption( 'iregex' );
90 $this->regex
= '/' . $this->regex
. '/';
91 if ( $this->hasOption( 'iregex' ) ) {
92 # case insensitive regex
97 if ( $this->hasOption( 'settings' ) ) {
98 $this->settings_list
= explode( ' ', $this->getOption( 'settings' ) );
100 foreach ( $this->settings_list
as $name ) {
101 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
102 $this->error( "Variable '$name' does start with 'wg'." );
104 } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
105 $this->error( "Variable '$name' is not set." );
107 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
108 $this->error( "Variable '$name' includes non-array, non-scalar, items." );
114 parent
::validateParamsAndArgs();
117 # Force help and quit
118 $this->maybeHelp( true );
122 public function execute() {
123 // Settings we will display
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];
149 switch ( strtolower( $this->getOption( 'format' ) ) ) {
152 $out = serialize( $res );
155 $out = $this->formatVarDump( $res );
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
);
165 $this->fatalError( "Invalid serialization format given." );
167 if ( !is_string( $out ) ) {
168 $this->fatalError( "Failed to serialize the requested settings." );
172 $this->output( $out . "\n" );
176 protected function formatVarDump( $res ) {
178 foreach ( $res as $key => $value ) {
179 # intercept var_dump() output
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 ) ) {
199 } elseif ( is_scalar( $value ) ||
$value === null ) {
207 // @codeCoverageIgnoreStart
208 $maintClass = GetConfiguration
::class;
209 require_once RUN_MAINTENANCE_IF_MAIN
;
210 // @codeCoverageIgnoreEnd