No point in fetching the result in Database::unlock() if we're not using it anyway.
[mediawiki.git] / maintenance / commandLine.inc
blobbc75f9ac91d75ba09b211ff4f8edc2512566e5af
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/SiteConfiguration.php" );
121         # Get $wgConf
122         require( "$IP/wgConf.php" );
124         if ( empty( $wgNoDBParam ) ) {
125                 # Check if we were passed a db name
126                 if ( isset( $options['wiki'] ) ) {
127                         $db = $options['wiki'];
128                 } else {
129                         $db = array_shift( $args );
130                 }
131                 list( $site, $lang ) = $wgConf->siteFromDB( $db );
133                 # If not, work out the language and site the old way
134                 if ( is_null( $site ) || is_null( $lang ) ) {
135                         if ( !$db ) {
136                                 $lang = 'aa';
137                         } else {
138                                 $lang = $db;
139                         }
140                         if ( isset( $args[0] ) ) {
141                                 $site = array_shift( $args );
142                         } else {
143                                 $site = 'wikipedia';
144                         }
145                 }
146         } else {
147                 $lang = 'aa';
148                 $site = 'wikipedia';
149         }
151         # This is for the IRC scripts, which now run as the apache user
152         # The apache user doesn't have access to the wikiadmin_pass command
153         if ( $_ENV['USER'] == 'apache' ) {
154         #if ( posix_geteuid() == 48 ) {
155                 $wgUseNormalUser = true;
156         }
158         putenv( 'wikilang='.$lang);
160         $DP = $IP;
161         ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
163         if ( $lang == 'test' && $site == 'wikipedia' ) {
164                 define( 'TESTWIKI', 1 );
165         }
166         
167         #require_once( $IP.'/includes/ProfilerStub.php' );
168         require_once( $IP.'/includes/Defines.php' );
169         require_once( $IP.'/CommonSettings.php' );
171         $bin = '/home/wikipedia/bin';
172         if ( $wgUseRootUser ) {
173                 $wgDBuser = $wgDBadminuser = 'root';
174                 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
175         } elseif ( !$wgUseNormalUser ) {
176                 $wgDBuser = $wgDBadminuser = 'wikiadmin';
177                 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
178         }
179 } else {
180         $wgWikiFarm = false;
181         if ( isset( $options['conf'] ) ) {
182                 $settingsFile = $options['conf'];
183         } else {
184                 $settingsFile = "$IP/LocalSettings.php";
185         }
186         if ( isset( $options['wiki'] ) ) {
187                 $bits = explode( '-', $options['wiki'] );
188                 if ( count( $bits ) == 1 ) {
189                         $bits[] = '';
190                 }
191                 define( 'MW_DB', $bits[0] );
192                 define( 'MW_PREFIX', $bits[1] );
193         }
195         if ( ! is_readable( $settingsFile ) ) {
196                 print "A copy of your installation's LocalSettings.php\n" .
197                   "must exist and be readable in the source directory.\n";
198                 exit( 1 );
199         }
200         $wgCommandLineMode = true;
201         $DP = $IP;
202         #require_once( $IP.'/includes/ProfilerStub.php' );
203         require_once( $IP.'/includes/Defines.php' );
204         require_once( $settingsFile );
205         /* ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" ); */
207         $adminSettings = isset( $options['aconf'] )
208                 ? $options['aconf']
209                 : "{$IP}/AdminSettings.php";
210         if( is_readable( $adminSettings ) )
211                 require_once( $adminSettings );
215 # Turn off output buffering again, it might have been turned on in the settings files
216 if( ob_get_level() ) {
217         ob_end_flush();
219 # Same with these
220 $wgCommandLineMode = true;
222 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
223         $wgDBuser = $wgDBadminuser;
224         $wgDBpassword = $wgDBadminpassword;
226         if( $wgDBservers ) {
227                 foreach ( $wgDBservers as $i => $server ) {
228                         $wgDBservers[$i]['user'] = $wgDBuser;
229                         $wgDBservers[$i]['password'] = $wgDBpassword;
230                 }
231         }
232         if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
233                 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
234                 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
235         }
238 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
239         $fn = MW_CMDLINE_CALLBACK;
240         $fn();
243 ini_set( 'memory_limit', -1 );
245 if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
246         // Send PHP warnings and errors to stderr instead of stdout.
247         // This aids in diagnosing problems, while keeping messages
248         // out of redirected output.
249         if( ini_get( 'display_errors' ) ) {
250                 ini_set( 'display_errors', 'stderr' );
251         }
252         
253         // Don't touch the setting on earlier versions of PHP,
254         // as setting it would disable output if you'd wanted it.
255         
256         // Note that exceptions are also sent to stderr when
257         // command-line mode is on, regardless of PHP version.
259 $wgShowSQLErrors = true;
261 require_once( "$IP/includes/Setup.php" );
262 require_once( "$IP/install-utils.inc" );
263 $wgTitle = null; # Much much faster startup than creating a title object
264 @set_time_limit(0);