show SQL errors in command line scripts
[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 #if ( posix_geteuid() == 48 ) {
136 $wgUseNormalUser = true;
137 }
138
139 putenv( 'wikilang='.$lang);
140
141 $DP = $IP;
142 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
143
144 #require_once( $IP.'/includes/ProfilerStub.php' );
145 require_once( $IP.'/includes/Defines.php' );
146 require_once( $IP.'/CommonSettings.php' );
147
148 $bin = '/home/wikipedia/bin';
149 if ( $wgUseRootUser ) {
150 $wgDBuser = $wgDBadminuser = 'root';
151 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
152 } elseif ( !$wgUseNormalUser ) {
153 $wgDBuser = $wgDBadminuser = 'wikiadmin';
154 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
155 }
156 } else {
157 $wgWikiFarm = false;
158 if ( isset( $options['conf'] ) ) {
159 $settingsFile = $options['conf'];
160 } else {
161 $settingsFile = "$IP/LocalSettings.php";
162 }
163
164 if ( ! is_readable( $settingsFile ) ) {
165 print "A copy of your installation's LocalSettings.php\n" .
166 "must exist in the source directory.\n";
167 exit( 1 );
168 }
169 $wgCommandLineMode = true;
170 $DP = $IP;
171 #require_once( $IP.'/includes/ProfilerStub.php' );
172 require_once( $IP.'/includes/Defines.php' );
173 require_once( $settingsFile );
174 ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
175
176 if ( is_readable( $IP.'/AdminSettings.php' ) ) {
177 require_once( $IP.'/AdminSettings.php' );
178 }
179 }
180
181 # Turn off output buffering again, it might have been turned on in the settings files
182 @ob_end_flush();
183 # Same with these
184 $wgCommandLineMode = true;
185
186 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
187 $wgDBuser = $wgDBadminuser;
188 $wgDBpassword = $wgDBadminpassword;
189
190 if( $wgDBservers ) {
191 foreach ( $wgDBservers as $i => $server ) {
192 $wgDBservers[$i]['user'] = $wgDBuser;
193 $wgDBservers[$i]['password'] = $wgDBpassword;
194 }
195 }
196 }
197
198 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
199 $fn = MW_CMDLINE_CALLBACK;
200 $fn();
201 }
202
203 ini_set( 'memory_limit', -1 );
204
205 $wgShowSQLErrors = true;
206
207 require_once( 'Setup.php' );
208 require_once( 'install-utils.inc' );
209 $wgTitle = null; # Much much faster startup than creating a title object
210 set_time_limit(0);
211
212 // --------------------------------------------------------------------
213 // Functions
214 // --------------------------------------------------------------------
215
216 function wfWaitForSlaves( $maxLag ) {
217 global $wgLoadBalancer;
218 if ( $maxLag ) {
219 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
220 while ( $lag > $maxLag ) {
221 $name = @gethostbyaddr( $host );
222 if ( $name !== false ) {
223 $host = $name;
224 }
225 print "Waiting for $host (lagged $lag seconds)...\n";
226 sleep($maxLag);
227 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
228 }
229 }
230 }
231
232
233
234 ?>