* Changed inclusion syntax to allow e.g. {{stub}}
[lhc/web/wiklou.git] / includes / Setup.php
1 <?php
2 # The main wiki script and things like database
3 # conversion and maintenance scripts all share a
4 # common setup of including lots of classes and
5 # setting up a few globals.
6 #
7
8 global $wgProfiling, $wgProfileSampleRate, $wgIP, $wgUseSquid;
9
10 if( !isset( $wgProfiling ) )
11 $wgProfiling = false;
12
13 if ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
14 include_once( "Profiling.php" );
15 } else {
16 function wfProfileIn( $fn ) {}
17 function wfProfileOut( $fn = "" ) {}
18 function wfGetProfilingOutput( $s, $e ) {}
19 function wfProfileClose() {}
20 }
21
22 /* collect the originating ips */
23 if( $wgUseSquid && isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) ) {
24 # If the web server is behind a reverse proxy, we need to find
25 # out where our requests are really coming from.
26 $hopips = array_map( "trim", explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
27
28 while(in_array(trim(end($hopips)), $wgSquidServers)){
29 array_pop($hopips);
30 }
31 $wgIP = trim(end($hopips));
32 } else {
33 $wgIP = getenv("REMOTE_ADDR");
34 }
35
36
37 $fname = "Setup.php";
38 wfProfileIn( $fname );
39 global $wgUseDynamicDates;
40 wfProfileIn( "$fname-includes" );
41
42 include_once( "GlobalFunctions.php" );
43 include_once( "Namespace.php" );
44 include_once( "RecentChange.php" );
45 include_once( "Skin.php" );
46 include_once( "OutputPage.php" );
47 include_once( "User.php" );
48 include_once( "LinkCache.php" );
49 include_once( "Title.php" );
50 include_once( "Article.php" );
51 include_once( "MagicWord.php" );
52 include_once( "memcached-client.php" );
53 include_once( "Block.php" );
54 include_once( "SearchEngine.php" );
55 include_once( "DifferenceEngine.php" );
56 include_once( "MessageCache.php" );
57 include_once( "BlockCache.php" );
58 include_once( "Parser.php" );
59 include_once( "ParserCache.php" );
60 include_once( "WebRequest.php" );
61 $wgRequest = new WebRequest();
62
63
64 wfProfileOut( "$fname-includes" );
65 wfProfileIn( "$fname-memcached" );
66 global $wgUser, $wgLang, $wgOut, $wgTitle;
67 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
68 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
69 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
70 global $wgMsgCacheExpiry, $wgDBname, $wgCommandLineMode;
71 global $wgBlockCache, $wgParserCache, $wgParser;
72
73 # Useful debug output
74 if ( function_exists( "getallheaders" ) ) {
75 wfDebug( "\nStart request\n" );
76 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
77 $headers = getallheaders();
78 foreach ($headers as $name => $value) {
79 wfDebug( "$name: $value\n" );
80 }
81 wfDebug( "\n" );
82 } else {
83 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
84 }
85
86 # Set up Memcached
87 #
88 class MemCachedClientforWiki extends memcached {
89 function _debugprint( $text ) {
90 wfDebug( "memcached: $text\n" );
91 }
92 }
93
94 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
95 # It acts as a memcached server with no RAM, that is, all objects are
96 # cleared the moment they are set. All set operations succeed and all
97 # get operations return null.
98
99 class FakeMemCachedClient {
100 function add ($key, $val, $exp = 0) { return true; }
101 function decr ($key, $amt=1) { return null; }
102 function delete ($key, $time = 0) { return false; }
103 function disconnect_all () { }
104 function enable_compress ($enable) { }
105 function forget_dead_hosts () { }
106 function get ($key) { return null; }
107 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
108 function incr ($key, $amt=1) { return null; }
109 function replace ($key, $value, $exp=0) { return false; }
110 function run_command ($sock, $cmd) { return null; }
111 function set ($key, $value, $exp=0){ return true; }
112 function set_compress_threshold ($thresh){ }
113 function set_debug ($dbg) { }
114 function set_servers ($list) { }
115 }
116
117 if( $wgUseMemCached ) {
118 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
119 $wgMemc->set_servers( $wgMemCachedServers );
120 $wgMemc->set_debug( $wgMemCachedDebug );
121
122 # Test it to see if it's working
123 # This is necessary because otherwise wfMsg would be extremely inefficient
124 if ( !$wgMemc->set( "test", "", 0 ) ) {
125 wfDebug( "Memcached failed setup test - connection error?\n" );
126 $wgUseMemCached = false;
127 $wgMemc = new FakeMemCachedClient();
128 }
129 } else {
130 $wgMemc = new FakeMemCachedClient();
131 }
132
133 wfProfileOut( "$fname-memcached" );
134 wfProfileIn( "$fname-misc" );
135
136 include_once( "Language.php" );
137
138 $wgMessageCache = new MessageCache;
139
140 $wgLangClass = "Language" . ucfirst( $wgLanguageCode );
141 if( ! class_exists( $wgLangClass ) ) {
142 include_once( "LanguageUtf8.php" );
143 $wgLangClass = "LanguageUtf8";
144 }
145
146 $wgLang = new $wgLangClass();
147 if ( !is_object($wgLang) ) {
148 print "No language class ($wgLang)\N";
149 }
150 $wgMessageCache->initialise( $wgUseMemCached, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname );
151
152 $wgOut = new OutputPage();
153 wfDebug( "\n\n" );
154
155 if ( $wgUseDynamicDates ) {
156 include_once( "DateFormatter.php" );
157 global $wgDateFormatter;
158 $wgDateFormatter = new DateFormatter;
159 }
160
161 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get("session.name")] ) || isset( $_COOKIE["{$wgDBname}Password"] ) ) ) {
162 User::SetupSession();
163 }
164
165 $wgBlockCache = new BlockCache( true );
166 $wgUser = User::loadFromSession();
167 $wgDeferredUpdateList = array();
168 $wgLinkCache = new LinkCache();
169 $wgMagicWords = array();
170 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
171 $wgParserCache = new ParserCache();
172 $wgParser = new Parser();
173 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
174
175 wfProfileOut( "$fname-misc" );
176 wfProfileOut( $fname );
177
178 ?>