* (bug 2118) Added a __LCFIRST__ magic word for forcing the first character of
[mediawiki.git] / maintenance / commandLine.inc
blob86f073184094ea13b2ade1bbecab5967e98c8fca
1 <?php
2 /**
3  * @todo document
4  * @package MediaWiki
5  * @subpackage Maintenance
6  */
8 /** */
9 # Abort if called from a web server
10 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
11         print "This script must be run from the command line\n";
12         exit();
15 define("MEDIAWIKI",true);
17 # Process command line arguments
18 # $options becomes an array with keys set to the option names
19 # $optionsWithArgs is an array of GNU-style options that take an argument. The arguments are returned
20 # in the values of $options.
22 if ( !isset( $optionsWithArgs ) ) {
23         $optionsWithArgs = array();
26 $self = array_shift( $argv );
27 $IP = realpath( dirname( $self ) . "/.." );
28 chdir( $IP );
30 $options = array();
31 $args = array();
33 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
34         if ( substr( $arg, 0, 2 ) == '--' ) {
35                 # Long options
36                 $option = substr( $arg, 2 );
37                 if ( in_array( $option, $optionsWithArgs ) ) {
38                         $param = next( $argv );
39                         if ( $param === false ) {
40                                 die( "$arg needs an value after it\n" );
41                         }
42                         $options[$option] = $param;
43                 } else {
44                         $bits = explode( '=', $option, 2 );
45                         if( count( $bits ) > 1 ) {
46                                 $option = $bits[0];
47                                 $param = $bits[1];
48                         } else {
49                                 $param = 1;
50                         }
51                         $options[$option] = $param;
52                 }
53         } elseif ( $arg{0} == '-' ) {
54                 # Short options
55                 for ( $p=1; $p<strlen( $arg ); $p++ ) {
56                         $option = $arg{$p};
57                         if ( in_array( $option, $optionsWithArgs ) ) {
58                                 $param = next( $argv );
59                                 if ( $param === false ) {
60                                         die( "$arg needs an value after it\n" );
61                                 }
62                                 $options[$option] = $param;
63                         } else {
64                                 $options[$option] = 1;
65                         }
66                 }
67         } else {
68                 $args[] = $arg;
69         }
72 # General initialisation
74 $wgCommandLineMode = true;
75 # Turn off output buffering if it's on
76 @ob_end_flush();
77 $sep = strchr( $include_path = ini_get( "include_path" ), ";" ) ? ";" : ":";
79 if ( $sep == ":" && strpos( `hostname`, "wikimedia.org" ) !== false ) {
80         $wgWikiFarm = true;
81         require_once( "$IP/includes/SiteConfiguration.php" );
83         # Get $conf
84         require( "$IP/InitialiseSettings.php" );
86         # Check if we were passed a db name
87         $db = array_shift( $args );
88         list( $site, $lang ) = $conf->siteFromDB( $db );
90         # If not, work out the language and site the old way
91         if ( is_null( $site ) || is_null( $lang ) ) {
92                 if ( !$db ) {   
93                         $lang = "aa";
94                 } else {
95                         $lang = $db;
96                 }
97                 if ( isset( $args[0] ) ) {
98                         $site = array_shift( $args );
99                 } else {
100                         $site = "wikipedia";
101                 }
102         }
104         # This is for the IRC scripts, which now run as the apache user
105         # The apache user doesn't have access to the wikiadmin_pass command
106         if ( $_ENV['USER'] != "apache" ) {
107                 $wgDBuser = $wgDBadminuser = "wikiadmin";
108                 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
109         }
111         putenv( "wikilang=$lang");
113         $DP = $IP;
114         ini_set( "include_path", ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
116         require_once( "$IP/includes/Defines.php" );
117         require_once( "/home/wikipedia/common/php-1.4/CommonSettings.php" );
118 } else {
119         $wgWikiFarm = false;
120         $settingsFile = "$IP/LocalSettings.php";
122         if ( ! is_readable( $settingsFile ) ) {
123                 print "A copy of your installation's LocalSettings.php\n" .
124                   "must exist in the source directory.\n";
125                 exit();
126         }
127         $wgCommandLineMode = true;
128         $DP = $IP;
129         require_once( "$IP/includes/Defines.php" );
130         require_once( $settingsFile );
131         ini_set( "include_path", ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
132         require_once( "$IP/AdminSettings.php" );
135 # Turn off output buffering again, it might have been turned on in the settings files
136 @ob_end_flush();
137 # Same with these
138 $wgCommandLineMode = true;
139 $wgDBuser = $wgDBadminuser;
140 $wgDBpassword = $wgDBadminpassword;
143 require_once( "Setup.php" );
144 require_once( "install-utils.inc" );
145 $wgTitle = Title::newFromText( "Command line script" );
146 set_time_limit(0);