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