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