When saving preferences, check hook and authentication plugin, and actually save...
[mediawiki.git] / maintenance / commandLine.inc
blob4466344fe77d6baa174cf3c1e15ff12caf409a68
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 = realpath( dirname( __FILE__ ) . '/..' );
41 #chdir( $IP );
42 require_once( "$IP/StartProfiler.php" );
44 $options = array();
45 $args = array();
48 # Parse arguments
49 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
50         if ( $arg == '--' ) {
51                 # End of options, remainder should be considered arguments
52                 $arg = next( $argv );
53                 while( $arg !== false ) {
54                         $args[] = $arg;
55                         $arg = next( $argv );
56                 }
57                 break;
58         } elseif ( substr( $arg, 0, 2 ) == '--' ) {
59                 # Long options
60                 $option = substr( $arg, 2 );
61                 if ( in_array( $option, $optionsWithArgs ) ) {
62                         $param = next( $argv );
63                         if ( $param === false ) {
64                                 echo "$arg needs an value after it\n";
65                                 die( -1 );
66                         }
67                         $options[$option] = $param;
68                 } else {
69                         $bits = explode( '=', $option, 2 );
70                         if( count( $bits ) > 1 ) {
71                                 $option = $bits[0];
72                                 $param = $bits[1];
73                         } else {
74                                 $param = 1;
75                         }
76                         $options[$option] = $param;
77                 }
78         } elseif ( substr( $arg, 0, 1 ) == '-' ) {
79                 # Short options
80                 for ( $p=1; $p<strlen( $arg ); $p++ ) {
81                         $option = $arg{$p};
82                         if ( in_array( $option, $optionsWithArgs ) ) {
83                                 $param = next( $argv );
84                                 if ( $param === false ) {
85                                         echo "$arg needs an value after it\n";
86                                         die( -1 );
87                                 }
88                                 $options[$option] = $param;
89                         } else {
90                                 $options[$option] = 1;
91                         }
92                 }
93         } else {
94                 $args[] = $arg;
95         }
99 # General initialisation
101 $wgCommandLineMode = true;
102 # Turn off output buffering if it's on
103 @ob_end_flush();
104 $sep = PATH_SEPARATOR;
106 if (!isset( $wgUseNormalUser ) ) {
107         $wgUseNormalUser = false;
110 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
111         $wgWikiFarm = true;
112         $cluster = trim( file_get_contents( '/etc/cluster' ) );
113         require_once( "$IP/includes/SiteConfiguration.php" );
115         # Get $wgConf
116         require( "$IP/wgConf.php" );
118         if ( empty( $wgNoDBParam ) ) {
119                 # Check if we were passed a db name
120                 $db = array_shift( $args );
121                 list( $site, $lang ) = $wgConf->siteFromDB( $db );
123                 # If not, work out the language and site the old way
124                 if ( is_null( $site ) || is_null( $lang ) ) {
125                         if ( !$db ) {
126                                 $lang = 'aa';
127                         } else {
128                                 $lang = $db;
129                         }
130                         if ( isset( $args[0] ) ) {
131                                 $site = array_shift( $args );
132                         } else {
133                                 $site = 'wikipedia';
134                         }
135                 }
136         } else {
137                 $lang = 'aa';
138                 $site = 'wikipedia';
139         }
141         # This is for the IRC scripts, which now run as the apache user
142         # The apache user doesn't have access to the wikiadmin_pass command
143         if ( $_ENV['USER'] == 'apache' ) {
144         #if ( posix_geteuid() == 48 ) {
145                 $wgUseNormalUser = true;
146         }
148         putenv( 'wikilang='.$lang);
150         $DP = $IP;
151         ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
153         #require_once( $IP.'/includes/ProfilerStub.php' );
154         require_once( $IP.'/includes/Defines.php' );
155         require_once( $IP.'/CommonSettings.php' );
157         $bin = '/home/wikipedia/bin';
158         if ( $wgUseRootUser ) {
159                 $wgDBuser = $wgDBadminuser = 'root';
160                 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
161         } elseif ( !$wgUseNormalUser ) {
162                 $wgDBuser = $wgDBadminuser = 'wikiadmin';
163                 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
164         }
165 } else {
166         $wgWikiFarm = false;
167         if ( isset( $options['conf'] ) ) {
168                 $settingsFile = $options['conf'];
169         } else {
170                 $settingsFile = "$IP/LocalSettings.php";
171         }
173         if ( ! is_readable( $settingsFile ) ) {
174                 print "A copy of your installation's LocalSettings.php\n" .
175                   "must exist and be readable in the source directory.\n";
176                 exit( 1 );
177         }
178         $wgCommandLineMode = true;
179         $DP = $IP;
180         #require_once( $IP.'/includes/ProfilerStub.php' );
181         require_once( $IP.'/includes/Defines.php' );
182         require_once( $settingsFile );
183         /* ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" ); */
185         $adminSettings = isset( $options['aconf'] )
186                 ? $options['aconf']
187                 : "{$IP}/AdminSettings.php";
188         if( is_readable( $adminSettings ) )
189                 require_once( $adminSettings );
193 # Turn off output buffering again, it might have been turned on in the settings files
194 if( ob_get_level() ) {
195         ob_end_flush();
197 # Same with these
198 $wgCommandLineMode = true;
200 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
201         $wgDBuser = $wgDBadminuser;
202         $wgDBpassword = $wgDBadminpassword;
204         if( $wgDBservers ) {
205                 foreach ( $wgDBservers as $i => $server ) {
206                         $wgDBservers[$i]['user'] = $wgDBuser;
207                         $wgDBservers[$i]['password'] = $wgDBpassword;
208                 }
209         }
212 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
213         $fn = MW_CMDLINE_CALLBACK;
214         $fn();
217 ini_set( 'memory_limit', -1 );
219 $wgShowSQLErrors = true;
221 require_once( "$IP/includes/Setup.php" );
222 require_once( "$IP/install-utils.inc" );
223 $wgTitle = null; # Much much faster startup than creating a title object
224 set_time_limit(0);
226 // --------------------------------------------------------------------
227 // Functions
228 // --------------------------------------------------------------------
230 function wfWaitForSlaves( $maxLag ) {
231         global $wgLoadBalancer;
232         if ( $maxLag ) {
233                 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
234                 while ( $lag > $maxLag ) {
235                         $name = @gethostbyaddr( $host );
236                         if ( $name !== false ) {
237                                 $host = $name;
238                         }
239                         print "Waiting for $host (lagged $lag seconds)...\n";
240                         sleep($maxLag);
241                         list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
242                 }
243         }