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 $cluster = trim( file_get_contents( '/etc/cluster' ) );
95 require_once( "$IP/includes/SiteConfiguration.php" );
98 require( "$IP/wgConf.php" );
100 if ( empty( $wgNoDBParam ) ) {
101 # Check if we were passed a db name
102 $db = array_shift( $args );
103 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 ) ) {
112 if ( isset( $args[0] ) ) {
113 $site = array_shift( $args );
123 # This is for the IRC scripts, which now run as the apache user
124 # The apache user doesn't have access to the wikiadmin_pass command
125 if ( $_ENV['USER'] == 'apache' ) {
126 $wgUseNormalUser = true;
129 putenv( 'wikilang='.$lang);
132 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
134 require_once( $IP.'/includes/ProfilerStub.php' );
135 require_once( $IP.'/includes/Defines.php' );
136 require_once( $IP.'/CommonSettings.php' );
138 $bin = '/home/wikipedia/bin';
139 if ( $wgUseRootUser ) {
140 $wgDBuser = $wgDBadminuser = 'root';
141 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
142 } elseif ( !$wgUseNormalUser ) {
143 $wgDBuser = $wgDBadminuser = 'wikiadmin';
144 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
148 if ( isset( $options['conf'] ) ) {
149 $settingsFile = $options['conf'];
151 $settingsFile = "$IP/LocalSettings.php";
154 if ( ! is_readable( $settingsFile ) ) {
155 print "A copy of your installation's LocalSettings.php\n" .
156 "must exist in the source directory.\n";
159 $wgCommandLineMode = true;
161 require_once( $IP.'/includes/ProfilerStub.php' );
162 require_once( $IP.'/includes/Defines.php' );
163 require_once( $settingsFile );
164 ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
166 if ( is_readable( $IP.'/AdminSettings.php' ) ) {
167 require_once( $IP.'/AdminSettings.php' );
171 # Turn off output buffering again, it might have been turned on in the settings files
174 $wgCommandLineMode = true;
176 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
177 $wgDBuser = $wgDBadminuser;
178 $wgDBpassword = $wgDBadminpassword;
180 foreach ( $wgDBservers as $i => $server ) {
181 $wgDBservers[$i]['user'] = $wgDBuser;
182 $wgDBservers[$i]['password'] = $wgDBpassword;
186 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
187 $fn = MW_CMDLINE_CALLBACK;
191 ini_set( 'memory_limit', -1 );
193 require_once( 'Setup.php' );
194 require_once( 'install-utils.inc' );
195 $wgTitle = Title::newFromText( 'Command line script' );
198 // --------------------------------------------------------------------
200 // --------------------------------------------------------------------
202 function wfWaitForSlaves( $maxLag ) {
203 global $wgLoadBalancer;
205 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
206 while ( $lag > $maxLag ) {
207 $name = @gethostbyaddr( $host );
208 if ( $name !== false ) {
211 print "Waiting for $host (lagged $lag seconds)...\n";
213 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();