updated warning about the UI switching code.
[lhc/web/wiklou.git] / includes / Setup.php
1 <?php
2 /**
3 * Include most things that's need to customize the site
4 * @package MediaWiki
5 */
6
7 /**
8 * This file is not a valid entry point, perform no further processing unless
9 * MEDIAWIKI is defined
10 */
11 if( defined( 'MEDIAWIKI' ) ) {
12
13 # The main wiki script and things like database
14 # conversion and maintenance scripts all share a
15 # common setup of including lots of classes and
16 # setting up a few globals.
17 #
18
19 global $wgProfiling, $wgProfileSampleRate, $wgIP, $wgUseSquid;
20
21 if( !isset( $wgProfiling ) )
22 $wgProfiling = false;
23
24 if ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
25 require_once( 'Profiling.php' );
26 } else {
27 function wfProfileIn( $fn = '' ) {}
28 function wfProfileOut( $fn = '' ) {}
29 function wfGetProfilingOutput( $s, $e ) {}
30 function wfProfileClose() {}
31 }
32
33 /* collect the originating ips */
34 if( $wgUseSquid && isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
35 # If the web server is behind a reverse proxy, we need to find
36 # out where our requests are really coming from.
37 $hopips = array_map( 'trim', explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
38
39 while(in_array(trim(end($hopips)), $wgSquidServers)){
40 array_pop($hopips);
41 }
42 $wgIP = trim(end($hopips));
43 } elseif( isset( $_SERVER['REMOTE_ADDR'] ) ) {
44 $wgIP = $_SERVER['REMOTE_ADDR'];
45 } else {
46 # Running on CLI?
47 $wgIP = '127.0.0.1';
48 }
49
50
51 $fname = 'Setup.php';
52 wfProfileIn( $fname );
53 global $wgUseDynamicDates;
54 wfProfileIn( $fname.'-includes' );
55
56 require_once( 'GlobalFunctions.php' );
57 require_once( 'Namespace.php' );
58 require_once( 'RecentChange.php' );
59 require_once( 'User.php' );
60 require_once( 'Skin.php' );
61 require_once( 'OutputPage.php' );
62 require_once( 'LinkCache.php' );
63 require_once( 'Title.php' );
64 require_once( 'Article.php' );
65 require_once( 'MagicWord.php' );
66 require_once( 'Block.php' );
67 require_once( 'MessageCache.php' );
68 require_once( 'BlockCache.php' );
69 require_once( 'Parser.php' );
70 require_once( 'ParserCache.php' );
71 require_once( 'WebRequest.php' );
72 require_once( 'LoadBalancer.php' );
73
74 $wgRequest = new WebRequest();
75
76
77
78 wfProfileOut( $fname.'-includes' );
79 wfProfileIn( $fname.'-misc1' );
80 global $wgUser, $wgLang, $wgContLang, $wgOut, $wgTitle;
81 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
82 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
83 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages, $wgContMessageCach;
84 global $wgMsgCacheExpiry, $wgCommandLineMode;
85 global $wgBlockCache, $wgParserCache, $wgParser, $wgDBConnections;
86 global $wgLoadBalancer, $wgDBservers, $wgDebugDumpSql;
87 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype;
88 global $wgUseOldExistenceCheck, $wgEnablePersistentLC;
89
90 global $wgFullyInitialised;
91
92 # Useful debug output
93 if ( $wgCommandLineMode ) {
94 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
95 } elseif ( function_exists( 'getallheaders' ) ) {
96 wfDebug( "\nStart request\n" );
97 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
98 $headers = getallheaders();
99 foreach ($headers as $name => $value) {
100 wfDebug( "$name: $value\n" );
101 }
102 wfDebug( "\n" );
103 } else {
104 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
105 }
106
107 # Disable linkscc except if the old existence check method is enabled
108 if (!$wgUseOldExistenceCheck) {
109 $wgEnablePersistentLC = false;
110 }
111
112 wfProfileOut( $fname.'-misc1' );
113 wfProfileIn( $fname.'-memcached' );
114
115 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
116 # It acts as a memcached server with no RAM, that is, all objects are
117 # cleared the moment they are set. All set operations succeed and all
118 # get operations return null.
119
120 if( $wgUseMemCached ) {
121 # Set up Memcached
122 #
123 require_once( 'memcached-client.php' );
124
125 /**
126 *
127 * @package MediaWiki
128 */
129 class MemCachedClientforWiki extends memcached {
130 function _debugprint( $text ) {
131 wfDebug( "memcached: $text\n" );
132 }
133 }
134
135 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
136 $wgMemc->set_servers( $wgMemCachedServers );
137 $wgMemc->set_debug( $wgMemCachedDebug );
138
139 $messageMemc = &$wgMemc;
140 } elseif ( $wgUseTurckShm ) {
141 # Turck shared memory
142 #
143 require_once( 'ObjectCache.php' );
144 $wgMemc = new TurckBagOStuff;
145 $messageMemc = &$wgMemc;
146 } else {
147 /**
148 * No shared memory
149 * @package MediaWiki
150 */
151 class FakeMemCachedClient {
152 function add ($key, $val, $exp = 0) { return true; }
153 function decr ($key, $amt=1) { return null; }
154 function delete ($key, $time = 0) { return false; }
155 function disconnect_all () { }
156 function enable_compress ($enable) { }
157 function forget_dead_hosts () { }
158 function get ($key) { return null; }
159 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
160 function incr ($key, $amt=1) { return null; }
161 function replace ($key, $value, $exp=0) { return false; }
162 function run_command ($sock, $cmd) { return null; }
163 function set ($key, $value, $exp=0){ return true; }
164 function set_compress_threshold ($thresh){ }
165 function set_debug ($dbg) { }
166 function set_servers ($list) { }
167 }
168 $wgMemc = new FakeMemCachedClient();
169
170 # Give the message cache a separate cache in the DB.
171 # This is a speedup over separately querying every message used
172 require_once( 'ObjectCache.php' );
173 $messageMemc = new MediaWikiBagOStuff('objectcache');
174 }
175
176 wfProfileOut( $fname.'-memcached' );
177 wfProfileIn( $fname.'-SetupSession' );
178
179 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get('session.name')] ) || isset( $_COOKIE[$wgDBname.'Password'] ) ) ) {
180 User::SetupSession();
181 $wgSessionStarted = true;
182 } else {
183 $wgSessionStarted = false;
184 }
185
186 wfProfileOut( $fname.'-SetupSession' );
187 wfProfileIn( $fname.'-database' );
188
189 if ( !$wgDBservers ) {
190 $wgDBservers = array(array(
191 'host' => $wgDBserver,
192 'user' => $wgDBuser,
193 'password' => $wgDBpassword,
194 'dbname' => $wgDBname,
195 'type' => $wgDBtype,
196 'load' => 1,
197 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
198 ));
199 }
200 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers );
201 $wgLoadBalancer->loadMasterPos();
202
203 wfProfileOut( $fname.'-database' );
204 wfProfileIn( $fname.'-User' );
205
206 # Extension setup functions
207 # Entries should be added to this variable during the inclusion
208 # of the extension file. This allows the extension to perform
209 # any necessary initialisation in the fully initialised environment
210 foreach ( $wgSkinExtensionFunctions as $func ) {
211 $func();
212 }
213
214 if( $wgCommandLineMode ) {
215 # Used for some maintenance scripts; user session cookies can screw things up
216 # when the database is in an in-between state.
217 $wgUser = new User();
218 } else {
219 $wgUser = User::loadFromSession();
220 }
221
222 wfProfileOut( $fname.'-User' );
223 wfProfileIn( $fname.'-language' );
224
225 function setupLangObj(&$langclass, $langcode) {
226 global $wgUseLatin1;
227
228
229 if( ! class_exists( $langclass ) || ($langcode == 'en' && !$wgUseLatin1) ) {
230 # Default to English/UTF-8
231 require_once( 'languages/LanguageUtf8.php' );
232 $langclass = 'LanguageUtf8';
233 }
234
235 $lang = new $langclass();
236 if ( !is_object($lang) ) {
237 print "No language class ($wgLang)\N";
238 }
239
240 if( $wgUseLatin1 && $langcode != 'en' ) {
241 # For non-UTF-8 non-English.
242 require_once( 'languages/LanguageLatin1.php' );
243 $xxx = new LanguageLatin1( $lang );
244 unset( $lang );
245 $lang = $xxx;
246 }
247 return $lang;
248 }
249
250 require_once( 'languages/Language.php' );
251
252 $wgContLangClass = 'Language' . str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
253
254 $wgContLang = setupLangObj($wgContLangClass, $wgContLangClass);
255 $n = get_class($wgContLang);
256
257 // set default user option from content language
258 if(!$wgUser->mDataLoaded) { $wgUser->loadDefaultFromLanguage(); }
259
260 // wgLanguageCode now specifically means the UI language
261 $wgLanguageCode = $wgUser->getOption('language');
262
263 $wgLangClass = 'Language'. str_replace( '-', '_', ucfirst( $wgLanguageCode ) );
264 if($wgLangClass == $wgContLangClass) {
265 $wgLang = &$wgContLang;
266 }
267 else {
268 include_once("languages/$wgLangClass.php");
269 $wgLang = setupLangObj($wgLangClass, $wgLanguageCode);
270 }
271
272
273 wfProfileOut( $fname.'-language' );
274 wfProfileIn( $fname.'-MessageCache' );
275
276 $wgContMessageCache = new MessageCache;
277 $wgContMessageCache->initialise( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname, $wgContLang, $wgContLanguageCode );
278 if($wgLangClass == $wgContLangClass) {
279 $wgMessageCache = &$wgContMessageCache;
280 }
281 else {
282 $wgMessageCache = new MessageCache;
283 $wgMessageCache->initialise( $messageMemc,false , $wgMsgCacheExpiry, $wgDBname.":$wgLangClass", $wgLang, $wgLanguageCode);
284 }
285
286 wfProfileOut( $fname.'-MessageCache' );
287
288 #
289 # I guess the warning about UI switching might still apply...
290 #
291 # FIXME: THE ABOVE MIGHT BREAK NAMESPACES, VARIABLES,
292 # SEARCH INDEX UPDATES, AND MANY MANY THINGS.
293 # DO NOT USE THIS MODE EXCEPT FOR TESTING RIGHT NOW.
294 #
295 # To disable it, the easiest thing could be to uncomment the
296 # following; they should effectively disable the UI switch functionality
297 #
298 # $wgLangClass = $wgContLangClass;
299 # $wgLanguageCode = $wgContLanguageCode;
300 # $wgLang = $wgContLang;
301 # $wgMessageCache = $wgContMessageCache;
302 #
303
304 wfProfileIn( $fname.'-OutputPage' );
305
306 $wgOut = new OutputPage();
307 wfDebug( "\n\n" );
308
309 wfProfileOut( $fname.'-OutputPage' );
310 wfProfileIn( $fname.'-DateFormatter' );
311
312 if ( $wgUseDynamicDates ) {
313 require_once( 'DateFormatter.php' );
314 global $wgDateFormatter;
315 $wgDateFormatter = new DateFormatter;
316 }
317
318 wfProfileOut( $fname.'-DateFormatter' );
319 wfProfileIn( $fname.'-BlockCache' );
320
321 $wgBlockCache = new BlockCache( true );
322
323 wfProfileOut( $fname.'-BlockCache' );
324 wfProfileIn( $fname.'-misc2' );
325
326 $wgDeferredUpdateList = array();
327 $wgLinkCache = new LinkCache();
328 $wgMagicWords = array();
329 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
330 $wgParserCache = new ParserCache();
331 $wgParser = new Parser();
332 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
333 $wgDBConnections = array();
334 wfSeedRandom();
335
336 # Placeholders in case of DB error
337 $wgTitle = Title::newFromText( wfMsgForContent( 'badtitle' ) );
338 $wgArticle = new Article($wgTitle);
339
340 wfProfileOut( $fname.'-misc2' );
341 wfProfileIn( $fname.'-extensions' );
342
343 # Extension setup functions
344 # Entries should be added to this variable during the inclusion
345 # of the extension file. This allows the extension to perform
346 # any necessary initialisation in the fully initialised environment
347 foreach ( $wgExtensionFunctions as $func ) {
348 $func();
349 }
350
351 $wgFullyInitialised = true;
352 wfProfileOut( $fname.'-extensions' );
353 wfProfileOut( $fname );
354
355 }
356 ?>