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