Fix yet another stupid mistake
[mediawiki.git] / maintenance / commandLine.inc
blob1fecc0a4da29b502ba796d7ac21eb7515500320e
1 <?php
2 /**
3  * @todo document
4  * @package MediaWiki
5  * @subpackage Maintenance
6  */
8 /** */
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";
12         exit();
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 $self = __FILE__;
29 $IP = realpath( dirname( $self ) . '/..' );
30 chdir( $IP );
32 $options = array();
33 $args = array();
36 # Parse arguments
38 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
39         if ( substr( $arg, 0, 2 ) == '--' ) {
40                 # Long options
41                 $option = substr( $arg, 2 );
42                 if ( in_array( $option, $optionsWithArgs ) ) {
43                         $param = next( $argv );
44                         if ( $param === false ) {
45                                 echo "$arg needs an value after it\n";
46                                 die( -1 );
47                         }
48                         $options[$option] = $param;
49                 } else {
50                         $bits = explode( '=', $option, 2 );
51                         if( count( $bits ) > 1 ) {
52                                 $option = $bits[0];
53                                 $param = $bits[1];
54                         } else {
55                                 $param = 1;
56                         }
57                         $options[$option] = $param;
58                 }
59         } elseif ( substr( $arg, 0, 1 ) == '-' ) {
60                 # Short options
61                 for ( $p=1; $p<strlen( $arg ); $p++ ) {
62                         $option = $arg{$p};
63                         if ( in_array( $option, $optionsWithArgs ) ) {
64                                 $param = next( $argv );
65                                 if ( $param === false ) {
66                                         echo "$arg needs an value after it\n";
67                                         die( -1 );
68                                 }
69                                 $options[$option] = $param;
70                         } else {
71                                 $options[$option] = 1;
72                         }
73                 }
74         } else {
75                 $args[] = $arg;
76         }
80 # General initialisation
82 $wgCommandLineMode = true;
83 # Turn off output buffering if it's on
84 @ob_end_flush();
85 $sep = PATH_SEPARATOR;
87 if (!isset( $wgUseNormalUser ) ) {
88         $wgUseNormalUser = false;
91 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
92         $wgWikiFarm = true;
93         require_once( $IP.'/includes/SiteConfiguration.php' );
95         # Get $conf
96         require( $IP.'/InitialiseSettings.php' );
98         if ( empty( $wgNoDBParam ) ) {
99                 # Check if we were passed a db name
100                 $db = array_shift( $args );
101                 list( $site, $lang ) = $wgConf->siteFromDB( $db );
103                 # If not, work out the language and site the old way
104                 if ( is_null( $site ) || is_null( $lang ) ) {
105                         if ( !$db ) {
106                                 $lang = 'aa';
107                         } else {
108                                 $lang = $db;
109                         }
110                         if ( isset( $args[0] ) ) {
111                                 $site = array_shift( $args );
112                         } else {
113                                 $site = 'wikipedia';
114                         }
115                 }
116         } else {
117                 $lang = 'aa';
118                 $site = 'wikipedia';
119         }
121         # This is for the IRC scripts, which now run as the apache user
122         # The apache user doesn't have access to the wikiadmin_pass command
123         if ( $_ENV['USER'] == 'apache' ) {
124                 $wgUseNormalUser = true;
125         }
127         putenv( 'wikilang='.$lang);
129         $DP = $IP;
130         ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
132         require_once( $IP.'/includes/Defines.php' );
133         require_once( $IP.'/CommonSettings.php' );
135         if ( $wgUseRootUser ) {
136                 $wgDBuser = $wgDBadminuser = 'root';
137                 $wgDBpassword = $wgDBadminpassword = trim(`mysql_root_pass`);
138         } elseif ( !$wgUseNormalUser ) {
139                 $wgDBuser = $wgDBadminuser = 'wikiadmin';
140                 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
141         }
142 } else {
143         $wgWikiFarm = false;
144         $settingsFile = $IP.'/LocalSettings.php';
146         if ( ! is_readable( $settingsFile ) ) {
147                 print "A copy of your installation's LocalSettings.php\n" .
148                   "must exist in the source directory.\n";
149                 exit();
150         }
151         $wgCommandLineMode = true;
152         $DP = $IP;
153         require_once( $IP.'/includes/Defines.php' );
154         require_once( $settingsFile );
155         ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
157         if ( is_readable( $IP.'/AdminSettings.php' ) ) {
158                 require_once( $IP.'/AdminSettings.php' );
159         }
162 # Turn off output buffering again, it might have been turned on in the settings files
163 @ob_end_flush();
164 # Same with these
165 $wgCommandLineMode = true;
167 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
168         $wgDBuser = $wgDBadminuser;
169         $wgDBpassword = $wgDBadminpassword;
171         foreach ( $wgDBservers as $i => $server ) {
172                 $wgDBservers[$i]['user'] = $wgDBuser;
173                 $wgDBservers[$i]['password'] = $wgDBpassword;
174         }
177 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
178         $fn = MW_CMDLINE_CALLBACK;
179         $fn();
182 ini_set( 'memory_limit', -1 );
184 require_once( 'Setup.php' );
185 require_once( 'install-utils.inc' );
186 $wgTitle = Title::newFromText( 'Command line script' );
187 set_time_limit(0);
189 // --------------------------------------------------------------------
190 // Functions
191 // --------------------------------------------------------------------
193 function wfWaitForSlaves( $maxLag ) {
194         global $wgLoadBalancer;
195         if ( $maxLag ) {
196                 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
197                 while ( $lag > $maxLag ) {
198                         $name = @gethostbyaddr( $host );
199                         if ( $name !== false ) {
200                                 $host = $name;
201                         }
202                         print "Waiting for $host (lagged $lag seconds)...\n";
203                         sleep($maxLag);
204                         list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
205                 }
206         }