10 define( 'MSG_LOAD_TIMEOUT', 60);
11 define( 'MSG_LOCK_TIMEOUT', 10);
12 define( 'MSG_WAIT_TIMEOUT', 10);
13 define( 'MSG_CACHE_VERSION', 1 );
17 * Performs various MediaWiki namespace-related functions
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;
28 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
29 wfProfileIn( __METHOD__
);
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;
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;
47 wfProfileOut( __METHOD__
);
50 function getParserOptions() {
51 if ( !$this->mParserOptions
) {
52 $this->mParserOptions
= new ParserOptions
;
54 return $this->mParserOptions
;
58 * Try to load the cache from a local file
60 function loadFromLocal( $hash ) {
61 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
63 if ( $wgLocalMessageCache === false ) {
67 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
70 $file = fopen( $filename, 'r' );
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
82 while ( !feof( $file ) ) {
83 $serialized .= fread( $file, 100000 );
85 $this->setCache( unserialize( $serialized ) );
89 $localHash=substr(fread($file,40),8);
91 if ($hash!=$localHash) {
95 require("$wgLocalMessageCache/messages-" . wfWikiID());
96 $this->setCache( $this->mCache
);
101 * Save the cache to a local file
103 function saveToLocal( $serialized, $hash ) {
104 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
106 if ( $wgLocalMessageCache === false ) {
110 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
111 $oldUmask = umask( 0 );
112 wfMkdirParents( $wgLocalMessageCache, 0777 );
115 $file = fopen( $filename, 'w' );
117 wfDebug( "Unable to open local cache file for writing\n" );
121 fwrite( $file, $hash . $serialized );
123 @chmod
( $filename, 0666 );
126 function loadFromScript( $hash ) {
127 wfDeprecated( __METHOD__
);
128 $this->loadFromLocal( $hash );
131 function saveToScript($array, $hash) {
132 global $wgLocalMessageCache;
133 if ( $wgLocalMessageCache === false ) {
137 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
138 $oldUmask = umask( 0 );
139 wfMkdirParents( $wgLocalMessageCache, 0777 );
141 $file = fopen( $filename.'.tmp', 'w');
142 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
144 foreach ($array as $key => $message) {
145 fwrite($file, "'". $this->escapeForScript($key).
146 "' => '" . $this->escapeForScript($message).
149 fwrite($file,");\n?>");
151 rename($filename.'.tmp',$filename);
154 function escapeForScript($string) {
155 $string = str_replace( '\\', '\\\\', $string );
156 $string = str_replace( '\'', '\\\'', $string );
161 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
163 function setCache( $cache ) {
164 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION
) {
165 $this->mCache
= $cache;
167 $this->mCache
= false;
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
177 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
179 if ( $this->mDisable
) {
180 static $shownDisabled = false;
181 if ( !$shownDisabled ) {
182 wfDebug( "MessageCache::load(): disabled\n" );
183 $shownDisabled = true;
187 if ( !$this->mUseCache
) {
188 $this->mDeferred
= false;
192 $fname = 'MessageCache::load';
193 wfProfileIn( $fname );
196 $this->mCache
= false;
199 if ( $wgLocalMessageCache !== false ) {
200 wfProfileIn( $fname.'-fromlocal' );
201 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
203 $this->loadFromLocal( $hash );
204 if ( $this->mCache
) {
205 wfDebug( "MessageCache::load(): got from local cache\n" );
208 wfProfileOut( $fname.'-fromlocal' );
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
);
221 $hash = md5( $serialized );
222 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
224 if ($wgLocalMessageCacheSerialized) {
225 $this->saveToLocal( $serialized,$hash );
227 $this->saveToScript( $this->mCache
, $hash );
231 wfProfileOut( $fname.'-fromcache' );
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" );
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
);
242 wfProfileIn( $fname.'-load' );
243 wfDebug( "MessageCache::load(): loading all messages from DB\n" );
245 wfProfileOut( $fname.'-load' );
248 # Keep trying if it fails, this is kind of important
249 wfProfileIn( $fname.'-save' );
251 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
253 usleep(mt_rand(500000,1500000));
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 );
264 $this->saveToScript( $this->mCache
, $hash );
268 wfProfileOut( $fname.'-save' );
270 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
271 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
273 $this->mMemc
->delete( $this->mMemcKey
.'-status' );
279 if ( !is_array( $this->mCache
) ) {
280 wfDebug( "MessageCache::load(): unable to load cache, disabled\n" );
281 $this->mDisable
= true;
282 $this->mCache
= false;
284 wfProfileOut( $fname );
285 $this->mDeferred
= false;
290 * Loads all or main part of cacheable messages from the database
292 function loadFromDB() {
293 global $wgMaxMsgCacheEntrySize;
295 wfProfileIn( __METHOD__
);
296 $dbr = wfGetDB( DB_SLAVE
);
297 $this->mCache
= array();
299 # Load titles for all oversized pages in the MediaWiki namespace
300 $res = $dbr->select( 'page', 'page_title',
302 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ),
303 'page_is_redirect' => 0,
304 'page_namespace' => NS_MEDIAWIKI
,
307 while ( $row = $dbr->fetchObject( $res ) ) {
308 $this->mCache
[$row->page_title
] = '!TOO BIG';
310 $dbr->freeResult( $res );
312 # Load text for the remaining pages
313 $res = $dbr->select( array( 'page', 'revision', 'text' ),
314 array( 'page_title', 'old_text', 'old_flags' ),
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 ) ),
323 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
324 $this->mCache
[$row->page_title
] = ' ' . Revision
::getRevisionText( $row );
326 $this->mCache
['VERSION'] = MSG_CACHE_VERSION
;
327 $dbr->freeResult( $res );
328 wfProfileOut( __METHOD__
);
331 function replace( $title, $text ) {
332 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
333 global $wgMaxMsgCacheEntrySize;
335 wfProfileIn( __METHOD__
);
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
);
347 $this->mCache
[$title] = ' ' . $text;
348 $this->mMemc
->delete( "$this->mMemcKey:{$title}" );
350 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
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 );
360 $this->saveToScript( $this->mCache
, $hash );
365 $parserMemc->delete(wfMemcKey('sidebar'));
366 wfRunHooks( 'wfMessageCacheReplace', array( $title, $text ) );
367 wfProfileOut( __METHOD__
);
372 * Represents a write lock on the messages key
375 if ( !$this->mUseCache
) {
379 $lockKey = $this->mMemcKey
. 'lock';
380 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
384 return $i >= MSG_WAIT_TIMEOUT
;
388 if ( !$this->mUseCache
) {
392 $lockKey = $this->mMemcKey
. 'lock';
393 $this->mMemc
->delete( $lockKey );
397 * Get a message from either the content language or the user language.
399 * @param string $key The message cache key
400 * @param bool $useDB Get the message from the DB, false to use only the localisation
401 * @param string $langcode Code of the language to get the message for, if
402 * it is a valid code create a language for that
403 * language, if it is a string but not a valid code
404 * then make a basic language object, if it is a
405 * false boolean then use the current users
406 * language (as a fallback for the old parameter
407 * functionality), or if it is a true boolean then
408 * use the wikis content language (also as a
410 * @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
412 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
413 global $wgContLanguageCode, $wgContLang, $wgLang;
415 # Identify which language to get or create a language object for.
416 if( $langcode === $wgContLang->getCode() ||
$langcode === true ) {
417 # $langcode is the language code of the wikis content language object.
418 # or it is a boolean and value is true
419 $lang =& $wgContLang;
420 } elseif( $langcode === $wgLang->getCode() ||
$langcode === false ) {
421 # $langcode is the language code of user language object.
422 # or it was a boolean and value is false
425 $validCodes = array_keys( Language
::getLanguageNames() );
426 if( in_array( $langcode, $validCodes ) ) {
427 # $langcode corresponds to a valid language.
428 $lang = Language
::factory( $langcode );
430 # $langcode is a string, but not a valid language code; use content language.
431 $lang =& $wgContLang;
432 wfDebug( 'Invalid language code passed to MessageCache::get, falling back to content language.' );
436 $langcode = $lang->getCode();
438 # If uninitialised, someone is trying to call this halfway through Setup.php
439 if( !$this->mInitialised
) {
440 return '<' . htmlspecialchars($key) . '>';
442 # If cache initialization was deferred, start it now.
443 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
449 # Normalise title-case input
450 $lckey = $wgContLang->lcfirst( $key );
451 $lckey = str_replace( ' ', '_', $lckey );
453 # Try the MediaWiki namespace
454 if( !$this->mDisable
&& $useDB ) {
455 $title = $wgContLang->ucfirst( $lckey );
456 if(!$isFullKey && ($langcode != $wgContLanguageCode) ) {
457 $title .= '/' . $langcode;
459 $message = $this->getMsgFromNamespace( $title );
461 # Try the extension array
462 if( $message === false && isset( $this->mExtensionMessages
[$langcode][$lckey] ) ) {
463 $message = $this->mExtensionMessages
[$langcode][$lckey];
465 if ( $message === false && isset( $this->mExtensionMessages
['en'][$lckey] ) ) {
466 $message = $this->mExtensionMessages
['en'][$lckey];
469 # Try the array in the language object
470 if( $message === false ) {
471 #wfDebug( "Trying language object for message $key\n" );
472 wfSuppressWarnings();
473 $message = $lang->getMessage( $lckey );
475 if ( is_null( $message ) ) {
480 # Try the array of another language
481 $pos = strrpos( $lckey, '/' );
482 if( $message === false && $pos !== false) {
483 $mkey = substr( $lckey, 0, $pos );
484 $code = substr( $lckey, $pos+
1 );
486 $validCodes = array_keys( Language
::getLanguageNames() );
487 if ( in_array( $code, $validCodes ) ) {
488 $message = Language
::getMessageFor( $mkey, $code );
489 if ( is_null( $message ) ) {
493 wfDebug( __METHOD__
. ": Invalid code $code for $mkey/$code, not trying messages array\n" );
498 # Is this a custom message? Try the default language in the db...
499 if( ($message === false ||
$message === '-' ) &&
500 !$this->mDisable
&& $useDB &&
501 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
502 $message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $lckey ) );
506 if( $message === false ) {
507 return '<' . htmlspecialchars($key) . '>';
513 * Get a message from the MediaWiki namespace, with caching. The key must
514 * first be converted to two-part lang/msg form if necessary.
516 * @param string $title Message cache key with initial uppercase letter
518 function getMsgFromNamespace( $title ) {
523 if( $this->mUseCache
&& isset( $this->mCache
[$title] ) ) {
524 $entry = $this->mCache
[$title];
525 $type = substr( $entry, 0, 1 );
526 if ( $type == ' ' ) {
527 return substr( $entry, 1 );
531 # Call message hooks, in case they are defined
532 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
533 if ( $message !== false ) {
537 # If there is no cache entry and no placeholder, it doesn't exist
538 if ( $type != '!' && $message === false ) {
542 $memcKey = $this->mMemcKey
. ':' . $title;
544 # Try the individual message cache
545 if ( $this->mUseCache
) {
546 $entry = $this->mMemc
->get( $memcKey );
548 $type = substr( $entry, 0, 1 );
550 if ( $type == ' ' ) {
551 $message = substr( $entry, 1 );
552 $this->mCache
[$title] = $entry;
554 } elseif ( $entry == '!NONEXISTENT' ) {
557 # Corrupt/obsolete entry, delete it
558 $this->mMemc
->delete( $memcKey );
564 # Try loading it from the DB
565 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
567 $message = $revision->getText();
568 if ($this->mUseCache
) {
569 $this->mCache
[$title] = ' ' . $message;
570 $this->mMemc
->set( $memcKey, $message, $this->mExpiry
);
574 # Use some special text instead of false, because false gets converted to '' somewhere
575 $this->mMemc
->set( $memcKey, '!NONEXISTENT', $this->mExpiry
);
576 $this->mCache
[$title] = false;
582 function transform( $message, $interface = false ) {
584 if ( !$this->mParser
&& isset( $wgParser ) ) {
585 # Do some initialisation so that we don't have to do it twice
586 $wgParser->firstCallInit();
587 # Clone it and store it
588 $this->mParser
= clone $wgParser;
590 if ( $this->mParser
) {
591 if( strpos( $message, '{{' ) !== false ) {
592 $popts = $this->getParserOptions();
593 $popts->setInterfaceMessage( $interface );
594 $message = $this->mParser
->transformMsg( $message, $popts );
600 function disable() { $this->mDisable
= true; }
601 function enable() { $this->mDisable
= false; }
604 function disableTransform(){
605 wfDeprecated( __METHOD__
);
607 function enableTransform() {
608 wfDeprecated( __METHOD__
);
610 function setTransform( $x ) {
611 wfDeprecated( __METHOD__
);
613 function getTransform() {
614 wfDeprecated( __METHOD__
);
619 * Add a message to the cache
622 * @param mixed $value
623 * @param string $lang The messages language, English by default
625 function addMessage( $key, $value, $lang = 'en' ) {
626 $this->mExtensionMessages
[$lang][$key] = $value;
630 * Add an associative array of message to the cache
632 * @param array $messages An associative array of key => values to be added
633 * @param string $lang The messages language, English by default
635 function addMessages( $messages, $lang = 'en' ) {
636 wfProfileIn( __METHOD__
);
637 if ( !is_array( $messages ) ) {
638 throw new MWException( __METHOD__
.': Invalid message array' );
640 if ( isset( $this->mExtensionMessages
[$lang] ) ) {
641 $this->mExtensionMessages
[$lang] = $messages +
$this->mExtensionMessages
[$lang];
643 $this->mExtensionMessages
[$lang] = $messages;
645 wfProfileOut( __METHOD__
);
649 * Add a 2-D array of messages by lang. Useful for extensions.
651 * @param array $messages The array to be added
653 function addMessagesByLang( $messages ) {
654 wfProfileIn( __METHOD__
);
655 foreach ( $messages as $key => $value ) {
656 $this->addMessages( $value, $key );
658 wfProfileOut( __METHOD__
);
662 * Get the extension messages for a specific language. Only English, interface
663 * and content language are guaranteed to be loaded.
665 * @param string $lang The messages language, English by default
667 function getExtensionMessagesFor( $lang = 'en' ) {
668 wfProfileIn( __METHOD__
);
670 if ( isset( $this->mExtensionMessages
[$lang] ) ) {
671 $messages = $this->mExtensionMessages
[$lang];
673 if ( $lang != 'en' ) {
674 $messages = $messages +
$this->mExtensionMessages
['en'];
676 wfProfileOut( __METHOD__
);
681 * Clear all stored messages. Mainly used after a mass rebuild.
684 global $wgLocalMessageCache;
685 if( $this->mUseCache
) {
687 $this->mMemc
->delete( $this->mMemcKey
);
688 # Invalidate all local caches
689 $this->mMemc
->delete( "{$this->mMemcKey}-hash" );
693 function loadAllMessages() {
694 global $wgExtensionMessagesFiles;
695 if ( $this->mAllMessagesLoaded
) {
698 $this->mAllMessagesLoaded
= true;
700 # Some extensions will load their messages when you load their class file
701 wfLoadAllExtensions();
702 # Others will respond to this hook
703 wfRunHooks( 'LoadAllMessages' );
704 # Some register their messages in $wgExtensionMessagesFiles
705 foreach ( $wgExtensionMessagesFiles as $name => $file ) {
706 wfLoadExtensionMessages( $name );
708 # Still others will respond to neither, they are EVIL. We sometimes need to know!
712 * Load messages from a given file
714 * @param string $filename Filename of file to load.
715 * @param string $langcode Language to load messages for, or false for
716 * default behvaiour (en, content language and user
719 function loadMessagesFile( $filename, $langcode = false ) {
720 global $wgLang, $wgContLang;
721 $messages = $magicWords = false;
722 require( $filename );
724 $validCodes = Language
::getLanguageNames();
725 if( is_string( $langcode ) && array_key_exists( $langcode, $validCodes ) ) {
726 # Load messages for given language code.
727 $this->processMessagesArray( $messages, $langcode );
728 } elseif( is_string( $langcode ) && !array_key_exists( $langcode, $validCodes ) ) {
729 wfDebug( "Invalid language '$langcode' code passed to MessageCache::loadMessagesFile()" );
731 # Load only languages that are usually used, and merge all
732 # fallbacks, except English.
733 $langs = array_unique( array( 'en', $wgContLang->getCode(), $wgLang->getCode() ) );
734 foreach( $langs as $code ) {
735 $this->processMessagesArray( $messages, $code );
739 if ( $magicWords !== false ) {
741 $wgContLang->addMagicWordsByLang( $magicWords );
746 * Process an array of messages, loading it into the message cache.
748 * @param array $messages Messages array.
749 * @param string $langcode Language code to process.
751 function processMessagesArray( $messages, $langcode ) {
752 $fallbackCode = $langcode;
753 $mergedMessages = array();
755 if ( isset($messages[$fallbackCode]) ) {
756 $mergedMessages +
= $messages[$fallbackCode];
758 $fallbackCode = Language
::getFallbackfor( $fallbackCode );
759 } while( $fallbackCode && $fallbackCode !== 'en' );
761 if ( !empty($mergedMessages) )
762 $this->addMessages( $mergedMessages, $langcode );