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