Job::pop() may occasionally return false when there are actually more jobs remaining...
[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 $cluster = trim( file_get_contents( '/etc/cluster' ) );
95 require_once( "$IP/includes/SiteConfiguration.php" );
96
97 # Get $wgConf
98 require( "$IP/wgConf.php" );
99
100 if ( empty( $wgNoDBParam ) ) {
101 # Check if we were passed a db name
102 $db = array_shift( $args );
103 list( $site, $lang ) = $wgConf->siteFromDB( $db );
104
105 # If not, work out the language and site the old way
106 if ( is_null( $site ) || is_null( $lang ) ) {
107 if ( !$db ) {
108 $lang = 'aa';
109 } else {
110 $lang = $db;
111 }
112 if ( isset( $args[0] ) ) {
113 $site = array_shift( $args );
114 } else {
115 $site = 'wikipedia';
116 }
117 }
118 } else {
119 $lang = 'aa';
120 $site = 'wikipedia';
121 }
122
123 # This is for the IRC scripts, which now run as the apache user
124 # The apache user doesn't have access to the wikiadmin_pass command
125 if ( $_ENV['USER'] == 'apache' ) {
126 $wgUseNormalUser = true;
127 }
128
129 putenv( 'wikilang='.$lang);
130
131 $DP = $IP;
132 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
133
134 require_once( $IP.'/includes/ProfilerStub.php' );
135 require_once( $IP.'/includes/Defines.php' );
136 require_once( $IP.'/CommonSettings.php' );
137
138 $bin = '/home/wikipedia/bin';
139 if ( $wgUseRootUser ) {
140 $wgDBuser = $wgDBadminuser = 'root';
141 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
142 } elseif ( !$wgUseNormalUser ) {
143 $wgDBuser = $wgDBadminuser = 'wikiadmin';
144 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
145 }
146 } else {
147 $wgWikiFarm = false;
148 if ( isset( $options['conf'] ) ) {
149 $settingsFile = $options['conf'];
150 } else {
151 $settingsFile = "$IP/LocalSettings.php";
152 }
153
154 if ( ! is_readable( $settingsFile ) ) {
155 print "A copy of your installation's LocalSettings.php\n" .
156 "must exist in the source directory.\n";
157 exit();
158 }
159 $wgCommandLineMode = true;
160 $DP = $IP;
161 require_once( $IP.'/includes/ProfilerStub.php' );
162 require_once( $IP.'/includes/Defines.php' );
163 require_once( $settingsFile );
164 ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
165
166 if ( is_readable( $IP.'/AdminSettings.php' ) ) {
167 require_once( $IP.'/AdminSettings.php' );
168 }
169 }
170
171 # Turn off output buffering again, it might have been turned on in the settings files
172 @ob_end_flush();
173 # Same with these
174 $wgCommandLineMode = true;
175
176 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
177 $wgDBuser = $wgDBadminuser;
178 $wgDBpassword = $wgDBadminpassword;
179
180 foreach ( $wgDBservers as $i => $server ) {
181 $wgDBservers[$i]['user'] = $wgDBuser;
182 $wgDBservers[$i]['password'] = $wgDBpassword;
183 }
184 }
185
186 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
187 $fn = MW_CMDLINE_CALLBACK;
188 $fn();
189 }
190
191 ini_set( 'memory_limit', -1 );
192
193 require_once( 'Setup.php' );
194 require_once( 'install-utils.inc' );
195 $wgTitle = Title::newFromText( 'Command line script' );
196 set_time_limit(0);
197
198 // --------------------------------------------------------------------
199 // Functions
200 // --------------------------------------------------------------------
201
202 function wfWaitForSlaves( $maxLag ) {
203 global $wgLoadBalancer;
204 if ( $maxLag ) {
205 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
206 while ( $lag > $maxLag ) {
207 $name = @gethostbyaddr( $host );
208 if ( $name !== false ) {
209 $host = $name;
210 }
211 print "Waiting for $host (lagged $lag seconds)...\n";
212 sleep($maxLag);
213 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
214 }
215 }
216 }
217
218
219
220 ?>