Copying .cvsignore and hoping to test notification mails...
[mediawiki.git] / maintenance / commandLine.inc
blob1d444c852b91755e3206c3b7024dad38f53101ea
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();
26 $optionsWithArgs[] = 'conf'; # For specifying the location of LocalSettings.php
28 $self = array_shift( $argv );
29 $self = __FILE__;
30 $IP = realpath( dirname( $self ) . '/..' );
31 #chdir( $IP );
33 $options = array();
34 $args = array();
37 # Parse arguments
39 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
40         if ( substr( $arg, 0, 2 ) == '--' ) {
41                 # Long options
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";
47                                 die( -1 );
48                         }
49                         $options[$option] = $param;
50                 } else {
51                         $bits = explode( '=', $option, 2 );
52                         if( count( $bits ) > 1 ) {
53                                 $option = $bits[0];
54                                 $param = $bits[1];
55                         } else {
56                                 $param = 1;
57                         }
58                         $options[$option] = $param;
59                 }
60         } elseif ( substr( $arg, 0, 1 ) == '-' ) {
61                 # Short options
62                 for ( $p=1; $p<strlen( $arg ); $p++ ) {
63                         $option = $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";
68                                         die( -1 );
69                                 }
70                                 $options[$option] = $param;
71                         } else {
72                                 $options[$option] = 1;
73                         }
74                 }
75         } else {
76                 $args[] = $arg;
77         }
81 # General initialisation
83 $wgCommandLineMode = true;
84 # Turn off output buffering if it's on
85 @ob_end_flush();
86 $sep = PATH_SEPARATOR;
88 if (!isset( $wgUseNormalUser ) ) {
89         $wgUseNormalUser = false;
92 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
93         $wgWikiFarm = true;
94         $cluster = trim( file_get_contents( '/etc/cluster' ) );
95         require_once( "$IP/includes/SiteConfiguration.php" );
97         # Get $wgConf
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 ) ) {
107                         if ( !$db ) {
108                                 $lang = 'aa';
109                         } else {
110                                 $lang = $db;
111                         }
112                         if ( isset( $args[0] ) ) {
113                                 $site = array_shift( $args );
114                         } else {
115                                 $site = 'wikipedia';
116                         }
117                 }
118         } else {
119                 $lang = 'aa';
120                 $site = 'wikipedia';
121         }
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;
127         }
129         putenv( 'wikilang='.$lang);
131         $DP = $IP;
132         ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
134         require_once( $IP.'/includes/Defines.php' );
135         require_once( $IP.'/CommonSettings.php' );
137         $bin = '/home/wikipedia/bin';
138         if ( $wgUseRootUser ) {
139                 $wgDBuser = $wgDBadminuser = 'root';
140                 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
141         } elseif ( !$wgUseNormalUser ) {
142                 $wgDBuser = $wgDBadminuser = 'wikiadmin';
143                 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
144         }
145 } else {
146         $wgWikiFarm = false;
147         if ( isset( $options['conf'] ) ) {
148                 $settingsFile = $options['conf'];
149         } else {
150                 $settingsFile = "$IP/LocalSettings.php";
151         }
153         if ( ! is_readable( $settingsFile ) ) {
154                 print "A copy of your installation's LocalSettings.php\n" .
155                   "must exist in the source directory.\n";
156                 exit();
157         }
158         $wgCommandLineMode = true;
159         $DP = $IP;
160         require_once( $IP.'/includes/Defines.php' );
161         require_once( $settingsFile );
162         ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
164         if ( is_readable( $IP.'/AdminSettings.php' ) ) {
165                 require_once( $IP.'/AdminSettings.php' );
166         }
169 # Turn off output buffering again, it might have been turned on in the settings files
170 @ob_end_flush();
171 # Same with these
172 $wgCommandLineMode = true;
174 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
175         $wgDBuser = $wgDBadminuser;
176         $wgDBpassword = $wgDBadminpassword;
178         foreach ( $wgDBservers as $i => $server ) {
179                 $wgDBservers[$i]['user'] = $wgDBuser;
180                 $wgDBservers[$i]['password'] = $wgDBpassword;
181         }
184 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
185         $fn = MW_CMDLINE_CALLBACK;
186         $fn();
189 ini_set( 'memory_limit', -1 );
191 require_once( 'Setup.php' );
192 require_once( 'install-utils.inc' );
193 $wgTitle = Title::newFromText( 'Command line script' );
194 set_time_limit(0);
196 // --------------------------------------------------------------------
197 // Functions
198 // --------------------------------------------------------------------
200 function wfWaitForSlaves( $maxLag ) {
201         global $wgLoadBalancer;
202         if ( $maxLag ) {
203                 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
204                 while ( $lag > $maxLag ) {
205                         $name = @gethostbyaddr( $host );
206                         if ( $name !== false ) {
207                                 $host = $name;
208                         }
209                         print "Waiting for $host (lagged $lag seconds)...\n";
210                         sleep($maxLag);
211                         list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
212                 }
213         }