minor command line mode fixes
[lhc/web/wiklou.git] / includes / Setup.php
1 <?php
2 # The main wiki script and things like database
3 # conversion and maintenance scripts all share a
4 # common setup of including lots of classes and
5 # setting up a few globals.
6 #
7
8 global $wgProfiling, $wgProfileSampleRate, $wgIP, $wgUseSquid;
9
10 if( !isset( $wgProfiling ) )
11 $wgProfiling = false;
12
13 if ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
14 include_once( "Profiling.php" );
15 } else {
16 function wfProfileIn( $fn ) {}
17 function wfProfileOut( $fn = "" ) {}
18 function wfGetProfilingOutput( $s, $e ) {}
19 function wfProfileClose() {}
20 }
21
22 /* collect the originating ips */
23 if( $wgUseSquid && isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) ) {
24 # If the web server is behind a reverse proxy, we need to find
25 # out where our requests are really coming from.
26 $hopips = array_map( "trim", explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
27
28 while(in_array(trim(end($hopips)), $wgSquidServers)){
29 array_pop($hopips);
30 }
31 $wgIP = trim(end($hopips));
32 } else {
33 $wgIP = getenv("REMOTE_ADDR");
34 }
35
36
37 $fname = "Setup.php";
38 wfProfileIn( $fname );
39 global $wgUseDynamicDates;
40 wfProfileIn( "$fname-includes" );
41
42 include_once( "GlobalFunctions.php" );
43 include_once( "Namespace.php" );
44 include_once( "RecentChange.php" );
45 include_once( "Skin.php" );
46 include_once( "OutputPage.php" );
47 include_once( "User.php" );
48 include_once( "LinkCache.php" );
49 include_once( "Title.php" );
50 include_once( "Article.php" );
51 include_once( "MagicWord.php" );
52 include_once( "memcached-client.php" );
53 include_once( "Block.php" );
54 include_once( "SearchEngine.php" );
55 include_once( "DifferenceEngine.php" );
56 include_once( "MessageCache.php" );
57 include_once( "BlockCache.php" );
58 include_once( "Parser.php" );
59 include_once( "ParserCache.php" );
60 include_once( "WebRequest.php" );
61 $wgRequest = new WebRequest();
62
63
64 wfProfileOut( "$fname-includes" );
65 wfProfileIn( "$fname-memcached" );
66 global $wgUser, $wgLang, $wgOut, $wgTitle;
67 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
68 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
69 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
70 global $wgMsgCacheExpiry, $wgDBname, $wgCommandLineMode;
71 global $wgBlockCache, $wgParserCache, $wgParser;
72
73 # Useful debug output
74 if ( $wgCommandLineMode ) {
75 wfDebug( '"' . implode( '" "', $argv ) . '"' );
76 } elseif ( function_exists( "getallheaders" ) ) {
77 wfDebug( "\nStart request\n" );
78 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
79 $headers = getallheaders();
80 foreach ($headers as $name => $value) {
81 wfDebug( "$name: $value\n" );
82 }
83 wfDebug( "\n" );
84 } else {
85 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
86 }
87
88 # Set up Memcached
89 #
90 class MemCachedClientforWiki extends memcached {
91 function _debugprint( $text ) {
92 wfDebug( "memcached: $text\n" );
93 }
94 }
95
96 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
97 # It acts as a memcached server with no RAM, that is, all objects are
98 # cleared the moment they are set. All set operations succeed and all
99 # get operations return null.
100
101 class FakeMemCachedClient {
102 function add ($key, $val, $exp = 0) { return true; }
103 function decr ($key, $amt=1) { return null; }
104 function delete ($key, $time = 0) { return false; }
105 function disconnect_all () { }
106 function enable_compress ($enable) { }
107 function forget_dead_hosts () { }
108 function get ($key) { return null; }
109 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
110 function incr ($key, $amt=1) { return null; }
111 function replace ($key, $value, $exp=0) { return false; }
112 function run_command ($sock, $cmd) { return null; }
113 function set ($key, $value, $exp=0){ return true; }
114 function set_compress_threshold ($thresh){ }
115 function set_debug ($dbg) { }
116 function set_servers ($list) { }
117 }
118
119 if( $wgUseMemCached ) {
120 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
121 $wgMemc->set_servers( $wgMemCachedServers );
122 $wgMemc->set_debug( $wgMemCachedDebug );
123
124 # Test it to see if it's working
125 # This is necessary because otherwise wfMsg would be extremely inefficient
126 if ( !$wgMemc->set( "test", "", 0 ) ) {
127 wfDebug( "Memcached failed setup test - connection error?\n" );
128 $wgUseMemCached = false;
129 $wgMemc = new FakeMemCachedClient();
130 }
131 } else {
132 $wgMemc = new FakeMemCachedClient();
133 }
134
135 wfProfileOut( "$fname-memcached" );
136 wfProfileIn( "$fname-misc" );
137
138 include_once( "Language.php" );
139
140 $wgMessageCache = new MessageCache;
141
142 $wgLangClass = "Language" . ucfirst( $wgLanguageCode );
143 if( ! class_exists( $wgLangClass ) ) {
144 include_once( "LanguageUtf8.php" );
145 $wgLangClass = "LanguageUtf8";
146 }
147
148 $wgLang = new $wgLangClass();
149 if ( !is_object($wgLang) ) {
150 print "No language class ($wgLang)\N";
151 }
152 $wgMessageCache->initialise( $wgUseMemCached, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname );
153
154 $wgOut = new OutputPage();
155 wfDebug( "\n\n" );
156
157 if ( $wgUseDynamicDates ) {
158 include_once( "DateFormatter.php" );
159 global $wgDateFormatter;
160 $wgDateFormatter = new DateFormatter;
161 }
162
163 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get("session.name")] ) || isset( $_COOKIE["{$wgDBname}Password"] ) ) ) {
164 User::SetupSession();
165 }
166
167 $wgBlockCache = new BlockCache( true );
168 $wgUser = User::loadFromSession();
169 $wgDeferredUpdateList = array();
170 $wgLinkCache = new LinkCache();
171 $wgMagicWords = array();
172 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
173 $wgParserCache = new ParserCache();
174 $wgParser = new Parser();
175 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
176
177 wfProfileOut( "$fname-misc" );
178 wfProfileOut( $fname );
179
180 ?>