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