* (bug 3990) Use existing session name if session.auto_start is on
[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 // Check to see if we are at the file scope
20 if ( !isset( $wgVersion ) ) {
21 echo "Error, Setup.php must be included from the file scope, after DefaultSettings.php\n";
22 die( -1 );
23 }
24
25 if( !isset( $wgProfiling ) )
26 $wgProfiling = false;
27
28 if ( function_exists( 'wfProfileIn' ) ) {
29 /* nada, everything should be done already */
30 } elseif ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
31 require_once( 'Profiling.php' );
32 $wgProfiling = true;
33 if ($wgProfilerType == "") {
34 $wgProfiler = new Profiler();
35 } else {
36 $prclass="Profiler{$wgProfilerType}";
37 require_once( $prclass.".php" );
38 $wgProfiler = new $prclass();
39 }
40 } else {
41 require_once( 'ProfilerStub.php' );
42 }
43
44 $fname = 'Setup.php';
45 wfProfileIn( $fname );
46 wfProfileIn( $fname.'-includes' );
47
48 require_once( 'GlobalFunctions.php' );
49 require_once( 'Hooks.php' );
50 require_once( 'Namespace.php' );
51 require_once( 'User.php' );
52 require_once( 'Skin.php' );
53 require_once( 'OutputPage.php' );
54 require_once( 'LinkCache.php' );
55 require_once( 'LinkBatch.php' );
56 require_once( 'Title.php' );
57 require_once( 'Article.php' );
58 require_once( 'MagicWord.php' );
59 require_once( 'Block.php' );
60 require_once( 'MessageCache.php' );
61 require_once( 'Parser.php' );
62 require_once( 'ParserCache.php' );
63 require_once( 'WebRequest.php' );
64 require_once( 'LoadBalancer.php' );
65 require_once( 'HistoryBlob.php' );
66 require_once( 'ProxyTools.php' );
67 require_once( 'ObjectCache.php' );
68 require_once( 'WikiError.php' );
69 require_once( 'SpecialPage.php' );
70
71 if ( $wgUseDynamicDates ) {
72 require_once( 'DateFormatter.php' );
73 }
74
75 wfProfileOut( $fname.'-includes' );
76 wfProfileIn( $fname.'-misc1' );
77
78 $wgIP = false; # Load on demand
79 $wgRequest = new WebRequest();
80 if ( function_exists( 'posix_uname' ) ) {
81 $wguname = posix_uname();
82 }
83 # Useful debug output
84 if ( $wgCommandLineMode ) {
85 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
86 } elseif ( function_exists( 'getallheaders' ) ) {
87 wfDebug( "\n\nStart request\n" );
88 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
89 $headers = getallheaders();
90 foreach ($headers as $name => $value) {
91 wfDebug( "$name: $value\n" );
92 }
93 wfDebug( "\n" );
94 } elseif( isset( $_SERVER['REQUEST_URI'] ) ) {
95 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
96 }
97
98 if ( $wgSkipSkin ) {
99 $wgSkipSkins[] = $wgSkipSkin;
100 }
101
102 $wgUseEnotif = $wgEnotifUserTalk || $wgEnotifWatchlist;
103
104 wfProfileOut( $fname.'-misc1' );
105 wfProfileIn( $fname.'-memcached' );
106
107 $wgMemc =& wfGetMainCache();
108 $messageMemc =& wfGetMessageCacheStorage();
109 $parserMemc =& wfGetParserCacheStorage();
110
111 wfDebug( 'Main cache: ' . get_class( $wgMemc ) .
112 "\nMessage cache: " . get_class( $messageMemc ) .
113 "\nParser cache: " . get_class( $parserMemc ) . "\n" );
114
115 wfProfileOut( $fname.'-memcached' );
116 wfProfileIn( $fname.'-SetupSession' );
117
118 # If session.auto_start is there, we can't touch session name
119 #
120
121 if (!ini_get('session.auto_start')) {
122 if ( $wgDBprefix ) {
123 session_name( $wgDBname . '_' . $wgDBprefix . '_session' );
124 } else {
125 session_name( $wgDBname . '_session' );
126 }
127 }
128
129 if( !$wgCommandLineMode && ( isset( $_COOKIE[session_name()] ) || isset( $_COOKIE[$wgDBname.'Token'] ) ) ) {
130 wfIncrStats( 'request_with_session' );
131 User::SetupSession();
132 $wgSessionStarted = true;
133 } else {
134 wfIncrStats( 'request_without_session' );
135 $wgSessionStarted = false;
136 }
137
138 wfProfileOut( $fname.'-SetupSession' );
139 wfProfileIn( $fname.'-database' );
140
141 if ( !$wgDBservers ) {
142 $wgDBservers = array(array(
143 'host' => $wgDBserver,
144 'user' => $wgDBuser,
145 'password' => $wgDBpassword,
146 'dbname' => $wgDBname,
147 'type' => $wgDBtype,
148 'load' => 1,
149 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
150 ));
151 }
152 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers, false, $wgMasterWaitTimeout );
153 $wgLoadBalancer->loadMasterPos();
154
155 wfProfileOut( $fname.'-database' );
156 wfProfileIn( $fname.'-language1' );
157
158 require_once( "$IP/languages/Language.php" );
159
160 function setupLangObj($langclass) {
161 global $IP;
162
163 if( ! class_exists( $langclass ) ) {
164 # Default to English/UTF-8
165 $baseclass = 'LanguageUtf8';
166 require_once( "$IP/languages/$baseclass.php" );
167 $lc = strtolower(substr($langclass, 8));
168 $snip = "
169 class $langclass extends $baseclass {
170 function getVariants() {
171 return array(\"$lc\");
172 }
173
174 }";
175 eval($snip);
176 }
177
178 $lang = new $langclass();
179
180 return $lang;
181 }
182
183 # $wgLanguageCode may be changed later to fit with user preference.
184 # The content language will remain fixed as per the configuration,
185 # so let's keep it.
186 $wgContLanguageCode = $wgLanguageCode;
187 $wgContLangClass = 'Language' . str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
188
189 $wgContLang = setupLangObj( $wgContLangClass );
190 $wgContLang->initEncoding();
191
192 wfProfileOut( $fname.'-language1' );
193 wfProfileIn( $fname.'-User' );
194
195 # Skin setup functions
196 # Entries can be added to this variable during the inclusion
197 # of the extension file. Skins can then perform any necessary initialisation.
198 #
199 # require_once is slow even on the second call, so this needs to be outside the loop
200 if ( count( $wgSkinExtensionFunctions ) ) {
201 require_once( 'PersistentObject.php' );
202 }
203 foreach ( $wgSkinExtensionFunctions as $func ) {
204 call_user_func( $func );
205 }
206
207 if( !is_object( $wgAuth ) ) {
208 require_once( 'AuthPlugin.php' );
209 $wgAuth = new AuthPlugin();
210 }
211
212 if( $wgCommandLineMode ) {
213 # Used for some maintenance scripts; user session cookies can screw things up
214 # when the database is in an in-between state.
215 $wgUser = new User();
216 # Prevent loading User settings from the DB.
217 $wgUser->setLoaded( true );
218 } else {
219 $wgUser = null;
220 wfRunHooks('AutoAuthenticate',array(&$wgUser));
221 if ($wgUser === null) {
222 $wgUser = User::loadFromSession();
223 }
224 }
225
226 wfProfileOut( $fname.'-User' );
227 wfProfileIn( $fname.'-language2' );
228
229 // wgLanguageCode now specifically means the UI language
230 $wgLanguageCode = $wgRequest->getText('uselang', '');
231 if ($wgLanguageCode == '')
232 $wgLanguageCode = $wgUser->getOption('language');
233 # Validate $wgLanguageCode, which will soon be sent to an eval()
234 if( empty( $wgLanguageCode ) || !preg_match( '/^[a-z]+(-[a-z]+)?$/', $wgLanguageCode ) ) {
235 $wgLanguageCode = $wgContLanguageCode;
236 }
237
238 $wgLangClass = 'Language'. str_replace( '-', '_', ucfirst( $wgLanguageCode ) );
239
240 if( $wgLangClass == $wgContLangClass ) {
241 $wgLang = &$wgContLang;
242 } else {
243 wfSuppressWarnings();
244 include_once("$IP/languages/$wgLangClass.php");
245 wfRestoreWarnings();
246
247 $wgLang = setupLangObj( $wgLangClass );
248 }
249
250 wfProfileOut( $fname.'-language2' );
251 wfProfileIn( $fname.'-MessageCache' );
252
253 $wgMessageCache = new MessageCache;
254 $wgMessageCache->initialise( $parserMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname);
255
256 wfProfileOut( $fname.'-MessageCache' );
257
258 #
259 # I guess the warning about UI switching might still apply...
260 #
261 # FIXME: THE ABOVE MIGHT BREAK NAMESPACES, VARIABLES,
262 # SEARCH INDEX UPDATES, AND MANY MANY THINGS.
263 # DO NOT USE THIS MODE EXCEPT FOR TESTING RIGHT NOW.
264 #
265 # To disable it, the easiest thing could be to uncomment the
266 # following; they should effectively disable the UI switch functionality
267 #
268 # $wgLangClass = $wgContLangClass;
269 # $wgLanguageCode = $wgContLanguageCode;
270 # $wgLang = $wgContLang;
271 #
272 # TODO: Need to change reference to $wgLang to $wgContLang at proper
273 # places, including namespaces, dates in signatures, magic words,
274 # and links
275 #
276 # TODO: Need to look at the issue of input/output encoding
277 #
278
279
280 wfProfileIn( $fname.'-OutputPage' );
281
282 $wgOut = new OutputPage();
283
284 wfProfileOut( $fname.'-OutputPage' );
285 wfProfileIn( $fname.'-misc2' );
286
287 $wgDeferredUpdateList = array();
288 $wgPostCommitUpdateList = array();
289
290 $wgMagicWords = array();
291 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
292
293 if ( $wgUseXMLparser ) {
294 require_once( 'ParserXML.php' );
295 $wgParser = new ParserXML();
296 } else {
297 $wgParser = new Parser();
298 }
299 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
300 $wgMsgParserOptions = ParserOptions::newFromUser($wgUser);
301 wfSeedRandom();
302
303 # Placeholders in case of DB error
304 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Error' );
305 $wgArticle = new Article($wgTitle);
306
307 wfProfileOut( $fname.'-misc2' );
308 wfProfileIn( $fname.'-extensions' );
309
310 # Extension setup functions for extensions other than skins
311 # Entries should be added to this variable during the inclusion
312 # of the extension file. This allows the extension to perform
313 # any necessary initialisation in the fully initialised environment
314 if ( count( $wgExtensionFunctions ) ) {
315 require_once( 'PersistentObject.php' );
316 }
317 foreach ( $wgExtensionFunctions as $func ) {
318 call_user_func( $func );
319 }
320
321 wfDebug( "\n" );
322 $wgFullyInitialised = true;
323 wfProfileOut( $fname.'-extensions' );
324 wfProfileOut( $fname );
325
326 }
327 ?>