ported refreshLinks.php improvements (and associated change to commandLine.inc and...
[lhc/web/wiklou.git] / maintenance / commandLine.inc
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage Maintenance
6 */
7
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();
13 }
14
15 define("MEDIAWIKI",true);
16
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
22
23 if ( !isset( $optionsWithArgs ) ) {
24 $optionsWithArgs = array();
25 }
26
27 $self = array_shift( $argv );
28 $IP = realpath( dirname( $self ) . "/.." );
29 chdir( $IP );
30
31 $options = array();
32 $args = array();
33
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 }
71 }
72
73 # General initialisation
74
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" ), ";" ) ? ";" : ":";
79
80 if ( $sep == ":" && strpos( `hostname`, "wikimedia.org" ) !== false ) {
81 $wgWikiFarm = true;
82 require_once( "$IP/includes/SiteConfiguration.php" );
83
84 # Get $conf
85 require( "$IP/InitialiseSettings.php" );
86
87 # Check if we were passed a db name
88 $db = array_shift( $args );
89 list( $site, $lang ) = $conf->siteFromDB( $db );
90
91 # If not, work out the language and site the old way
92 if ( is_null( $site ) || is_null( $lang ) ) {
93 if ( !$db ) {
94 $lang = "aa";
95 } else {
96 $lang = $db;
97 }
98 if ( isset( $args[0] ) ) {
99 $site = array_shift( $args );
100 } else {
101 $site = "wikipedia";
102 }
103 }
104
105 # This is for the IRC scripts, which now run as the apache user
106 # The apache user doesn't have access to the wikiadmin_pass command
107 if ( $_ENV['USER'] != "apache" ) {
108 $wgDBuser = $wgDBadminuser = "wikiadmin";
109 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
110 }
111
112 putenv( "wikilang=$lang");
113
114 $DP = $IP;
115 ini_set( "include_path", ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
116
117 require_once( "$IP/includes/Defines.php" );
118 require_once( "/home/wikipedia/common/php-1.4/CommonSettings.php" );
119 } else {
120 $wgWikiFarm = false;
121 $settingsFile = "$IP/LocalSettings.php";
122
123 if ( ! is_readable( $settingsFile ) ) {
124 print "A copy of your installation's LocalSettings.php\n" .
125 "must exist in the source directory.\n";
126 exit();
127 }
128 $wgCommandLineMode = true;
129 $DP = $IP;
130 require_once( "$IP/includes/Defines.php" );
131 require_once( $settingsFile );
132 ini_set( "include_path", ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
133
134 if ( is_readable( "$IP/AdminSettings.php" ) ) {
135 require_once( "$IP/AdminSettings.php" );
136 }
137 }
138
139 # Turn off output buffering again, it might have been turned on in the settings files
140 @ob_end_flush();
141 # Same with these
142 $wgCommandLineMode = true;
143 $wgDBuser = $wgDBadminuser;
144 $wgDBpassword = $wgDBadminpassword;
145
146 if ( !empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
147 $wgDBuser = $wgDBadminuser;
148 $wgDBpassword = $wgDBadminpassword;
149
150 foreach ( $wgDBservers as $i => $server ) {
151 $wgDBservers[$i]['user'] = $wgDBuser;
152 $wgDBservers[$i]['password'] = $wgDBpassword;
153 }
154 }
155
156 ini_set( 'memory_limit', -1 );
157
158 require_once( "Setup.php" );
159 require_once( "install-utils.inc" );
160 $wgTitle = Title::newFromText( "Command line script" );
161 set_time_limit(0);
162
163 // --------------------------------------------------------------------
164 // Functions
165 // --------------------------------------------------------------------
166
167 function wfWaitForSlaves( $maxLag ) {
168 global $wgLoadBalancer;
169 if ( $maxLag ) {
170 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
171 while ( $lag > $maxLag ) {
172 $name = @gethostbyaddr( $host );
173 if ( $name !== false ) {
174 $host = $name;
175 }
176 print "Waiting for $host (lagged $lag seconds)...\n";
177 sleep($maxLag);
178 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
179 }
180 }
181 }
182
183
184
185 ?>