* Fix language selection for upgraded accounts [merge from 1.4]
[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, $IP;
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 if ( $wgUseData ) {
51 $wgExtraNamespaces[20] = 'Data' ;
52 $wgExtraNamespaces[21] = 'Data_talk' ;
53 }
54
55 $fname = 'Setup.php';
56 wfProfileIn( $fname );
57 global $wgUseDynamicDates;
58 wfProfileIn( $fname.'-includes' );
59
60 require_once( 'GlobalFunctions.php' );
61 require_once( 'Hooks.php' );
62 require_once( 'Namespace.php' );
63 require_once( 'RecentChange.php' );
64 require_once( 'User.php' );
65 require_once( 'Skin.php' );
66 require_once( 'OutputPage.php' );
67 require_once( 'LinkCache.php' );
68 require_once( 'Title.php' );
69 require_once( 'Article.php' );
70 require_once( 'MagicWord.php' );
71 require_once( 'Block.php' );
72 require_once( 'MessageCache.php' );
73 require_once( 'BlockCache.php' );
74 require_once( 'Parser.php' );
75 require_once( 'ParserXML.php' );
76 require_once( 'ParserCache.php' );
77 require_once( 'WebRequest.php' );
78 require_once( 'LoadBalancer.php' );
79 require_once( 'HistoryBlob.php' );
80
81 $wgRequest = new WebRequest();
82
83
84
85 wfProfileOut( $fname.'-includes' );
86 wfProfileIn( $fname.'-misc1' );
87 global $wgUser, $wgLang, $wgContLang, $wgOut, $wgTitle;
88 global $wgLangClass, $wgContLangClass;
89 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
90 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
91 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
92 global $wgMsgCacheExpiry, $wgCommandLineMode;
93 global $wgBlockCache, $wgParserCache, $wgParser, $wgMsgParserOptions;
94 global $wgLoadBalancer, $wgDBservers, $wgDebugDumpSql;
95 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype;
96 global $wgUseOldExistenceCheck, $wgEnablePersistentLC;
97
98 global $wgFullyInitialised;
99
100 # Useful debug output
101 if ( $wgCommandLineMode ) {
102 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
103 } elseif ( function_exists( 'getallheaders' ) ) {
104 wfDebug( "\nStart request\n" );
105 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
106 $headers = getallheaders();
107 foreach ($headers as $name => $value) {
108 wfDebug( "$name: $value\n" );
109 }
110 wfDebug( "\n" );
111 } else {
112 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
113 }
114
115 # Disable linkscc except if the old existence check method is enabled
116 if (!$wgUseOldExistenceCheck) {
117 $wgEnablePersistentLC = false;
118 }
119
120 wfProfileOut( $fname.'-misc1' );
121 wfProfileIn( $fname.'-memcached' );
122
123 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
124 # It acts as a memcached server with no RAM, that is, all objects are
125 # cleared the moment they are set. All set operations succeed and all
126 # get operations return null.
127
128 if( $wgUseMemCached ) {
129 # Set up Memcached
130 #
131 require_once( 'memcached-client.php' );
132
133 /**
134 *
135 * @package MediaWiki
136 */
137 class MemCachedClientforWiki extends memcached {
138 function _debugprint( $text ) {
139 wfDebug( "memcached: $text\n" );
140 }
141 }
142
143 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
144 $wgMemc->set_servers( $wgMemCachedServers );
145 $wgMemc->set_debug( $wgMemCachedDebug );
146
147 $messageMemc = &$wgMemc;
148 } elseif ( $wgUseTurckShm ) {
149 # Turck shared memory
150 #
151 require_once( 'ObjectCache.php' );
152 $wgMemc = new TurckBagOStuff;
153 $messageMemc = &$wgMemc;
154 } else {
155 /**
156 * No shared memory
157 * @package MediaWiki
158 */
159 class FakeMemCachedClient {
160 function add ($key, $val, $exp = 0) { return true; }
161 function decr ($key, $amt=1) { return null; }
162 function delete ($key, $time = 0) { return false; }
163 function disconnect_all () { }
164 function enable_compress ($enable) { }
165 function forget_dead_hosts () { }
166 function get ($key) { return null; }
167 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
168 function incr ($key, $amt=1) { return null; }
169 function replace ($key, $value, $exp=0) { return false; }
170 function run_command ($sock, $cmd) { return null; }
171 function set ($key, $value, $exp=0){ return true; }
172 function set_compress_threshold ($thresh){ }
173 function set_debug ($dbg) { }
174 function set_servers ($list) { }
175 }
176 $wgMemc = new FakeMemCachedClient();
177
178 # Give the message cache a separate cache in the DB.
179 # This is a speedup over separately querying every message used
180 require_once( 'ObjectCache.php' );
181 $messageMemc = new MediaWikiBagOStuff('objectcache');
182 }
183
184 wfProfileOut( $fname.'-memcached' );
185 wfProfileIn( $fname.'-SetupSession' );
186
187 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get('session.name')] ) || isset( $_COOKIE[$wgDBname.'Token'] ) ) ) {
188 User::SetupSession();
189 $wgSessionStarted = true;
190 } else {
191 $wgSessionStarted = false;
192 }
193
194 wfProfileOut( $fname.'-SetupSession' );
195 wfProfileIn( $fname.'-database' );
196
197 if ( !$wgDBservers ) {
198 $wgDBservers = array(array(
199 'host' => $wgDBserver,
200 'user' => $wgDBuser,
201 'password' => $wgDBpassword,
202 'dbname' => $wgDBname,
203 'type' => $wgDBtype,
204 'load' => 1,
205 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
206 ));
207 }
208 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers );
209 $wgLoadBalancer->loadMasterPos();
210
211 wfProfileOut( $fname.'-database' );
212 wfProfileIn( $fname.'-language1' );
213
214 require_once( "$IP/languages/Language.php" );
215
216 wfProfileOut( $fname.'-language1' );
217 wfProfileIn( $fname.'-User' );
218
219 # Skin setup functions
220 # Entries can be added to this variable during the inclusion
221 # of the extension file. Skins can then perform any necessary initialisation.
222 foreach ( $wgSkinExtensionFunctions as $func ) {
223 $func();
224 }
225
226 if( !is_object( $wgAuth ) ) {
227 require_once( 'AuthPlugin.php' );
228 $wgAuth = new AuthPlugin();
229 }
230
231 if( $wgCommandLineMode ) {
232 # Used for some maintenance scripts; user session cookies can screw things up
233 # when the database is in an in-between state.
234 $wgUser = new User();
235 } else {
236 $wgUser = User::loadFromSession();
237 }
238
239 wfProfileOut( $fname.'-User' );
240 wfProfileIn( $fname.'-language2' );
241
242 function setupLangObj(&$langclass) {
243 global $wgUseLatin1, $IP;
244
245 if( ! class_exists( $langclass ) ) {
246 # Default to English/UTF-8, or for non-UTF-8, to latin-1
247 $baseclass = 'LanguageUtf8';
248 if( $wgUseLatin1 )
249 $baseclass = 'LanguageLatin1';
250 require_once( "$IP/languages/$baseclass.php" );
251 $lc = strtolower(substr($langclass, 8));
252 $snip = "
253 class $langclass extends $baseclass {
254 function getVariants() {
255 return array(\"$lc\");
256 }
257
258 }";
259
260 eval($snip);
261 }
262
263 $lang = new $langclass();
264
265 return $lang;
266 }
267
268 # $wgLanguageCode may be changed later to fit with user preference.
269 # The content language will remain fixed as per the configuration,
270 # so let's keep it.
271 $wgContLanguageCode = $wgLanguageCode;
272 $wgContLangClass = 'Language' . str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
273
274 $wgContLang = setupLangObj( $wgContLangClass );
275
276 // set default user option from content language
277 if( !$wgUser->mDataLoaded ) {
278 $wgUser->loadDefaultFromLanguage();
279 }
280
281 // wgLanguageCode now specifically means the UI language
282 $wgLanguageCode = $wgUser->getOption('language');
283 if( empty( $wgLanguageCode ) ) {
284 # Quick hack for upgrades where this will be blank,
285 # and it's not handled right. Set to default.
286 $wgLanguageCode = $wgContLanguageCode;
287 }
288
289 $wgLangClass = 'Language'. str_replace( '-', '_', ucfirst( $wgLanguageCode ) );
290
291 if( $wgLangClass == $wgContLangClass ) {
292 $wgLang = &$wgContLang;
293 } else {
294 wfSuppressWarnings();
295 include_once("$IP/languages/$wgLangClass.php");
296 wfRestoreWarnings();
297
298 $wgLang = setupLangObj( $wgLangClass );
299 }
300
301 wfProfileOut( $fname.'-language' );
302 wfProfileIn( $fname.'-MessageCache' );
303
304 $wgMessageCache = new MessageCache;
305 $wgMessageCache->initialise( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname);
306
307 wfProfileOut( $fname.'-MessageCache' );
308
309 #
310 # I guess the warning about UI switching might still apply...
311 #
312 # FIXME: THE ABOVE MIGHT BREAK NAMESPACES, VARIABLES,
313 # SEARCH INDEX UPDATES, AND MANY MANY THINGS.
314 # DO NOT USE THIS MODE EXCEPT FOR TESTING RIGHT NOW.
315 #
316 # To disable it, the easiest thing could be to uncomment the
317 # following; they should effectively disable the UI switch functionality
318 #
319 # $wgLangClass = $wgContLangClass;
320 # $wgLanguageCode = $wgContLanguageCode;
321 # $wgLang = $wgContLang;
322 #
323 # TODO: Need to change reference to $wgLang to $wgContLang at proper
324 # places, including namespaces, dates in signatures, magic words,
325 # and links
326 #
327 # TODO: Need to look at the issue of input/output encoding
328 #
329
330
331 wfProfileIn( $fname.'-OutputPage' );
332
333 $wgOut = new OutputPage();
334 wfDebug( "\n\n" );
335
336 wfProfileOut( $fname.'-OutputPage' );
337 wfProfileIn( $fname.'-DateFormatter' );
338
339 if ( $wgUseDynamicDates ) {
340 require_once( 'DateFormatter.php' );
341 global $wgDateFormatter;
342 $wgDateFormatter = new DateFormatter;
343 }
344
345 wfProfileOut( $fname.'-DateFormatter' );
346 wfProfileIn( $fname.'-BlockCache' );
347
348 $wgBlockCache = new BlockCache( true );
349
350 wfProfileOut( $fname.'-BlockCache' );
351 wfProfileIn( $fname.'-misc2' );
352
353 $wgDeferredUpdateList = array();
354 $wgLinkCache = new LinkCache();
355 $wgMagicWords = array();
356 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
357 $wgParserCache = new ParserCache( $messageMemc );
358
359 if ( $wgUseXMLparser ) $wgParser = new ParserXML();
360 else $wgParser = new Parser();
361 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
362 $wgMsgParserOptions = ParserOptions::newFromUser($wgUser);
363 wfSeedRandom();
364
365 # Placeholders in case of DB error
366 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Error' );
367 $wgArticle = new Article($wgTitle);
368
369 wfProfileOut( $fname.'-misc2' );
370 wfProfileIn( $fname.'-extensions' );
371
372 # Extension setup functions for extensions other than skins
373 # Entries should be added to this variable during the inclusion
374 # of the extension file. This allows the extension to perform
375 # any necessary initialisation in the fully initialised environment
376 foreach ( $wgExtensionFunctions as $func ) {
377 $func();
378 }
379
380 $wgFullyInitialised = true;
381 wfProfileOut( $fname.'-extensions' );
382 wfProfileOut( $fname );
383
384 }
385 ?>