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