Fix opening of session for users visiting with saved password token
[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 while(in_array(trim(end($hopips)), $wgSquidServers)){
40 array_pop($hopips);
41 }
42 $wgIP = trim(end($hopips));
43 } elseif( isset( $_SERVER['REMOTE_ADDR'] ) ) {
44 $wgIP = $_SERVER['REMOTE_ADDR'];
45 } else {
46 # Running on CLI?
47 $wgIP = '127.0.0.1';
48 }
49
50 if ( $wgUseData ) {
51 $wgExtraNamespaces[20] = 'Data' ;
52 $wgExtraNamespaces[21] = 'Data_talk' ;
53 }
54
55 $fname = 'Setup.php';
56 wfProfileIn( $fname );
57 global $wgUseDynamicDates;
58 wfProfileIn( $fname.'-includes' );
59
60 require_once( 'GlobalFunctions.php' );
61 require_once( 'Namespace.php' );
62 require_once( 'RecentChange.php' );
63 require_once( 'User.php' );
64 require_once( 'Skin.php' );
65 require_once( 'OutputPage.php' );
66 require_once( 'LinkCache.php' );
67 require_once( 'Title.php' );
68 require_once( 'Article.php' );
69 require_once( 'MagicWord.php' );
70 require_once( 'Block.php' );
71 require_once( 'MessageCache.php' );
72 require_once( 'BlockCache.php' );
73 require_once( 'Parser.php' );
74 require_once( 'ParserXML.php' );
75 require_once( 'ParserCache.php' );
76 require_once( 'WebRequest.php' );
77 require_once( 'LoadBalancer.php' );
78 require_once( 'HistoryBlob.php' );
79
80 $wgRequest = new WebRequest();
81
82
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;
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 );
208 $wgLoadBalancer->loadMasterPos();
209
210 wfProfileOut( $fname.'-database' );
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( $wgCommandLineMode ) {
221 # Used for some maintenance scripts; user session cookies can screw things up
222 # when the database is in an in-between state.
223 $wgUser = new User();
224 } else {
225 $wgUser = User::loadFromSession();
226 }
227
228 wfProfileOut( $fname.'-User' );
229 wfProfileIn( $fname.'-language' );
230
231 function setupLangObj(&$langclass, $langcode) {
232 global $wgUseLatin1, $IP;
233
234
235 if( ! class_exists( $langclass ) ) {
236 # Default to English/UTF-8
237 require_once( "$IP/languages/LanguageUtf8.php" );
238 $langclass = 'LanguageUtf8';
239 }
240
241 $lang = new $langclass();
242 if ( !is_object($lang) ) {
243 print "No language class ($wgLang)\N";
244 }
245
246 if( $wgUseLatin1 ) {
247 # For non-UTF-8 latin-1 downconversion
248 require_once( "$IP/languages/LanguageLatin1.php" );
249 $xxx = new LanguageLatin1( $lang );
250 unset( $lang );
251 $lang = $xxx;
252 }
253 return $lang;
254 }
255
256 require_once( "$IP/languages/Language.php" );
257
258 # $wgLanguageCode may be changed later to fit with user preference.
259 # The content language will remain fixed as per the configuration,
260 # so let's keep it.
261 $wgContLanguageCode = $wgLanguageCode;
262 $wgContLangClass = 'Language' . str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
263
264 $wgContLang = setupLangObj( $wgContLangClass, $wgContLangClass );
265 $n = get_class($wgContLang);
266
267 // set default user option from content language
268 if( !$wgUser->mDataLoaded ) {
269 $wgUser->loadDefaultFromLanguage();
270 }
271
272 // wgLanguageCode now specifically means the UI language
273 $wgLanguageCode = $wgUser->getOption('language');
274
275 $wgLangClass = 'Language'. str_replace( '-', '_', ucfirst( $wgLanguageCode ) );
276
277 if( $wgLangClass == $wgContLangClass ) {
278 $wgLang = &$wgContLang;
279 } else {
280 require_once("$IP/languages/$wgLangClass.php");
281 $wgLang = setupLangObj( $wgLangClass, $wgLanguageCode );
282 }
283
284
285 wfProfileOut( $fname.'-language' );
286 wfProfileIn( $fname.'-MessageCache' );
287
288 $wgMessageCache = new MessageCache;
289 $wgMessageCache->initialise( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname);
290
291 wfProfileOut( $fname.'-MessageCache' );
292
293 #
294 # I guess the warning about UI switching might still apply...
295 #
296 # FIXME: THE ABOVE MIGHT BREAK NAMESPACES, VARIABLES,
297 # SEARCH INDEX UPDATES, AND MANY MANY THINGS.
298 # DO NOT USE THIS MODE EXCEPT FOR TESTING RIGHT NOW.
299 #
300 # To disable it, the easiest thing could be to uncomment the
301 # following; they should effectively disable the UI switch functionality
302 #
303 # $wgLangClass = $wgContLangClass;
304 # $wgLanguageCode = $wgContLanguageCode;
305 # $wgLang = $wgContLang;
306 #
307 # TODO: Need to change reference to $wgLang to $wgContLang at proper
308 # places, including namespaces, dates in signatures, magic words,
309 # and links
310 #
311 # TODO: Need to look at the issue of input/output encoding
312 #
313
314
315 wfProfileIn( $fname.'-OutputPage' );
316
317 $wgOut = new OutputPage();
318 wfDebug( "\n\n" );
319
320 wfProfileOut( $fname.'-OutputPage' );
321 wfProfileIn( $fname.'-DateFormatter' );
322
323 if ( $wgUseDynamicDates ) {
324 require_once( 'DateFormatter.php' );
325 global $wgDateFormatter;
326 $wgDateFormatter = new DateFormatter;
327 }
328
329 wfProfileOut( $fname.'-DateFormatter' );
330 wfProfileIn( $fname.'-BlockCache' );
331
332 $wgBlockCache = new BlockCache( true );
333
334 wfProfileOut( $fname.'-BlockCache' );
335 wfProfileIn( $fname.'-misc2' );
336
337 $wgDeferredUpdateList = array();
338 $wgLinkCache = new LinkCache();
339 $wgMagicWords = array();
340 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
341 $wgParserCache = new ParserCache();
342
343 if ( $wgUseXMLparser ) $wgParser = new ParserXML();
344 else $wgParser = new Parser();
345 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
346 $wgMsgParserOptions = ParserOptions::newFromUser($wgUser);
347 wfSeedRandom();
348
349 # Placeholders in case of DB error
350 $wgTitle = Title::newFromText( wfMsgForContent( 'badtitle' ) );
351 $wgArticle = new Article($wgTitle);
352
353 wfProfileOut( $fname.'-misc2' );
354 wfProfileIn( $fname.'-extensions' );
355
356 # Extension setup functions for extensions other than skins
357 # Entries should be added to this variable during the inclusion
358 # of the extension file. This allows the extension to perform
359 # any necessary initialisation in the fully initialised environment
360 foreach ( $wgExtensionFunctions as $func ) {
361 $func();
362 }
363
364 $wgFullyInitialised = true;
365 wfProfileOut( $fname.'-extensions' );
366 wfProfileOut( $fname );
367
368 }
369 ?>