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