MediaWiki
REL1_21
|
00001 <?php 00026 require_once( __DIR__ . '/Maintenance.php' ); 00027 00033 class GetConfiguration extends Maintenance { 00034 public function __construct() { 00035 parent::__construct(); 00036 $this->mDescription = "Get serialized MediaWiki site configuration"; 00037 $this->addOption( 'settings', 'Space-separated list of wg* variables', true, true ); 00038 $this->addOption( 'format', 'PHP or JSON', true, true ); 00039 $this->addOption( 'wiki', 'Wiki ID', true, true ); 00040 } 00041 00042 public function execute() { 00043 $res = array(); 00044 foreach ( explode( ' ', $this->getOption( 'settings' ) ) as $name ) { 00045 if ( !preg_match( '/^wg[A-Z]/', $name ) ) { 00046 throw new MWException( "Variable '$name' does start with 'wg'." ); 00047 } elseif ( !isset( $GLOBALS[$name] ) ) { 00048 throw new MWException( "Variable '$name' is not set." ); 00049 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) { 00050 throw new MWException( "Variable '$name' includes non-array, non-scalar, items." ); 00051 } 00052 $res[$name] = $GLOBALS[$name]; 00053 } 00054 00055 $out = null; 00056 switch( $this->getOption( 'format' ) ) { 00057 case 'PHP': 00058 $out = serialize( $res ); 00059 break; 00060 case 'JSON': 00061 $out = FormatJson::encode( $res ); 00062 break; 00063 default: 00064 throw new MWException( "Invalid serialization format given." ); 00065 } 00066 if ( !is_string( $out ) ) { 00067 throw new MWException( "Failed to serialize the requested settings." ); 00068 } 00069 00070 $this->output( $out . "\n" ); 00071 } 00072 00073 private function isAllowedVariable( $value ) { 00074 if ( is_array( $value ) ) { 00075 foreach ( $value as $k => $v ) { 00076 if ( !$this->isAllowedVariable( $v ) ) { 00077 return false; 00078 } 00079 } 00080 return true; 00081 } elseif ( is_scalar( $value ) ) { 00082 return true; 00083 } 00084 return false; 00085 } 00086 } 00087 00088 $maintClass = "GetConfiguration"; 00089 require_once( RUN_MAINTENANCE_IF_MAIN );