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