Allow cookies to be shared between multiple wikis with a shared user database.
[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 ( $wgDBprefix ) {
119 $wgCookiePrefix = $wgDBname . '_' . $wgDBprefix;
120 } elseif ( $wgSharedDB ) {
121 $wgCookiePrefix = $wgSharedDB;
122 } else {
123 $wgCookiePrefix = $wgDBname;
124 }
125
126 # If session.auto_start is there, we can't touch session name
127 #
128 if (!ini_get('session.auto_start')) {
129 session_name( $wgCookiePrefix . '_session' );
130 }
131
132 if( !$wgCommandLineMode && ( isset( $_COOKIE[session_name()] ) || isset( $_COOKIE[$wgCookiePrefix.'Token'] ) ) ) {
133 wfIncrStats( 'request_with_session' );
134 User::SetupSession();
135 $wgSessionStarted = true;
136 } else {
137 wfIncrStats( 'request_without_session' );
138 $wgSessionStarted = false;
139 }
140
141 wfProfileOut( $fname.'-SetupSession' );
142 wfProfileIn( $fname.'-database' );
143
144 if ( !$wgDBservers ) {
145 $wgDBservers = array(array(
146 'host' => $wgDBserver,
147 'user' => $wgDBuser,
148 'password' => $wgDBpassword,
149 'dbname' => $wgDBname,
150 'type' => $wgDBtype,
151 'load' => 1,
152 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
153 ));
154 }
155 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers, false, $wgMasterWaitTimeout );
156 $wgLoadBalancer->loadMasterPos();
157
158 wfProfileOut( $fname.'-database' );
159 wfProfileIn( $fname.'-language1' );
160
161 require_once( "$IP/languages/Language.php" );
162
163 function setupLangObj($langclass) {
164 global $IP;
165
166 if( ! class_exists( $langclass ) ) {
167 # Default to English/UTF-8
168 $baseclass = 'LanguageUtf8';
169 require_once( "$IP/languages/$baseclass.php" );
170 $lc = strtolower(substr($langclass, 8));
171 $snip = "
172 class $langclass extends $baseclass {
173 function getVariants() {
174 return array(\"$lc\");
175 }
176
177 }";
178 eval($snip);
179 }
180
181 $lang = new $langclass();
182
183 return $lang;
184 }
185
186 # $wgLanguageCode may be changed later to fit with user preference.
187 # The content language will remain fixed as per the configuration,
188 # so let's keep it.
189 $wgContLanguageCode = $wgLanguageCode;
190 $wgContLangClass = 'Language' . str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
191
192 $wgContLang = setupLangObj( $wgContLangClass );
193 $wgContLang->initEncoding();
194
195 wfProfileOut( $fname.'-language1' );
196 wfProfileIn( $fname.'-User' );
197
198 # Skin setup functions
199 # Entries can be added to this variable during the inclusion
200 # of the extension file. Skins can then perform any necessary initialisation.
201 #
202 # require_once is slow even on the second call, so this needs to be outside the loop
203 if ( count( $wgSkinExtensionFunctions ) ) {
204 require_once( 'PersistentObject.php' );
205 }
206 foreach ( $wgSkinExtensionFunctions as $func ) {
207 call_user_func( $func );
208 }
209
210 if( !is_object( $wgAuth ) ) {
211 require_once( 'AuthPlugin.php' );
212 $wgAuth = new AuthPlugin();
213 }
214
215 if( $wgCommandLineMode ) {
216 # Used for some maintenance scripts; user session cookies can screw things up
217 # when the database is in an in-between state.
218 $wgUser = new User();
219 # Prevent loading User settings from the DB.
220 $wgUser->setLoaded( true );
221 } else {
222 $wgUser = null;
223 wfRunHooks('AutoAuthenticate',array(&$wgUser));
224 if ($wgUser === null) {
225 $wgUser = User::loadFromSession();
226 }
227 }
228
229 wfProfileOut( $fname.'-User' );
230 wfProfileIn( $fname.'-language2' );
231
232 // wgLanguageCode now specifically means the UI language
233 $wgLanguageCode = $wgRequest->getText('uselang', '');
234 if ($wgLanguageCode == '')
235 $wgLanguageCode = $wgUser->getOption('language');
236 # Validate $wgLanguageCode, which will soon be sent to an eval()
237 if( empty( $wgLanguageCode ) || !preg_match( '/^[a-z]+(-[a-z]+)?$/', $wgLanguageCode ) ) {
238 $wgLanguageCode = $wgContLanguageCode;
239 }
240
241 $wgLangClass = 'Language'. str_replace( '-', '_', ucfirst( $wgLanguageCode ) );
242
243 if( $wgLangClass == $wgContLangClass ) {
244 $wgLang = &$wgContLang;
245 } else {
246 wfSuppressWarnings();
247 // Preload base classes to work around APC/PHP5 bug
248 include_once("$IP/languages/$wgLangClass.deps.php");
249 include_once("$IP/languages/$wgLangClass.php");
250 wfRestoreWarnings();
251
252 $wgLang = setupLangObj( $wgLangClass );
253 }
254
255 wfProfileOut( $fname.'-language2' );
256 wfProfileIn( $fname.'-MessageCache' );
257
258 $wgMessageCache = new MessageCache;
259 $wgMessageCache->initialise( $parserMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname);
260
261 wfProfileOut( $fname.'-MessageCache' );
262
263 #
264 # I guess the warning about UI switching might still apply...
265 #
266 # FIXME: THE ABOVE MIGHT BREAK NAMESPACES, VARIABLES,
267 # SEARCH INDEX UPDATES, AND MANY MANY THINGS.
268 # DO NOT USE THIS MODE EXCEPT FOR TESTING RIGHT NOW.
269 #
270 # To disable it, the easiest thing could be to uncomment the
271 # following; they should effectively disable the UI switch functionality
272 #
273 # $wgLangClass = $wgContLangClass;
274 # $wgLanguageCode = $wgContLanguageCode;
275 # $wgLang = $wgContLang;
276 #
277 # TODO: Need to change reference to $wgLang to $wgContLang at proper
278 # places, including namespaces, dates in signatures, magic words,
279 # and links
280 #
281 # TODO: Need to look at the issue of input/output encoding
282 #
283
284
285 wfProfileIn( $fname.'-OutputPage' );
286
287 $wgOut = new OutputPage();
288
289 wfProfileOut( $fname.'-OutputPage' );
290 wfProfileIn( $fname.'-misc2' );
291
292 $wgDeferredUpdateList = array();
293 $wgPostCommitUpdateList = array();
294
295 $wgMagicWords = array();
296 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
297
298 if ( $wgUseXMLparser ) {
299 require_once( 'ParserXML.php' );
300 $wgParser = new ParserXML();
301 } else {
302 $wgParser = new Parser();
303 }
304 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
305 $wgMsgParserOptions = ParserOptions::newFromUser($wgUser);
306 wfSeedRandom();
307
308 # Placeholders in case of DB error
309 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Error' );
310 $wgArticle = new Article($wgTitle);
311
312 wfProfileOut( $fname.'-misc2' );
313 wfProfileIn( $fname.'-extensions' );
314
315 # Extension setup functions for extensions other than skins
316 # Entries should be added to this variable during the inclusion
317 # of the extension file. This allows the extension to perform
318 # any necessary initialisation in the fully initialised environment
319 if ( count( $wgExtensionFunctions ) ) {
320 require_once( 'PersistentObject.php' );
321 }
322 foreach ( $wgExtensionFunctions as $func ) {
323 call_user_func( $func );
324 }
325
326 wfDebug( "\n" );
327 $wgFullyInitialised = true;
328 wfProfileOut( $fname.'-extensions' );
329 wfProfileOut( $fname );
330
331 }
332 ?>