Only load memcached-client.php if we're really using it. Saves about 120kb memory...
[lhc/web/wiklou.git] / includes / Setup.php
1 <?php
2
3 # This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
4 if( defined( "MEDIAWIKI" ) ) {
5
6 # The main wiki script and things like database
7 # conversion and maintenance scripts all share a
8 # common setup of including lots of classes and
9 # setting up a few globals.
10 #
11
12 global $wgProfiling, $wgProfileSampleRate, $wgIP, $wgUseSquid;
13
14 if( !isset( $wgProfiling ) )
15 $wgProfiling = false;
16
17 if ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
18 require_once( 'Profiling.php' );
19 } else {
20 function wfProfileIn( $fn = '' ) {}
21 function wfProfileOut( $fn = '' ) {}
22 function wfGetProfilingOutput( $s, $e ) {}
23 function wfProfileClose() {}
24 }
25
26 /* collect the originating ips */
27 if( $wgUseSquid && isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
28 # If the web server is behind a reverse proxy, we need to find
29 # out where our requests are really coming from.
30 $hopips = array_map( 'trim', explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
31
32 while(in_array(trim(end($hopips)), $wgSquidServers)){
33 array_pop($hopips);
34 }
35 $wgIP = trim(end($hopips));
36 } else {
37 $wgIP = getenv('REMOTE_ADDR');
38 }
39
40
41 $fname = 'Setup.php';
42 wfProfileIn( $fname );
43 global $wgUseDynamicDates;
44 wfProfileIn( $fname.'-includes' );
45
46 require_once( 'GlobalFunctions.php' );
47 require_once( 'Namespace.php' );
48 require_once( 'RecentChange.php' );
49 require_once( 'User.php' );
50 require_once( 'Skin.php' );
51 require_once( 'OutputPage.php' );
52 require_once( 'LinkCache.php' );
53 require_once( 'Title.php' );
54 require_once( 'Article.php' );
55 require_once( 'MagicWord.php' );
56 require_once( 'Block.php' );
57 require_once( 'MessageCache.php' );
58 require_once( 'BlockCache.php' );
59 require_once( 'Parser.php' );
60 require_once( 'ParserCache.php' );
61 require_once( 'WebRequest.php' );
62 require_once( 'LoadBalancer.php' );
63
64 $wgRequest = new WebRequest();
65
66
67
68 wfProfileOut( $fname.'-includes' );
69 wfProfileIn( $fname.'-misc1' );
70 global $wgUser, $wgLang, $wgOut, $wgTitle;
71 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
72 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
73 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
74 global $wgMsgCacheExpiry, $wgCommandLineMode;
75 global $wgBlockCache, $wgParserCache, $wgParser, $wgDBConnections;
76 global $wgLoadBalancer, $wgDBservers, $wgDebugDumpSql;
77 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype;
78 global $wgUseOldExistenceCheck, $wgEnablePersistentLC;
79
80 global $wgFullyInitialised;
81
82 # Useful debug output
83 if ( $wgCommandLineMode ) {
84 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
85 } elseif ( function_exists( 'getallheaders' ) ) {
86 wfDebug( "\nStart request\n" );
87 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
88 $headers = getallheaders();
89 foreach ($headers as $name => $value) {
90 wfDebug( "$name: $value\n" );
91 }
92 wfDebug( "\n" );
93 } else {
94 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
95 }
96
97 # Disable linkscc except if the old existence check method is enabled
98 if (!$wgUseOldExistenceCheck) {
99 $wgEnablePersistentLC = false;
100 }
101
102 wfProfileOut( $fname.'-misc1' );
103 wfProfileIn( $fname.'-memcached' );
104
105 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
106 # It acts as a memcached server with no RAM, that is, all objects are
107 # cleared the moment they are set. All set operations succeed and all
108 # get operations return null.
109
110 if( $wgUseMemCached ) {
111 # Set up Memcached
112 #
113 require_once( 'memcached-client.php' );
114 class MemCachedClientforWiki extends memcached {
115 function _debugprint( $text ) {
116 wfDebug( "memcached: $text\n" );
117 }
118 }
119
120 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
121 $wgMemc->set_servers( $wgMemCachedServers );
122 $wgMemc->set_debug( $wgMemCachedDebug );
123
124 $messageMemc = &$wgMemc;
125 } else {
126 class FakeMemCachedClient {
127 function add ($key, $val, $exp = 0) { return true; }
128 function decr ($key, $amt=1) { return null; }
129 function delete ($key, $time = 0) { return false; }
130 function disconnect_all () { }
131 function enable_compress ($enable) { }
132 function forget_dead_hosts () { }
133 function get ($key) { return null; }
134 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
135 function incr ($key, $amt=1) { return null; }
136 function replace ($key, $value, $exp=0) { return false; }
137 function run_command ($sock, $cmd) { return null; }
138 function set ($key, $value, $exp=0){ return true; }
139 function set_compress_threshold ($thresh){ }
140 function set_debug ($dbg) { }
141 function set_servers ($list) { }
142 }
143 $wgMemc = new FakeMemCachedClient();
144
145 # Give the message cache a separate cache in the DB.
146 # This is a speedup over separately querying every message used
147 require_once( 'ObjectCache.php' );
148 $messageMemc = new MediaWikiBagOStuff('objectcache');
149 }
150
151 wfProfileOut( $fname.'-memcached' );
152 wfProfileIn( $fname.'-SetupSession' );
153
154 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get('session.name')] ) || isset( $_COOKIE[$wgDBname.'Password'] ) ) ) {
155 User::SetupSession();
156 $wgSessionStarted = true;
157 } else {
158 $wgSessionStarted = false;
159 }
160
161 wfProfileOut( $fname.'-SetupSession' );
162 wfProfileIn( $fname.'-database' );
163
164 if ( !$wgDBservers ) {
165 $wgDBservers = array(array(
166 'host' => $wgDBserver,
167 'user' => $wgDBuser,
168 'password' => $wgDBpassword,
169 'dbname' => $wgDBname,
170 'type' => $wgDBtype,
171 'load' => 1,
172 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
173 ));
174 }
175 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers );
176 $wgLoadBalancer->loadMasterPos();
177
178 wfProfileOut( $fname.'-database' );
179 wfProfileIn( $fname.'-language' );
180 require_once( 'languages/Language.php' );
181
182 $wgMessageCache = new MessageCache;
183
184 $wgLangClass = 'Language' . ucfirst( $wgLanguageCode );
185 if( ! class_exists( $wgLangClass ) || ($wgLanguageCode == 'en' && !$wgUseLatin1) ) {
186 # Default to English/UTF-8
187 require_once( 'languages/LanguageUtf8.php' );
188 $wgLangClass = 'LanguageUtf8';
189 }
190
191 $wgLang = new $wgLangClass();
192 if ( !is_object($wgLang) ) {
193 print "No language class ($wgLang)\N";
194 }
195
196 if( $wgUseLatin1 && $wgLanguageCode != 'en' ) {
197 # For non-UTF-8 non-English.
198 require_once( 'languages/LanguageLatin1.php' );
199 $xxx = new LanguageLatin1( $wgLang );
200 unset( $wgLang );
201 $wgLang = $xxx;
202 }
203 wfProfileOut( $fname.'-language' );
204 wfProfileIn( $fname.'-MessageCache' );
205
206 $wgMessageCache->initialise( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname );
207
208 wfProfileOut( $fname.'-MessageCache' );
209 wfProfileIn( $fname.'-OutputPage' );
210
211 $wgOut = new OutputPage();
212 wfDebug( "\n\n" );
213
214 wfProfileOut( $fname.'-OutputPage' );
215 wfProfileIn( $fname.'-DateFormatter' );
216
217 if ( $wgUseDynamicDates ) {
218 require_once( 'DateFormatter.php' );
219 global $wgDateFormatter;
220 $wgDateFormatter = new DateFormatter;
221 }
222
223 wfProfileOut( $fname.'-DateFormatter' );
224 wfProfileIn( $fname.'-BlockCache' );
225
226 $wgBlockCache = new BlockCache( true );
227
228 wfProfileOut( $fname.'-BlockCache' );
229 wfProfileIn( $fname.'-User' );
230
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 } else {
236 $wgUser = User::loadFromSession();
237 }
238
239 wfProfileOut( $fname.'-User' );
240 wfProfileIn( $fname.'-misc2' );
241
242 $wgDeferredUpdateList = array();
243 $wgLinkCache = new LinkCache();
244 $wgMagicWords = array();
245 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
246 $wgParserCache = new ParserCache();
247 $wgParser = new Parser();
248 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
249 $wgDBConnections = array();
250 wfSeedRandom();
251
252 # Placeholders in case of DB error
253 $wgTitle = Title::newFromText( wfMsg( 'badtitle' ) );
254 $wgArticle = new Article($wgTitle);
255
256 wfProfileOut( $fname.'-misc2' );
257 wfProfileIn( $fname.'-extensions' );
258
259 # Extension setup functions
260 # Entries should be added to this variable during the inclusion
261 # of the extension file. This allows the extension to perform
262 # any necessary initialisation in the fully initialised environment
263 foreach ( $wgExtensionFunctions as $func ) {
264 $func();
265 }
266
267 $wgFullyInitialised = true;
268 wfProfileOut( $fname.'-extensions' );
269 wfProfileOut( $fname );
270
271 }
272 ?>