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