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