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