5 * @subpackage Maintenance
8 $wgRequestTime = microtime();
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 ) ) {
42 if ( substr( $arg, 0, 2 ) == '--' ) {
44 $option = substr( $arg, 2 );
45 if ( in_array( $option, $optionsWithArgs ) ) {
46 $param = next( $argv );
47 if ( $param === false ) {
48 echo "$arg needs an value after it\n";
51 $options[$option] = $param;
53 $bits = explode( '=', $option, 2 );
54 if( count( $bits ) > 1 ) {
60 $options[$option] = $param;
62 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
64 for ( $p=1; $p<strlen( $arg ); $p++ ) {
66 if ( in_array( $option, $optionsWithArgs ) ) {
67 $param = next( $argv );
68 if ( $param === false ) {
69 echo "$arg needs an value after it\n";
72 $options[$option] = $param;
74 $options[$option] = 1;
83 # General initialisation
85 $wgCommandLineMode = true;
86 # Turn off output buffering if it's on
88 $sep = PATH_SEPARATOR;
90 if (!isset( $wgUseNormalUser ) ) {
91 $wgUseNormalUser = false;
94 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
96 $cluster = trim( file_get_contents( '/etc/cluster' ) );
97 require_once( "$IP/includes/SiteConfiguration.php" );
100 require( "$IP/wgConf.php" );
102 if ( empty( $wgNoDBParam ) ) {
103 # Check if we were passed a db name
104 $db = array_shift( $args );
105 list( $site, $lang ) = $wgConf->siteFromDB( $db );
107 # If not, work out the language and site the old way
108 if ( is_null( $site ) || is_null( $lang ) ) {
114 if ( isset( $args[0] ) ) {
115 $site = array_shift( $args );
125 # This is for the IRC scripts, which now run as the apache user
126 # The apache user doesn't have access to the wikiadmin_pass command
127 if ( $_ENV['USER'] == 'apache' ) {
128 $wgUseNormalUser = true;
131 putenv( 'wikilang='.$lang);
134 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
136 require_once( $IP.'/includes/ProfilerStub.php' );
137 require_once( $IP.'/includes/Defines.php' );
138 require_once( $IP.'/CommonSettings.php' );
140 $bin = '/home/wikipedia/bin';
141 if ( $wgUseRootUser ) {
142 $wgDBuser = $wgDBadminuser = 'root';
143 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
144 } elseif ( !$wgUseNormalUser ) {
145 $wgDBuser = $wgDBadminuser = 'wikiadmin';
146 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
150 if ( isset( $options['conf'] ) ) {
151 $settingsFile = $options['conf'];
153 $settingsFile = "$IP/LocalSettings.php";
156 if ( ! is_readable( $settingsFile ) ) {
157 print "A copy of your installation's LocalSettings.php\n" .
158 "must exist in the source directory.\n";
161 $wgCommandLineMode = true;
163 require_once( $IP.'/includes/ProfilerStub.php' );
164 require_once( $IP.'/includes/Defines.php' );
165 require_once( $settingsFile );
166 ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
168 if ( is_readable( $IP.'/AdminSettings.php' ) ) {
169 require_once( $IP.'/AdminSettings.php' );
173 # Turn off output buffering again, it might have been turned on in the settings files
176 $wgCommandLineMode = true;
178 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
179 $wgDBuser = $wgDBadminuser;
180 $wgDBpassword = $wgDBadminpassword;
182 foreach ( $wgDBservers as $i => $server ) {
183 $wgDBservers[$i]['user'] = $wgDBuser;
184 $wgDBservers[$i]['password'] = $wgDBpassword;
188 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
189 $fn = MW_CMDLINE_CALLBACK;
193 ini_set( 'memory_limit', -1 );
195 require_once( 'Setup.php' );
196 require_once( 'install-utils.inc' );
197 $wgTitle = Title::newFromText( 'Command line script' );
200 // --------------------------------------------------------------------
202 // --------------------------------------------------------------------
204 function wfWaitForSlaves( $maxLag ) {
205 global $wgLoadBalancer;
207 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
208 while ( $lag > $maxLag ) {
209 $name = @gethostbyaddr( $host );
210 if ( $name !== false ) {
213 print "Waiting for $host (lagged $lag seconds)...\n";
215 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();