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