* Defer message cache initialization, shaving a few ms off file cache hits
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 */
6
7 /**
8 *
9 */
10 define( 'MSG_LOAD_TIMEOUT', 60);
11 define( 'MSG_LOCK_TIMEOUT', 10);
12 define( 'MSG_WAIT_TIMEOUT', 10);
13
14 /**
15 * Message cache
16 * Performs various useful MediaWiki namespace-related functions
17 *
18 * @package MediaWiki
19 */
20 class MessageCache
21 {
22 var $mCache, $mUseCache, $mDisable, $mExpiry;
23 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
24 var $mExtensionMessages;
25 var $mInitialised = false;
26 var $mDeferred = true;
27
28 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
29 $fname = 'MessageCache::initialise';
30 wfProfileIn( $fname );
31
32 $this->mUseCache = !is_null( $memCached );
33 $this->mMemc = &$memCached;
34 $this->mDisable = !$useDB;
35 $this->mExpiry = $expiry;
36 $this->mDisableTransform = false;
37 $this->mMemcKey = $memcPrefix.':messages';
38 $this->mKeys = false; # initialised on demand
39 $this->mInitialised = true;
40
41 wfProfileIn( $fname.'-parseropt' );
42 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
43 wfProfileOut( $fname.'-parseropt' );
44 wfProfileIn( $fname.'-parser' );
45 $this->mParser = new Parser;
46 wfProfileOut( $fname.'-parser' );
47
48 # When we first get asked for a message,
49 # then we'll fill up the cache. If we
50 # can return a cache hit, this saves
51 # some extra milliseconds
52 $this->mDeferred = true;
53
54 wfProfileOut( $fname );
55 }
56
57 /**
58 * Loads messages either from memcached or the database, if not disabled
59 * On error, quietly switches to a fallback mode
60 * Returns false for a reportable error, true otherwise
61 */
62 function load() {
63 global $wgAllMessagesEn;
64
65 if ( $this->mDisable ) {
66 wfDebug( "MessageCache::load(): disabled\n" );
67 return true;
68 }
69 $fname = 'MessageCache::load';
70 wfProfileIn( $fname );
71 $success = true;
72
73 if ( $this->mUseCache ) {
74 wfProfileIn( $fname.'-fromcache' );
75 $this->mCache = $this->mMemc->get( $this->mMemcKey );
76 wfProfileOut( $fname.'-fromcache' );
77
78 # If there's nothing in memcached, load all the messages from the database
79 if ( !$this->mCache ) {
80 wfDebug( "MessageCache::load(): loading all messages\n" );
81 $this->lock();
82 # Other threads don't need to load the messages if another thread is doing it.
83 $success = $this->mMemc->add( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
84 if ( $success ) {
85 wfProfileIn( $fname.'-load' );
86 $this->loadFromDB();
87 wfProfileOut( $fname.'-load' );
88 # Save in memcached
89 # Keep trying if it fails, this is kind of important
90 wfProfileIn( $fname.'-save' );
91 for ( $i=0; $i<20 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
92 usleep(mt_rand(500000,1500000));
93 }
94 wfProfileOut( $fname.'-save' );
95 if ( $i == 20 ) {
96 $this->mMemc->set( $this->mMemcKey, 'error', 86400 );
97 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
98 }
99 }
100 $this->unlock();
101 }
102
103 if ( !is_array( $this->mCache ) ) {
104 wfMsg( "MessageCache::load(): individual message mode\n" );
105 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
106 # Causing too much DB load, disabling -- TS
107 $this->mDisable = true;
108 /*
109 if ( $this->mCache == "loading" ) {
110 $this->mUseCache = false;
111 } elseif ( $this->mCache == "error" ) {
112 $this->mUseCache = false;
113 $success = false;
114 } else {
115 $this->mDisable = true;
116 $success = false;
117 }*/
118 $this->mCache = false;
119 }
120 }
121 wfProfileOut( $fname );
122 $this->mDeferred = false;
123 return $success;
124 }
125
126 /**
127 * Loads all or main part of cacheable messages from the database
128 */
129 function loadFromDB() {
130 global $wgPartialMessageCache;
131 $fname = 'MessageCache::loadFromDB';
132 $dbr =& wfGetDB( DB_SLAVE );
133 $conditions = array( 'cur_is_redirect' => 0,
134 'cur_namespace' => NS_MEDIAWIKI);
135 if ($wgPartialMessageCache) {
136 if (is_array($wgPartialMessageCache)) {
137 $conditions['cur_title']=$wgPartialMessageCache;
138 } else {
139 require_once("MessageCacheHints.php");
140 $conditions['cur_title']=MessageCacheHints::get();
141 }
142 }
143 $res = $dbr->select( 'cur',
144 array( 'cur_title', 'cur_text' ), $conditions, $fname);
145
146 $this->mCache = array();
147 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
148 $this->mCache[$row->cur_title] = $row->cur_text;
149 }
150
151 $dbr->freeResult( $res );
152 }
153
154 /**
155 * Not really needed anymore
156 */
157 function getKeys() {
158 global $wgAllMessagesEn, $wgContLang;
159 if ( !$this->mKeys ) {
160 $this->mKeys = array();
161 foreach ( $wgAllMessagesEn as $key => $value ) {
162 array_push( $this->mKeys, $wgContLang->ucfirst( $key ) );
163 }
164 }
165 return $this->mKeys;
166 }
167
168 /**
169 * Obsolete
170 */
171 function isCacheable( $key ) {
172 return true;
173 /*
174 global $wgAllMessagesEn, $wgLang;
175 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
176 array_key_exists( $key, $wgAllMessagesEn );
177 */
178 }
179
180 function replace( $title, $text ) {
181 $this->lock();
182 $this->load();
183 if ( is_array( $this->mCache ) ) {
184 $this->mCache[$title] = $text;
185 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
186 }
187 $this->unlock();
188 }
189
190 /**
191 * Returns success
192 * Represents a write lock on the messages key
193 */
194 function lock() {
195 if ( !$this->mUseCache ) {
196 return true;
197 }
198
199 $lockKey = $this->mMemcKey . 'lock';
200 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
201 sleep(1);
202 }
203
204 return $i >= MSG_WAIT_TIMEOUT;
205 }
206
207 function unlock() {
208 if ( !$this->mUseCache ) {
209 return;
210 }
211
212 $lockKey = $this->mMemcKey . 'lock';
213 $this->mMemc->delete( $lockKey );
214 }
215
216 function get( $key, $useDB, $forcontent=true ) {
217 global $wgContLanguageCode;
218 if( $forcontent ) {
219 global $wgContLang;
220 $lang =& $wgContLang;
221 $langcode = $wgContLanguageCode;
222 } else {
223 global $wgLang, $wgLanguageCode;
224 $lang =& $wgLang;
225 $langcode = $wgLanguageCode;
226 }
227 # If uninitialised, someone is trying to call this halfway through Setup.php
228 if( !$this->mInitialised ) {
229 return "&lt;$key&gt;";
230 }
231 # If cache initialization was deferred, start it now.
232 if( $this->mDeferred ) {
233 $this->load();
234 }
235
236 $message = false;
237 if( !$this->mDisable && $useDB ) {
238 $title = $lang->ucfirst( $key );
239 if( $langcode != $wgContLanguageCode ) {
240 $title .= '/' . $langcode;
241 }
242
243 # Try the cache
244 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
245 $message = $this->mCache[$title];
246 }
247
248 if ( !$message && $this->mUseCache ) {
249 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
250 if( $message ) {
251 $this->mCache[$title] = $message;
252 }
253 }
254
255 # If it wasn't in the cache, load each message from the DB individually
256 if ( !$message ) {
257 $dbr =& wfGetDB( DB_SLAVE );
258 $result = $dbr->selectRow( 'cur', array('cur_text'),
259 array( 'cur_namespace' => NS_MEDIAWIKI, 'cur_title' => $title ),
260 'MessageCache::get' );
261 if ( $result ) {
262 $message = $result->cur_text;
263 if( $this->mUseCache ) {
264 $this->mCache[$title] = $message;
265 /* individual messages may be often
266 recached until proper purge code exists
267 */
268 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
269 }
270 }
271 }
272 }
273 # Try the extension array
274 if( !$message ) {
275 $message = @$this->mExtensionMessages[$key];
276 }
277
278 # Try the array in the language object
279 if( !$message ) {
280 wfSuppressWarnings();
281 $message = $lang->getMessage( $key );
282 wfRestoreWarnings();
283 }
284
285 # Try the English array
286 if( !$message && $langcode != 'en' ) {
287 wfSuppressWarnings();
288 $message = Language::getMessage( $key );
289 wfRestoreWarnings();
290 }
291
292 # Final fallback
293 if( !$message ) {
294 $message = "&lt;$key&gt;";
295 }
296
297 # Replace brace tags
298 $message = $this->transform( $message );
299 return $message;
300 }
301
302 function transform( $message ) {
303 if( !$this->mDisableTransform ) {
304 if( strpos( $message, '{{' ) !== false ) {
305 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
306 }
307 }
308 return $message;
309 }
310
311 function disable() { $this->mDisable = true; }
312 function enable() { $this->mDisable = false; }
313 function disableTransform() { $this->mDisableTransform = true; }
314 function enableTransform() { $this->mDisableTransform = false; }
315
316 function addMessage( $key, $value ) {
317 $this->mExtensionMessages[$key] = $value;
318 }
319
320 function addMessages( $messages ) {
321 foreach ( $messages as $key => $value ) {
322 $this->mExtensionMessages[$key] = $value;
323 }
324 }
325
326 /**
327 * Clear all stored messages. Mainly used after a mass rebuild.
328 */
329 function clear() {
330 if( $this->mUseCache ) {
331 $this->mMemc->delete( $this->mMemcKey );
332 }
333 }
334 }
335 ?>