2bb5389e82a716f3e4705e4766d608d0a84a7b29
[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 $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 ( $arg == '--' ) {
43 # End of options, remainder should be considered arguments
44 $arg = next( $argv );
45 while( $arg !== false ) {
46 $args[] = $arg;
47 $arg = next( $argv );
48 }
49 break;
50 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
51 # Long options
52 $option = substr( $arg, 2 );
53 if ( in_array( $option, $optionsWithArgs ) ) {
54 $param = next( $argv );
55 if ( $param === false ) {
56 echo "$arg needs an value after it\n";
57 die( -1 );
58 }
59 $options[$option] = $param;
60 } else {
61 $bits = explode( '=', $option, 2 );
62 if( count( $bits ) > 1 ) {
63 $option = $bits[0];
64 $param = $bits[1];
65 } else {
66 $param = 1;
67 }
68 $options[$option] = $param;
69 }
70 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
71 # Short options
72 for ( $p=1; $p<strlen( $arg ); $p++ ) {
73 $option = $arg{$p};
74 if ( in_array( $option, $optionsWithArgs ) ) {
75 $param = next( $argv );
76 if ( $param === false ) {
77 echo "$arg needs an value after it\n";
78 die( -1 );
79 }
80 $options[$option] = $param;
81 } else {
82 $options[$option] = 1;
83 }
84 }
85 } else {
86 $args[] = $arg;
87 }
88 }
89
90
91 # General initialisation
92
93 $wgCommandLineMode = true;
94 # Turn off output buffering if it's on
95 @ob_end_flush();
96 $sep = PATH_SEPARATOR;
97
98 if (!isset( $wgUseNormalUser ) ) {
99 $wgUseNormalUser = false;
100 }
101
102 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
103 $wgWikiFarm = true;
104 $cluster = trim( file_get_contents( '/etc/cluster' ) );
105 require_once( "$IP/includes/SiteConfiguration.php" );
106
107 # Get $wgConf
108 require( "$IP/wgConf.php" );
109
110 if ( empty( $wgNoDBParam ) ) {
111 # Check if we were passed a db name
112 $db = array_shift( $args );
113 list( $site, $lang ) = $wgConf->siteFromDB( $db );
114
115 # If not, work out the language and site the old way
116 if ( is_null( $site ) || is_null( $lang ) ) {
117 if ( !$db ) {
118 $lang = 'aa';
119 } else {
120 $lang = $db;
121 }
122 if ( isset( $args[0] ) ) {
123 $site = array_shift( $args );
124 } else {
125 $site = 'wikipedia';
126 }
127 }
128 } else {
129 $lang = 'aa';
130 $site = 'wikipedia';
131 }
132
133 # This is for the IRC scripts, which now run as the apache user
134 # The apache user doesn't have access to the wikiadmin_pass command
135 if ( $_ENV['USER'] == 'apache' ) {
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 require_once( 'Setup.php' );
206 require_once( 'install-utils.inc' );
207 $wgTitle = Title::newFromText( 'Command line script' );
208 set_time_limit(0);
209
210 // --------------------------------------------------------------------
211 // Functions
212 // --------------------------------------------------------------------
213
214 function wfWaitForSlaves( $maxLag ) {
215 global $wgLoadBalancer;
216 if ( $maxLag ) {
217 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
218 while ( $lag > $maxLag ) {
219 $name = @gethostbyaddr( $host );
220 if ( $name !== false ) {
221 $host = $name;
222 }
223 print "Waiting for $host (lagged $lag seconds)...\n";
224 sleep($maxLag);
225 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
226 }
227 }
228 }
229
230
231
232 ?>