* Fix for r50316 and r50324: require ProfilerStub.php if StartProfiler.php doesn...
[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 $optionsWithArgs[] = 'wiki'; # For specifying the wiki ID
41
42 $self = array_shift( $argv );
43 $IP = strval( getenv('MW_INSTALL_PATH') ) !== ''
44 ? getenv('MW_INSTALL_PATH')
45 : realpath( dirname( __FILE__ ) . '/..' );
46 #chdir( $IP );
47 if ( file_exists( "$IP/StartProfiler.php" ) ) {
48 require_once( "$IP/StartProfiler.php" );
49 } else {
50 require_once( "$IP/includes/ProfilerStub.php" );
51 }
52
53 $options = array();
54 $args = array();
55
56
57 # Parse arguments
58 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
59 if ( $arg == '--' ) {
60 # End of options, remainder should be considered arguments
61 $arg = next( $argv );
62 while( $arg !== false ) {
63 $args[] = $arg;
64 $arg = next( $argv );
65 }
66 break;
67 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
68 # Long options
69 $option = substr( $arg, 2 );
70 if ( in_array( $option, $optionsWithArgs ) ) {
71 $param = next( $argv );
72 if ( $param === false ) {
73 echo "$arg needs a value after it\n";
74 die( -1 );
75 }
76 $options[$option] = $param;
77 } else {
78 $bits = explode( '=', $option, 2 );
79 if( count( $bits ) > 1 ) {
80 $option = $bits[0];
81 $param = $bits[1];
82 } else {
83 $param = 1;
84 }
85 $options[$option] = $param;
86 }
87 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
88 # Short options
89 for ( $p=1; $p<strlen( $arg ); $p++ ) {
90 $option = $arg{$p};
91 if ( in_array( $option, $optionsWithArgs ) ) {
92 $param = next( $argv );
93 if ( $param === false ) {
94 echo "$arg needs a value after it\n";
95 die( -1 );
96 }
97 $options[$option] = $param;
98 } else {
99 $options[$option] = 1;
100 }
101 }
102 } else {
103 $args[] = $arg;
104 }
105 }
106
107
108 # General initialisation
109
110 $wgCommandLineMode = true;
111 # Turn off output buffering if it's on
112 @ob_end_flush();
113 $sep = PATH_SEPARATOR;
114
115 if (!isset( $wgUseNormalUser ) ) {
116 $wgUseNormalUser = false;
117 }
118
119 if ( file_exists( dirname(__FILE__).'/wikimedia-mode' ) ) {
120 $wgWikiFarm = true;
121 $cluster = 'pmtpa';
122 require_once( "$IP/includes/AutoLoader.php" );
123 require_once( "$IP/includes/SiteConfiguration.php" );
124
125 # Get $wgConf
126 require( "$IP/wgConf.php" );
127
128 if ( empty( $wgNoDBParam ) ) {
129 # Check if we were passed a db name
130 if ( isset( $options['wiki'] ) ) {
131 $db = $options['wiki'];
132 } else {
133 $db = array_shift( $args );
134 }
135 list( $site, $lang ) = $wgConf->siteFromDB( $db );
136
137 # If not, work out the language and site the old way
138 if ( is_null( $site ) || is_null( $lang ) ) {
139 if ( !$db ) {
140 $lang = 'aa';
141 } else {
142 $lang = $db;
143 }
144 if ( isset( $args[0] ) ) {
145 $site = array_shift( $args );
146 } else {
147 $site = 'wikipedia';
148 }
149 }
150 } else {
151 $lang = 'aa';
152 $site = 'wikipedia';
153 }
154
155 # This is for the IRC scripts, which now run as the apache user
156 # The apache user doesn't have access to the wikiadmin_pass command
157 if ( $_ENV['USER'] == 'apache' ) {
158 #if ( posix_geteuid() == 48 ) {
159 $wgUseNormalUser = true;
160 }
161
162 putenv( 'wikilang='.$lang);
163
164 $DP = $IP;
165 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
166
167 if ( $lang == 'test' && $site == 'wikipedia' ) {
168 define( 'TESTWIKI', 1 );
169 }
170
171 #require_once( $IP.'/includes/ProfilerStub.php' );
172 require( $IP.'/includes/Defines.php' );
173 require( $IP.'/CommonSettings.php' );
174 if ( !$wgUseNormalUser ) {
175 require( $IP.'/AdminSettings.php' );
176 }
177 } else {
178 $wgWikiFarm = false;
179 if ( isset( $options['conf'] ) ) {
180 $settingsFile = $options['conf'];
181 } else {
182 $settingsFile = "$IP/LocalSettings.php";
183 }
184 if ( isset( $options['wiki'] ) ) {
185 $bits = explode( '-', $options['wiki'] );
186 if ( count( $bits ) == 1 ) {
187 $bits[] = '';
188 }
189 define( 'MW_DB', $bits[0] );
190 define( 'MW_PREFIX', $bits[1] );
191 }
192
193 if ( ! is_readable( $settingsFile ) ) {
194 print "A copy of your installation's LocalSettings.php\n" .
195 "must exist and be readable in the source directory.\n";
196 exit( 1 );
197 }
198 $wgCommandLineMode = true;
199 $DP = $IP;
200 require_once( "$IP/includes/AutoLoader.php" );
201 require_once( "$IP/includes/Defines.php" );
202 require_once( $settingsFile );
203 /* ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" ); */
204
205 $adminSettings = isset( $options['aconf'] )
206 ? $options['aconf']
207 : "{$IP}/AdminSettings.php";
208 if( is_readable( $adminSettings ) )
209 require_once( $adminSettings );
210
211 }
212
213 # Turn off output buffering again, it might have been turned on in the settings files
214 if( ob_get_level() ) {
215 ob_end_flush();
216 }
217 # Same with these
218 $wgCommandLineMode = true;
219
220 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
221 $wgDBuser = $wgDBadminuser;
222 $wgDBpassword = $wgDBadminpassword;
223
224 if( $wgDBservers ) {
225 foreach ( $wgDBservers as $i => $server ) {
226 $wgDBservers[$i]['user'] = $wgDBuser;
227 $wgDBservers[$i]['password'] = $wgDBpassword;
228 }
229 }
230 if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
231 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
232 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
233 }
234 }
235
236 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
237 $fn = MW_CMDLINE_CALLBACK;
238 $fn();
239 }
240
241 ini_set( 'memory_limit', -1 );
242
243 if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
244 // Send PHP warnings and errors to stderr instead of stdout.
245 // This aids in diagnosing problems, while keeping messages
246 // out of redirected output.
247 if( ini_get( 'display_errors' ) ) {
248 ini_set( 'display_errors', 'stderr' );
249 }
250
251 // Don't touch the setting on earlier versions of PHP,
252 // as setting it would disable output if you'd wanted it.
253
254 // Note that exceptions are also sent to stderr when
255 // command-line mode is on, regardless of PHP version.
256 }
257 $wgShowSQLErrors = true;
258
259 require_once( "$IP/includes/Setup.php" );
260 require_once( "$IP/install-utils.inc" );
261 $wgTitle = null; # Much much faster startup than creating a title object
262 @set_time_limit(0);
263
264 $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors