Replacing var keyword with private / public as we now require PHP5.
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /** */
9 require_once( 'Revision.php' );
10
11 /**
12 *
13 */
14 define( 'MSG_LOAD_TIMEOUT', 60);
15 define( 'MSG_LOCK_TIMEOUT', 10);
16 define( 'MSG_WAIT_TIMEOUT', 10);
17
18 /**
19 * Message cache
20 * Performs various useful MediaWiki namespace-related functions
21 *
22 * @package MediaWiki
23 */
24 class MessageCache {
25 private
26 $mCache,
27 $mDeferred = true,
28 $mDisable,
29 $mExpiry,
30 $mExtensionMessages = array(),
31 $mInitialised = false,
32 $mKeys,
33 $mMemcKey,
34 $mParser,
35 $mParserOptions,
36 $mUseCache ;
37
38 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
39 $fname = 'MessageCache::initialise';
40 wfProfileIn( $fname );
41
42 $this->mUseCache = !is_null( $memCached );
43 $this->mMemc = &$memCached;
44 $this->mDisable = !$useDB;
45 $this->mExpiry = $expiry;
46 $this->mDisableTransform = false;
47 $this->mMemcKey = $memcPrefix.':messages';
48 $this->mKeys = false; # initialised on demand
49 $this->mInitialised = true;
50
51 wfProfileIn( $fname.'-parseropt' );
52 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
53 wfProfileOut( $fname.'-parseropt' );
54 wfProfileIn( $fname.'-parser' );
55 $this->mParser = new Parser;
56 wfProfileOut( $fname.'-parser' );
57
58 # When we first get asked for a message,
59 # then we'll fill up the cache. If we
60 # can return a cache hit, this saves
61 # some extra milliseconds
62 $this->mDeferred = true;
63
64 wfProfileOut( $fname );
65 }
66
67 /**
68 * Try to load the cache from a local file
69 */
70 function loadFromLocal( $hash ) {
71 global $wgLocalMessageCache, $wgDBname;
72
73 $this->mCache = false;
74 if ( $wgLocalMessageCache === false ) {
75 return;
76 }
77
78 $filename = "$wgLocalMessageCache/messages-$wgDBname";
79
80 wfSuppressWarnings();
81 $file = fopen( $filename, 'r' );
82 wfRestoreWarnings();
83 if ( !$file ) {
84 return;
85 }
86
87 // Check to see if the file has the hash specified
88 $localHash = fread( $file, 32 );
89 if ( $hash == $localHash ) {
90 // All good, get the rest of it
91 $serialized = fread( $file, 1000000 );
92 $this->mCache = unserialize( $serialized );
93 }
94 fclose( $file );
95 }
96
97 /**
98 * Save the cache to a local file
99 */
100 function saveToLocal( $serialized, $hash ) {
101 global $wgLocalMessageCache, $wgDBname;
102
103 if ( $wgLocalMessageCache === false ) {
104 return;
105 }
106
107 $filename = "$wgLocalMessageCache/messages-$wgDBname";
108 $oldUmask = umask( 0 );
109 wfMkdirParents( $wgLocalMessageCache, 0777 );
110 umask( $oldUmask );
111
112 $file = fopen( $filename, 'w' );
113 if ( !$file ) {
114 wfDebug( "Unable to open local cache file for writing\n" );
115 return;
116 }
117
118 fwrite( $file, $hash . $serialized );
119 fclose( $file );
120 @chmod( $filename, 0666 );
121 }
122
123
124 /**
125 * Loads messages either from memcached or the database, if not disabled
126 * On error, quietly switches to a fallback mode
127 * Returns false for a reportable error, true otherwise
128 */
129 function load() {
130 global $wgLocalMessageCache;
131
132 if ( $this->mDisable ) {
133 static $shownDisabled = false;
134 if ( !$shownDisabled ) {
135 wfDebug( "MessageCache::load(): disabled\n" );
136 $shownDisabled = true;
137 }
138 return true;
139 }
140 $fname = 'MessageCache::load';
141 wfProfileIn( $fname );
142 $success = true;
143
144 if ( $this->mUseCache ) {
145 $this->mCache = false;
146
147 # Try local cache
148 wfProfileIn( $fname.'-fromlocal' );
149 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
150 if ( $hash ) {
151 $this->loadFromLocal( $hash );
152 }
153 wfProfileOut( $fname.'-fromlocal' );
154
155 # Try memcached
156 if ( !$this->mCache ) {
157 wfProfileIn( $fname.'-fromcache' );
158 $this->mCache = $this->mMemc->get( $this->mMemcKey );
159
160 # Save to local cache
161 if ( $wgLocalMessageCache !== false ) {
162 $serialized = serialize( $this->mCache );
163 if ( !$hash ) {
164 $hash = md5( $serialized );
165 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
166 }
167 $this->saveToLocal( $serialized, $hash );
168 }
169 wfProfileOut( $fname.'-fromcache' );
170 }
171
172
173 # If there's nothing in memcached, load all the messages from the database
174 if ( !$this->mCache ) {
175 wfDebug( "MessageCache::load(): loading all messages\n" );
176 $this->lock();
177 # Other threads don't need to load the messages if another thread is doing it.
178 $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
179 if ( $success ) {
180 wfProfileIn( $fname.'-load' );
181 $this->loadFromDB();
182 wfProfileOut( $fname.'-load' );
183
184 # Save in memcached
185 # Keep trying if it fails, this is kind of important
186 wfProfileIn( $fname.'-save' );
187 for ($i=0; $i<20 &&
188 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
189 $i++ ) {
190 usleep(mt_rand(500000,1500000));
191 }
192
193 # Save to local cache
194 if ( $wgLocalMessageCache !== false ) {
195 $serialized = serialize( $this->mCache );
196 $hash = md5( $serialized );
197 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
198 $this->saveToLocal( $serialized, $hash );
199 }
200
201 wfProfileOut( $fname.'-save' );
202 if ( $i == 20 ) {
203 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
204 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
205 }
206 }
207 $this->unlock();
208 }
209
210 if ( !is_array( $this->mCache ) ) {
211 wfDebug( "MessageCache::load(): individual message mode\n" );
212 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
213 # Causing too much DB load, disabling -- TS
214 $this->mDisable = true;
215 /*
216 if ( $this->mCache == "loading" ) {
217 $this->mUseCache = false;
218 } elseif ( $this->mCache == "error" ) {
219 $this->mUseCache = false;
220 $success = false;
221 } else {
222 $this->mDisable = true;
223 $success = false;
224 }*/
225 $this->mCache = false;
226 }
227 }
228 wfProfileOut( $fname );
229 $this->mDeferred = false;
230 return $success;
231 }
232
233 /**
234 * Loads all or main part of cacheable messages from the database
235 */
236 function loadFromDB() {
237 global $wgAllMessagesEn, $wgLang;
238
239 $fname = 'MessageCache::loadFromDB';
240 $dbr =& wfGetDB( DB_SLAVE );
241 if ( !$dbr ) {
242 wfDebugDieBacktrace( 'Invalid database object' );
243 }
244 $conditions = array( 'page_is_redirect' => 0,
245 'page_namespace' => NS_MEDIAWIKI);
246 $res = $dbr->select( array( 'page', 'revision', 'text' ),
247 array( 'page_title', 'old_text', 'old_flags' ),
248 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
249 $fname
250 );
251
252 $this->mCache = array();
253 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
254 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
255 }
256
257 # Negative caching
258 # Go through the language array and the extension array and make a note of
259 # any keys missing from the cache
260 foreach ( $wgAllMessagesEn as $key => $value ) {
261 $uckey = $wgLang->ucfirst( $key );
262 if ( !array_key_exists( $uckey, $this->mCache ) ) {
263 $this->mCache[$uckey] = false;
264 }
265 }
266 foreach ( $this->mExtensionMessages as $key => $value ) {
267 $uckey = $wgLang->ucfirst( $key );
268 if ( !array_key_exists( $uckey, $this->mCache ) ) {
269 $this->mCache[$uckey] = false;
270 }
271 }
272
273 $dbr->freeResult( $res );
274 }
275
276 /**
277 * Not really needed anymore
278 */
279 function getKeys() {
280 global $wgAllMessagesEn, $wgContLang;
281 if ( !$this->mKeys ) {
282 $this->mKeys = array();
283 foreach ( $wgAllMessagesEn as $key => $value ) {
284 $title = $wgContLang->ucfirst( $key );
285 array_push( $this->mKeys, $title );
286 }
287 }
288 return $this->mKeys;
289 }
290
291 /**
292 * @deprecated
293 */
294 function isCacheable( $key ) {
295 return true;
296 }
297
298 function replace( $title, $text ) {
299 global $wgLocalMessageCache, $parserMemc, $wgDBname;
300
301 $this->lock();
302 $this->load();
303 $parserMemc->delete("$wgDBname:sidebar");
304 if ( is_array( $this->mCache ) ) {
305 $this->mCache[$title] = $text;
306 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
307
308 # Save to local cache
309 if ( $wgLocalMessageCache !== false ) {
310 $serialized = serialize( $this->mCache );
311 $hash = md5( $serialized );
312 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
313 $this->saveToLocal( $serialized, $hash );
314 }
315
316
317 }
318 $this->unlock();
319 }
320
321 /**
322 * Returns success
323 * Represents a write lock on the messages key
324 */
325 function lock() {
326 if ( !$this->mUseCache ) {
327 return true;
328 }
329
330 $lockKey = $this->mMemcKey . 'lock';
331 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
332 sleep(1);
333 }
334
335 return $i >= MSG_WAIT_TIMEOUT;
336 }
337
338 function unlock() {
339 if ( !$this->mUseCache ) {
340 return;
341 }
342
343 $lockKey = $this->mMemcKey . 'lock';
344 $this->mMemc->delete( $lockKey );
345 }
346
347 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
348 global $wgContLanguageCode;
349 if( $forcontent ) {
350 global $wgContLang;
351 $lang =& $wgContLang;
352 $langcode = $wgContLanguageCode;
353 } else {
354 global $wgLang, $wgLanguageCode;
355 $lang =& $wgLang;
356 $langcode = $wgLanguageCode;
357 }
358 # If uninitialised, someone is trying to call this halfway through Setup.php
359 if( !$this->mInitialised ) {
360 return '&lt;' . htmlspecialchars($key) . '&gt;';
361 }
362 # If cache initialization was deferred, start it now.
363 if( $this->mDeferred && !$this->mDisable && $useDB ) {
364 $this->load();
365 }
366
367 $message = false;
368 if( !$this->mDisable && $useDB ) {
369 $title = $lang->ucfirst( $key );
370 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
371 $title .= '/' . $langcode;
372 }
373 $message = $this->getFromCache( $title );
374 }
375 # Try the extension array
376 if( $message === false && array_key_exists( $key, $this->mExtensionMessages ) ) {
377 $message = $this->mExtensionMessages[$key];
378 }
379
380 # Try the array in the language object
381 if( $message === false ) {
382 wfSuppressWarnings();
383 $message = $lang->getMessage( $key );
384 wfRestoreWarnings();
385 if ( is_null( $message ) ) {
386 $message = false;
387 }
388 }
389
390 # Try the English array
391 if( $message === false && $langcode != 'en' ) {
392 wfSuppressWarnings();
393 $message = Language::getMessage( $key );
394 wfRestoreWarnings();
395 if ( is_null( $message ) ) {
396 $message = false;
397 }
398 }
399
400 # Is this a custom message? Try the default language in the db...
401 if( $message === false &&
402 !$this->mDisable && $useDB &&
403 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
404 $message = $this->getFromCache( $lang->ucfirst( $key ) );
405 }
406
407 # Final fallback
408 if( $message === false ) {
409 return '&lt;' . htmlspecialchars($key) . '&gt;';
410 }
411
412 # Replace brace tags
413 $message = $this->transform( $message );
414 return $message;
415 }
416
417 function getFromCache( $title ) {
418 $message = false;
419
420 # Try the cache
421 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
422 return $this->mCache[$title];
423 }
424
425 # Try individual message cache
426 if ( $this->mUseCache ) {
427 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
428 if ( $message == '###NONEXISTENT###' ) {
429 return false;
430 } elseif( !is_null( $message ) ) {
431 $this->mCache[$title] = $message;
432 return $message;
433 } else {
434 $message = false;
435 }
436 }
437
438 # Call message Hooks, in case they are defined
439 wfRunHooks('MessagesPreLoad',array($title,&$message));
440
441 # If it wasn't in the cache, load each message from the DB individually
442 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
443 if( $revision ) {
444 $message = $revision->getText();
445 if ($this->mUseCache) {
446 $this->mCache[$title]=$message;
447 /* individual messages may be often
448 recached until proper purge code exists
449 */
450 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
451 }
452 } else {
453 # Negative caching
454 # Use some special text instead of false, because false gets converted to '' somewhere
455 $this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
456 }
457
458 return $message;
459 }
460
461 function transform( $message ) {
462 if( !$this->mDisableTransform ) {
463 if( strpos( $message, '{{' ) !== false ) {
464 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
465 }
466 }
467 return $message;
468 }
469
470 function disable() { $this->mDisable = true; }
471 function enable() { $this->mDisable = false; }
472 function disableTransform() { $this->mDisableTransform = true; }
473 function enableTransform() { $this->mDisableTransform = false; }
474 function setTransform( $x ) { $this->mDisableTransform = $x; }
475 function getTransform() { return $this->mDisableTransform; }
476
477 /**
478 * Add a message to the cache
479 *
480 * @param mixed $key
481 * @param mixed $value
482 */
483 function addMessage( $key, $value ) {
484 $this->mExtensionMessages[$key] = $value;
485 }
486
487 /**
488 * Add an associative array of message to the cache
489 *
490 * @param array $messages An associative array of key => values to be added
491 */
492 function addMessages( $messages ) {
493 foreach ( $messages as $key => $value ) {
494 $this->addMessage( $key, $value );
495 }
496 }
497
498 /**
499 * Clear all stored messages. Mainly used after a mass rebuild.
500 */
501 function clear() {
502 if( $this->mUseCache ) {
503 $this->mMemc->delete( $this->mMemcKey );
504 }
505 }
506 }
507 ?>