Make export depth > 1 actually work. :) Assuming it doesn't bomb and die using too...
[mediawiki.git] / maintenance / commandLine.inc
blobe83a19160a9532fcedad9c6d4e8858527cb27d81
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 = strval( getenv('MW_INSTALL_PATH') ) !== ''
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( dirname(__FILE__).'/wikimedia-mode' ) ) {
116         $wgWikiFarm = true;
117         $cluster = 'pmtpa';
118         require_once( "$IP/includes/AutoLoader.php" );
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( $IP.'/includes/Defines.php' );
169         require( $IP.'/CommonSettings.php' );
170         if ( !$wgUseNormalUser ) {
171                 require( $IP.'/AdminSettings.php' );
172         }
173 } else {
174         $wgWikiFarm = false;
175         if ( isset( $options['conf'] ) ) {
176                 $settingsFile = $options['conf'];
177         } else {
178                 $settingsFile = "$IP/LocalSettings.php";
179         }
180         if ( isset( $options['wiki'] ) ) {
181                 $bits = explode( '-', $options['wiki'] );
182                 if ( count( $bits ) == 1 ) {
183                         $bits[] = '';
184                 }
185                 define( 'MW_DB', $bits[0] );
186                 define( 'MW_PREFIX', $bits[1] );
187         }
189         if ( ! is_readable( $settingsFile ) ) {
190                 print "A copy of your installation's LocalSettings.php\n" .
191                   "must exist and be readable in the source directory.\n";
192                 exit( 1 );
193         }
194         $wgCommandLineMode = true;
195         $DP = $IP;
196         require_once( "$IP/includes/AutoLoader.php" );
197         #require_once( $IP.'/includes/ProfilerStub.php' );
198         require_once( $IP.'/includes/Defines.php' );
199         require_once( $settingsFile );
200         /* ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" ); */
202         $adminSettings = isset( $options['aconf'] )
203                 ? $options['aconf']
204                 : "{$IP}/AdminSettings.php";
205         if( is_readable( $adminSettings ) )
206                 require_once( $adminSettings );
210 # Turn off output buffering again, it might have been turned on in the settings files
211 if( ob_get_level() ) {
212         ob_end_flush();
214 # Same with these
215 $wgCommandLineMode = true;
217 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
218         $wgDBuser = $wgDBadminuser;
219         $wgDBpassword = $wgDBadminpassword;
221         if( $wgDBservers ) {
222                 foreach ( $wgDBservers as $i => $server ) {
223                         $wgDBservers[$i]['user'] = $wgDBuser;
224                         $wgDBservers[$i]['password'] = $wgDBpassword;
225                 }
226         }
227         if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
228                 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
229                 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
230         }
233 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
234         $fn = MW_CMDLINE_CALLBACK;
235         $fn();
238 ini_set( 'memory_limit', -1 );
240 if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
241         // Send PHP warnings and errors to stderr instead of stdout.
242         // This aids in diagnosing problems, while keeping messages
243         // out of redirected output.
244         if( ini_get( 'display_errors' ) ) {
245                 ini_set( 'display_errors', 'stderr' );
246         }
247         
248         // Don't touch the setting on earlier versions of PHP,
249         // as setting it would disable output if you'd wanted it.
250         
251         // Note that exceptions are also sent to stderr when
252         // command-line mode is on, regardless of PHP version.
254 $wgShowSQLErrors = true;
256 require_once( "$IP/includes/Setup.php" );
257 require_once( "$IP/install-utils.inc" );
258 $wgTitle = null; # Much much faster startup than creating a title object
259 @set_time_limit(0);
261 $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors