getConfiguration.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. require_once __DIR__ . '/Maintenance.php';
  26. /**
  27. * Print serialized output of MediaWiki config vars
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class GetConfiguration extends Maintenance {
  32. protected $regex = null;
  33. protected $settings_list = [];
  34. /**
  35. * List of format output internally supported.
  36. * Each item MUST be lower case.
  37. */
  38. protected static $outFormats = [
  39. 'json',
  40. 'php',
  41. 'serialize',
  42. 'vardump',
  43. ];
  44. public function __construct() {
  45. parent::__construct();
  46. $this->addDescription( 'Get serialized MediaWiki site configuration' );
  47. $this->addOption( 'regex', 'regex to filter variables with', false, true );
  48. $this->addOption( 'iregex', 'same as --regex but case insensitive', false, true );
  49. $this->addOption( 'settings', 'Space-separated list of wg* variables', false, true );
  50. $this->addOption( 'format', implode( ', ', self::$outFormats ), false, true );
  51. }
  52. protected function validateParamsAndArgs() {
  53. $error_out = false;
  54. # Get the format and make sure it is set to a valid default value
  55. $format = strtolower( $this->getOption( 'format', 'PHP' ) );
  56. $validFormat = in_array( $format, self::$outFormats );
  57. if ( !$validFormat ) {
  58. $this->error( "--format set to an unrecognized format" );
  59. $error_out = true;
  60. }
  61. if ( $this->getOption( 'regex' ) && $this->getOption( 'iregex' ) ) {
  62. $this->error( "Can only use either --regex or --iregex" );
  63. $error_out = true;
  64. }
  65. parent::validateParamsAndArgs();
  66. if ( $error_out ) {
  67. # Force help and quit
  68. $this->maybeHelp( true );
  69. }
  70. }
  71. /**
  72. * finalSetup() since we need MWException
  73. */
  74. public function finalSetup() {
  75. parent::finalSetup();
  76. $this->regex = $this->getOption( 'regex' ) ?: $this->getOption( 'iregex' );
  77. if ( $this->regex ) {
  78. $this->regex = '/' . $this->regex . '/';
  79. if ( $this->hasOption( 'iregex' ) ) {
  80. $this->regex .= 'i'; # case insensitive regex
  81. }
  82. }
  83. if ( $this->hasOption( 'settings' ) ) {
  84. $this->settings_list = explode( ' ', $this->getOption( 'settings' ) );
  85. # Values validation
  86. foreach ( $this->settings_list as $name ) {
  87. if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
  88. throw new MWException( "Variable '$name' does start with 'wg'." );
  89. } elseif ( !array_key_exists( $name, $GLOBALS ) ) {
  90. throw new MWException( "Variable '$name' is not set." );
  91. } elseif ( !$this->isAllowedVariable( $GLOBALS[$name] ) ) {
  92. throw new MWException( "Variable '$name' includes non-array, non-scalar, items." );
  93. }
  94. }
  95. }
  96. }
  97. public function execute() {
  98. // Settings we will display
  99. $res = [];
  100. # Sane default: dump any wg / wmg variable
  101. if ( !$this->regex && !$this->getOption( 'settings' ) ) {
  102. $this->regex = '/^wm?g/';
  103. }
  104. # Filter out globals based on the regex
  105. if ( $this->regex ) {
  106. $res = [];
  107. foreach ( $GLOBALS as $name => $value ) {
  108. if ( preg_match( $this->regex, $name ) ) {
  109. $res[$name] = $value;
  110. }
  111. }
  112. }
  113. # Explicitly dumps a list of provided global names
  114. if ( $this->settings_list ) {
  115. foreach ( $this->settings_list as $name ) {
  116. $res[$name] = $GLOBALS[$name];
  117. }
  118. }
  119. ksort( $res );
  120. $out = null;
  121. switch ( strtolower( $this->getOption( 'format' ) ) ) {
  122. case 'serialize':
  123. case 'php':
  124. $out = serialize( $res );
  125. break;
  126. case 'vardump':
  127. $out = $this->formatVarDump( $res );
  128. break;
  129. case 'json':
  130. $out = FormatJson::encode( $res );
  131. break;
  132. default:
  133. throw new MWException( "Invalid serialization format given." );
  134. }
  135. if ( !is_string( $out ) ) {
  136. throw new MWException( "Failed to serialize the requested settings." );
  137. }
  138. if ( $out ) {
  139. $this->output( $out . "\n" );
  140. }
  141. }
  142. protected function formatVarDump( $res ) {
  143. $ret = '';
  144. foreach ( $res as $key => $value ) {
  145. ob_start(); # intercept var_dump() output
  146. print "\${$key} = ";
  147. var_dump( $value );
  148. # grab var_dump() output and discard it from the output buffer
  149. $ret .= trim( ob_get_clean() ) . ";\n";
  150. }
  151. return trim( $ret, "\n" );
  152. }
  153. private function isAllowedVariable( $value ) {
  154. if ( is_array( $value ) ) {
  155. foreach ( $value as $k => $v ) {
  156. if ( !$this->isAllowedVariable( $v ) ) {
  157. return false;
  158. }
  159. }
  160. return true;
  161. } elseif ( is_scalar( $value ) || $value === null ) {
  162. return true;
  163. }
  164. return false;
  165. }
  166. }
  167. $maintClass = GetConfiguration::class;
  168. require_once RUN_MAINTENANCE_IF_MAIN;