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