eAccelerator caching support, patches from Jamie Bliss
[mediawiki.git] / includes / Setup.php
blob2d483296801846f522f5e9157eeee42832bbeff8
1 <?php
2 /**
3 * Include most things that's need to customize the site
4 * @package MediaWiki
5 */
7 /**
8 * This file is not a valid entry point, perform no further processing unless
9 * MEDIAWIKI is defined
11 if( defined( 'MEDIAWIKI' ) ) {
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.
19 global $wgProfiling, $wgProfileSampleRate, $wgIP, $wgUseSquid, $IP;
21 if( !isset( $wgProfiling ) )
22 $wgProfiling = false;
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() {}
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'] ) );
39 $allsquids = array_merge($wgSquidServers, $wgSquidServersNoPurge);
40 while(in_array(trim(end($hopips)), $allsquids)){
41 array_pop($hopips);
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';
51 $fname = 'Setup.php';
52 wfProfileIn( $fname );
53 global $wgUseDynamicDates;
54 wfProfileIn( $fname.'-includes' );
56 require_once( 'GlobalFunctions.php' );
57 require_once( 'Hooks.php' );
58 require_once( 'Namespace.php' );
59 require_once( 'RecentChange.php' );
60 require_once( 'User.php' );
61 require_once( 'Skin.php' );
62 require_once( 'OutputPage.php' );
63 require_once( 'LinkCache.php' );
64 require_once( 'Title.php' );
65 require_once( 'Article.php' );
66 require_once( 'MagicWord.php' );
67 require_once( 'Block.php' );
68 require_once( 'MessageCache.php' );
69 require_once( 'BlockCache.php' );
70 require_once( 'Parser.php' );
71 require_once( 'Parser.php' );
72 require_once( 'ParserCache.php' );
73 require_once( 'WebRequest.php' );
74 require_once( 'LoadBalancer.php' );
75 require_once( 'HistoryBlob.php' );
77 $wgRequest = new WebRequest();
79 wfProfileOut( $fname.'-includes' );
80 wfProfileIn( $fname.'-misc1' );
81 global $wgUser, $wgLang, $wgContLang, $wgOut, $wgTitle;
82 global $wgLangClass, $wgContLangClass;
83 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
84 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
85 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
86 global $wgMsgCacheExpiry, $wgCommandLineMode;
87 global $wgBlockCache, $wgParserCache, $wgParser, $wgMsgParserOptions;
88 global $wgLoadBalancer, $wgDBservers, $wgDebugDumpSql;
89 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype;
90 global $wgUseOldExistenceCheck, $wgEnablePersistentLC, $wgMasterWaitTimeout;
92 global $wgFullyInitialised;
94 # Useful debug output
95 if ( $wgCommandLineMode ) {
96 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
97 } elseif ( function_exists( 'getallheaders' ) ) {
98 wfDebug( "\nStart request\n" );
99 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
100 $headers = getallheaders();
101 foreach ($headers as $name => $value) {
102 wfDebug( "$name: $value\n" );
104 wfDebug( "\n" );
105 } else {
106 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
109 # Disable linkscc except if the old existence check method is enabled
110 if (!$wgUseOldExistenceCheck) {
111 $wgEnablePersistentLC = false;
114 wfProfileOut( $fname.'-misc1' );
115 wfProfileIn( $fname.'-memcached' );
117 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
118 # It acts as a memcached server with no RAM, that is, all objects are
119 # cleared the moment they are set. All set operations succeed and all
120 # get operations return null.
122 if( $wgUseMemCached ) {
123 # Set up Memcached
125 require_once( 'memcached-client.php' );
129 * @package MediaWiki
131 class MemCachedClientforWiki extends memcached {
132 function _debugprint( $text ) {
133 wfDebug( "memcached: $text\n" );
137 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
138 $wgMemc->set_servers( $wgMemCachedServers );
139 $wgMemc->set_debug( $wgMemCachedDebug );
141 $messageMemc = &$wgMemc;
142 } elseif ( $wgUseTurckShm ) {
143 # Turck shared memory
145 require_once( 'ObjectCache.php' );
146 $wgMemc = new TurckBagOStuff;
147 $messageMemc = &$wgMemc;
148 } elseif ( $wgUseEAccelShm ) {
149 # eAccelerator shared memory
151 require_once( 'ObjectCache.php' );
152 $wgMemc = new eAccelBagOStuff;
153 $messageMemc = &$wgMemc;
154 } else {
156 * No shared memory
157 * @package MediaWiki
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) { }
176 $wgMemc = new FakeMemCachedClient();
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');
184 wfProfileOut( $fname.'-memcached' );
185 wfProfileIn( $fname.'-SetupSession' );
187 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get('session.name')] ) || isset( $_COOKIE[$wgDBname.'Token'] ) ) ) {
188 User::SetupSession();
189 $wgSessionStarted = true;
190 } else {
191 $wgSessionStarted = false;
194 wfProfileOut( $fname.'-SetupSession' );
195 wfProfileIn( $fname.'-database' );
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
208 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers, false, $wgMasterWaitTimeout );
209 $wgLoadBalancer->loadMasterPos();
211 wfProfileOut( $fname.'-database' );
212 wfProfileIn( $fname.'-language1' );
214 require_once( "$IP/languages/Language.php" );
216 wfProfileOut( $fname.'-language1' );
217 wfProfileIn( $fname.'-User' );
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();
226 if( !is_object( $wgAuth ) ) {
227 require_once( 'AuthPlugin.php' );
228 $wgAuth = new AuthPlugin();
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 # Prevent loading User settings from the DB.
236 $wgUser->setLoaded( true );
237 } else {
238 $wgUser = User::loadFromSession();
241 wfProfileOut( $fname.'-User' );
242 wfProfileIn( $fname.'-language2' );
244 function setupLangObj(&$langclass) {
245 global $wgUseLatin1, $IP;
247 if( ! class_exists( $langclass ) ) {
248 # Default to English/UTF-8, or for non-UTF-8, to latin-1
249 $baseclass = 'LanguageUtf8';
250 if( $wgUseLatin1 )
251 $baseclass = 'LanguageLatin1';
252 require_once( "$IP/languages/$baseclass.php" );
253 $lc = strtolower(substr($langclass, 8));
254 $snip = "
255 class $langclass extends $baseclass {
256 function getVariants() {
257 return array(\"$lc\");
261 eval($snip);
264 $lang = new $langclass();
266 return $lang;
269 # $wgLanguageCode may be changed later to fit with user preference.
270 # The content language will remain fixed as per the configuration,
271 # so let's keep it.
272 $wgContLanguageCode = $wgLanguageCode;
273 $wgContLangClass = 'Language' . str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
275 $wgContLang = setupLangObj( $wgContLangClass );
277 // set default user option from content language
278 if( !$wgUser->mDataLoaded ) {
279 $wgUser->loadDefaultFromLanguage();
282 // wgLanguageCode now specifically means the UI language
283 $wgLanguageCode = $wgUser->getOption('language');
284 # Validate $wgLanguageCode, which will soon be sent to an eval()
285 if( empty( $wgLanguageCode ) || !preg_match( '/^[a-z\-]*$/', $wgLanguageCode ) ) {
286 $wgLanguageCode = $wgContLanguageCode;
289 $wgLangClass = 'Language'. str_replace( '-', '_', ucfirst( $wgLanguageCode ) );
291 if( $wgLangClass == $wgContLangClass ) {
292 $wgLang = &$wgContLang;
293 } else {
294 wfSuppressWarnings();
295 include_once("$IP/languages/$wgLangClass.php");
296 wfRestoreWarnings();
298 $wgLang = setupLangObj( $wgLangClass );
301 wfProfileOut( $fname.'-language' );
302 wfProfileIn( $fname.'-MessageCache' );
304 $wgMessageCache = new MessageCache;
305 $wgMessageCache->initialise( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname);
307 wfProfileOut( $fname.'-MessageCache' );
310 # I guess the warning about UI switching might still apply...
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.
316 # To disable it, the easiest thing could be to uncomment the
317 # following; they should effectively disable the UI switch functionality
319 # $wgLangClass = $wgContLangClass;
320 # $wgLanguageCode = $wgContLanguageCode;
321 # $wgLang = $wgContLang;
323 # TODO: Need to change reference to $wgLang to $wgContLang at proper
324 # places, including namespaces, dates in signatures, magic words,
325 # and links
327 # TODO: Need to look at the issue of input/output encoding
331 wfProfileIn( $fname.'-OutputPage' );
333 $wgOut = new OutputPage();
334 wfDebug( "\n\n" );
336 wfProfileOut( $fname.'-OutputPage' );
337 wfProfileIn( $fname.'-DateFormatter' );
339 if ( $wgUseDynamicDates ) {
340 require_once( 'DateFormatter.php' );
341 global $wgDateFormatter;
342 $wgDateFormatter = new DateFormatter;
345 wfProfileOut( $fname.'-DateFormatter' );
346 wfProfileIn( $fname.'-BlockCache' );
348 $wgBlockCache = new BlockCache( true );
350 wfProfileOut( $fname.'-BlockCache' );
351 wfProfileIn( $fname.'-misc2' );
353 $wgDeferredUpdateList = array();
354 $wgLinkCache = new LinkCache();
355 $wgMagicWords = array();
356 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
357 $wgParserCache = new ParserCache( $messageMemc );
359 if ( $wgUseXMLparser ) {
360 require_once( 'ParserXML.php' );
361 $wgParser = new ParserXML();
362 } else {
363 $wgParser = new Parser();
365 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
366 $wgMsgParserOptions = ParserOptions::newFromUser($wgUser);
367 wfSeedRandom();
369 # Placeholders in case of DB error
370 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Error' );
371 $wgArticle = new Article($wgTitle);
373 # Site notice
374 # FIXME: This is an ugly hack, which wastes runtime on cache hits
375 # and raw page views by forcing initialization of the message cache.
376 # Try to fake around it for raw at least:
377 if( !isset( $_REQUEST['action'] ) || $_REQUEST['action'] != 'raw' ) {
378 $notice = wfMsg( 'sitenotice' );
379 if($notice == '&lt;sitenotice&gt;') $notice = '';
380 # Allow individual wikis to turn it off
381 if ( $notice == '-' ) {
382 $wgSiteNotice = '';
383 } else {
384 # if($wgSiteNotice) $notice .= $wgSiteNotice;
385 if ($notice == '') {
386 $notice = $wgSiteNotice;
388 if($notice != '-' && $notice != '') {
389 $specialparser = new Parser();
390 $parserOutput = $specialparser->parse( $notice, $wgTitle, $wgOut->mParserOptions, false );
391 $wgSiteNotice = $parserOutput->getText();
396 wfProfileOut( $fname.'-misc2' );
397 wfProfileIn( $fname.'-extensions' );
399 # Extension setup functions for extensions other than skins
400 # Entries should be added to this variable during the inclusion
401 # of the extension file. This allows the extension to perform
402 # any necessary initialisation in the fully initialised environment
403 foreach ( $wgExtensionFunctions as $func ) {
404 $func();
407 $wgFullyInitialised = true;
408 wfProfileOut( $fname.'-extensions' );
409 wfProfileOut( $fname );