use Database::timestamp() for cutoff
[lhc/web/wiklou.git] / includes / Setup.php
1 <?php
2 if( defined( "MEDIAWIKI" ) ) {
3
4 # The main wiki script and things like database
5 # conversion and maintenance scripts all share a
6 # common setup of including lots of classes and
7 # setting up a few globals.
8 #
9
10 global $wgProfiling, $wgProfileSampleRate, $wgIP, $wgUseSquid;
11
12 if( !isset( $wgProfiling ) )
13 $wgProfiling = false;
14
15 if ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
16 require_once( 'Profiling.php' );
17 } else {
18 function wfProfileIn( $fn = '' ) {}
19 function wfProfileOut( $fn = '' ) {}
20 function wfGetProfilingOutput( $s, $e ) {}
21 function wfProfileClose() {}
22 }
23
24 /* collect the originating ips */
25 if( $wgUseSquid && isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
26 # If the web server is behind a reverse proxy, we need to find
27 # out where our requests are really coming from.
28 $hopips = array_map( 'trim', explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
29
30 while(in_array(trim(end($hopips)), $wgSquidServers)){
31 array_pop($hopips);
32 }
33 $wgIP = trim(end($hopips));
34 } else {
35 $wgIP = getenv('REMOTE_ADDR');
36 }
37
38
39 $fname = 'Setup.php';
40 wfProfileIn( $fname );
41 global $wgUseDynamicDates;
42 wfProfileIn( $fname.'-includes' );
43
44 require_once( 'GlobalFunctions.php' );
45 require_once( 'Namespace.php' );
46 require_once( 'RecentChange.php' );
47 require_once( 'User.php' );
48 require_once( 'Skin.php' );
49 require_once( 'OutputPage.php' );
50 require_once( 'LinkCache.php' );
51 require_once( 'Title.php' );
52 require_once( 'Article.php' );
53 require_once( 'MagicWord.php' );
54 require_once( 'memcached-client.php' );
55 require_once( 'Block.php' );
56 require_once( 'SearchEngine.php' );
57 require_once( 'DifferenceEngine.php' );
58 require_once( 'MessageCache.php' );
59 require_once( 'BlockCache.php' );
60 require_once( 'Parser.php' );
61 require_once( 'ParserCache.php' );
62 require_once( 'WebRequest.php' );
63 require_once( 'LoadBalancer.php' );
64
65 $wgRequest = new WebRequest();
66
67
68
69 wfProfileOut( $fname.'-includes' );
70 wfProfileIn( $fname.'-memcached' );
71 global $wgUser, $wgLang, $wgOut, $wgTitle;
72 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
73 global $wgMemc, $wgMagicWords, $wgMwRedir, $wgDebugLogFile;
74 global $wgMessageCache, $wgUseMemCached, $wgUseDatabaseMessages;
75 global $wgMsgCacheExpiry, $wgCommandLineMode;
76 global $wgBlockCache, $wgParserCache, $wgParser, $wgDBConnections;
77 global $wgLoadBalancer, $wgDBservers, $wgDebugDumpSql;
78 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype;
79 global $wgFullyInitialised;
80
81 # Useful debug output
82 if ( $wgCommandLineMode ) {
83 # wfDebug( '"' . implode( '" "', $argv ) . '"' );
84 } elseif ( function_exists( 'getallheaders' ) ) {
85 wfDebug( "\nStart request\n" );
86 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
87 $headers = getallheaders();
88 foreach ($headers as $name => $value) {
89 wfDebug( "$name: $value\n" );
90 }
91 wfDebug( "\n" );
92 } else {
93 wfDebug( $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . "\n" );
94 }
95
96 # Set up Memcached
97 #
98 class MemCachedClientforWiki extends memcached {
99 function _debugprint( $text ) {
100 wfDebug( "memcached: $text\n" );
101 }
102 }
103
104 # FakeMemCachedClient imitates the API of memcached-client v. 0.1.2.
105 # It acts as a memcached server with no RAM, that is, all objects are
106 # cleared the moment they are set. All set operations succeed and all
107 # get operations return null.
108
109 class FakeMemCachedClient {
110 function add ($key, $val, $exp = 0) { return true; }
111 function decr ($key, $amt=1) { return null; }
112 function delete ($key, $time = 0) { return false; }
113 function disconnect_all () { }
114 function enable_compress ($enable) { }
115 function forget_dead_hosts () { }
116 function get ($key) { return null; }
117 function get_multi ($keys) { return array_pad(array(), count($keys), null); }
118 function incr ($key, $amt=1) { return null; }
119 function replace ($key, $value, $exp=0) { return false; }
120 function run_command ($sock, $cmd) { return null; }
121 function set ($key, $value, $exp=0){ return true; }
122 function set_compress_threshold ($thresh){ }
123 function set_debug ($dbg) { }
124 function set_servers ($list) { }
125 }
126
127 if( $wgUseMemCached ) {
128 $wgMemc = new MemCachedClientforWiki( array('persistant' => true) );
129 $wgMemc->set_servers( $wgMemCachedServers );
130 $wgMemc->set_debug( $wgMemCachedDebug );
131
132 $messageMemc = &$wgMemc;
133 } else {
134 $wgMemc = new FakeMemCachedClient();
135
136 # Give the message cache a separate cache in the DB.
137 # This is a speedup over separately querying every message used
138 require_once( 'ObjectCache.php' );
139 $messageMemc = new MediaWikiBagOStuff('objectcache');
140 }
141
142 wfProfileOut( $fname.'-memcached' );
143 wfProfileIn( $fname.'-SetupSession' );
144
145 if( !$wgCommandLineMode && ( isset( $_COOKIE[ini_get('session.name')] ) || isset( $_COOKIE[$wgDBname.'Password'] ) ) ) {
146 User::SetupSession();
147 $wgSessionStarted = true;
148 } else {
149 $wgSessionStarted = false;
150 }
151
152 wfProfileOut( $fname.'-SetupSession' );
153 wfProfileIn( $fname.'-database' );
154
155 if ( !$wgDBservers ) {
156 $wgDBservers = array(array(
157 'host' => $wgDBserver,
158 'user' => $wgDBuser,
159 'password' => $wgDBpassword,
160 'dbname' => $wgDBname,
161 'type' => $wgDBtype,
162 'load' => 1,
163 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
164 ));
165 }
166 $wgLoadBalancer = LoadBalancer::newFromParams( $wgDBservers );
167 $wgLoadBalancer->loadMasterPos();
168
169 wfProfileOut( $fname.'-database' );
170 wfProfileIn( $fname.'-language' );
171 require_once( 'languages/Language.php' );
172
173 $wgMessageCache = new MessageCache;
174
175 $wgLangClass = 'Language' . ucfirst( $wgLanguageCode );
176 if( ! class_exists( $wgLangClass ) || ($wgLanguageCode == 'en' && !$wgUseLatin1) ) {
177 # Default to English/UTF-8
178 require_once( 'languages/LanguageUtf8.php' );
179 $wgLangClass = 'LanguageUtf8';
180 }
181
182 $wgLang = new $wgLangClass();
183 if ( !is_object($wgLang) ) {
184 print "No language class ($wgLang)\N";
185 }
186
187 if( $wgUseLatin1 && $wgLanguageCode != 'en' ) {
188 # For non-UTF-8 non-English.
189 require_once( 'languages/LanguageLatin1.php' );
190 $xxx = new LanguageLatin1( $wgLang );
191 unset( $wgLang );
192 $wgLang = $xxx;
193 }
194 wfProfileOut( $fname.'-language' );
195 wfProfileIn( $fname.'-MessageCache' );
196
197 $wgMessageCache->initialise( $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname );
198
199 wfProfileOut( $fname.'-MessageCache' );
200 wfProfileIn( $fname.'-OutputPage' );
201
202 $wgOut = new OutputPage();
203 wfDebug( "\n\n" );
204
205 wfProfileOut( $fname.'-OutputPage' );
206 wfProfileIn( $fname.'-DateFormatter' );
207
208 if ( $wgUseDynamicDates ) {
209 require_once( 'DateFormatter.php' );
210 global $wgDateFormatter;
211 $wgDateFormatter = new DateFormatter;
212 }
213
214 wfProfileOut( $fname.'-DateFormatter' );
215 wfProfileIn( $fname.'-BlockCache' );
216
217 $wgBlockCache = new BlockCache( true );
218
219 wfProfileOut( $fname.'-BlockCache' );
220 wfProfileIn( $fname.'-User' );
221
222 if( $wgCommandLineMode ) {
223 # Used for some maintenance scripts; user session cookies can screw things up
224 # when the database is in an in-between state.
225 $wgUser = new User();
226 } else {
227 $wgUser = User::loadFromSession();
228 }
229
230 wfProfileOut( $fname.'-User' );
231 wfProfileIn( $fname.'-misc' );
232
233 $wgDeferredUpdateList = array();
234 $wgLinkCache = new LinkCache();
235 $wgMagicWords = array();
236 $wgMwRedir =& MagicWord::get( MAG_REDIRECT );
237 $wgParserCache = new ParserCache();
238 $wgParser = new Parser();
239 $wgOut->setParserOptions( ParserOptions::newFromUser( $wgUser ) );
240 $wgDBConnections = array();
241 wfSeedRandom();
242
243 # Placeholders in case of DB error
244 $wgTitle = Title::newFromText( wfMsg( 'badtitle' ) );
245 $wgArticle = new Article($wgTitle);
246
247 wfProfileOut( $fname.'-misc' );
248 wfProfileIn( $fname.'-extensions' );
249
250 # Extension setup functions
251 # Entries should be added to this variable during the inclusion
252 # of the extension file. This allows the extension to perform
253 # any necessary initialisation in the fully initialised environment
254 foreach ( $wgExtensionFunctions as $func ) {
255 $func();
256 }
257
258 $wgFullyInitialised = true;
259 wfProfileOut( $fname.'-extensions' );
260 wfProfileOut( $fname );
261
262 }
263 ?>