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 );
29 $IP = realpath( dirname( $self ) . "/.." );
35 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
36 if ( substr( $arg, 0, 2 ) == '--' ) {
38 $option = substr( $arg, 2 );
39 if ( in_array( $option, $optionsWithArgs ) ) {
40 $param = next( $argv );
41 if ( $param === false ) {
42 die( "$arg needs an value after it\n" );
44 $options[$option] = $param;
46 $bits = explode( '=', $option, 2 );
47 if( count( $bits ) > 1 ) {
53 $options[$option] = $param;
55 } elseif ( $arg{0} == '-' ) {
57 for ( $p=1; $p<strlen( $arg ); $p++ ) {
59 if ( in_array( $option, $optionsWithArgs ) ) {
60 $param = next( $argv );
61 if ( $param === false ) {
62 die( "$arg needs an value after it\n" );
64 $options[$option] = $param;
66 $options[$option] = 1;
74 # General initialisation
76 $wgCommandLineMode = true;
77 # Turn off output buffering if it's on
79 $sep = strchr( $include_path = ini_get( "include_path" ), ";" ) ? ";" : ":";
81 if (!isset( $wgUseNormalUser ) ) {
82 $wgUseNormalUser = false;
85 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
87 require_once( "$IP/includes/SiteConfiguration.php" );
90 require( "$IP/InitialiseSettings.php" );
92 if ( empty( $wgNoDBParam ) ) {
93 # Check if we were passed a db name
94 $db = array_shift( $args );
95 list( $site, $lang ) = $wgConf->siteFromDB( $db );
97 # If not, work out the language and site the old way
98 if ( is_null( $site ) || is_null( $lang ) ) {
104 if ( isset( $args[0] ) ) {
105 $site = array_shift( $args );
115 # This is for the IRC scripts, which now run as the apache user
116 # The apache user doesn't have access to the wikiadmin_pass command
117 if ( $_ENV['USER'] == "apache" ) {
118 $wgUseNormalUser = true;
121 putenv( "wikilang=$lang");
124 ini_set( "include_path", ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
126 require_once( "$IP/includes/Defines.php" );
127 require_once( "$IP/CommonSettings.php" );
129 if ( !$wgUseNormalUser ) {
130 $wgDBuser = $wgDBadminuser = "wikiadmin";
131 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
135 $settingsFile = "$IP/LocalSettings.php";
137 if ( ! is_readable( $settingsFile ) ) {
138 print "A copy of your installation's LocalSettings.php\n" .
139 "must exist in the source directory.\n";
142 $wgCommandLineMode = true;
144 require_once( "$IP/includes/Defines.php" );
145 require_once( $settingsFile );
146 ini_set( "include_path", ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
148 if ( is_readable( "$IP/AdminSettings.php" ) ) {
149 require_once( "$IP/AdminSettings.php" );
153 # Turn off output buffering again, it might have been turned on in the settings files
156 $wgCommandLineMode = true;
158 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
159 $wgDBuser = $wgDBadminuser;
160 $wgDBpassword = $wgDBadminpassword;
162 foreach ( $wgDBservers as $i => $server ) {
163 $wgDBservers[$i]['user'] = $wgDBuser;
164 $wgDBservers[$i]['password'] = $wgDBpassword;
168 ini_set( 'memory_limit', -1 );
170 require_once( "Setup.php" );
171 require_once( "install-utils.inc" );
172 $wgTitle = Title::newFromText( "Command line script" );
175 // --------------------------------------------------------------------
177 // --------------------------------------------------------------------
179 function wfWaitForSlaves( $maxLag ) {
180 global $wgLoadBalancer;
182 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
183 while ( $lag > $maxLag ) {
184 $name = @gethostbyaddr( $host );
185 if ( $name !== false ) {
188 print "Waiting for $host (lagged $lag seconds)...\n";
190 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();