PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mediawiki-integration/source/php/mediawiki/maintenance/commandLine.inc

https://code.google.com/
PHP | 245 lines | 195 code | 24 blank | 26 comment | 36 complexity | bc3102d4c160be16cade12179c1a9b6e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * @todo document
  4. * @package MediaWiki
  5. * @subpackage Maintenance
  6. */
  7. $wgRequestTime = microtime(true);
  8. /** */
  9. # Abort if called from a web server
  10. if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
  11. print "This script must be run from the command line\n";
  12. exit();
  13. }
  14. if( version_compare( PHP_VERSION, '5.0.0' ) < 0 ) {
  15. print "Sorry! This version of MediaWiki requires PHP 5; you are running " .
  16. PHP_VERSION . ".\n\n" .
  17. "If you are sure you already have PHP 5 installed, it may be " .
  18. "installed\n" .
  19. "in a different path from PHP 4. Check with your system administrator.\n";
  20. die( -1 );
  21. }
  22. define('MEDIAWIKI',true);
  23. # Process command line arguments
  24. # $options becomes an array with keys set to the option names
  25. # $optionsWithArgs is an array of GNU-style options that take an argument. The arguments are returned
  26. # in the values of $options.
  27. # $args becomes a zero-based array containing the non-option arguments
  28. if ( !isset( $optionsWithArgs ) ) {
  29. $optionsWithArgs = array();
  30. }
  31. $optionsWithArgs[] = 'conf'; # For specifying the location of LocalSettings.php
  32. $self = array_shift( $argv );
  33. $IP = realpath( dirname( __FILE__ ) . '/..' );
  34. #chdir( $IP );
  35. require_once( "$IP/StartProfiler.php" );
  36. $options = array();
  37. $args = array();
  38. # Parse arguments
  39. for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
  40. if ( $arg == '--' ) {
  41. # End of options, remainder should be considered arguments
  42. $arg = next( $argv );
  43. while( $arg !== false ) {
  44. $args[] = $arg;
  45. $arg = next( $argv );
  46. }
  47. break;
  48. } elseif ( substr( $arg, 0, 2 ) == '--' ) {
  49. # Long options
  50. $option = substr( $arg, 2 );
  51. if ( in_array( $option, $optionsWithArgs ) ) {
  52. $param = next( $argv );
  53. if ( $param === false ) {
  54. echo "$arg needs an value after it\n";
  55. die( -1 );
  56. }
  57. $options[$option] = $param;
  58. } else {
  59. $bits = explode( '=', $option, 2 );
  60. if( count( $bits ) > 1 ) {
  61. $option = $bits[0];
  62. $param = $bits[1];
  63. } else {
  64. $param = 1;
  65. }
  66. $options[$option] = $param;
  67. }
  68. } elseif ( substr( $arg, 0, 1 ) == '-' ) {
  69. # Short options
  70. for ( $p=1; $p<strlen( $arg ); $p++ ) {
  71. $option = $arg{$p};
  72. if ( in_array( $option, $optionsWithArgs ) ) {
  73. $param = next( $argv );
  74. if ( $param === false ) {
  75. echo "$arg needs an value after it\n";
  76. die( -1 );
  77. }
  78. $options[$option] = $param;
  79. } else {
  80. $options[$option] = 1;
  81. }
  82. }
  83. } else {
  84. $args[] = $arg;
  85. }
  86. }
  87. # General initialisation
  88. $wgCommandLineMode = true;
  89. # Turn off output buffering if it's on
  90. @ob_end_flush();
  91. $sep = PATH_SEPARATOR;
  92. if (!isset( $wgUseNormalUser ) ) {
  93. $wgUseNormalUser = false;
  94. }
  95. if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
  96. $wgWikiFarm = true;
  97. $cluster = trim( file_get_contents( '/etc/cluster' ) );
  98. require_once( "$IP/includes/SiteConfiguration.php" );
  99. # Get $wgConf
  100. require( "$IP/wgConf.php" );
  101. if ( empty( $wgNoDBParam ) ) {
  102. # Check if we were passed a db name
  103. $db = array_shift( $args );
  104. list( $site, $lang ) = $wgConf->siteFromDB( $db );
  105. # If not, work out the language and site the old way
  106. if ( is_null( $site ) || is_null( $lang ) ) {
  107. if ( !$db ) {
  108. $lang = 'aa';
  109. } else {
  110. $lang = $db;
  111. }
  112. if ( isset( $args[0] ) ) {
  113. $site = array_shift( $args );
  114. } else {
  115. $site = 'wikipedia';
  116. }
  117. }
  118. } else {
  119. $lang = 'aa';
  120. $site = 'wikipedia';
  121. }
  122. # This is for the IRC scripts, which now run as the apache user
  123. # The apache user doesn't have access to the wikiadmin_pass command
  124. if ( $_ENV['USER'] == 'apache' ) {
  125. #if ( posix_geteuid() == 48 ) {
  126. $wgUseNormalUser = true;
  127. }
  128. putenv( 'wikilang='.$lang);
  129. $DP = $IP;
  130. ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
  131. #require_once( $IP.'/includes/ProfilerStub.php' );
  132. require_once( $IP.'/includes/Defines.php' );
  133. require_once( $IP.'/CommonSettings.php' );
  134. $bin = '/home/wikipedia/bin';
  135. if ( $wgUseRootUser ) {
  136. $wgDBuser = $wgDBadminuser = 'root';
  137. $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
  138. } elseif ( !$wgUseNormalUser ) {
  139. $wgDBuser = $wgDBadminuser = 'wikiadmin';
  140. $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
  141. }
  142. } else {
  143. $wgWikiFarm = false;
  144. if ( isset( $options['conf'] ) ) {
  145. $settingsFile = $options['conf'];
  146. } else {
  147. $settingsFile = "$IP/LocalSettings.php";
  148. }
  149. if ( ! is_readable( $settingsFile ) ) {
  150. print "A copy of your installation's LocalSettings.php\n" .
  151. "must exist in the source directory.\n";
  152. exit( 1 );
  153. }
  154. $wgCommandLineMode = true;
  155. $DP = $IP;
  156. #require_once( $IP.'/includes/ProfilerStub.php' );
  157. require_once( $IP.'/includes/Defines.php' );
  158. require_once( $settingsFile );
  159. ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
  160. if ( is_readable( $IP.'/AdminSettings.php' ) ) {
  161. require_once( $IP.'/AdminSettings.php' );
  162. }
  163. }
  164. # Turn off output buffering again, it might have been turned on in the settings files
  165. if( ob_get_level() ) {
  166. ob_end_flush();
  167. }
  168. # Same with these
  169. $wgCommandLineMode = true;
  170. if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
  171. $wgDBuser = $wgDBadminuser;
  172. $wgDBpassword = $wgDBadminpassword;
  173. if( $wgDBservers ) {
  174. foreach ( $wgDBservers as $i => $server ) {
  175. $wgDBservers[$i]['user'] = $wgDBuser;
  176. $wgDBservers[$i]['password'] = $wgDBpassword;
  177. }
  178. }
  179. }
  180. if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
  181. $fn = MW_CMDLINE_CALLBACK;
  182. $fn();
  183. }
  184. ini_set( 'memory_limit', -1 );
  185. $wgShowSQLErrors = true;
  186. require_once( 'Setup.php' );
  187. require_once( 'install-utils.inc' );
  188. $wgTitle = null; # Much much faster startup than creating a title object
  189. set_time_limit(0);
  190. // --------------------------------------------------------------------
  191. // Functions
  192. // --------------------------------------------------------------------
  193. function wfWaitForSlaves( $maxLag ) {
  194. global $wgLoadBalancer;
  195. if ( $maxLag ) {
  196. list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
  197. while ( $lag > $maxLag ) {
  198. $name = @gethostbyaddr( $host );
  199. if ( $name !== false ) {
  200. $host = $name;
  201. }
  202. print "Waiting for $host (lagged $lag seconds)...\n";
  203. sleep($maxLag);
  204. list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
  205. }
  206. }
  207. }
  208. ?>