[JobQueue] Use target wiki configuration for some key functions.
[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
24 */
25
26 require_once( __DIR__ . '/Maintenance.php' );
27
28 /**
29 * Print serialized output of MediaWiki config vars
30 *
31 * @ingroup Maintenance
32 */
33 class GetConfiguration extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Get serialized MediaWiki site configuration";
37 $this->addOption( 'settings', 'Space-separated list of wg* variables', true, true );
38 $this->addOption( 'format', 'PHP or JSON', true, true );
39 $this->addOption( 'wiki', 'Wiki ID', true, true );
40 }
41
42 public function execute() {
43 $res = array();
44 foreach ( explode( ' ', $this->getOption( 'settings' ) ) as $name ) {
45 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
46 throw new MWException( "Variable '$name' does start with 'wg'." );
47 } elseif ( !isset( $GLOBALS[$name] ) ) {
48 throw new MWException( "Variable '$name' is not set." );
49 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
50 throw new MWException( "Variable '$name' includes non-array, non-scalar, items." );
51 }
52 $res[$name] = $GLOBALS[$name];
53 }
54
55 $out = null;
56 switch( $this->getOption( 'format' ) ) {
57 case 'PHP':
58 $out = serialize( $res );
59 break;
60 case 'JSON':
61 $out = FormatJson::encode( $res );
62 break;
63 default:
64 throw new MWException( "Invalid serialization format given." );
65 }
66 if ( !is_string( $out ) ) {
67 throw new MWException( "Failed to serialize the requested settings." );
68 }
69
70 $this->output( $out . "\n" );
71 }
72
73 private function isAllowedVariable( $value ) {
74 if ( is_array( $value ) ) {
75 foreach ( $value as $k => $v ) {
76 if ( !$this->isAllowedVariable( $v ) ) {
77 return false;
78 }
79 }
80 return true;
81 } elseif ( is_scalar( $value ) ) {
82 return true;
83 }
84 return false;
85 }
86 }
87
88 $maintClass = "GetConfiguration";
89 require_once( RUN_MAINTENANCE_IF_MAIN );