MediaWiki  REL1_22
getConfiguration.php
Go to the documentation of this file.
00001 <?php
00026 require_once __DIR__ . '/Maintenance.php';
00027 
00033 class GetConfiguration extends Maintenance {
00034 
00035     protected $regex = null;
00036 
00037     protected $settings_list = array();
00038 
00043     protected static $outFormats = array(
00044         'json',
00045         'php',
00046         'serialize',
00047         'vardump',
00048     );
00049 
00050     public function __construct() {
00051         parent::__construct();
00052         $this->mDescription = "Get serialized MediaWiki site configuration";
00053         $this->addOption( 'regex', 'regex to filter variables with', false, true );
00054         $this->addOption( 'iregex', 'same as --regex but case insensitive', false, true );
00055         $this->addOption( 'settings', 'Space-separated list of wg* variables', false, true );
00056         $this->addOption( 'format', join( ', ', self::$outFormats ), false, true );
00057     }
00058 
00059     protected function validateParamsAndArgs() {
00060         $error_out = false;
00061 
00062         # Get the format and make sure it is set to a valid default value
00063         $format = strtolower( $this->getOption( 'format', 'PHP' ) );
00064 
00065         $validFormat = in_array( $format, self::$outFormats );
00066         if ( ! $validFormat ) {
00067             $this->error( "--format set to an unrecognized format", 0 );
00068             $error_out = true;
00069         }
00070 
00071         if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
00072             $this->error( "Can only use either --regex or --iregex" );
00073             $error_out = true;
00074         }
00075 
00076         parent::validateParamsAndArgs();
00077 
00078         if ( $error_out ) {
00079             # Force help and quit
00080             $this->maybeHelp( true );
00081         }
00082     }
00083 
00087     public function finalSetup() {
00088         parent::finalSetup();
00089 
00090         $this->regex = $this->getOption( 'regex' ) ? : $this->getOption( 'iregex' );
00091         if ( $this->regex ) {
00092             $this->regex = '/' . $this->regex . '/';
00093             if ( $this->hasOption( 'iregex' ) ) {
00094                 $this->regex .= 'i';  # case insensitive regex
00095             }
00096         }
00097 
00098         if ( $this->hasOption( 'settings' ) ) {
00099             $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
00100             # Values validation
00101             foreach ( $this->settings_list as $name ) {
00102                 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
00103                     throw new MWException( "Variable '$name' does start with 'wg'." );
00104                 } elseif ( !isset( $GLOBALS[$name] ) ) {
00105                     throw new MWException( "Variable '$name' is not set." );
00106                 } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
00107                     throw new MWException( "Variable '$name' includes non-array, non-scalar, items." );
00108                 }
00109             }
00110         }
00111     }
00112 
00113     public function execute() {
00114         // Settings we will display
00115         $res = array();
00116 
00117         # Sane default: dump any wg / wmg variable
00118         if ( ! $this->regex && ! $this->getOption( 'settings' ) ) {
00119             $this->regex = '/^wm?g/';
00120         }
00121 
00122         # Filter out globals based on the regex
00123         if ( $this->regex ) {
00124             $res = array();
00125             foreach ( $GLOBALS as $name => $value ) {
00126                 if ( preg_match( $this->regex, $name ) ) {
00127                     $res[$name] = $value;
00128                 }
00129             }
00130         }
00131 
00132         # Explicitly dumps a list of provided global names
00133         if ( $this->settings_list ) {
00134             foreach ( $this->settings_list as $name ) {
00135                 $res[$name] = $GLOBALS[$name];
00136             }
00137         }
00138 
00139         ksort( $res );
00140 
00141         $out = null;
00142         switch ( strtolower( $this->getOption( 'format' ) ) ) {
00143             case 'serialize':
00144             case 'php':
00145                 $out = serialize( $res );
00146                 break;
00147             case 'vardump':
00148                 $out = $this->formatVarDump( $res );
00149                 break;
00150             case 'json':
00151                 $out = FormatJson::encode( $res );
00152                 break;
00153             default:
00154                 throw new MWException( "Invalid serialization format given." );
00155         }
00156         if ( !is_string( $out ) ) {
00157             throw new MWException( "Failed to serialize the requested settings." );
00158         }
00159 
00160         if ( $out ) {
00161             $this->output( $out . "\n" );
00162         }
00163     }
00164 
00165     protected function formatVarDump( $res ) {
00166         $ret = '';
00167         foreach ( $res as $key => $value ) {
00168             ob_start();  # intercept var_dump() output
00169             print "\${$key} = ";
00170             var_dump( $value );
00171             # grab var_dump() output and discard it from the output buffer
00172             $ret .= trim( ob_get_clean() ) . ";\n";
00173         }
00174 
00175         return trim( $ret, "\n" );
00176     }
00177 
00178     private function isAllowedVariable( $value ) {
00179         if ( is_array( $value ) ) {
00180             foreach ( $value as $k => $v ) {
00181                 if ( !$this->isAllowedVariable( $v ) ) {
00182                     return false;
00183                 }
00184             }
00185             return true;
00186         } elseif ( is_scalar( $value ) ) {
00187             return true;
00188         }
00189         return false;
00190     }
00191 }
00192 
00193 $maintClass = "GetConfiguration";
00194 require_once RUN_MAINTENANCE_IF_MAIN;