apply live hacks from wikimedia site:
[lhc/web/wiklou.git] / maintenance / commandLine.inc
1 <?php
2 /**
3 * @todo document
4 * @addtogroup Maintenance
5 */
6
7 $wgRequestTime = microtime(true);
8
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();
14 }
15
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 );
23 }
24
25 define('MEDIAWIKI',true);
26
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
32
33 if ( !isset( $optionsWithArgs ) ) {
34 $optionsWithArgs = array();
35 }
36 $optionsWithArgs[] = 'conf'; # For specifying the location of LocalSettings.php
37 $optionsWithArgs[] = 'aconf'; # As above for AdminSettings.php
38
39 $self = array_shift( $argv );
40 $IP = ( getenv('MW_INSTALL_PATH') !== false
41 ? getenv('MW_INSTALL_PATH')
42 : realpath( dirname( __FILE__ ) . '/..' ) );
43 #chdir( $IP );
44 require_once( "$IP/StartProfiler.php" );
45
46 $options = array();
47 $args = array();
48
49
50 # Parse arguments
51 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
52 if ( $arg == '--' ) {
53 # End of options, remainder should be considered arguments
54 $arg = next( $argv );
55 while( $arg !== false ) {
56 $args[] = $arg;
57 $arg = next( $argv );
58 }
59 break;
60 } elseif ( substr( $arg, 0, 2 ) == '--' ) {
61 # Long options
62 $option = substr( $arg, 2 );
63 if ( in_array( $option, $optionsWithArgs ) ) {
64 $param = next( $argv );
65 if ( $param === false ) {
66 echo "$arg needs a value after it\n";
67 die( -1 );
68 }
69 $options[$option] = $param;
70 } else {
71 $bits = explode( '=', $option, 2 );
72 if( count( $bits ) > 1 ) {
73 $option = $bits[0];
74 $param = $bits[1];
75 } else {
76 $param = 1;
77 }
78 $options[$option] = $param;
79 }
80 } elseif ( substr( $arg, 0, 1 ) == '-' ) {
81 # Short options
82 for ( $p=1; $p<strlen( $arg ); $p++ ) {
83 $option = $arg{$p};
84 if ( in_array( $option, $optionsWithArgs ) ) {
85 $param = next( $argv );
86 if ( $param === false ) {
87 echo "$arg needs a value after it\n";
88 die( -1 );
89 }
90 $options[$option] = $param;
91 } else {
92 $options[$option] = 1;
93 }
94 }
95 } else {
96 $args[] = $arg;
97 }
98 }
99
100
101 # General initialisation
102
103 $wgCommandLineMode = true;
104 # Turn off output buffering if it's on
105 @ob_end_flush();
106 $sep = PATH_SEPARATOR;
107
108 if (!isset( $wgUseNormalUser ) ) {
109 $wgUseNormalUser = false;
110 }
111
112 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
113 $wgWikiFarm = true;
114 #$cluster = trim( file_get_contents( '/etc/cluster' ) );
115 $cluster = 'pmtpa';
116 require_once( "$IP/includes/SiteConfiguration.php" );
117
118 # Get $wgConf
119 require( "$IP/wgConf.php" );
120
121 if ( empty( $wgNoDBParam ) ) {
122 # Check if we were passed a db name
123 $db = array_shift( $args );
124 list( $site, $lang ) = $wgConf->siteFromDB( $db );
125
126 # If not, work out the language and site the old way
127 if ( is_null( $site ) || is_null( $lang ) ) {
128 if ( !$db ) {
129 $lang = 'aa';
130 } else {
131 $lang = $db;
132 }
133 if ( isset( $args[0] ) ) {
134 $site = array_shift( $args );
135 } else {
136 $site = 'wikipedia';
137 }
138 }
139 } else {
140 $lang = 'aa';
141 $site = 'wikipedia';
142 }
143
144 # This is for the IRC scripts, which now run as the apache user
145 # The apache user doesn't have access to the wikiadmin_pass command
146 if ( $_ENV['USER'] == 'apache' ) {
147 #if ( posix_geteuid() == 48 ) {
148 $wgUseNormalUser = true;
149 }
150
151 putenv( 'wikilang='.$lang);
152
153 $DP = $IP;
154 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
155
156 if ( $lang == 'test' && $site == 'wikipedia' ) {
157 define( 'TESTWIKI', 1 );
158 }
159
160 #require_once( $IP.'/includes/ProfilerStub.php' );
161 require_once( $IP.'/includes/Defines.php' );
162 require_once( $IP.'/CommonSettings.php' );
163
164 $bin = '/home/wikipedia/bin';
165 if ( $wgUseRootUser ) {
166 $wgDBuser = $wgDBadminuser = 'root';
167 $wgDBpassword = $wgDBadminpassword = trim(`$bin/mysql_root_pass`);
168 } elseif ( !$wgUseNormalUser ) {
169 $wgDBuser = $wgDBadminuser = 'wikiadmin';
170 $wgDBpassword = $wgDBadminpassword = trim(`$bin/wikiadmin_pass`);
171 }
172 } else {
173 $wgWikiFarm = false;
174 if ( isset( $options['conf'] ) ) {
175 $settingsFile = $options['conf'];
176 } else {
177 $settingsFile = "$IP/LocalSettings.php";
178 }
179
180 if ( ! is_readable( $settingsFile ) ) {
181 print "A copy of your installation's LocalSettings.php\n" .
182 "must exist and be readable in the source directory.\n";
183 exit( 1 );
184 }
185 $wgCommandLineMode = true;
186 $DP = $IP;
187 #require_once( $IP.'/includes/ProfilerStub.php' );
188 require_once( $IP.'/includes/Defines.php' );
189 require_once( $settingsFile );
190 /* ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" ); */
191
192 $adminSettings = isset( $options['aconf'] )
193 ? $options['aconf']
194 : "{$IP}/AdminSettings.php";
195 if( is_readable( $adminSettings ) )
196 require_once( $adminSettings );
197
198 }
199
200 # Turn off output buffering again, it might have been turned on in the settings files
201 if( ob_get_level() ) {
202 ob_end_flush();
203 }
204 # Same with these
205 $wgCommandLineMode = true;
206
207 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
208 $wgDBuser = $wgDBadminuser;
209 $wgDBpassword = $wgDBadminpassword;
210
211 if( $wgDBservers ) {
212 foreach ( $wgDBservers as $i => $server ) {
213 $wgDBservers[$i]['user'] = $wgDBuser;
214 $wgDBservers[$i]['password'] = $wgDBpassword;
215 }
216 }
217 if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
218 $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
219 $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
220 }
221 }
222
223 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
224 $fn = MW_CMDLINE_CALLBACK;
225 $fn();
226 }
227
228 ini_set( 'memory_limit', -1 );
229
230 if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
231 // Send PHP warnings and errors to stderr instead of stdout.
232 // This aids in diagnosing problems, while keeping messages
233 // out of redirected output.
234 if( ini_get( 'display_errors' ) ) {
235 ini_set( 'display_errors', 'stderr' );
236 }
237
238 // Don't touch the setting on earlier versions of PHP,
239 // as setting it would disable output if you'd wanted it.
240
241 // Note that exceptions are also sent to stderr when
242 // command-line mode is on, regardless of PHP version.
243 }
244 $wgShowSQLErrors = true;
245
246 require_once( "$IP/includes/Setup.php" );
247 require_once( "$IP/install-utils.inc" );
248 $wgTitle = null; # Much much faster startup than creating a title object
249 @set_time_limit(0);