more hardcoding
[mediawiki.git] / maintenance / commandLine.inc
blobfc5147c6851e7f08b230cdb2ee26f1dbf0d1ae44
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();
35 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
36         if ( substr( $arg, 0, 2 ) == '--' ) {
37                 # Long options
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" );
43                         }
44                         $options[$option] = $param;
45                 } else {
46                         $bits = explode( '=', $option, 2 );
47                         if( count( $bits ) > 1 ) {
48                                 $option = $bits[0];
49                                 $param = $bits[1];
50                         } else {
51                                 $param = 1;
52                         }
53                         $options[$option] = $param;
54                 }
55         } elseif ( $arg{0} == '-' ) {
56                 # Short options
57                 for ( $p=1; $p<strlen( $arg ); $p++ ) {
58                         $option = $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" );
63                                 }
64                                 $options[$option] = $param;
65                         } else {
66                                 $options[$option] = 1;
67                         }
68                 }
69         } else {
70                 $args[] = $arg;
71         }
74 # General initialisation
76 $wgCommandLineMode = true;
77 # Turn off output buffering if it's on
78 @ob_end_flush();
79 $sep = strchr( $include_path = ini_get( "include_path" ), ";" ) ? ";" : ":";
81 if (!isset( $wgUseNormalUser ) ) {
82         $wgUseNormalUser = false;
85 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
86         $wgWikiFarm = true;
87         require_once( "$IP/includes/SiteConfiguration.php" );
89         # Get $conf
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 ) ) {
99                         if ( !$db ) {   
100                                 $lang = "aa";
101                         } else {
102                                 $lang = $db;
103                         }
104                         if ( isset( $args[0] ) ) {
105                                 $site = array_shift( $args );
106                         } else {
107                                 $site = "wikipedia";
108                         }
109                 }
110         } else {
111                 $lang = "aa";
112                 $site = "wikipedia";
113         }
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;
119         }
121         putenv( "wikilang=$lang");
123         $DP = $IP;
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`);
132         }
133 } else {
134         $wgWikiFarm = false;
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";
140                 exit();
141         }
142         $wgCommandLineMode = true;
143         $DP = $IP;
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" );
147         
148         if ( is_readable( "$IP/AdminSettings.php" ) ) {
149                 require_once( "$IP/AdminSettings.php" );
150         }
153 # Turn off output buffering again, it might have been turned on in the settings files
154 @ob_end_flush();
155 # Same with these
156 $wgCommandLineMode = true;
158 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
159         $wgDBuser = $wgDBadminuser;
160         $wgDBpassword = $wgDBadminpassword;
162         foreach ( $wgDBservers as $i => $server ) {
163                 $wgDBservers[$i]['user'] = $wgDBuser;
164                 $wgDBservers[$i]['password'] = $wgDBpassword;
165         }
168 ini_set( 'memory_limit', -1 );
170 require_once( "Setup.php" );
171 require_once( "install-utils.inc" );
172 $wgTitle = Title::newFromText( "Command line script" );
173 set_time_limit(0);
175 // --------------------------------------------------------------------
176 // Functions
177 // --------------------------------------------------------------------
179 function wfWaitForSlaves( $maxLag ) {
180         global $wgLoadBalancer;
181         if ( $maxLag ) {
182                 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
183                 while ( $lag > $maxLag ) {
184                         $name = @gethostbyaddr( $host );
185                         if ( $name !== false ) {
186                                 $host = $name;
187                         }
188                         print "Waiting for $host (lagged $lag seconds)...\n";
189                         sleep($maxLag);
190                         list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
191                 }
192         }