Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / includes / MessageCache.php
blob2236bdd702624a6f01e48dbb430af9778245f348
1 <?php
2 /**
3 * @file
4 * @ingroup Cache
5 */
7 /**
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 );
15 /**
16 * Message cache
17 * Performs various MediaWiki namespace-related functions
18 * @ingroup Cache
20 class MessageCache {
21 // Holds loaded messages that are defined in MediaWiki namespace.
22 var $mCache;
24 var $mUseCache, $mDisable, $mExpiry;
25 var $mKeys, $mParserOptions, $mParser;
26 var $mExtensionMessages = array();
27 var $mInitialised = false;
28 var $mAllMessagesLoaded = array(); // Extension messages
30 // Variable for tracking which variables are loaded
31 var $mLoadedLanguages = array();
33 function __construct( &$memCached, $useDB, $expiry, /*ignored*/ $memcPrefix ) {
34 $this->mUseCache = !is_null( $memCached );
35 $this->mMemc = &$memCached;
36 $this->mDisable = !$useDB;
37 $this->mExpiry = $expiry;
38 $this->mDisableTransform = false;
39 $this->mKeys = false; # initialised on demand
40 $this->mInitialised = true;
41 $this->mParser = null;
45 /**
46 * ParserOptions is lazy initialised.
48 function getParserOptions() {
49 if ( !$this->mParserOptions ) {
50 $this->mParserOptions = new ParserOptions;
52 return $this->mParserOptions;
55 /**
56 * Try to load the cache from a local file.
57 * Actual format of the file depends on the $wgLocalMessageCacheSerialized
58 * setting.
60 * @param $hash String: the hash of contents, to check validity.
61 * @param $code Mixed: Optional language code, see documenation of load().
62 * @return false on failure.
64 function loadFromLocal( $hash, $code ) {
65 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
67 $filename = "$wgLocalMessageCache/messages-" . wfWikiID() . "-$code";
69 # Check file existence
70 wfSuppressWarnings();
71 $file = fopen( $filename, 'r' );
72 wfRestoreWarnings();
73 if ( !$file ) {
74 return false; // No cache file
77 if ( $wgLocalMessageCacheSerialized ) {
78 // Check to see if the file has the hash specified
79 $localHash = fread( $file, 32 );
80 if ( $hash === $localHash ) {
81 // All good, get the rest of it
82 $serialized = '';
83 while ( !feof( $file ) ) {
84 $serialized .= fread( $file, 100000 );
86 fclose( $file );
87 return $this->setCache( unserialize( $serialized ), $code );
88 } else {
89 fclose( $file );
90 return false; // Wrong hash
92 } else {
93 $localHash=substr(fread($file,40),8);
94 fclose($file);
95 if ($hash!=$localHash) {
96 return false; // Wrong hash
99 # Require overwrites the member variable or just shadows it?
100 require( $filename );
101 return $this->setCache( $this->mCache, $code );
106 * Save the cache to a local file.
108 function saveToLocal( $serialized, $hash, $code ) {
109 global $wgLocalMessageCache;
111 $filename = "$wgLocalMessageCache/messages-" . wfWikiID() . "-$code";
112 wfMkdirParents( $wgLocalMessageCache ); // might fail
114 wfSuppressWarnings();
115 $file = fopen( $filename, 'w' );
116 wfRestoreWarnings();
118 if ( !$file ) {
119 wfDebug( "Unable to open local cache file for writing\n" );
120 return;
123 fwrite( $file, $hash . $serialized );
124 fclose( $file );
125 @chmod( $filename, 0666 );
128 function saveToScript( $array, $hash, $code ) {
129 global $wgLocalMessageCache;
131 $filename = "$wgLocalMessageCache/messages-" . wfWikiID() . "-$code";
132 $tempFilename = $filename . '.tmp';
133 wfMkdirParents( $wgLocalMessageCache ); // might fail
135 wfSuppressWarnings();
136 $file = fopen( $tempFilename, 'w');
137 wfRestoreWarnings();
139 if ( !$file ) {
140 wfDebug( "Unable to open local cache file for writing\n" );
141 return;
144 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
146 foreach ($array as $key => $message) {
147 $key = $this->escapeForScript($key);
148 $messages = $this->escapeForScript($message);
149 fwrite($file, "'$key' => '$message',\n");
152 fwrite($file,");\n?>");
153 fclose($file);
154 rename($tempFilename, $filename);
157 function escapeForScript($string) {
158 $string = str_replace( '\\', '\\\\', $string );
159 $string = str_replace( '\'', '\\\'', $string );
160 return $string;
164 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
166 function setCache( $cache, $code ) {
167 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
168 $this->mCache[$code] = $cache;
169 return true;
170 } else {
171 return false;
176 * Loads messages from caches or from database in this order:
177 * (1) local message cache (if $wgLocalMessageCache is enabled)
178 * (2) memcached
179 * (3) from the database.
181 * When succesfully loading from (2) or (3), all higher level caches are
182 * updated for the newest version.
184 * Nothing is loaded if member variable mDisabled is true, either manually
185 * set by calling code or if message loading fails (is this possible?).
187 * Returns true if cache is already populated or it was succesfully populated,
188 * or false if populating empty cache fails. Also returns true if MessageCache
189 * is disabled.
191 * @param $code String: language to which load messages
193 function load( $code = false ) {
194 global $wgLocalMessageCache;
196 if ( !$this->mUseCache ) {
197 return true;
200 if( !is_string( $code ) ) {
201 # This isn't really nice, so at least make a note about it and try to
202 # fall back
203 wfDebug( __METHOD__ . " called without providing a language code\n" );
204 $code = 'en';
207 # Don't do double loading...
208 if ( isset($this->mLoadedLanguages[$code]) ) return true;
210 # 8 lines of code just to say (once) that message cache is disabled
211 if ( $this->mDisable ) {
212 static $shownDisabled = false;
213 if ( !$shownDisabled ) {
214 wfDebug( __METHOD__ . ": disabled\n" );
215 $shownDisabled = true;
217 return true;
220 # Loading code starts
221 wfProfileIn( __METHOD__ );
222 $success = false; # Keep track of success
223 $where = array(); # Debug info, delayed to avoid spamming debug log too much
224 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
227 # (1) local cache
228 # Hash of the contents is stored in memcache, to detect if local cache goes
229 # out of date (due to update in other thread?)
230 if ( $wgLocalMessageCache !== false ) {
231 wfProfileIn( __METHOD__ . '-fromlocal' );
233 $hash = $this->mMemc->get( wfMemcKey( 'messages', $code, 'hash' ) );
234 if ( $hash ) {
235 $success = $this->loadFromLocal( $hash, $code );
236 if ( $success ) $where[] = 'got from local cache';
238 wfProfileOut( __METHOD__ . '-fromlocal' );
241 # (2) memcache
242 # Fails if nothing in cache, or in the wrong version.
243 if ( !$success ) {
244 wfProfileIn( __METHOD__ . '-fromcache' );
245 $cache = $this->mMemc->get( $cacheKey );
246 $success = $this->setCache( $cache, $code );
247 if ( $success ) {
248 $where[] = 'got from global cache';
249 $this->saveToCaches( $cache, false, $code );
251 wfProfileOut( __METHOD__ . '-fromcache' );
255 # (3)
256 # Nothing in caches... so we need create one and store it in caches
257 if ( !$success ) {
258 $where[] = 'cache is empty';
259 $where[] = 'loading from database';
261 $this->lock($cacheKey);
263 # Limit the concurrency of loadFromDB to a single process
264 # This prevents the site from going down when the cache expires
265 $statusKey = wfMemcKey( 'messages', $code, 'status' );
266 $success = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
267 if ( $success ) {
268 $cache = $this->loadFromDB( $code );
269 $success = $this->setCache( $cache, $code );
271 if ( $success ) {
272 $success = $this->saveToCaches( $cache, true, $code );
273 if ( $success ) {
274 $this->mMemc->delete( $statusKey );
275 } else {
276 $this->mMemc->set( $statusKey, 'error', 60*5 );
277 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
280 $this->unlock($cacheKey);
283 if ( !$success ) {
284 # Bad luck... this should not happen
285 $where[] = 'loading FAILED - cache is disabled';
286 $info = implode( ', ', $where );
287 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
288 $this->mDisable = true;
289 $this->mCache = false;
290 } else {
291 # All good, just record the success
292 $info = implode( ', ', $where );
293 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
294 $this->mLoadedLanguages[$code] = true;
296 wfProfileOut( __METHOD__ );
297 return $success;
301 * Loads cacheable messages from the database. Messages bigger than
302 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
303 * on-demand from the database later.
305 * @param $code Optional language code, see documenation of load().
306 * @return Array: Loaded messages for storing in caches.
308 function loadFromDB( $code = false ) {
309 wfProfileIn( __METHOD__ );
310 global $wgMaxMsgCacheEntrySize, $wgContLanguageCode;
311 $dbr = wfGetDB( DB_SLAVE );
312 $cache = array();
314 # Common conditions
315 $conds = array(
316 'page_is_redirect' => 0,
317 'page_namespace' => NS_MEDIAWIKI,
320 if ( $code ) {
321 # Is this fast enough. Should not matter if the filtering is done in the
322 # database or in code.
323 if ( $code !== $wgContLanguageCode ) {
324 # Messages for particular language
325 $escapedCode = $dbr->escapeLike( $code );
326 $conds[] = "page_title like '%%/$escapedCode'";
327 } else {
328 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
329 # other than language code.
330 $conds[] = "page_title not like '%%/%%'";
334 # Conditions to fetch oversized pages to ignore them
335 $bigConds = $conds;
336 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
338 # Load titles for all oversized pages in the MediaWiki namespace
339 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ );
340 while ( $row = $dbr->fetchObject( $res ) ) {
341 $cache[$row->page_title] = '!TOO BIG';
343 $dbr->freeResult( $res );
345 # Conditions to load the remaining pages with their contents
346 $smallConds = $conds;
347 $smallConds[] = 'page_latest=rev_id';
348 $smallConds[] = 'rev_text_id=old_id';
349 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
351 $res = $dbr->select( array( 'page', 'revision', 'text' ),
352 array( 'page_title', 'old_text', 'old_flags' ),
353 $smallConds, __METHOD__ );
355 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
356 $cache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
358 $dbr->freeResult( $res );
360 $cache['VERSION'] = MSG_CACHE_VERSION;
361 wfProfileOut( __METHOD__ );
362 return $cache;
366 * Updates cache as necessary when message page is changed
368 * @param $title String: name of the page changed.
369 * @param $text Mixed: new contents of the page.
371 public function replace( $title, $text ) {
372 global $wgMaxMsgCacheEntrySize;
373 wfProfileIn( __METHOD__ );
376 list( , $code ) = $this->figureMessage( $title );
378 $cacheKey = wfMemcKey( 'messages', $code );
379 $this->load($code);
380 $this->lock($cacheKey);
382 if ( is_array($this->mCache[$code]) ) {
383 $titleKey = wfMemcKey( 'messages', 'individual', $title );
385 if ( $text === false ) {
386 # Article was deleted
387 unset( $this->mCache[$code][$title] );
388 $this->mMemc->delete( $titleKey );
390 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
391 # Check for size
392 $this->mCache[$code][$title] = '!TOO BIG';
393 $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
395 } else {
396 $this->mCache[$code][$title] = ' ' . $text;
397 $this->mMemc->delete( $titleKey );
400 # Update caches
401 $this->saveToCaches( $this->mCache[$code], true, $code );
403 $this->unlock($cacheKey);
405 // Also delete cached sidebar... just in case it is affected
406 global $parserMemc;
407 $sidebarKey = wfMemcKey( 'sidebar', $code );
408 $parserMemc->delete( $sidebarKey );
410 wfProfileOut( __METHOD__ );
414 * Shortcut to update caches.
416 * @param $cache Array: cached messages with a version.
417 * @param $cacheKey String: Identifier for the cache.
418 * @param $memc Bool: Wether to update or not memcache.
419 * @param $code String: Language code.
420 * @return False on somekind of error.
422 protected function saveToCaches( $cache, $memc = true, $code = false ) {
423 wfProfileIn( __METHOD__ );
424 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
426 $cacheKey = wfMemcKey( 'messages', $code );
428 $i = 0;
429 if ( $memc ) {
430 # Save in memcached
431 # Keep trying if it fails, this is kind of important
433 for ($i=0; $i<20 &&
434 !$this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
435 $i++ ) {
436 usleep(mt_rand(500000,1500000));
440 # Save to local cache
441 if ( $wgLocalMessageCache !== false ) {
442 $serialized = serialize( $cache );
443 $hash = md5( $serialized );
444 $this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry );
445 if ($wgLocalMessageCacheSerialized) {
446 $this->saveToLocal( $serialized, $hash, $code );
447 } else {
448 $this->saveToScript( $cache, $hash, $code );
452 if ( $i == 20 ) {
453 $success = false;
454 } else {
455 $success = true;
457 wfProfileOut( __METHOD__ );
458 return $success;
462 * Returns success
463 * Represents a write lock on the messages key
465 function lock($key) {
466 if ( !$this->mUseCache ) {
467 return true;
470 $lockKey = $key . ':lock';
471 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
472 sleep(1);
475 return $i >= MSG_WAIT_TIMEOUT;
478 function unlock($key) {
479 if ( !$this->mUseCache ) {
480 return;
483 $lockKey = $key . ':lock';
484 $this->mMemc->delete( $lockKey );
488 * Get a message from either the content language or the user language.
490 * @param string $key The message cache key
491 * @param bool $useDB Get the message from the DB, false to use only the localisation
492 * @param string $langcode Code of the language to get the message for, if
493 * it is a valid code create a language for that
494 * language, if it is a string but not a valid code
495 * then make a basic language object, if it is a
496 * false boolean then use the current users
497 * language (as a fallback for the old parameter
498 * functionality), or if it is a true boolean then
499 * use the wikis content language (also as a
500 * fallback).
501 * @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
503 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
504 global $wgContLanguageCode, $wgContLang;
506 $lang = wfGetLangObj( $langcode );
507 $langcode = $lang->getCode();
509 # If uninitialised, someone is trying to call this halfway through Setup.php
510 if( !$this->mInitialised ) {
511 return '&lt;' . htmlspecialchars($key) . '&gt;';
514 $message = false;
516 # Normalise title-case input
517 $lckey = $wgContLang->lcfirst( $key );
518 $lckey = str_replace( ' ', '_', $lckey );
520 # Try the MediaWiki namespace
521 if( !$this->mDisable && $useDB ) {
522 $title = $wgContLang->ucfirst( $lckey );
523 if(!$isFullKey && ($langcode != $wgContLanguageCode) ) {
524 $title .= '/' . $langcode;
526 $message = $this->getMsgFromNamespace( $title, $langcode );
529 # Try the extension array
530 if ( $message === false && isset( $this->mExtensionMessages[$langcode][$lckey] ) ) {
531 $message = $this->mExtensionMessages[$langcode][$lckey];
533 if ( $message === false && isset( $this->mExtensionMessages['en'][$lckey] ) ) {
534 $message = $this->mExtensionMessages['en'][$lckey];
537 # Try the array in the language object
538 if ( $message === false ) {
539 $message = $lang->getMessage( $lckey );
540 if ( is_null( $message ) ) {
541 $message = false;
545 # Try the array of another language
546 $pos = strrpos( $lckey, '/' );
547 if( $message === false && $pos !== false) {
548 $mkey = substr( $lckey, 0, $pos );
549 $code = substr( $lckey, $pos+1 );
550 if ( $code ) {
551 # We may get calls for things that are http-urls from sidebar
552 # Let's not load nonexistent languages for those
553 $validCodes = array_keys( Language::getLanguageNames() );
554 if ( in_array( $code, $validCodes ) ) {
555 $message = Language::getMessageFor( $mkey, $code );
556 if ( is_null( $message ) ) {
557 $message = false;
563 # Is this a custom message? Try the default language in the db...
564 if( ($message === false || $message === '-' ) &&
565 !$this->mDisable && $useDB &&
566 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
567 $message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $lckey ), $wgContLanguageCode );
570 # Final fallback
571 if( $message === false ) {
572 return '&lt;' . htmlspecialchars($key) . '&gt;';
574 return $message;
578 * Get a message from the MediaWiki namespace, with caching. The key must
579 * first be converted to two-part lang/msg form if necessary.
581 * @param $title String: Message cache key with initial uppercase letter.
582 * @param $code String: code denoting the language to try.
584 function getMsgFromNamespace( $title, $code ) {
585 $type = false;
586 $message = false;
588 if ( $this->mUseCache ) {
589 $this->load( $code );
590 if (isset( $this->mCache[$code][$title] ) ) {
591 $entry = $this->mCache[$code][$title];
592 $type = substr( $entry, 0, 1 );
593 if ( $type == ' ' ) {
594 return substr( $entry, 1 );
599 # Call message hooks, in case they are defined
600 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
601 if ( $message !== false ) {
602 return $message;
605 # If there is no cache entry and no placeholder, it doesn't exist
606 if ( $type !== '!' ) {
607 return false;
610 $titleKey = wfMemcKey( 'messages', 'individual', $title );
612 # Try the individual message cache
613 if ( $this->mUseCache ) {
614 $entry = $this->mMemc->get( $titleKey );
615 if ( $entry ) {
616 $type = substr( $entry, 0, 1 );
618 if ( $type === ' ' ) {
619 # Ok!
620 $message = substr( $entry, 1 );
621 $this->mCache[$code][$title] = $entry;
622 return $message;
623 } elseif ( $entry === '!NONEXISTENT' ) {
624 return false;
625 } else {
626 # Corrupt/obsolete entry, delete it
627 $this->mMemc->delete( $titleKey );
633 # Try loading it from the DB
634 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
635 if( $revision ) {
636 $message = $revision->getText();
637 if ($this->mUseCache) {
638 $this->mCache[$code][$title] = ' ' . $message;
639 $this->mMemc->set( $titleKey, $message, $this->mExpiry );
641 } else {
642 # Negative caching
643 # Use some special text instead of false, because false gets converted to '' somewhere
644 $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
645 $this->mCache[$code][$title] = false;
647 return $message;
650 function transform( $message, $interface = false, $language = null ) {
651 // Avoid creating parser if nothing to transfrom
652 if( strpos( $message, '{{' ) === false ) {
653 return $message;
656 global $wgParser, $wgParserConf;
657 if ( !$this->mParser && isset( $wgParser ) ) {
658 # Do some initialisation so that we don't have to do it twice
659 $wgParser->firstCallInit();
660 # Clone it and store it
661 $class = $wgParserConf['class'];
662 if ( $class == 'Parser_DiffTest' ) {
663 # Uncloneable
664 $this->mParser = new $class( $wgParserConf );
665 } else {
666 $this->mParser = clone $wgParser;
668 #wfDebug( __METHOD__ . ": following contents triggered transform: $message\n" );
670 if ( $this->mParser ) {
671 $popts = $this->getParserOptions();
672 $popts->setInterfaceMessage( $interface );
673 $popts->setTargetLanguage( $language );
674 $message = $this->mParser->transformMsg( $message, $popts );
676 return $message;
679 function disable() { $this->mDisable = true; }
680 function enable() { $this->mDisable = false; }
682 /** @deprecated */
683 function disableTransform(){
684 wfDeprecated( __METHOD__ );
686 function enableTransform() {
687 wfDeprecated( __METHOD__ );
689 function setTransform( $x ) {
690 wfDeprecated( __METHOD__ );
692 function getTransform() {
693 wfDeprecated( __METHOD__ );
694 return false;
698 * Add a message to the cache
700 * @param mixed $key
701 * @param mixed $value
702 * @param string $lang The messages language, English by default
704 function addMessage( $key, $value, $lang = 'en' ) {
705 global $wgContLang;
706 # Normalise title-case input
707 $lckey = str_replace( ' ', '_', $wgContLang->lcfirst( $key ) );
708 $this->mExtensionMessages[$lang][$lckey] = $value;
712 * Add an associative array of message to the cache
714 * @param array $messages An associative array of key => values to be added
715 * @param string $lang The messages language, English by default
717 function addMessages( $messages, $lang = 'en' ) {
718 wfProfileIn( __METHOD__ );
719 if ( !is_array( $messages ) ) {
720 throw new MWException( __METHOD__.': Invalid message array' );
722 if ( isset( $this->mExtensionMessages[$lang] ) ) {
723 $this->mExtensionMessages[$lang] = $messages + $this->mExtensionMessages[$lang];
724 } else {
725 $this->mExtensionMessages[$lang] = $messages;
727 wfProfileOut( __METHOD__ );
731 * Add a 2-D array of messages by lang. Useful for extensions.
733 * @param array $messages The array to be added
735 function addMessagesByLang( $messages ) {
736 wfProfileIn( __METHOD__ );
737 foreach ( $messages as $key => $value ) {
738 $this->addMessages( $value, $key );
740 wfProfileOut( __METHOD__ );
744 * Get the extension messages for a specific language. Only English, interface
745 * and content language are guaranteed to be loaded.
747 * @param string $lang The messages language, English by default
749 function getExtensionMessagesFor( $lang = 'en' ) {
750 wfProfileIn( __METHOD__ );
751 $messages = array();
752 if ( isset( $this->mExtensionMessages[$lang] ) ) {
753 $messages = $this->mExtensionMessages[$lang];
755 if ( $lang != 'en' ) {
756 $messages = $messages + $this->mExtensionMessages['en'];
758 wfProfileOut( __METHOD__ );
759 return $messages;
763 * Clear all stored messages. Mainly used after a mass rebuild.
765 function clear() {
766 if( $this->mUseCache ) {
767 $langs = Language::getLanguageNames( false );
768 foreach ( array_keys($langs) as $code ) {
769 # Global cache
770 $this->mMemc->delete( wfMemcKey( 'messages', $code ) );
771 # Invalidate all local caches
772 $this->mMemc->delete( wfMemcKey( 'messages', $code, 'hash' ) );
777 function loadAllMessages( $lang = false ) {
778 global $wgExtensionMessagesFiles;
779 $key = $lang === false ? '*' : $lang;
780 if ( isset( $this->mAllMessagesLoaded[$key] ) ) {
781 return;
783 $this->mAllMessagesLoaded[$key] = true;
785 # Some extensions will load their messages when you load their class file
786 wfLoadAllExtensions();
787 # Others will respond to this hook
788 wfRunHooks( 'LoadAllMessages' );
789 # Some register their messages in $wgExtensionMessagesFiles
790 foreach ( $wgExtensionMessagesFiles as $name => $file ) {
791 wfLoadExtensionMessages( $name, $lang );
793 # Still others will respond to neither, they are EVIL. We sometimes need to know!
797 * Load messages from a given file
799 * @param string $filename Filename of file to load.
800 * @param string $langcode Language to load messages for, or false for
801 * default behvaiour (en, content language and user
802 * language).
804 function loadMessagesFile( $filename, $langcode = false ) {
805 global $wgLang, $wgContLang;
806 wfProfileIn( __METHOD__ );
807 $messages = $magicWords = false;
808 require( $filename );
810 $validCodes = Language::getLanguageNames();
811 if( is_string( $langcode ) && array_key_exists( $langcode, $validCodes ) ) {
812 # Load messages for given language code.
813 $this->processMessagesArray( $messages, $langcode );
814 } elseif( is_string( $langcode ) && !array_key_exists( $langcode, $validCodes ) ) {
815 wfDebug( "Invalid language '$langcode' code passed to MessageCache::loadMessagesFile()" );
816 } else {
817 # Load only languages that are usually used, and merge all
818 # fallbacks, except English.
819 $langs = array_unique( array( 'en', $wgContLang->getCode(), $wgLang->getCode() ) );
820 foreach( $langs as $code ) {
821 $this->processMessagesArray( $messages, $code );
825 if ( $magicWords !== false ) {
826 global $wgContLang;
827 $wgContLang->addMagicWordsByLang( $magicWords );
829 wfProfileOut( __METHOD__ );
833 * Process an array of messages, loading it into the message cache.
835 * @param array $messages Messages array.
836 * @param string $langcode Language code to process.
838 function processMessagesArray( $messages, $langcode ) {
839 wfProfileIn( __METHOD__ );
840 $fallbackCode = $langcode;
841 $mergedMessages = array();
842 do {
843 if ( isset($messages[$fallbackCode]) ) {
844 $mergedMessages += $messages[$fallbackCode];
846 $fallbackCode = Language::getFallbackfor( $fallbackCode );
847 } while( $fallbackCode && $fallbackCode !== 'en' );
849 if ( !empty($mergedMessages) )
850 $this->addMessages( $mergedMessages, $langcode );
851 wfProfileOut( __METHOD__ );
854 public function figureMessage( $key ) {
855 global $wgContLanguageCode;
856 $pieces = explode( '/', $key );
857 if( count( $pieces ) < 2 )
858 return array( $key, $wgContLanguageCode );
860 $lang = array_pop( $pieces );
861 $validCodes = Language::getLanguageNames();
862 if( !array_key_exists( $lang, $validCodes ) )
863 return array( $key, $wgContLanguageCode );
865 $message = implode( '/', $pieces );
866 return array( $message, $lang );