5 * @subpackage Maintenance
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";
15 define('MEDIAWIKI',true);
17 # Process command line arguments
18 # $options becomes an array with keys set to the option names
19 # $optionsWithArgs is an array of GNU-style options that take an argument. The arguments are returned
20 # in the values of $options.
21 # $args becomes a zero-based array containing the non-option arguments
23 if ( !isset( $optionsWithArgs ) ) {
24 $optionsWithArgs = array();
26 $optionsWithArgs[] = 'conf'; # For specifying the location of LocalSettings.php
28 $self = array_shift( $argv );
30 $IP = realpath( dirname( $self ) . '/..' );
39 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
40 if ( substr( $arg, 0, 2 ) == '--' ) {
42 $option = substr( $arg, 2 );
43 if ( in_array( $option, $optionsWithArgs ) ) {
44 $param = next( $argv );
45 if ( $param === false ) {
46 echo "$arg needs an value after it\n";
49 $options[$option] = $param;
51 $bits = explode( '=', $option, 2 );
52 if( count( $bits ) > 1 ) {
58 $options[$option] = $param;
60 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
62 for ( $p=1; $p<strlen( $arg ); $p++ ) {
64 if ( in_array( $option, $optionsWithArgs ) ) {
65 $param = next( $argv );
66 if ( $param === false ) {
67 echo "$arg needs an value after it\n";
70 $options[$option] = $param;
72 $options[$option] = 1;
81 # General initialisation
83 $wgCommandLineMode = true;
84 # Turn off output buffering if it's on
86 $sep = PATH_SEPARATOR;
88 if (!isset( $wgUseNormalUser ) ) {
89 $wgUseNormalUser = false;
92 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
94 require_once( $IP.'/includes/SiteConfiguration.php' );
97 require( $IP.'/InitialiseSettings.php' );
99 if ( empty( $wgNoDBParam ) ) {
100 # Check if we were passed a db name
101 $db = array_shift( $args );
102 list( $site, $lang ) = $wgConf->siteFromDB( $db );
104 # If not, work out the language and site the old way
105 if ( is_null( $site ) || is_null( $lang ) ) {
111 if ( isset( $args[0] ) ) {
112 $site = array_shift( $args );
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 $wgUseNormalUser = true;
128 putenv( 'wikilang='.$lang);
131 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
133 require_once( $IP.'/includes/Defines.php' );
134 require_once( $IP.'/CommonSettings.php' );
136 if ( $wgUseRootUser ) {
137 $wgDBuser = $wgDBadminuser = 'root';
138 $wgDBpassword = $wgDBadminpassword = trim(`mysql_root_pass`);
139 } elseif ( !$wgUseNormalUser ) {
140 $wgDBuser = $wgDBadminuser = 'wikiadmin';
141 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
145 if ( isset( $options['conf'] ) ) {
146 $settingsFile = $options['conf'];
148 $settingsFile = "$IP/LocalSettings.php";
151 if ( ! is_readable( $settingsFile ) ) {
152 print "A copy of your installation's LocalSettings.php\n" .
153 "must exist in the source directory.\n";
156 $wgCommandLineMode = true;
158 require_once( $IP.'/includes/Defines.php' );
159 require_once( $settingsFile );
160 ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
162 if ( is_readable( $IP.'/AdminSettings.php' ) ) {
163 require_once( $IP.'/AdminSettings.php' );
167 # Turn off output buffering again, it might have been turned on in the settings files
170 $wgCommandLineMode = true;
172 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
173 $wgDBuser = $wgDBadminuser;
174 $wgDBpassword = $wgDBadminpassword;
176 foreach ( $wgDBservers as $i => $server ) {
177 $wgDBservers[$i]['user'] = $wgDBuser;
178 $wgDBservers[$i]['password'] = $wgDBpassword;
182 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
183 $fn = MW_CMDLINE_CALLBACK;
187 ini_set( 'memory_limit', -1 );
189 require_once( 'Setup.php' );
190 require_once( 'install-utils.inc' );
191 $wgTitle = Title::newFromText( 'Command line script' );
194 // --------------------------------------------------------------------
196 // --------------------------------------------------------------------
198 function wfWaitForSlaves( $maxLag ) {
199 global $wgLoadBalancer;
201 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
202 while ( $lag > $maxLag ) {
203 $name = @gethostbyaddr( $host );
204 if ( $name !== false ) {
207 print "Waiting for $host (lagged $lag seconds)...\n";
209 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();