3a11444c656606ba5bb2092033bc012af870a5f4
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @addtogroup Cache
5 */
6
7 /**
8 *
9 */
10 define( 'MSG_LOAD_TIMEOUT', 60);
11 define( 'MSG_LOCK_TIMEOUT', 10);
12 define( 'MSG_WAIT_TIMEOUT', 10);
13 define( 'MSG_CACHE_VERSION', 1 );
14
15 /**
16 * Message cache
17 * Performs various MediaWiki namespace-related functions
18 *
19 */
20 class MessageCache {
21 var $mCache, $mUseCache, $mDisable, $mExpiry;
22 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
23 var $mExtensionMessages = array();
24 var $mInitialised = false;
25 var $mDeferred = true;
26 var $mAllMessagesLoaded;
27
28 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
29 wfProfileIn( __METHOD__ );
30
31 $this->mUseCache = !is_null( $memCached );
32 $this->mMemc = &$memCached;
33 $this->mDisable = !$useDB;
34 $this->mExpiry = $expiry;
35 $this->mDisableTransform = false;
36 $this->mMemcKey = $memcPrefix.':messages';
37 $this->mKeys = false; # initialised on demand
38 $this->mInitialised = true;
39 $this->mParser = null;
40
41 # When we first get asked for a message,
42 # then we'll fill up the cache. If we
43 # can return a cache hit, this saves
44 # some extra milliseconds
45 $this->mDeferred = true;
46
47 wfProfileOut( __METHOD__ );
48 }
49
50 function getParserOptions() {
51 if ( !$this->mParserOptions ) {
52 $this->mParserOptions = new ParserOptions;
53 }
54 return $this->mParserOptions;
55 }
56
57 /**
58 * Try to load the cache from a local file
59 */
60 function loadFromLocal( $hash ) {
61 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
62
63 if ( $wgLocalMessageCache === false ) {
64 return;
65 }
66
67 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
68
69 wfSuppressWarnings();
70 $file = fopen( $filename, 'r' );
71 wfRestoreWarnings();
72 if ( !$file ) {
73 return;
74 }
75
76 if ( $wgLocalMessageCacheSerialized ) {
77 // Check to see if the file has the hash specified
78 $localHash = fread( $file, 32 );
79 if ( $hash === $localHash ) {
80 // All good, get the rest of it
81 $serialized = '';
82 while ( !feof( $file ) ) {
83 $serialized .= fread( $file, 100000 );
84 }
85 $this->setCache( unserialize( $serialized ) );
86 }
87 fclose( $file );
88 } else {
89 $localHash=substr(fread($file,40),8);
90 fclose($file);
91 if ($hash!=$localHash) {
92 return;
93 }
94
95 require("$wgLocalMessageCache/messages-" . wfWikiID());
96 $this->setCache( $this->mCache);
97 }
98 }
99
100 /**
101 * Save the cache to a local file
102 */
103 function saveToLocal( $serialized, $hash ) {
104 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
105
106 if ( $wgLocalMessageCache === false ) {
107 return;
108 }
109
110 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
111 $oldUmask = umask( 0 );
112 wfMkdirParents( $wgLocalMessageCache, 0777 );
113 umask( $oldUmask );
114
115 $file = fopen( $filename, 'w' );
116 if ( !$file ) {
117 wfDebug( "Unable to open local cache file for writing\n" );
118 return;
119 }
120
121 fwrite( $file, $hash . $serialized );
122 fclose( $file );
123 @chmod( $filename, 0666 );
124 }
125
126 function loadFromScript( $hash ) {
127 wfDeprecated( __METHOD__ );
128 $this->loadFromLocal( $hash );
129 }
130
131 function saveToScript($array, $hash) {
132 global $wgLocalMessageCache;
133 if ( $wgLocalMessageCache === false ) {
134 return;
135 }
136
137 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
138 $oldUmask = umask( 0 );
139 wfMkdirParents( $wgLocalMessageCache, 0777 );
140 umask( $oldUmask );
141 $file = fopen( $filename.'.tmp', 'w');
142 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
143
144 foreach ($array as $key => $message) {
145 fwrite($file, "'". $this->escapeForScript($key).
146 "' => '" . $this->escapeForScript($message).
147 "',\n");
148 }
149 fwrite($file,");\n?>");
150 fclose($file);
151 rename($filename.'.tmp',$filename);
152 }
153
154 function escapeForScript($string) {
155 $string = str_replace( '\\', '\\\\', $string );
156 $string = str_replace( '\'', '\\\'', $string );
157 return $string;
158 }
159
160 /**
161 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
162 */
163 function setCache( $cache ) {
164 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
165 $this->mCache = $cache;
166 } else {
167 $this->mCache = false;
168 }
169 }
170
171 /**
172 * Loads messages either from memcached or the database, if not disabled
173 * On error, quietly switches to a fallback mode
174 * Returns false for a reportable error, true otherwise
175 */
176 function load() {
177 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
178
179 if ( $this->mDisable ) {
180 static $shownDisabled = false;
181 if ( !$shownDisabled ) {
182 wfDebug( "MessageCache::load(): disabled\n" );
183 $shownDisabled = true;
184 }
185 return true;
186 }
187 if ( !$this->mUseCache ) {
188 $this->mDeferred = false;
189 return true;
190 }
191
192 $fname = 'MessageCache::load';
193 wfProfileIn( $fname );
194 $success = true;
195
196 $this->mCache = false;
197
198 # Try local cache
199 if ( $wgLocalMessageCache !== false ) {
200 wfProfileIn( $fname.'-fromlocal' );
201 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
202 if ( $hash ) {
203 $this->loadFromLocal( $hash );
204 if ( $this->mCache ) {
205 wfDebug( "MessageCache::load(): got from local cache\n" );
206 }
207 }
208 wfProfileOut( $fname.'-fromlocal' );
209 }
210
211 # Try memcached
212 if ( !$this->mCache ) {
213 wfProfileIn( $fname.'-fromcache' );
214 $this->setCache( $this->mMemc->get( $this->mMemcKey ) );
215 if ( $this->mCache ) {
216 wfDebug( "MessageCache::load(): got from global cache\n" );
217 # Save to local cache
218 if ( $wgLocalMessageCache !== false ) {
219 $serialized = serialize( $this->mCache );
220 if ( !$hash ) {
221 $hash = md5( $serialized );
222 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
223 }
224 if ($wgLocalMessageCacheSerialized) {
225 $this->saveToLocal( $serialized,$hash );
226 } else {
227 $this->saveToScript( $this->mCache, $hash );
228 }
229 }
230 }
231 wfProfileOut( $fname.'-fromcache' );
232 }
233
234
235 # If there's nothing in memcached, load all the messages from the database
236 if ( !$this->mCache ) {
237 wfDebug( "MessageCache::load(): cache is empty\n" );
238 $this->lock();
239 # Other threads don't need to load the messages if another thread is doing it.
240 $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
241 if ( $success ) {
242 wfProfileIn( $fname.'-load' );
243 wfDebug( "MessageCache::load(): loading all messages from DB\n" );
244 $this->loadFromDB();
245 wfProfileOut( $fname.'-load' );
246
247 # Save in memcached
248 # Keep trying if it fails, this is kind of important
249 wfProfileIn( $fname.'-save' );
250 for ($i=0; $i<20 &&
251 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
252 $i++ ) {
253 usleep(mt_rand(500000,1500000));
254 }
255
256 # Save to local cache
257 if ( $wgLocalMessageCache !== false ) {
258 $serialized = serialize( $this->mCache );
259 $hash = md5( $serialized );
260 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
261 if ($wgLocalMessageCacheSerialized) {
262 $this->saveToLocal( $serialized,$hash );
263 } else {
264 $this->saveToScript( $this->mCache, $hash );
265 }
266 }
267
268 wfProfileOut( $fname.'-save' );
269 if ( $i == 20 ) {
270 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
271 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
272 } else {
273 $this->mMemc->delete( $this->mMemcKey.'-status' );
274 }
275 }
276 $this->unlock();
277 }
278
279 if ( !is_array( $this->mCache ) ) {
280 wfDebug( "MessageCache::load(): unable to load cache, disabled\n" );
281 $this->mDisable = true;
282 $this->mCache = false;
283 }
284 wfProfileOut( $fname );
285 $this->mDeferred = false;
286 return $success;
287 }
288
289 /**
290 * Loads all or main part of cacheable messages from the database
291 */
292 function loadFromDB() {
293 global $wgMaxMsgCacheEntrySize;
294
295 wfProfileIn( __METHOD__ );
296 $dbr = wfGetDB( DB_SLAVE );
297 $this->mCache = array();
298
299 # Load titles for all oversized pages in the MediaWiki namespace
300 $res = $dbr->select( 'page', 'page_title',
301 array(
302 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ),
303 'page_is_redirect' => 0,
304 'page_namespace' => NS_MEDIAWIKI,
305 ),
306 __METHOD__ );
307 while ( $row = $dbr->fetchObject( $res ) ) {
308 $this->mCache[$row->page_title] = '!TOO BIG';
309 }
310 $dbr->freeResult( $res );
311
312 # Load text for the remaining pages
313 $res = $dbr->select( array( 'page', 'revision', 'text' ),
314 array( 'page_title', 'old_text', 'old_flags' ),
315 array(
316 'page_is_redirect' => 0,
317 'page_namespace' => NS_MEDIAWIKI,
318 'page_latest=rev_id',
319 'rev_text_id=old_id',
320 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ),
321 __METHOD__ );
322
323 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
324 $this->mCache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
325 }
326 $this->mCache['VERSION'] = MSG_CACHE_VERSION;
327 $dbr->freeResult( $res );
328 wfProfileOut( __METHOD__ );
329 }
330
331 function replace( $title, $text ) {
332 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
333 global $wgMaxMsgCacheEntrySize;
334
335 wfProfileIn( __METHOD__ );
336 $this->lock();
337 $this->load();
338 if ( is_array( $this->mCache ) ) {
339 if ( $text === false ) {
340 # Article was deleted
341 unset( $this->mCache[$title] );
342 $this->mMemc->delete( "$this->mMemcKey:{$title}" );
343 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
344 $this->mCache[$title] = '!TOO BIG';
345 $this->mMemc->set( "$this->mMemcKey:{$title}", ' '.$text, $this->mExpiry );
346 } else {
347 $this->mCache[$title] = ' ' . $text;
348 $this->mMemc->delete( "$this->mMemcKey:{$title}" );
349 }
350 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
351
352 # Save to local cache
353 if ( $wgLocalMessageCache !== false ) {
354 $serialized = serialize( $this->mCache );
355 $hash = md5( $serialized );
356 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
357 if ($wgLocalMessageCacheSerialized) {
358 $this->saveToLocal( $serialized,$hash );
359 } else {
360 $this->saveToScript( $this->mCache, $hash );
361 }
362 }
363 }
364 $this->unlock();
365 $parserMemc->delete(wfMemcKey('sidebar'));
366 wfProfileOut( __METHOD__ );
367 }
368
369 /**
370 * Returns success
371 * Represents a write lock on the messages key
372 */
373 function lock() {
374 if ( !$this->mUseCache ) {
375 return true;
376 }
377
378 $lockKey = $this->mMemcKey . 'lock';
379 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
380 sleep(1);
381 }
382
383 return $i >= MSG_WAIT_TIMEOUT;
384 }
385
386 function unlock() {
387 if ( !$this->mUseCache ) {
388 return;
389 }
390
391 $lockKey = $this->mMemcKey . 'lock';
392 $this->mMemc->delete( $lockKey );
393 }
394
395 /**
396 * Get a message from either the content language or the user language.
397 *
398 * @param string $key The message cache key
399 * @param bool $useDB Get the message from the DB, false to use only the localisation
400 * @param bool $forContent Get the message from the content language rather than the
401 * user language
402 * @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
403 */
404 function get( $key, $useDB = true, $forContent = true, $isFullKey = false ) {
405 global $wgContLanguageCode, $wgContLang, $wgLang;
406 if( $forContent ) {
407 $lang =& $wgContLang;
408 } else {
409 $lang =& $wgLang;
410 }
411 $langcode = $lang->getCode();
412 # If uninitialised, someone is trying to call this halfway through Setup.php
413 if( !$this->mInitialised ) {
414 return '&lt;' . htmlspecialchars($key) . '&gt;';
415 }
416 # If cache initialization was deferred, start it now.
417 if( $this->mDeferred && !$this->mDisable && $useDB ) {
418 $this->load();
419 }
420
421 $message = false;
422
423 # Normalise title-case input
424 $lckey = $wgContLang->lcfirst( $key );
425 $lckey = str_replace( ' ', '_', $lckey );
426
427 # Try the MediaWiki namespace
428 if( !$this->mDisable && $useDB ) {
429 $title = $wgContLang->ucfirst( $lckey );
430 if(!$isFullKey && ($langcode != $wgContLanguageCode) ) {
431 $title .= '/' . $langcode;
432 }
433 $message = $this->getMsgFromNamespace( $title );
434 }
435 # Try the extension array
436 if( $message === false && isset( $this->mExtensionMessages[$langcode][$lckey] ) ) {
437 $message = $this->mExtensionMessages[$langcode][$lckey];
438 }
439 if ( $message === false && isset( $this->mExtensionMessages['en'][$lckey] ) ) {
440 $message = $this->mExtensionMessages['en'][$lckey];
441 }
442
443 # Try the array in the language object
444 if( $message === false ) {
445 #wfDebug( "Trying language object for message $key\n" );
446 wfSuppressWarnings();
447 $message = $lang->getMessage( $lckey );
448 wfRestoreWarnings();
449 if ( is_null( $message ) ) {
450 $message = false;
451 }
452 }
453
454 # Try the array of another language
455 $pos = strrpos( $lckey, '/' );
456 if( $message === false && $pos !== false) {
457 $mkey = substr( $lckey, 0, $pos );
458 $code = substr( $lckey, $pos+1 );
459 if ( $code ) {
460 $validCodes = array_keys( Language::getLanguageNames() );
461 if ( in_array( $code, $validCodes ) ) {
462 $message = Language::getMessageFor( $mkey, $code );
463 if ( is_null( $message ) ) {
464 $message = false;
465 }
466 } else {
467 wfDebug( __METHOD__ . ": Invalid code $code for $mkey/$code, not trying messages array\n" );
468 }
469 }
470 }
471
472 # Is this a custom message? Try the default language in the db...
473 if( ($message === false || $message === '-' ) &&
474 !$this->mDisable && $useDB &&
475 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
476 $message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $lckey ) );
477 }
478
479 # Final fallback
480 if( $message === false ) {
481 return '&lt;' . htmlspecialchars($key) . '&gt;';
482 }
483 return $message;
484 }
485
486 /**
487 * Get a message from the MediaWiki namespace, with caching. The key must
488 * first be converted to two-part lang/msg form if necessary.
489 *
490 * @param string $title Message cache key with initial uppercase letter
491 */
492 function getMsgFromNamespace( $title ) {
493 $message = false;
494 $type = false;
495
496 # Try the cache
497 if( $this->mUseCache && isset( $this->mCache[$title] ) ) {
498 $entry = $this->mCache[$title];
499 $type = substr( $entry, 0, 1 );
500 if ( $type == ' ' ) {
501 return substr( $entry, 1 );
502 }
503 }
504
505 # Call message hooks, in case they are defined
506 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
507 if ( $message !== false ) {
508 return $message;
509 }
510
511 # If there is no cache entry and no placeholder, it doesn't exist
512 if ( $type != '!' && $message === false ) {
513 return false;
514 }
515
516 $memcKey = $this->mMemcKey . ':' . $title;
517
518 # Try the individual message cache
519 if ( $this->mUseCache ) {
520 $entry = $this->mMemc->get( $memcKey );
521 if ( $entry ) {
522 $type = substr( $entry, 0, 1 );
523
524 if ( $type == ' ' ) {
525 $message = substr( $entry, 1 );
526 $this->mCache[$title] = $entry;
527 return $message;
528 } elseif ( $entry == '!NONEXISTENT' ) {
529 return false;
530 } else {
531 # Corrupt/obsolete entry, delete it
532 $this->mMemc->delete( $memcKey );
533 }
534
535 }
536 }
537
538 # Try loading it from the DB
539 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
540 if( $revision ) {
541 $message = $revision->getText();
542 if ($this->mUseCache) {
543 $this->mCache[$title] = ' ' . $message;
544 $this->mMemc->set( $memcKey, $message, $this->mExpiry );
545 }
546 } else {
547 # Negative caching
548 # Use some special text instead of false, because false gets converted to '' somewhere
549 $this->mMemc->set( $memcKey, '!NONEXISTENT', $this->mExpiry );
550 $this->mCache[$title] = false;
551 }
552
553 return $message;
554 }
555
556 function transform( $message, $interface = false ) {
557 global $wgParser;
558 if ( !$this->mParser && isset( $wgParser ) ) {
559 # Do some initialisation so that we don't have to do it twice
560 $wgParser->firstCallInit();
561 # Clone it and store it
562 $this->mParser = clone $wgParser;
563 }
564 if ( $this->mParser ) {
565 if( strpos( $message, '{{' ) !== false ) {
566 $popts = $this->getParserOptions();
567 $popts->setInterfaceMessage( $interface );
568 $message = $this->mParser->transformMsg( $message, $popts );
569 }
570 }
571 return $message;
572 }
573
574 function disable() { $this->mDisable = true; }
575 function enable() { $this->mDisable = false; }
576
577 /** @deprecated */
578 function disableTransform(){
579 wfDeprecated( __METHOD__ );
580 }
581 function enableTransform() {
582 wfDeprecated( __METHOD__ );
583 }
584 function setTransform( $x ) {
585 wfDeprecated( __METHOD__ );
586 }
587 function getTransform() {
588 wfDeprecated( __METHOD__ );
589 return false;
590 }
591
592 /**
593 * Add a message to the cache
594 *
595 * @param mixed $key
596 * @param mixed $value
597 * @param string $lang The messages language, English by default
598 */
599 function addMessage( $key, $value, $lang = 'en' ) {
600 $this->mExtensionMessages[$lang][$key] = $value;
601 }
602
603 /**
604 * Add an associative array of message to the cache
605 *
606 * @param array $messages An associative array of key => values to be added
607 * @param string $lang The messages language, English by default
608 */
609 function addMessages( $messages, $lang = 'en' ) {
610 wfProfileIn( __METHOD__ );
611 if ( !is_array( $messages ) ) {
612 throw new MWException( __METHOD__.': Invalid message array' );
613 }
614 if ( isset( $this->mExtensionMessages[$lang] ) ) {
615 $this->mExtensionMessages[$lang] = $messages + $this->mExtensionMessages[$lang];
616 } else {
617 $this->mExtensionMessages[$lang] = $messages;
618 }
619 wfProfileOut( __METHOD__ );
620 }
621
622 /**
623 * Add a 2-D array of messages by lang. Useful for extensions.
624 *
625 * @param array $messages The array to be added
626 */
627 function addMessagesByLang( $messages ) {
628 wfProfileIn( __METHOD__ );
629 foreach ( $messages as $key => $value ) {
630 $this->addMessages( $value, $key );
631 }
632 wfProfileOut( __METHOD__ );
633 }
634
635 /**
636 * Get the extension messages for a specific language. Only English, interface
637 * and content language are guaranteed to be loaded.
638 *
639 * @param string $lang The messages language, English by default
640 */
641 function getExtensionMessagesFor( $lang = 'en' ) {
642 wfProfileIn( __METHOD__ );
643 $messages = array();
644 if ( isset( $this->mExtensionMessages[$lang] ) ) {
645 $messages = $this->mExtensionMessages[$lang];
646 }
647 if ( $lang != 'en' ) {
648 $messages = $messages + $this->mExtensionMessages['en'];
649 }
650 wfProfileOut( __METHOD__ );
651 return $messages;
652 }
653
654 /**
655 * Clear all stored messages. Mainly used after a mass rebuild.
656 */
657 function clear() {
658 global $wgLocalMessageCache;
659 if( $this->mUseCache ) {
660 # Global cache
661 $this->mMemc->delete( $this->mMemcKey );
662 # Invalidate all local caches
663 $this->mMemc->delete( "{$this->mMemcKey}-hash" );
664 }
665 }
666
667 function loadAllMessages() {
668 global $wgExtensionMessagesFiles;
669 if ( $this->mAllMessagesLoaded ) {
670 return;
671 }
672 $this->mAllMessagesLoaded = true;
673
674 # Some extensions will load their messages when you load their class file
675 wfLoadAllExtensions();
676 # Others will respond to this hook
677 wfRunHooks( 'LoadAllMessages' );
678 # Some register their messages in $wgExtensionMessagesFiles
679 foreach ( $wgExtensionMessagesFiles as $name => $file ) {
680 if ( $file ) {
681 $this->loadMessagesFile( $file );
682 $wgExtensionMessagesFiles[$name] = false;
683 }
684 }
685 # Still others will respond to neither, they are EVIL. We sometimes need to know!
686 }
687
688 /**
689 * Load messages from a given file
690 */
691 function loadMessagesFile( $filename ) {
692 global $wgLang, $wgContLang;
693 $messages = $magicWords = false;
694 require( $filename );
695
696 /*
697 * Load only languages that are usually used, and merge all fallbacks,
698 * except English.
699 */
700 $langs = array_unique( array( 'en', $wgContLang->getCode(), $wgLang->getCode() ) );
701 foreach( $langs as $code ) {
702 $fbcode = $code;
703 $mergedMessages = array();
704 do {
705 if ( isset($messages[$fbcode]) ) {
706 $mergedMessages += $messages[$fbcode];
707 }
708 $fbcode = Language::getFallbackfor( $fbcode );
709 } while( $fbcode && $fbcode !== 'en' );
710
711 if ( !empty($mergedMessages) )
712 $this->addMessages( $mergedMessages, $code );
713 }
714
715 if ( $magicWords !== false ) {
716 global $wgContLang;
717 $wgContLang->addMagicWordsByLang( $magicWords );
718 }
719 }
720 }