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