02ae3689c54424f0e65be4ca1e3bf234f8d1c07c
[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 require_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 require_once( 'GlobalFunctions.php' );
43 require_once( 'Namespace.php' );
44 require_once( 'RecentChange.php' );
45 require_once( 'User.php' );
46 require_once( 'Skin.php' );
47 require_once( 'OutputPage.php' );
48 require_once( 'LinkCache.php' );
49 require_once( 'Title.php' );
50 require_once( 'Article.php' );
51 require_once( 'MagicWord.php' );
52 require_once( 'memcached-client.php' );
53 require_once( 'Block.php' );
54 require_once( 'SearchEngine.php' );
55 require_once( 'DifferenceEngine.php' );
56 require_once( 'MessageCache.php' );
57 require_once( 'BlockCache.php' );
58 require_once( 'Parser.php' );
59 require_once( 'ParserCache.php' );
60 require_once( 'WebRequest.php' );
61 require_once( 'LoadBalancer.php' );
62
63 $wgRequest = new WebRequest();
64
65
66
67 wfProfileOut( $fname.'-includes' );
68 wfProfileIn( $fname.'-memcached' );
69 global $wgUser, $wgLang, $wgOut, $wgTitle;
70 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
71 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
72 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
73 global $wgMsgCacheExpiry, $wgCommandLineMode;
74 global $wgBlockCache, $wgParserCache, $wgParser, $wgDBConnections;
75 global $wgLoadBalancer, $wgDBservers, $wgDebugDumpSql;
76 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype;
77 global $wgFullyInitialised;
78
79 # Useful debug output
80 if ( $wgCommandLineMode ) {
81 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
82 } elseif ( function_exists( 'getallheaders' ) ) {
83 wfDebug( "\nStart request\n" );
84 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
85 $headers = getallheaders();
86 foreach ($headers as $name => $value) {
87 wfDebug( "$name: $value\n" );
88 }
89 wfDebug( "\n" );
90 } else {
91 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
92 }
93
94 # Set up Memcached
95 #
96 class MemCachedClientforWiki extends memcached {
97 function _debugprint( $text ) {
98 wfDebug( "memcached: $text\n" );
99 }
100 }
101
102 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
103 # It acts as a memcached server with no RAM, that is, all objects are
104 # cleared the moment they are set. All set operations succeed and all
105 # get operations return null.
106
107 class FakeMemCachedClient {
108 function add ($key, $val, $exp = 0) { return true; }
109 function decr ($key, $amt=1) { return null; }
110 function delete ($key, $time = 0) { return false; }
111 function disconnect_all () { }
112 function enable_compress ($enable) { }
113 function forget_dead_hosts () { }
114 function get ($key) { return null; }
115 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
116 function incr ($key, $amt=1) { return null; }
117 function replace ($key, $value, $exp=0) { return false; }
118 function run_command ($sock, $cmd) { return null; }
119 function set ($key, $value, $exp=0){ return true; }
120 function set_compress_threshold ($thresh){ }
121 function set_debug ($dbg) { }
122 function set_servers ($list) { }
123 }
124
125 if( $wgUseMemCached ) {
126 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
127 $wgMemc->set_servers( $wgMemCachedServers );
128 $wgMemc->set_debug( $wgMemCachedDebug );
129
130 $messageMemc = &$wgMemc;
131 } else {
132 $wgMemc = new FakeMemCachedClient();
133
134 # Give the message cache a separate cache in the DB.
135 # This is a speedup over separately querying every message used
136 require_once( 'ObjectCache.php' );
137 $messageMemc = new MediaWikiBagOStuff('objectcache');
138 }
139
140 wfProfileOut( $fname.'-memcached' );
141 wfProfileIn( $fname.'-SetupSession' );
142
143 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get('session.name')] ) || isset( $_COOKIE[$wgDBname.'Password'] ) ) ) {
144 User::SetupSession();
145 $wgSessionStarted = true;
146 } else {
147 $wgSessionStarted = false;
148 }
149
150 wfProfileOut( $fname.'-SetupSession' );
151 wfProfileIn( $fname.'-database' );
152
153 if ( !$wgDBservers ) {
154 $wgDBservers = array(array(
155 'host' => $wgDBserver,
156 'user' => $wgDBuser,
157 'password' => $wgDBpassword,
158 'dbname' => $wgDBname,
159 'type' => $wgDBtype,
160 'load' => 1,
161 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
162 ));
163 }
164 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers );
165 $wgLoadBalancer->loadMasterPos();
166
167 wfProfileOut( $fname.'-database' );
168 wfProfileIn( $fname.'-language' );
169 require_once( 'languages/Language.php' );
170
171 $wgMessageCache = new MessageCache;
172
173 $wgLangClass = 'Language' . ucfirst( $wgLanguageCode );
174 if( ! class_exists( $wgLangClass ) || ($wgLanguageCode == 'en' && !$wgUseLatin1) ) {
175 # Default to English/UTF-8
176 require_once( 'languages/LanguageUtf8.php' );
177 $wgLangClass = 'LanguageUtf8';
178 }
179
180 $wgLang = new $wgLangClass();
181 if ( !is_object($wgLang) ) {
182 print "No language class ($wgLang)\N";
183 }
184
185 if( $wgUseLatin1 && $wgLanguageCode != 'en' ) {
186 # For non-UTF-8 non-English.
187 require_once( 'languages/LanguageLatin1.php' );
188 $xxx = new LanguageLatin1( $wgLang );
189 unset( $wgLang );
190 $wgLang = $xxx;
191 }
192 wfProfileOut( $fname.'-language' );
193 wfProfileIn( $fname.'-MessageCache' );
194
195 $wgMessageCache->initialise( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname );
196
197 wfProfileOut( $fname.'-MessageCache' );
198 wfProfileIn( $fname.'-OutputPage' );
199
200 $wgOut = new OutputPage();
201 wfDebug( "\n\n" );
202
203 wfProfileOut( $fname.'-OutputPage' );
204 wfProfileIn( $fname.'-DateFormatter' );
205
206 if ( $wgUseDynamicDates ) {
207 require_once( 'DateFormatter.php' );
208 global $wgDateFormatter;
209 $wgDateFormatter = new DateFormatter;
210 }
211
212 wfProfileOut( $fname.'-DateFormatter' );
213 wfProfileIn( $fname.'-BlockCache' );
214
215 $wgBlockCache = new BlockCache( true );
216
217 wfProfileOut( $fname.'-BlockCache' );
218 wfProfileIn( $fname.'-User' );
219
220 if( $wgCommandLineMode ) {
221 # Used for some maintenance scripts; user session cookies can screw things up
222 # when the database is in an in-between state.
223 $wgUser = new User();
224 } else {
225 $wgUser = User::loadFromSession();
226 }
227
228 wfProfileOut( $fname.'-User' );
229 wfProfileIn( $fname.'-misc' );
230
231 $wgDeferredUpdateList = array();
232 $wgLinkCache = new LinkCache();
233 $wgMagicWords = array();
234 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
235 $wgParserCache = new ParserCache();
236 $wgParser = new Parser();
237 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
238 $wgDBConnections = array();
239 wfSeedRandom();
240
241 # Placeholders in case of DB error
242 $wgTitle = Title::newFromText( wfMsg( 'badtitle' ) );
243 $wgArticle = new Article($wgTitle);
244
245 wfProfileOut( $fname.'-misc' );
246 wfProfileIn( $fname.'-extensions' );
247
248 # Extension setup functions
249 # Entries should be added to this variable during the inclusion
250 # of the extension file. This allows the extension to perform
251 # any necessary initialisation in the fully initialised environment
252 foreach ( $wgExtensionFunctions as $func ) {
253 $func();
254 }
255
256 $wgFullyInitialised = true;
257 wfProfileOut( $fname.'-extensions' );
258 wfProfileOut( $fname );
259
260
261 ?>