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();
27 $self = array_shift( $argv );
28 $IP = realpath( dirname( $self ) . "/.." );
34 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
35 if ( substr( $arg, 0, 2 ) == '--' ) {
37 $option = substr( $arg, 2 );
38 if ( in_array( $option, $optionsWithArgs ) ) {
39 $param = next( $argv );
40 if ( $param === false ) {
41 die( "$arg needs an value after it\n" );
43 $options[$option] = $param;
45 $bits = explode( '=', $option, 2 );
46 if( count( $bits ) > 1 ) {
52 $options[$option] = $param;
54 } elseif ( $arg{0} == '-' ) {
56 for ( $p=1; $p<strlen( $arg ); $p++ ) {
58 if ( in_array( $option, $optionsWithArgs ) ) {
59 $param = next( $argv );
60 if ( $param === false ) {
61 die( "$arg needs an value after it\n" );
63 $options[$option] = $param;
65 $options[$option] = 1;
73 # General initialisation
75 $wgCommandLineMode = true;
76 # Turn off output buffering if it's on
78 $sep = strchr( $include_path = ini_get( "include_path" ), ";" ) ? ";" : ":";
80 if ( $sep == ":" && strpos( `hostname`, "wikimedia.org" ) !== false ) {
82 require_once( "$IP/includes/SiteConfiguration.php" );
85 require( "$IP/InitialiseSettings.php" );
87 if ( empty( $wgNoDBParam ) ) {
88 # Check if we were passed a db name
89 $db = array_shift( $args );
90 list( $site, $lang ) = $conf->siteFromDB( $db );
92 # If not, work out the language and site the old way
93 if ( is_null( $site ) || is_null( $lang ) ) {
99 if ( isset( $args[0] ) ) {
100 $site = array_shift( $args );
110 # This is for the IRC scripts, which now run as the apache user
111 # The apache user doesn't have access to the wikiadmin_pass command
112 if ( $_ENV['USER'] != "apache" ) {
113 $wgDBuser = $wgDBadminuser = "wikiadmin";
114 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
117 putenv( "wikilang=$lang");
120 ini_set( "include_path", ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
122 require_once( "$IP/includes/Defines.php" );
123 require_once( "/home/wikipedia/common/php-1.4/CommonSettings.php" );
126 $settingsFile = "$IP/LocalSettings.php";
128 if ( ! is_readable( $settingsFile ) ) {
129 print "A copy of your installation's LocalSettings.php\n" .
130 "must exist in the source directory.\n";
133 $wgCommandLineMode = true;
135 require_once( "$IP/includes/Defines.php" );
136 require_once( $settingsFile );
137 ini_set( "include_path", ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
139 if ( is_readable( "$IP/AdminSettings.php" ) ) {
140 require_once( "$IP/AdminSettings.php" );
144 # Turn off output buffering again, it might have been turned on in the settings files
147 $wgCommandLineMode = true;
148 $wgDBuser = $wgDBadminuser;
149 $wgDBpassword = $wgDBadminpassword;
151 if ( !empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
152 $wgDBuser = $wgDBadminuser;
153 $wgDBpassword = $wgDBadminpassword;
155 foreach ( $wgDBservers as $i => $server ) {
156 $wgDBservers[$i]['user'] = $wgDBuser;
157 $wgDBservers[$i]['password'] = $wgDBpassword;
161 ini_set( 'memory_limit', -1 );
163 require_once( "Setup.php" );
164 require_once( "install-utils.inc" );
165 $wgTitle = Title::newFromText( "Command line script" );
168 // --------------------------------------------------------------------
170 // --------------------------------------------------------------------
172 function wfWaitForSlaves( $maxLag ) {
173 global $wgLoadBalancer;
175 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
176 while ( $lag > $maxLag ) {
177 $name = @gethostbyaddr( $host );
178 if ( $name !== false ) {
181 print "Waiting for $host (lagged $lag seconds)...\n";
183 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();