Merged localisation-work branch:
[lhc/web/wiklou.git] / maintenance / commandLine.inc
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage Maintenance
6 */
7
8 $wgRequestTime = microtime(true);
9
10 /** */
11 # Abort if called from a web server
12 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
13 print "This script must be run from the command line\n";
14 exit();
15 }
16
17 define('MEDIAWIKI',true);
18
19 # Process command line arguments
20 # $options becomes an array with keys set to the option names
21 # $optionsWithArgs is an array of GNU-style options that take an argument. The arguments are returned
22 # in the values of $options.
23 # $args becomes a zero-based array containing the non-option arguments
24
25 if ( !isset( $optionsWithArgs ) ) {
26 $optionsWithArgs = array();
27 }
28 $optionsWithArgs[] = 'conf'; # For specifying the location of LocalSettings.php
29
30 $self = array_shift( $argv );
31 $IP = realpath( dirname( __FILE__ ) . '/..' );
32 #chdir( $IP );
33 require_once( "$IP/StartProfiler.php" );
34
35 $options = array();
36 $args = array();
37
38
39 # Parse arguments
40 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
41 if ( $arg == '--' ) {
42 # End of options, remainder should be considered arguments
43 $arg = next( $argv );
44 while( $arg !== false ) {
45 $args[] = $arg;
46 $arg = next( $argv );
47 }
48 break;
49 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
50 # Long options
51 $option = substr( $arg, 2 );
52 if ( in_array( $option, $optionsWithArgs ) ) {
53 $param = next( $argv );
54 if ( $param === false ) {
55 echo "$arg needs an value after it\n";
56 die( -1 );
57 }
58 $options[$option] = $param;
59 } else {
60 $bits = explode( '=', $option, 2 );
61 if( count( $bits ) > 1 ) {
62 $option = $bits[0];
63 $param = $bits[1];
64 } else {
65 $param = 1;
66 }
67 $options[$option] = $param;
68 }
69 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
70 # Short options
71 for ( $p=1; $p<strlen( $arg ); $p++ ) {
72 $option = $arg{$p};
73 if ( in_array( $option, $optionsWithArgs ) ) {
74 $param = next( $argv );
75 if ( $param === false ) {
76 echo "$arg needs an value after it\n";
77 die( -1 );
78 }
79 $options[$option] = $param;
80 } else {
81 $options[$option] = 1;
82 }
83 }
84 } else {
85 $args[] = $arg;
86 }
87 }
88
89
90 # General initialisation
91
92 $wgCommandLineMode = true;
93 # Turn off output buffering if it's on
94 @ob_end_flush();
95 $sep = PATH_SEPARATOR;
96
97 if (!isset( $wgUseNormalUser ) ) {
98 $wgUseNormalUser = false;
99 }
100
101 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
102 $wgWikiFarm = true;
103 $cluster = trim( file_get_contents( '/etc/cluster' ) );
104 require_once( "$IP/includes/SiteConfiguration.php" );
105
106 # Get $wgConf
107 require( "$IP/wgConf.php" );
108
109 if ( empty( $wgNoDBParam ) ) {
110 # Check if we were passed a db name
111 $db = array_shift( $args );
112 list( $site, $lang ) = $wgConf->siteFromDB( $db );
113
114 # If not, work out the language and site the old way
115 if ( is_null( $site ) || is_null( $lang ) ) {
116 if ( !$db ) {
117 $lang = 'aa';
118 } else {
119 $lang = $db;
120 }
121 if ( isset( $args[0] ) ) {
122 $site = array_shift( $args );
123 } else {
124 $site = 'wikipedia';
125 }
126 }
127 } else {
128 $lang = 'aa';
129 $site = 'wikipedia';
130 }
131
132 # This is for the IRC scripts, which now run as the apache user
133 # The apache user doesn't have access to the wikiadmin_pass command
134 if ( $_ENV['USER'] == 'apache' ) {
135 $wgUseNormalUser = true;
136 }
137
138 putenv( 'wikilang='.$lang);
139
140 $DP = $IP;
141 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
142
143 #require_once( $IP.'/includes/ProfilerStub.php' );
144 require_once( $IP.'/includes/Defines.php' );
145 require_once( $IP.'/CommonSettings.php' );
146
147 $bin = '/home/wikipedia/bin';
148 if ( $wgUseRootUser ) {
149 $wgDBuser = $wgDBadminuser = 'root';
150 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
151 } elseif ( !$wgUseNormalUser ) {
152 $wgDBuser = $wgDBadminuser = 'wikiadmin';
153 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
154 }
155 } else {
156 $wgWikiFarm = false;
157 if ( isset( $options['conf'] ) ) {
158 $settingsFile = $options['conf'];
159 } else {
160 $settingsFile = "$IP/LocalSettings.php";
161 }
162
163 if ( ! is_readable( $settingsFile ) ) {
164 print "A copy of your installation's LocalSettings.php\n" .
165 "must exist in the source directory.\n";
166 exit( 1 );
167 }
168 $wgCommandLineMode = true;
169 $DP = $IP;
170 #require_once( $IP.'/includes/ProfilerStub.php' );
171 require_once( $IP.'/includes/Defines.php' );
172 require_once( $settingsFile );
173 ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
174
175 if ( is_readable( $IP.'/AdminSettings.php' ) ) {
176 require_once( $IP.'/AdminSettings.php' );
177 }
178 }
179
180 # Turn off output buffering again, it might have been turned on in the settings files
181 @ob_end_flush();
182 # Same with these
183 $wgCommandLineMode = true;
184
185 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
186 $wgDBuser = $wgDBadminuser;
187 $wgDBpassword = $wgDBadminpassword;
188
189 if( $wgDBservers ) {
190 foreach ( $wgDBservers as $i => $server ) {
191 $wgDBservers[$i]['user'] = $wgDBuser;
192 $wgDBservers[$i]['password'] = $wgDBpassword;
193 }
194 }
195 }
196
197 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
198 $fn = MW_CMDLINE_CALLBACK;
199 $fn();
200 }
201
202 ini_set( 'memory_limit', -1 );
203
204 require_once( 'Setup.php' );
205 require_once( 'install-utils.inc' );
206 $wgTitle = null; # Much much faster startup than creating a title object
207 set_time_limit(0);
208
209 // --------------------------------------------------------------------
210 // Functions
211 // --------------------------------------------------------------------
212
213 function wfWaitForSlaves( $maxLag ) {
214 global $wgLoadBalancer;
215 if ( $maxLag ) {
216 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
217 while ( $lag > $maxLag ) {
218 $name = @gethostbyaddr( $host );
219 if ( $name !== false ) {
220 $host = $name;
221 }
222 print "Waiting for $host (lagged $lag seconds)...\n";
223 sleep($maxLag);
224 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
225 }
226 }
227 }
228
229
230
231 ?>