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