--conf option for specifying a different LocalSettings.php. This allows multiple...
[lhc/web/wiklou.git] / maintenance / commandLine.inc
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage Maintenance
6 */
7
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();
13 }
14
15 define('MEDIAWIKI',true);
16
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
22
23 if ( !isset( $optionsWithArgs ) ) {
24 $optionsWithArgs = array();
25 }
26 $optionsWithArgs[] = 'conf'; # For specifying the location of LocalSettings.php
27
28 $self = array_shift( $argv );
29 $self = __FILE__;
30 $IP = realpath( dirname( $self ) . '/..' );
31 chdir( $IP );
32
33 $options = array();
34 $args = array();
35
36
37 # Parse arguments
38
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 }
78 }
79
80
81 # General initialisation
82
83 $wgCommandLineMode = true;
84 # Turn off output buffering if it's on
85 @ob_end_flush();
86 $sep = PATH_SEPARATOR;
87
88 if (!isset( $wgUseNormalUser ) ) {
89 $wgUseNormalUser = false;
90 }
91
92 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
93 $wgWikiFarm = true;
94 require_once( $IP.'/includes/SiteConfiguration.php' );
95
96 # Get $conf
97 require( $IP.'/InitialiseSettings.php' );
98
99 if ( empty( $wgNoDBParam ) ) {
100 # Check if we were passed a db name
101 $db = array_shift( $args );
102 list( $site, $lang ) = $wgConf->siteFromDB( $db );
103
104 # If not, work out the language and site the old way
105 if ( is_null( $site ) || is_null( $lang ) ) {
106 if ( !$db ) {
107 $lang = 'aa';
108 } else {
109 $lang = $db;
110 }
111 if ( isset( $args[0] ) ) {
112 $site = array_shift( $args );
113 } else {
114 $site = 'wikipedia';
115 }
116 }
117 } else {
118 $lang = 'aa';
119 $site = 'wikipedia';
120 }
121
122 # This is for the IRC scripts, which now run as the apache user
123 # The apache user doesn't have access to the wikiadmin_pass command
124 if ( $_ENV['USER'] == 'apache' ) {
125 $wgUseNormalUser = true;
126 }
127
128 putenv( 'wikilang='.$lang);
129
130 $DP = $IP;
131 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
132
133 require_once( $IP.'/includes/Defines.php' );
134 require_once( $IP.'/CommonSettings.php' );
135
136 if ( $wgUseRootUser ) {
137 $wgDBuser = $wgDBadminuser = 'root';
138 $wgDBpassword = $wgDBadminpassword = trim(`mysql_root_pass`);
139 } elseif ( !$wgUseNormalUser ) {
140 $wgDBuser = $wgDBadminuser = 'wikiadmin';
141 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
142 }
143 } else {
144 $wgWikiFarm = false;
145 if ( isset( $options['conf'] ) ) {
146 $settingsFile = $options['conf'];
147 } else {
148 $settingsFile = "$IP/LocalSettings.php";
149 }
150
151 if ( ! is_readable( $settingsFile ) ) {
152 print "A copy of your installation's LocalSettings.php\n" .
153 "must exist in the source directory.\n";
154 exit();
155 }
156 $wgCommandLineMode = true;
157 $DP = $IP;
158 require_once( $IP.'/includes/Defines.php' );
159 require_once( $settingsFile );
160 ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
161
162 if ( is_readable( $IP.'/AdminSettings.php' ) ) {
163 require_once( $IP.'/AdminSettings.php' );
164 }
165 }
166
167 # Turn off output buffering again, it might have been turned on in the settings files
168 @ob_end_flush();
169 # Same with these
170 $wgCommandLineMode = true;
171
172 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
173 $wgDBuser = $wgDBadminuser;
174 $wgDBpassword = $wgDBadminpassword;
175
176 foreach ( $wgDBservers as $i => $server ) {
177 $wgDBservers[$i]['user'] = $wgDBuser;
178 $wgDBservers[$i]['password'] = $wgDBpassword;
179 }
180 }
181
182 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
183 $fn = MW_CMDLINE_CALLBACK;
184 $fn();
185 }
186
187 ini_set( 'memory_limit', -1 );
188
189 require_once( 'Setup.php' );
190 require_once( 'install-utils.inc' );
191 $wgTitle = Title::newFromText( 'Command line script' );
192 set_time_limit(0);
193
194 // --------------------------------------------------------------------
195 // Functions
196 // --------------------------------------------------------------------
197
198 function wfWaitForSlaves( $maxLag ) {
199 global $wgLoadBalancer;
200 if ( $maxLag ) {
201 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
202 while ( $lag > $maxLag ) {
203 $name = @gethostbyaddr( $host );
204 if ( $name !== false ) {
205 $host = $name;
206 }
207 print "Waiting for $host (lagged $lag seconds)...\n";
208 sleep($maxLag);
209 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
210 }
211 }
212 }
213
214
215
216 ?>