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