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