5 * @subpackage Maintenance
8 $wgRequestTime = microtime(true);
11 # Abort if called from a web server
12 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
13 print "This script must be run from the command line\n";
17 define('MEDIAWIKI',true);
19 # Process command line arguments
20 # $options becomes an array with keys set to the option names
21 # $optionsWithArgs is an array of GNU-style options that take an argument. The arguments are returned
22 # in the values of $options.
23 # $args becomes a zero-based array containing the non-option arguments
25 if ( !isset( $optionsWithArgs ) ) {
26 $optionsWithArgs = array();
28 $optionsWithArgs[] = 'conf'; # For specifying the location of LocalSettings.php
30 $self = array_shift( $argv );
32 $IP = realpath( dirname( $self ) . '/..' );
41 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
43 # End of options, remainder should be considered arguments
45 while( $arg !== false ) {
50 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
52 $option = substr( $arg, 2 );
53 if ( in_array( $option, $optionsWithArgs ) ) {
54 $param = next( $argv );
55 if ( $param === false ) {
56 echo "$arg needs an value after it\n";
59 $options[$option] = $param;
61 $bits = explode( '=', $option, 2 );
62 if( count( $bits ) > 1 ) {
68 $options[$option] = $param;
70 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
72 for ( $p=1; $p<strlen( $arg ); $p++ ) {
74 if ( in_array( $option, $optionsWithArgs ) ) {
75 $param = next( $argv );
76 if ( $param === false ) {
77 echo "$arg needs an value after it\n";
80 $options[$option] = $param;
82 $options[$option] = 1;
91 # General initialisation
93 $wgCommandLineMode = true;
94 # Turn off output buffering if it's on
96 $sep = PATH_SEPARATOR;
98 if (!isset( $wgUseNormalUser ) ) {
99 $wgUseNormalUser = false;
102 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
104 $cluster = trim( file_get_contents( '/etc/cluster' ) );
105 require_once( "$IP/includes/SiteConfiguration.php" );
108 require( "$IP/wgConf.php" );
110 if ( empty( $wgNoDBParam ) ) {
111 # Check if we were passed a db name
112 $db = array_shift( $args );
113 list( $site, $lang ) = $wgConf->siteFromDB( $db );
115 # If not, work out the language and site the old way
116 if ( is_null( $site ) || is_null( $lang ) ) {
122 if ( isset( $args[0] ) ) {
123 $site = array_shift( $args );
133 # This is for the IRC scripts, which now run as the apache user
134 # The apache user doesn't have access to the wikiadmin_pass command
135 if ( $_ENV['USER'] == 'apache' ) {
136 $wgUseNormalUser = true;
139 putenv( 'wikilang='.$lang);
142 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
144 require_once( $IP.'/includes/ProfilerStub.php' );
145 require_once( $IP.'/includes/Defines.php' );
146 require_once( $IP.'/CommonSettings.php' );
148 $bin = '/home/wikipedia/bin';
149 if ( $wgUseRootUser ) {
150 $wgDBuser = $wgDBadminuser = 'root';
151 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
152 } elseif ( !$wgUseNormalUser ) {
153 $wgDBuser = $wgDBadminuser = 'wikiadmin';
154 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
158 if ( isset( $options['conf'] ) ) {
159 $settingsFile = $options['conf'];
161 $settingsFile = "$IP/LocalSettings.php";
164 if ( ! is_readable( $settingsFile ) ) {
165 print "A copy of your installation's LocalSettings.php\n" .
166 "must exist in the source directory.\n";
169 $wgCommandLineMode = true;
171 require_once( $IP.'/includes/ProfilerStub.php' );
172 require_once( $IP.'/includes/Defines.php' );
173 require_once( $settingsFile );
174 ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
176 if ( is_readable( $IP.'/AdminSettings.php' ) ) {
177 require_once( $IP.'/AdminSettings.php' );
181 # Turn off output buffering again, it might have been turned on in the settings files
184 $wgCommandLineMode = true;
186 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
187 $wgDBuser = $wgDBadminuser;
188 $wgDBpassword = $wgDBadminpassword;
191 foreach ( $wgDBservers as $i => $server ) {
192 $wgDBservers[$i]['user'] = $wgDBuser;
193 $wgDBservers[$i]['password'] = $wgDBpassword;
198 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
199 $fn = MW_CMDLINE_CALLBACK;
203 ini_set( 'memory_limit', -1 );
205 require_once( 'Setup.php' );
206 require_once( 'install-utils.inc' );
207 $wgTitle = Title::newFromText( 'Command line script' );
210 // --------------------------------------------------------------------
212 // --------------------------------------------------------------------
214 function wfWaitForSlaves( $maxLag ) {
215 global $wgLoadBalancer;
217 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
218 while ( $lag > $maxLag ) {
219 $name = @gethostbyaddr( $host );
220 if ( $name !== false ) {
223 print "Waiting for $host (lagged $lag seconds)...\n";
225 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();