Clean up experiments for special:data, special:validate, special:geo.
[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 global $wgProfiling, $wgProfileSampleRate, $wgIP, $wgUseSquid, $IP;
20
21 if( !isset( $wgProfiling ) )
22 $wgProfiling = false;
23
24 if ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
25 require_once( 'Profiling.php' );
26 } else {
27 function wfProfileIn( $fn = '' ) {}
28 function wfProfileOut( $fn = '' ) {}
29 function wfGetProfilingOutput( $s, $e ) {}
30 function wfProfileClose() {}
31 }
32
33 /* collect the originating ips */
34 if( $wgUseSquid && isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
35 # If the web server is behind a reverse proxy, we need to find
36 # out where our requests are really coming from.
37 $hopips = array_map( 'trim', explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
38
39 $allsquids = array_merge($wgSquidServers, $wgSquidServersNoPurge);
40 while(in_array(trim(end($hopips)), $allsquids)){
41 array_pop($hopips);
42 }
43 $wgIP = trim(end($hopips));
44 } elseif( isset( $_SERVER['REMOTE_ADDR'] ) ) {
45 $wgIP = $_SERVER['REMOTE_ADDR'];
46 } else {
47 # Running on CLI?
48 $wgIP = '127.0.0.1';
49 }
50
51 $fname = 'Setup.php';
52 wfProfileIn( $fname );
53 global $wgUseDynamicDates;
54 wfProfileIn( $fname.'-includes' );
55
56 require_once( 'GlobalFunctions.php' );
57 require_once( 'Hooks.php' );
58 require_once( 'Namespace.php' );
59 require_once( 'RecentChange.php' );
60 require_once( 'User.php' );
61 require_once( 'Skin.php' );
62 require_once( 'OutputPage.php' );
63 require_once( 'LinkCache.php' );
64 require_once( 'Title.php' );
65 require_once( 'Article.php' );
66 require_once( 'MagicWord.php' );
67 require_once( 'Block.php' );
68 require_once( 'MessageCache.php' );
69 require_once( 'BlockCache.php' );
70 require_once( 'Parser.php' );
71 require_once( 'Parser.php' );
72 require_once( 'ParserCache.php' );
73 require_once( 'WebRequest.php' );
74 require_once( 'LoadBalancer.php' );
75 require_once( 'HistoryBlob.php' );
76
77 $wgRequest = new WebRequest();
78
79 wfProfileOut( $fname.'-includes' );
80 wfProfileIn( $fname.'-misc1' );
81 global $wgUser, $wgLang, $wgContLang, $wgOut, $wgTitle;
82 global $wgLangClass, $wgContLangClass;
83 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
84 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
85 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
86 global $wgMsgCacheExpiry, $wgCommandLineMode;
87 global $wgBlockCache, $wgParserCache, $wgParser, $wgMsgParserOptions;
88 global $wgLoadBalancer, $wgDBservers, $wgDebugDumpSql;
89 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype;
90 global $wgUseOldExistenceCheck, $wgEnablePersistentLC, $wgMasterWaitTimeout;
91
92 global $wgFullyInitialised;
93
94 # Useful debug output
95 if ( $wgCommandLineMode ) {
96 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
97 } elseif ( function_exists( 'getallheaders' ) ) {
98 wfDebug( "\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 } else {
106 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
107 }
108
109 # Disable linkscc except if the old existence check method is enabled
110 if (!$wgUseOldExistenceCheck) {
111 $wgEnablePersistentLC = false;
112 }
113
114 wfProfileOut( $fname.'-misc1' );
115 wfProfileIn( $fname.'-memcached' );
116
117 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
118 # It acts as a memcached server with no RAM, that is, all objects are
119 # cleared the moment they are set. All set operations succeed and all
120 # get operations return null.
121
122 if( $wgUseMemCached ) {
123 # Set up Memcached
124 #
125 require_once( 'memcached-client.php' );
126
127 /**
128 *
129 * @package MediaWiki
130 */
131 class MemCachedClientforWiki extends memcached {
132 function _debugprint( $text ) {
133 wfDebug( "memcached: $text\n" );
134 }
135 }
136
137 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
138 $wgMemc->set_servers( $wgMemCachedServers );
139 $wgMemc->set_debug( $wgMemCachedDebug );
140
141 $messageMemc = &$wgMemc;
142 } elseif ( $wgUseTurckShm ) {
143 # Turck shared memory
144 #
145 require_once( 'ObjectCache.php' );
146 $wgMemc = new TurckBagOStuff;
147 $messageMemc = &$wgMemc;
148 } else {
149 /**
150 * No shared memory
151 * @package MediaWiki
152 */
153 class FakeMemCachedClient {
154 function add ($key, $val, $exp = 0) { return true; }
155 function decr ($key, $amt=1) { return null; }
156 function delete ($key, $time = 0) { return false; }
157 function disconnect_all () { }
158 function enable_compress ($enable) { }
159 function forget_dead_hosts () { }
160 function get ($key) { return null; }
161 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
162 function incr ($key, $amt=1) { return null; }
163 function replace ($key, $value, $exp=0) { return false; }
164 function run_command ($sock, $cmd) { return null; }
165 function set ($key, $value, $exp=0){ return true; }
166 function set_compress_threshold ($thresh){ }
167 function set_debug ($dbg) { }
168 function set_servers ($list) { }
169 }
170 $wgMemc = new FakeMemCachedClient();
171
172 # Give the message cache a separate cache in the DB.
173 # This is a speedup over separately querying every message used
174 require_once( 'ObjectCache.php' );
175 $messageMemc = new MediaWikiBagOStuff('objectcache');
176 }
177
178 wfProfileOut( $fname.'-memcached' );
179 wfProfileIn( $fname.'-SetupSession' );
180
181 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get('session.name')] ) || isset( $_COOKIE[$wgDBname.'Token'] ) ) ) {
182 User::SetupSession();
183 $wgSessionStarted = true;
184 } else {
185 $wgSessionStarted = false;
186 }
187
188 wfProfileOut( $fname.'-SetupSession' );
189 wfProfileIn( $fname.'-database' );
190
191 if ( !$wgDBservers ) {
192 $wgDBservers = array(array(
193 'host' => $wgDBserver,
194 'user' => $wgDBuser,
195 'password' => $wgDBpassword,
196 'dbname' => $wgDBname,
197 'type' => $wgDBtype,
198 'load' => 1,
199 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
200 ));
201 }
202 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers, false, $wgMasterWaitTimeout );
203 $wgLoadBalancer->loadMasterPos();
204
205 wfProfileOut( $fname.'-database' );
206 wfProfileIn( $fname.'-language1' );
207
208 require_once( "$IP/languages/Language.php" );
209
210 wfProfileOut( $fname.'-language1' );
211 wfProfileIn( $fname.'-User' );
212
213 # Skin setup functions
214 # Entries can be added to this variable during the inclusion
215 # of the extension file. Skins can then perform any necessary initialisation.
216 foreach ( $wgSkinExtensionFunctions as $func ) {
217 $func();
218 }
219
220 if( !is_object( $wgAuth ) ) {
221 require_once( 'AuthPlugin.php' );
222 $wgAuth = new AuthPlugin();
223 }
224
225 if( $wgCommandLineMode ) {
226 # Used for some maintenance scripts; user session cookies can screw things up
227 # when the database is in an in-between state.
228 $wgUser = new User();
229 # Prevent loading User settings from the DB.
230 $wgUser->setLoaded( true );
231 } else {
232 $wgUser = User::loadFromSession();
233 }
234
235 wfProfileOut( $fname.'-User' );
236 wfProfileIn( $fname.'-language2' );
237
238 function setupLangObj(&$langclass) {
239 global $wgUseLatin1, $IP;
240
241 if( ! class_exists( $langclass ) ) {
242 # Default to English/UTF-8, or for non-UTF-8, to latin-1
243 $baseclass = 'LanguageUtf8';
244 if( $wgUseLatin1 )
245 $baseclass = 'LanguageLatin1';
246 require_once( "$IP/languages/$baseclass.php" );
247 $lc = strtolower(substr($langclass, 8));
248 $snip = "
249 class $langclass extends $baseclass {
250 function getVariants() {
251 return array(\"$lc\");
252 }
253
254 }";
255 eval($snip);
256 }
257
258 $lang = new $langclass();
259
260 return $lang;
261 }
262
263 # $wgLanguageCode may be changed later to fit with user preference.
264 # The content language will remain fixed as per the configuration,
265 # so let's keep it.
266 $wgContLanguageCode = $wgLanguageCode;
267 $wgContLangClass = 'Language' . str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
268
269 $wgContLang = setupLangObj( $wgContLangClass );
270
271 // set default user option from content language
272 if( !$wgUser->mDataLoaded ) {
273 $wgUser->loadDefaultFromLanguage();
274 }
275
276 // wgLanguageCode now specifically means the UI language
277 $wgLanguageCode = $wgUser->getOption('language');
278 # Validate $wgLanguageCode, which will soon be sent to an eval()
279 if( empty( $wgLanguageCode ) || !preg_match( '/^[a-z\-]*$/', $wgLanguageCode ) ) {
280 $wgLanguageCode = $wgContLanguageCode;
281 }
282
283 $wgLangClass = 'Language'. str_replace( '-', '_', ucfirst( $wgLanguageCode ) );
284
285 if( $wgLangClass == $wgContLangClass ) {
286 $wgLang = &$wgContLang;
287 } else {
288 wfSuppressWarnings();
289 include_once("$IP/languages/$wgLangClass.php");
290 wfRestoreWarnings();
291
292 $wgLang = setupLangObj( $wgLangClass );
293 }
294
295 wfProfileOut( $fname.'-language' );
296 wfProfileIn( $fname.'-MessageCache' );
297
298 $wgMessageCache = new MessageCache;
299 $wgMessageCache->initialise( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname);
300
301 wfProfileOut( $fname.'-MessageCache' );
302
303 #
304 # I guess the warning about UI switching might still apply...
305 #
306 # FIXME: THE ABOVE MIGHT BREAK NAMESPACES, VARIABLES,
307 # SEARCH INDEX UPDATES, AND MANY MANY THINGS.
308 # DO NOT USE THIS MODE EXCEPT FOR TESTING RIGHT NOW.
309 #
310 # To disable it, the easiest thing could be to uncomment the
311 # following; they should effectively disable the UI switch functionality
312 #
313 # $wgLangClass = $wgContLangClass;
314 # $wgLanguageCode = $wgContLanguageCode;
315 # $wgLang = $wgContLang;
316 #
317 # TODO: Need to change reference to $wgLang to $wgContLang at proper
318 # places, including namespaces, dates in signatures, magic words,
319 # and links
320 #
321 # TODO: Need to look at the issue of input/output encoding
322 #
323
324
325 wfProfileIn( $fname.'-OutputPage' );
326
327 $wgOut = new OutputPage();
328 wfDebug( "\n\n" );
329
330 wfProfileOut( $fname.'-OutputPage' );
331 wfProfileIn( $fname.'-DateFormatter' );
332
333 if ( $wgUseDynamicDates ) {
334 require_once( 'DateFormatter.php' );
335 global $wgDateFormatter;
336 $wgDateFormatter = new DateFormatter;
337 }
338
339 wfProfileOut( $fname.'-DateFormatter' );
340 wfProfileIn( $fname.'-BlockCache' );
341
342 $wgBlockCache = new BlockCache( true );
343
344 wfProfileOut( $fname.'-BlockCache' );
345 wfProfileIn( $fname.'-misc2' );
346
347 $wgDeferredUpdateList = array();
348 $wgLinkCache = new LinkCache();
349 $wgMagicWords = array();
350 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
351 $wgParserCache = new ParserCache( $messageMemc );
352
353 if ( $wgUseXMLparser ) {
354 require_once( 'ParserXML.php' );
355 $wgParser = new ParserXML();
356 } else {
357 $wgParser = new Parser();
358 }
359 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
360 $wgMsgParserOptions = ParserOptions::newFromUser($wgUser);
361 wfSeedRandom();
362
363 # Placeholders in case of DB error
364 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Error' );
365 $wgArticle = new Article($wgTitle);
366
367 # Site notice
368 # FIXME: This is an ugly hack, which wastes runtime on cache hits
369 # and raw page views by forcing initialization of the message cache.
370 # Try to fake around it for raw at least:
371 if( !isset( $_REQUEST['action'] ) || $_REQUEST['action'] != 'raw' ) {
372 $notice = wfMsg( 'sitenotice' );
373 if($notice == '&lt;sitenotice&gt;') $notice = '';
374 # Allow individual wikis to turn it off
375 if ( $notice == '-' ) {
376 $wgSiteNotice = '';
377 } else {
378 # if($wgSiteNotice) $notice .= $wgSiteNotice;
379 if ($notice == '') {
380 $notice = $wgSiteNotice;
381 }
382 if($notice != '-' && $notice != '') {
383 $specialparser = new Parser();
384 $parserOutput = $specialparser->parse( $notice, $wgTitle, $wgOut->mParserOptions, false );
385 $wgSiteNotice = $parserOutput->getText();
386 }
387 }
388 }
389
390 wfProfileOut( $fname.'-misc2' );
391 wfProfileIn( $fname.'-extensions' );
392
393 # Extension setup functions for extensions other than skins
394 # Entries should be added to this variable during the inclusion
395 # of the extension file. This allows the extension to perform
396 # any necessary initialisation in the fully initialised environment
397 foreach ( $wgExtensionFunctions as $func ) {
398 $func();
399 }
400
401 $wgFullyInitialised = true;
402 wfProfileOut( $fname.'-extensions' );
403 wfProfileOut( $fname );
404
405 }
406 ?>