Improve OutputPage::showErrorPage method documentation
[lhc/web/wiklou.git] / maintenance / getConfiguration.php
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 define( 'MW_SETUP_NO_CACHE', 1 );
27 define( 'MW_SETUP_NO_CONTEXT', 1 );
28 require_once __DIR__ . '/Maintenance.php';
29
30 /**
31 * Print serialized output of MediaWiki config vars
32 *
33 * @ingroup Maintenance
34 */
35 class GetConfiguration extends Maintenance {
36
37 protected $regex = null;
38
39 protected $settings_list = array();
40
41 /**
42 * List of format output internally supported.
43 * Each item MUST be lower case.
44 */
45 protected static $outFormats = array(
46 'json',
47 'php',
48 'serialize',
49 'vardump',
50 );
51
52 public function __construct() {
53 parent::__construct();
54 $this->mDescription = "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', join( ', ', self::$outFormats ), false, true );
59 }
60
61 protected function validateParamsAndArgs() {
62 $error_out = false;
63
64 # Get the format and make sure it is set to a valid default value
65 $format = strtolower( $this->getOption( 'format', 'PHP' ) );
66
67 $validFormat = in_array( $format, self::$outFormats );
68 if ( ! $validFormat ) {
69 $this->error( "--format set to an unrecognized format", 0 );
70 $error_out = true;
71 }
72
73 if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
74 $this->error( "Can only use either --regex or --iregex" );
75 $error_out = true;
76 }
77
78 parent::validateParamsAndArgs();
79
80 if ( $error_out ) {
81 # Force help and quit
82 $this->maybeHelp( true );
83 }
84 }
85
86 /**
87 * finalSetup() since we need MWException
88 */
89 public function finalSetup() {
90 parent::finalSetup();
91
92 $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' );
93 if ( $this->regex ) {
94 $this->regex = '/' . $this->regex . '/';
95 if ( $this->hasOption( 'iregex' ) ) {
96 $this->regex .= 'i'; # case insensitive regex
97 }
98 }
99
100 if ( $this->hasOption( 'settings' ) ) {
101 $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
102 # Values validation
103 foreach ( $this->settings_list as $name ) {
104 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
105 throw new MWException( "Variable '$name' does start with 'wg'." );
106 } elseif ( !isset( $GLOBALS[$name] ) ) {
107 throw new MWException( "Variable '$name' is not set." );
108 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
109 throw new MWException( "Variable '$name' includes non-array, non-scalar, items." );
110 }
111 }
112 }
113 }
114
115 public function execute() {
116 // Settings we will display
117 $res = array();
118
119 # Sane 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 $res = array();
127 foreach ( $GLOBALS as $name => $value ) {
128 if ( preg_match( $this->regex, $name ) ) {
129 $res[$name] = $value;
130 }
131 }
132 }
133
134 # Explicitly dumps a list of provided global names
135 if ( $this->settings_list ) {
136 foreach ( $this->settings_list as $name ) {
137 $res[$name] = $GLOBALS[$name];
138 }
139 }
140
141 ksort( $res );
142
143 $out = null;
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 break;
155 default:
156 throw new MWException( "Invalid serialization format given." );
157 }
158 if ( !is_string( $out ) ) {
159 throw new MWException( "Failed to serialize the requested settings." );
160 }
161
162 if ( $out ) {
163 $this->output( $out . "\n" );
164 }
165 }
166
167 protected function formatVarDump( $res ) {
168 $ret = '';
169 foreach ( $res as $key => $value ) {
170 ob_start(); # intercept var_dump() output
171 print "\${$key} = ";
172 var_dump( $value );
173 # grab var_dump() output and discard it from the output buffer
174 $ret .= trim( ob_get_clean() ) . ";\n";
175 }
176
177 return trim( $ret, "\n" );
178 }
179
180 private function isAllowedVariable( $value ) {
181 if ( is_array( $value ) ) {
182 foreach ( $value as $k => $v ) {
183 if ( !$this->isAllowedVariable( $v ) ) {
184 return false;
185 }
186 }
187 return true;
188 } elseif ( is_scalar( $value ) ) {
189 return true;
190 }
191 return false;
192 }
193 }
194
195 $maintClass = "GetConfiguration";
196 require_once RUN_MAINTENANCE_IF_MAIN;