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 require_once __DIR__
. '/Maintenance.php';
29 * Print serialized output of MediaWiki config vars
31 * @ingroup Maintenance
33 class GetConfiguration
extends Maintenance
{
35 protected $regex = null;
37 protected $settings_list = [];
40 * List of format output internally supported.
41 * Each item MUST be lower case.
43 protected static $outFormats = [
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 );
59 public function validateParamsAndArgs() {
62 # Get the format and make sure it is set to a valid default value
63 $format = strtolower( $this->getOption( 'format', 'PHP' ) );
65 $validFormat = in_array( $format, self
::$outFormats );
66 if ( !$validFormat ) {
67 $this->error( "--format set to an unrecognized format" );
71 if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
72 $this->error( "Can only use either --regex or --iregex" );
76 parent
::validateParamsAndArgs();
80 $this->maybeHelp( true );
85 * finalSetup() since we need MWException
87 public function finalSetup() {
90 $this->regex
= $this->getOption( 'regex' ) ?
: $this->getOption( 'iregex' );
92 $this->regex
= '/' . $this->regex
. '/';
93 if ( $this->hasOption( 'iregex' ) ) {
94 # case insensitive regex
99 if ( $this->hasOption( 'settings' ) ) {
100 $this->settings_list
= explode( ' ', $this->getOption( 'settings' ) );
102 foreach ( $this->settings_list
as $name ) {
103 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
104 throw new MWException( "Variable '$name' does start with 'wg'." );
105 } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
106 throw new MWException( "Variable '$name' is not set." );
107 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
108 throw new MWException( "Variable '$name' includes non-array, non-scalar, items." );
114 public function execute() {
115 // Settings we will display
118 # Sane default: dump any wg / wmg variable
119 if ( !$this->regex
&& !$this->getOption( 'settings' ) ) {
120 $this->regex
= '/^wm?g/';
123 # Filter out globals based on the regex
124 if ( $this->regex
) {
126 foreach ( $GLOBALS as $name => $value ) {
127 if ( preg_match( $this->regex
, $name ) ) {
128 $res[$name] = $value;
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];
143 switch ( strtolower( $this->getOption( 'format' ) ) ) {
146 $out = serialize( $res );
149 $out = $this->formatVarDump( $res );
152 $out = FormatJson
::encode( $res );
155 throw new MWException( "Invalid serialization format given." );
157 if ( !is_string( $out ) ) {
158 throw new MWException( "Failed to serialize the requested settings." );
162 $this->output( $out . "\n" );
166 protected function formatVarDump( $res ) {
168 foreach ( $res as $key => $value ) {
169 # intercept var_dump() output
173 # grab var_dump() output and discard it from the output buffer
174 $ret .= trim( ob_get_clean() ) . ";\n";
177 return trim( $ret, "\n" );
180 private function isAllowedVariable( $value ) {
181 if ( is_array( $value ) ) {
182 foreach ( $value as $k => $v ) {
183 if ( !$this->isAllowedVariable( $v ) ) {
189 } elseif ( is_scalar( $value ) ||
$value === null ) {
197 $maintClass = GetConfiguration
::class;
198 require_once RUN_MAINTENANCE_IF_MAIN
;