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