* (bug 2514) Fixing msg:searchquery, setSubtitle() no longer accepts wikisyntax
[mediawiki.git] / maintenance / commandLine.inc
blob6c6b3d483cb1d1949e26448303c6eafcedc4b5d4
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.
21 # $args becomes a zero-based array containing the non-option arguments
23 if ( !isset( $optionsWithArgs ) ) {
24         $optionsWithArgs = array();
27 $self = array_shift( $argv );
28 $IP = realpath( dirname( $self ) . "/.." );
29 chdir( $IP );
31 $options = array();
32 $args = array();
34 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
35         if ( substr( $arg, 0, 2 ) == '--' ) {
36                 # Long options
37                 $option = substr( $arg, 2 );
38                 if ( in_array( $option, $optionsWithArgs ) ) {
39                         $param = next( $argv );
40                         if ( $param === false ) {
41                                 die( "$arg needs an value after it\n" );
42                         }
43                         $options[$option] = $param;
44                 } else {
45                         $bits = explode( '=', $option, 2 );
46                         if( count( $bits ) > 1 ) {
47                                 $option = $bits[0];
48                                 $param = $bits[1];
49                         } else {
50                                 $param = 1;
51                         }
52                         $options[$option] = $param;
53                 }
54         } elseif ( $arg{0} == '-' ) {
55                 # Short options
56                 for ( $p=1; $p<strlen( $arg ); $p++ ) {
57                         $option = $arg{$p};
58                         if ( in_array( $option, $optionsWithArgs ) ) {
59                                 $param = next( $argv );
60                                 if ( $param === false ) {
61                                         die( "$arg needs an value after it\n" );
62                                 }
63                                 $options[$option] = $param;
64                         } else {
65                                 $options[$option] = 1;
66                         }
67                 }
68         } else {
69                 $args[] = $arg;
70         }
73 # General initialisation
75 $wgCommandLineMode = true;
76 # Turn off output buffering if it's on
77 @ob_end_flush();
78 $sep = strchr( $include_path = ini_get( "include_path" ), ";" ) ? ";" : ":";
80 if ( $sep == ":" && strpos( `hostname`, "wikimedia.org" ) !== false ) {
81         $wgWikiFarm = true;
82         require_once( "$IP/includes/SiteConfiguration.php" );
84         # Get $conf
85         require( "$IP/InitialiseSettings.php" );
87         if ( empty( $wgNoDBParam ) ) {
88                 # Check if we were passed a db name
89                 $db = array_shift( $args );
90                 list( $site, $lang ) = $conf->siteFromDB( $db );
92                 # If not, work out the language and site the old way
93                 if ( is_null( $site ) || is_null( $lang ) ) {
94                         if ( !$db ) {   
95                                 $lang = "aa";
96                         } else {
97                                 $lang = $db;
98                         }
99                         if ( isset( $args[0] ) ) {
100                                 $site = array_shift( $args );
101                         } else {
102                                 $site = "wikipedia";
103                         }
104                 }
105         } else {
106                 $lang = "aa";
107                 $site = "wikipedia";
108         }
110         # This is for the IRC scripts, which now run as the apache user
111         # The apache user doesn't have access to the wikiadmin_pass command
112         if ( $_ENV['USER'] != "apache" ) {
113                 $wgDBuser = $wgDBadminuser = "wikiadmin";
114                 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
115         }
117         putenv( "wikilang=$lang");
119         $DP = $IP;
120         ini_set( "include_path", ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
122         require_once( "$IP/includes/Defines.php" );
123         require_once( "/home/wikipedia/common/php-1.4/CommonSettings.php" );
124 } else {
125         $wgWikiFarm = false;
126         $settingsFile = "$IP/LocalSettings.php";
128         if ( ! is_readable( $settingsFile ) ) {
129                 print "A copy of your installation's LocalSettings.php\n" .
130                   "must exist in the source directory.\n";
131                 exit();
132         }
133         $wgCommandLineMode = true;
134         $DP = $IP;
135         require_once( "$IP/includes/Defines.php" );
136         require_once( $settingsFile );
137         ini_set( "include_path", ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
138         
139         if ( is_readable( "$IP/AdminSettings.php" ) ) {
140                 require_once( "$IP/AdminSettings.php" );
141         }
144 # Turn off output buffering again, it might have been turned on in the settings files
145 @ob_end_flush();
146 # Same with these
147 $wgCommandLineMode = true;
148 $wgDBuser = $wgDBadminuser;
149 $wgDBpassword = $wgDBadminpassword;
151 if ( !empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
152         $wgDBuser = $wgDBadminuser;
153         $wgDBpassword = $wgDBadminpassword;
155         foreach ( $wgDBservers as $i => $server ) {
156                 $wgDBservers[$i]['user'] = $wgDBuser;
157                 $wgDBservers[$i]['password'] = $wgDBpassword;
158         }
161 ini_set( 'memory_limit', -1 );
163 require_once( "Setup.php" );
164 require_once( "install-utils.inc" );
165 $wgTitle = Title::newFromText( "Command line script" );
166 set_time_limit(0);
168 // --------------------------------------------------------------------
169 // Functions
170 // --------------------------------------------------------------------
172 function wfWaitForSlaves( $maxLag ) {
173         global $wgLoadBalancer;
174         if ( $maxLag ) {
175                 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
176                 while ( $lag > $maxLag ) {
177                         $name = @gethostbyaddr( $host );
178                         if ( $name !== false ) {
179                                 $host = $name;
180                         }
181                         print "Waiting for $host (lagged $lag seconds)...\n";
182                         sleep($maxLag);
183                         list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
184                 }
185         }