* Fix for short_open_tag off again; please don't break this, guys
[mediawiki.git] / maintenance / commandLine.inc
blob42cc918382821271fe9023f6c6c610162f37a674
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                                 die( "$arg needs an value after it\n" );
46                         }
47                         $options[$option] = $param;
48                 } else {
49                         $bits = explode( '=', $option, 2 );
50                         if( count( $bits ) > 1 ) {
51                                 $option = $bits[0];
52                                 $param = $bits[1];
53                         } else {
54                                 $param = 1;
55                         }
56                         $options[$option] = $param;
57                 }
58         } elseif ( $arg{0} == '-' ) {
59                 # Short options
60                 for ( $p=1; $p<strlen( $arg ); $p++ ) {
61                         $option = $arg{$p};
62                         if ( in_array( $option, $optionsWithArgs ) ) {
63                                 $param = next( $argv );
64                                 if ( $param === false ) {
65                                         die( "$arg needs an value after it\n" );
66                                 }
67                                 $options[$option] = $param;
68                         } else {
69                                 $options[$option] = 1;
70                         }
71                 }
72         } else {
73                 $args[] = $arg;
74         }
78 # General initialisation
80 $wgCommandLineMode = true;
81 # Turn off output buffering if it's on
82 @ob_end_flush();
83 $sep = PATH_SEPARATOR;
85 if (!isset( $wgUseNormalUser ) ) {
86         $wgUseNormalUser = false;
89 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
90         $wgWikiFarm = true;
91         require_once( $IP.'/includes/SiteConfiguration.php' );
93         # Get $conf
94         require( $IP.'/InitialiseSettings.php' );
96         if ( empty( $wgNoDBParam ) ) {
97                 # Check if we were passed a db name
98                 $db = array_shift( $args );
99                 list( $site, $lang ) = $wgConf->siteFromDB( $db );
101                 # If not, work out the language and site the old way
102                 if ( is_null( $site ) || is_null( $lang ) ) {
103                         if ( !$db ) {   
104                                 $lang = 'aa';
105                         } else {
106                                 $lang = $db;
107                         }
108                         if ( isset( $args[0] ) ) {
109                                 $site = array_shift( $args );
110                         } else {
111                                 $site = 'wikipedia';
112                         }
113                 }
114         } else {
115                 $lang = 'aa';
116                 $site = 'wikipedia';
117         }
119         # This is for the IRC scripts, which now run as the apache user
120         # The apache user doesn't have access to the wikiadmin_pass command
121         if ( $_ENV['USER'] == 'apache' ) {
122                 $wgUseNormalUser = true;
123         }
125         putenv( 'wikilang='.$lang);
127         $DP = $IP;
128         ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
130         require_once( $IP.'/includes/Defines.php' );
131         require_once( $IP.'/CommonSettings.php' );
133         if ( $wgUseRootUser ) {
134                 $wgDBuser = $wgDBadminuser = 'root';
135                 $wgDBpassword = $wgDBadminpassword = trim(`mysql_root_pass`);
136         } elseif ( !$wgUseNormalUser ) {
137                 $wgDBuser = $wgDBadminuser = 'wikiadmin';
138                 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
139         }
140 } else {
141         $wgWikiFarm = false;
142         $settingsFile = $IP.'/LocalSettings.php';
144         if ( ! is_readable( $settingsFile ) ) {
145                 print "A copy of your installation's LocalSettings.php\n" .
146                   "must exist in the source directory.\n";
147                 exit();
148         }
149         $wgCommandLineMode = true;
150         $DP = $IP;
151         require_once( $IP.'/includes/Defines.php' );
152         require_once( $settingsFile );
153         ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
154         
155         if ( is_readable( $IP.'/AdminSettings.php' ) ) {
156                 require_once( $IP.'/AdminSettings.php' );
157         }
160 # Turn off output buffering again, it might have been turned on in the settings files
161 @ob_end_flush();
162 # Same with these
163 $wgCommandLineMode = true;
165 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
166         $wgDBuser = $wgDBadminuser;
167         $wgDBpassword = $wgDBadminpassword;
169         foreach ( $wgDBservers as $i => $server ) {
170                 $wgDBservers[$i]['user'] = $wgDBuser;
171                 $wgDBservers[$i]['password'] = $wgDBpassword;
172         }
175 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
176         $fn = MW_CMDLINE_CALLBACK;
177         $fn();
180 ini_set( 'memory_limit', -1 );
182 require_once( 'Setup.php' );
183 require_once( 'install-utils.inc' );
184 $wgTitle = Title::newFromText( 'Command line script' );
185 set_time_limit(0);
187 // --------------------------------------------------------------------
188 // Functions
189 // --------------------------------------------------------------------
191 function wfWaitForSlaves( $maxLag ) {
192         global $wgLoadBalancer;
193         if ( $maxLag ) {
194                 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
195                 while ( $lag > $maxLag ) {
196                         $name = @gethostbyaddr( $host );
197                         if ( $name !== false ) {
198                                 $host = $name;
199                         }
200                         print "Waiting for $host (lagged $lag seconds)...\n";
201                         sleep($maxLag);
202                         list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
203                 }
204         }