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