Various performance and initialisation issues:
[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 require_once( "$IP/includes/AutoLoader.php" );
29
30 if ( function_exists( 'wfProfileIn' ) ) {
31 /* nada, everything should be done already */
32 } elseif ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
33 $wgProfiling = true;
34 if ($wgProfilerType == "") {
35 $wgProfiler = new Profiler();
36 } else {
37 $prclass="Profiler{$wgProfilerType}";
38 require_once( $prclass.".php" );
39 $wgProfiler = new $prclass();
40 }
41 } else {
42 require_once( "$IP/includes/ProfilerStub.php" );
43 }
44
45 $fname = 'Setup.php';
46 wfProfileIn( $fname );
47
48 wfProfileIn( $fname.'-exception' );
49 require_once( "$IP/includes/Exception.php" );
50 wfInstallExceptionHandler();
51 wfProfileOut( $fname.'-exception' );
52
53 wfProfileIn( $fname.'-includes' );
54
55 require_once( "$IP/includes/GlobalFunctions.php" );
56 require_once( "$IP/includes/Hooks.php" );
57 require_once( "$IP/includes/Namespace.php" );
58 require_once( "$IP/includes/User.php" );
59 require_once( "$IP/includes/OutputPage.php" );
60 require_once( "$IP/includes/MagicWord.php" );
61 require_once( "$IP/includes/MessageCache.php" );
62 require_once( "$IP/includes/Parser.php" );
63 require_once( "$IP/includes/LoadBalancer.php" );
64 require_once( "$IP/includes/ProxyTools.php" );
65 require_once( "$IP/includes/ObjectCache.php" );
66 require_once( "$IP/includes/ImageFunctions.php" );
67
68 if ( $wgUseDynamicDates ) {
69 require_once( "$IP/includes/DateFormatter.php" );
70 }
71
72 wfProfileOut( $fname.'-includes' );
73 wfProfileIn( $fname.'-misc1' );
74
75 $wgIP = false; # Load on demand
76 $wgRequest = new WebRequest();
77 if ( function_exists( 'posix_uname' ) ) {
78 $wguname = posix_uname();
79 $wgNodeName = $wguname['nodename'];
80 } else {
81 $wgNodeName = '';
82 }
83
84 # Useful debug output
85 if ( $wgCommandLineMode ) {
86 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
87 } elseif ( function_exists( 'getallheaders' ) ) {
88 wfDebug( "\n\nStart request\n" );
89 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
90 $headers = getallheaders();
91 foreach ($headers as $name => $value) {
92 wfDebug( "$name: $value\n" );
93 }
94 wfDebug( "\n" );
95 } elseif( isset( $_SERVER['REQUEST_URI'] ) ) {
96 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
97 }
98
99 if ( $wgSkipSkin ) {
100 $wgSkipSkins[] = $wgSkipSkin;
101 }
102
103 $wgUseEnotif = $wgEnotifUserTalk || $wgEnotifWatchlist;
104
105 wfProfileOut( $fname.'-misc1' );
106 wfProfileIn( $fname.'-memcached' );
107
108 $wgMemc =& wfGetMainCache();
109 $messageMemc =& wfGetMessageCacheStorage();
110 $parserMemc =& wfGetParserCacheStorage();
111
112 wfDebug( 'Main cache: ' . get_class( $wgMemc ) .
113 "\nMessage cache: " . get_class( $messageMemc ) .
114 "\nParser cache: " . get_class( $parserMemc ) . "\n" );
115
116 wfProfileOut( $fname.'-memcached' );
117 wfProfileIn( $fname.'-SetupSession' );
118
119 if ( $wgDBprefix ) {
120 $wgCookiePrefix = $wgDBname . '_' . $wgDBprefix;
121 } elseif ( $wgSharedDB ) {
122 $wgCookiePrefix = $wgSharedDB;
123 } else {
124 $wgCookiePrefix = $wgDBname;
125 }
126
127 # If session.auto_start is there, we can't touch session name
128 #
129 if (!ini_get('session.auto_start')) {
130 session_name( $wgCookiePrefix . '_session' );
131 }
132
133 if( !$wgCommandLineMode && ( isset( $_COOKIE[session_name()] ) || isset( $_COOKIE[$wgCookiePrefix.'Token'] ) ) ) {
134 wfIncrStats( 'request_with_session' );
135 User::SetupSession();
136 $wgSessionStarted = true;
137 } else {
138 wfIncrStats( 'request_without_session' );
139 $wgSessionStarted = false;
140 }
141
142 wfProfileOut( $fname.'-SetupSession' );
143 wfProfileIn( $fname.'-database' );
144
145 if ( !$wgDBservers ) {
146 $wgDBservers = array(array(
147 'host' => $wgDBserver,
148 'user' => $wgDBuser,
149 'password' => $wgDBpassword,
150 'dbname' => $wgDBname,
151 'type' => $wgDBtype,
152 'load' => 1,
153 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
154 ));
155 }
156 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers, false, $wgMasterWaitTimeout );
157 $wgLoadBalancer->loadMasterPos();
158
159 wfProfileOut( $fname.'-database' );
160 wfProfileIn( $fname.'-language1' );
161
162 require_once( "$IP/languages/Language.php" );
163
164 function setupLangObj($langclass) {
165 global $IP;
166
167 if( ! class_exists( $langclass ) ) {
168 # Default to English/UTF-8
169 $baseclass = 'LanguageUtf8';
170 require_once( "$IP/languages/$baseclass.php" );
171 $lc = strtolower(substr($langclass, 8));
172 $snip = "
173 class $langclass extends $baseclass {
174 function getVariants() {
175 return array(\"$lc\");
176 }
177
178 }";
179 eval($snip);
180 }
181
182 $lang = new $langclass();
183
184 return $lang;
185 }
186
187 # $wgLanguageCode may be changed later to fit with user preference.
188 # The content language will remain fixed as per the configuration,
189 # so let's keep it.
190 $wgContLanguageCode = $wgLanguageCode;
191 $wgContLangClass = 'Language' . str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
192
193 $wgContLang = setupLangObj( $wgContLangClass );
194 $wgContLang->initEncoding();
195
196 wfProfileOut( $fname.'-language1' );
197 wfProfileIn( $fname.'-User' );
198
199 # Skin setup functions
200 # Entries can be added to this variable during the inclusion
201 # of the extension file. Skins can then perform any necessary initialisation.
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 // Preload base classes to work around APC/PHP5 bug
245 include_once("$IP/languages/$wgLangClass.deps.php");
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( $parserMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname);
256
257 wfProfileOut( $fname.'-MessageCache' );
258
259 #
260 # I guess the warning about UI switching might still apply...
261 #
262 # FIXME: THE ABOVE MIGHT BREAK NAMESPACES, VARIABLES,
263 # SEARCH INDEX UPDATES, AND MANY MANY THINGS.
264 # DO NOT USE THIS MODE EXCEPT FOR TESTING RIGHT NOW.
265 #
266 # To disable it, the easiest thing could be to uncomment the
267 # following; they should effectively disable the UI switch functionality
268 #
269 # $wgLangClass = $wgContLangClass;
270 # $wgLanguageCode = $wgContLanguageCode;
271 # $wgLang = $wgContLang;
272 #
273 # TODO: Need to change reference to $wgLang to $wgContLang at proper
274 # places, including namespaces, dates in signatures, magic words,
275 # and links
276 #
277 # TODO: Need to look at the issue of input/output encoding
278 #
279
280
281 wfProfileIn( $fname.'-OutputPage' );
282
283 $wgOut = new OutputPage();
284
285 wfProfileOut( $fname.'-OutputPage' );
286 wfProfileIn( $fname.'-misc2' );
287
288 $wgDeferredUpdateList = array();
289 $wgPostCommitUpdateList = array();
290
291 $wgMagicWords = array();
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 foreach ( $wgExtensionFunctions as $func ) {
315 call_user_func( $func );
316 }
317
318 // For compatibility
319 wfRunHooks( 'LogPageValidTypes', array( &$wgLogTypes ) );
320 wfRunHooks( 'LogPageLogName', array( &$wgLogNames ) );
321 wfRunHooks( 'LogPageLogHeader', array( &$wgLogHeaders ) );
322 wfRunHooks( 'LogPageActionText', array( &$wgLogActions ) );
323
324
325 wfDebug( "\n" );
326 $wgFullyInitialised = true;
327 wfProfileOut( $fname.'-extensions' );
328 wfProfileOut( $fname );
329
330 }
331 ?>