6cd167ca4bec14e1fef66b231e0d4f9303bdd6bf
3 * Localisation messages cache.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
27 define( 'MSG_LOAD_TIMEOUT', 60 );
28 define( 'MSG_LOCK_TIMEOUT', 10 );
29 define( 'MSG_WAIT_TIMEOUT', 10 );
30 define( 'MSG_CACHE_VERSION', 1 );
34 * Performs various MediaWiki namespace-related functions
39 * Process local cache of loaded messages that are defined in
40 * MediaWiki namespace. First array level is a language code,
41 * second level is message key and the values are either message
42 * content prefixed with space, or !NONEXISTENT for negative
47 // Should mean that database cannot be used, but check
50 /// Lifetime for cache, used by object caching
54 * Message cache has it's own parser which it uses to transform
57 protected $mParserOptions, $mParser;
59 /// Variable for tracking which variables are already loaded
60 protected $mLoadedLanguages = array();
67 private static $instance;
72 protected $mInParser = false;
75 * Get the signleton instance of this class
78 * @return MessageCache object
80 public static function singleton() {
81 if ( is_null( self
::$instance ) ) {
82 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
83 self
::$instance = new self( wfGetMessageCacheStorage(), $wgUseDatabaseMessages, $wgMsgCacheExpiry );
85 return self
::$instance;
89 * Destroy the singleton instance
93 public static function destroyInstance() {
94 self
::$instance = null;
97 function __construct( $memCached, $useDB, $expiry ) {
99 $memCached = wfGetCache( CACHE_NONE
);
102 $this->mMemc
= $memCached;
103 $this->mDisable
= !$useDB;
104 $this->mExpiry
= $expiry;
108 * ParserOptions is lazy initialised.
110 * @return ParserOptions
112 function getParserOptions() {
113 if ( !$this->mParserOptions
) {
114 $this->mParserOptions
= new ParserOptions
;
115 $this->mParserOptions
->setEditSection( false );
117 return $this->mParserOptions
;
121 * Try to load the cache from a local file.
122 * Actual format of the file depends on the $wgLocalMessageCacheSerialized
125 * @param string $hash the hash of contents, to check validity.
126 * @param $code Mixed: Optional language code, see documenation of load().
127 * @return bool on failure.
129 function loadFromLocal( $hash, $code ) {
130 global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
132 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
134 # Check file existence
135 wfSuppressWarnings();
136 $file = fopen( $filename, 'r' );
139 return false; // No cache file
142 if ( $wgLocalMessageCacheSerialized ) {
143 // Check to see if the file has the hash specified
144 $localHash = fread( $file, 32 );
145 if ( $hash === $localHash ) {
146 // All good, get the rest of it
148 while ( !feof( $file ) ) {
149 $serialized .= fread( $file, 100000 );
152 return $this->setCache( unserialize( $serialized ), $code );
155 return false; // Wrong hash
158 $localHash = substr( fread( $file, 40 ), 8 );
160 if ( $hash != $localHash ) {
161 return false; // Wrong hash
164 # Require overwrites the member variable or just shadows it?
165 require( $filename );
166 return $this->setCache( $this->mCache
, $code );
171 * Save the cache to a local file.
173 function saveToLocal( $serialized, $hash, $code ) {
174 global $wgCacheDirectory;
176 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
177 wfMkdirParents( $wgCacheDirectory, null, __METHOD__
); // might fail
179 wfSuppressWarnings();
180 $file = fopen( $filename, 'w' );
184 wfDebug( "Unable to open local cache file for writing\n" );
188 fwrite( $file, $hash . $serialized );
190 wfSuppressWarnings();
191 chmod( $filename, 0666 );
195 function saveToScript( $array, $hash, $code ) {
196 global $wgCacheDirectory;
198 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
199 $tempFilename = $filename . '.tmp';
200 wfMkdirParents( $wgCacheDirectory, null, __METHOD__
); // might fail
202 wfSuppressWarnings();
203 $file = fopen( $tempFilename, 'w' );
207 wfDebug( "Unable to open local cache file for writing\n" );
211 fwrite( $file, "<?php\n//$hash\n\n \$this->mCache = array(" );
213 foreach ( $array as $key => $message ) {
214 $key = $this->escapeForScript( $key );
215 $message = $this->escapeForScript( $message );
216 fwrite( $file, "'$key' => '$message',\n" );
219 fwrite( $file, ");\n?>" );
221 rename( $tempFilename, $filename );
224 function escapeForScript( $string ) {
225 $string = str_replace( '\\', '\\\\', $string );
226 $string = str_replace( '\'', '\\\'', $string );
231 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
235 function setCache( $cache, $code ) {
236 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION
) {
237 $this->mCache
[$code] = $cache;
245 * Loads messages from caches or from database in this order:
246 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
248 * (3) from the database.
250 * When succesfully loading from (2) or (3), all higher level caches are
251 * updated for the newest version.
253 * Nothing is loaded if member variable mDisable is true, either manually
254 * set by calling code or if message loading fails (is this possible?).
256 * Returns true if cache is already populated or it was succesfully populated,
257 * or false if populating empty cache fails. Also returns true if MessageCache
260 * @param bool|String $code String: language to which load messages
261 * @throws MWException
264 function load( $code = false ) {
265 global $wgUseLocalMessageCache;
267 $exception = null; // deferred error
269 if( !is_string( $code ) ) {
270 # This isn't really nice, so at least make a note about it and try to
272 wfDebug( __METHOD__
. " called without providing a language code\n" );
276 # Don't do double loading...
277 if ( isset( $this->mLoadedLanguages
[$code] ) ) {
281 # 8 lines of code just to say (once) that message cache is disabled
282 if ( $this->mDisable
) {
283 static $shownDisabled = false;
284 if ( !$shownDisabled ) {
285 wfDebug( __METHOD__
. ": disabled\n" );
286 $shownDisabled = true;
291 # Loading code starts
292 wfProfileIn( __METHOD__
);
293 $success = false; # Keep track of success
294 $where = array(); # Debug info, delayed to avoid spamming debug log too much
295 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
298 # Hash of the contents is stored in memcache, to detect if local cache goes
299 # out of date (due to update in other thread?)
300 if ( $wgUseLocalMessageCache ) {
301 wfProfileIn( __METHOD__
. '-fromlocal' );
303 $hash = $this->mMemc
->get( wfMemcKey( 'messages', $code, 'hash' ) );
305 $success = $this->loadFromLocal( $hash, $code );
306 if ( $success ) $where[] = 'got from local cache';
308 wfProfileOut( __METHOD__
. '-fromlocal' );
312 # Fails if nothing in cache, or in the wrong version.
314 wfProfileIn( __METHOD__
. '-fromcache' );
315 $cache = $this->mMemc
->get( $cacheKey );
316 $success = $this->setCache( $cache, $code );
318 $where[] = 'got from global cache';
319 $this->saveToCaches( $cache, false, $code );
321 wfProfileOut( __METHOD__
. '-fromcache' );
325 # Nothing in caches... so we need create one and store it in caches
327 $where[] = 'cache is empty';
328 $where[] = 'loading from database';
330 if ( $this->lock( $cacheKey ) ) {
332 $osc = new ScopedCallback( function() use ( $that, $cacheKey ) {
333 $that->unlock( $cacheKey );
336 # Limit the concurrency of loadFromDB to a single process
337 # This prevents the site from going down when the cache expires
338 $statusKey = wfMemcKey( 'messages', $code, 'status' );
339 $success = $this->mMemc
->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT
);
340 if ( $success ) { // acquired lock
341 $cache = $this->mMemc
;
342 $isc = new ScopedCallback( function() use ( $cache, $statusKey ) {
343 $cache->delete( $statusKey );
345 $cache = $this->loadFromDB( $code );
346 $success = $this->setCache( $cache, $code );
347 if ( $success ) { // messages loaded
348 $success = $this->saveToCaches( $cache, true, $code );
349 $isc = null; // unlock
351 $this->mMemc
->set( $statusKey, 'error', 60 * 5 );
352 wfDebug( __METHOD__
. ": set() error: restart memcached server!\n" );
353 $exception = new MWException( "Could not save cache for '$code'." );
356 $isc = null; // unlock
357 $exception = new MWException( "Could not load cache from DB for '$code'." );
360 $exception = new MWException( "Could not acquire '$statusKey' lock." );
362 $osc = null; // unlock
366 $this->mDisable
= true;
367 $this->mCache
= false;
368 // This used to go on, but that led to lots of nasty side
369 // effects like gadgets and sidebar getting cached with their
371 if ( $exception instanceof Exception
) {
372 wfProfileOut( __METHOD__
);
375 wfProfileOut( __METHOD__
);
376 throw new MWException( "MessageCache failed to load messages" );
379 # All good, just record the success
380 $info = implode( ', ', $where );
381 wfDebug( __METHOD__
. ": Loading $code... $info\n" );
382 $this->mLoadedLanguages
[$code] = true;
384 wfProfileOut( __METHOD__
);
389 * Loads cacheable messages from the database. Messages bigger than
390 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
391 * on-demand from the database later.
393 * @param string $code language code.
394 * @return Array: loaded messages for storing in caches.
396 function loadFromDB( $code ) {
397 wfProfileIn( __METHOD__
);
398 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
399 $dbr = wfGetDB( DB_SLAVE
);
404 'page_is_redirect' => 0,
405 'page_namespace' => NS_MEDIAWIKI
,
409 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
410 if ( !isset( $this->mCache
[$wgLanguageCode] ) ) {
411 $this->load( $wgLanguageCode );
413 $mostused = array_keys( $this->mCache
[$wgLanguageCode] );
414 foreach ( $mostused as $key => $value ) {
415 $mostused[$key] = "$value/$code";
419 if ( count( $mostused ) ) {
420 $conds['page_title'] = $mostused;
421 } elseif ( $code !== $wgLanguageCode ) {
422 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
424 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
425 # other than language code.
426 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
429 # Conditions to fetch oversized pages to ignore them
431 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
433 # Load titles for all oversized pages in the MediaWiki namespace
434 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__
. "($code)-big" );
435 foreach ( $res as $row ) {
436 $cache[$row->page_title
] = '!TOO BIG';
439 # Conditions to load the remaining pages with their contents
440 $smallConds = $conds;
441 $smallConds[] = 'page_latest=rev_id';
442 $smallConds[] = 'rev_text_id=old_id';
443 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
446 array( 'page', 'revision', 'text' ),
447 array( 'page_title', 'old_text', 'old_flags' ),
449 __METHOD__
. "($code)-small"
452 foreach ( $res as $row ) {
453 $text = Revision
::getRevisionText( $row );
454 if( $text === false ) {
455 // Failed to fetch data; possible ES errors?
456 // Store a marker to fetch on-demand as a workaround...
458 wfDebugLog( 'MessageCache', __METHOD__
. ": failed to load message page text for {$row->page_title} ($code)" );
460 $entry = ' ' . $text;
462 $cache[$row->page_title
] = $entry;
465 $cache['VERSION'] = MSG_CACHE_VERSION
;
466 wfProfileOut( __METHOD__
);
471 * Updates cache as necessary when message page is changed
473 * @param string $title name of the page changed.
474 * @param $text Mixed: new contents of the page.
476 public function replace( $title, $text ) {
477 global $wgMaxMsgCacheEntrySize;
478 wfProfileIn( __METHOD__
);
480 if ( $this->mDisable
) {
481 wfProfileOut( __METHOD__
);
485 list( $msg, $code ) = $this->figureMessage( $title );
487 $cacheKey = wfMemcKey( 'messages', $code );
488 $this->load( $code );
489 $this->lock( $cacheKey );
491 $titleKey = wfMemcKey( 'messages', 'individual', $title );
493 if ( $text === false ) {
494 # Article was deleted
495 $this->mCache
[$code][$title] = '!NONEXISTENT';
496 $this->mMemc
->delete( $titleKey );
497 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
499 $this->mCache
[$code][$title] = '!TOO BIG';
500 $this->mMemc
->set( $titleKey, ' ' . $text, $this->mExpiry
);
502 $this->mCache
[$code][$title] = ' ' . $text;
503 $this->mMemc
->delete( $titleKey );
507 $this->saveToCaches( $this->mCache
[$code], true, $code );
508 $this->unlock( $cacheKey );
510 // Also delete cached sidebar... just in case it is affected
511 $codes = array( $code );
512 if ( $code === 'en' ) {
513 // Delete all sidebars, like for example on action=purge on the
515 $codes = array_keys( Language
::fetchLanguageNames() );
519 foreach ( $codes as $code ) {
520 $sidebarKey = wfMemcKey( 'sidebar', $code );
521 $wgMemc->delete( $sidebarKey );
524 // Update the message in the message blob store
526 MessageBlobStore
::updateMessage( $wgContLang->lcfirst( $msg ) );
528 wfRunHooks( 'MessageCacheReplace', array( $title, $text ) );
530 wfProfileOut( __METHOD__
);
534 * Shortcut to update caches.
536 * @param array $cache cached messages with a version.
537 * @param bool $memc Wether to update or not memcache.
538 * @param string $code Language code.
539 * @return bool on somekind of error.
541 protected function saveToCaches( $cache, $memc = true, $code = false ) {
542 wfProfileIn( __METHOD__
);
543 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
545 $cacheKey = wfMemcKey( 'messages', $code );
548 $success = $this->mMemc
->set( $cacheKey, $cache, $this->mExpiry
);
553 # Save to local cache
554 if ( $wgUseLocalMessageCache ) {
555 $serialized = serialize( $cache );
556 $hash = md5( $serialized );
557 $this->mMemc
->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry
);
558 if ( $wgLocalMessageCacheSerialized ) {
559 $this->saveToLocal( $serialized, $hash, $code );
561 $this->saveToScript( $cache, $hash, $code );
565 wfProfileOut( __METHOD__
);
570 * Represents a write lock on the messages key
574 * @return Boolean: success
576 function lock( $key ) {
577 $lockKey = $key . ':lock';
578 for ( $i = 0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
582 return $i >= MSG_WAIT_TIMEOUT
;
585 function unlock( $key ) {
586 $lockKey = $key . ':lock';
587 $this->mMemc
->delete( $lockKey );
591 * Get a message from either the content language or the user language.
593 * @param $key String: the message cache key
594 * @param $useDB Boolean: get the message from the DB, false to use only
596 * @param bool|string $langcode Code of the language to get the message for, if
597 * it is a valid code create a language for that language,
598 * if it is a string but not a valid code then make a basic
599 * language object, if it is a false boolean then use the
600 * current users language (as a fallback for the old
601 * parameter functionality), or if it is a true boolean
602 * then use the wikis content language (also as a
604 * @param $isFullKey Boolean: specifies whether $key is a two part key
607 * @throws MWException
608 * @return string|bool
610 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
611 global $wgLanguageCode, $wgContLang;
613 if ( is_int( $key ) ) {
614 // "Non-string key given" exception sometimes happens for numerical strings that become ints somewhere on their way here
615 $key = strval( $key );
618 if ( !is_string( $key ) ) {
619 throw new MWException( 'Non-string key given' );
622 if ( strval( $key ) === '' ) {
623 # Shortcut: the empty key is always missing
627 $lang = wfGetLangObj( $langcode );
629 throw new MWException( "Bad lang code $langcode given" );
632 $langcode = $lang->getCode();
636 # Normalise title-case input (with some inlining)
637 $lckey = str_replace( ' ', '_', $key );
638 if ( ord( $key ) < 128 ) {
639 $lckey[0] = strtolower( $lckey[0] );
640 $uckey = ucfirst( $lckey );
642 $lckey = $wgContLang->lcfirst( $lckey );
643 $uckey = $wgContLang->ucfirst( $lckey );
646 # Try the MediaWiki namespace
647 if( !$this->mDisable
&& $useDB ) {
649 if( !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
650 $title .= '/' . $langcode;
652 $message = $this->getMsgFromNamespace( $title, $langcode );
655 # Try the array in the language object
656 if ( $message === false ) {
657 $message = $lang->getMessage( $lckey );
658 if ( is_null( $message ) ) {
663 # Try the array of another language
664 if( $message === false ) {
665 $parts = explode( '/', $lckey );
666 # We may get calls for things that are http-urls from sidebar
667 # Let's not load nonexistent languages for those
668 # They usually have more than one slash.
669 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
670 $message = Language
::getMessageFor( $parts[0], $parts[1] );
671 if ( is_null( $message ) ) {
677 # Is this a custom message? Try the default language in the db...
678 if( ( $message === false ||
$message === '-' ) &&
679 !$this->mDisable
&& $useDB &&
680 !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
681 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
685 if( $message === false ) {
690 $message = strtr( $message,
692 # Fix for trailing whitespace, removed by textarea
694 # Fix for NBSP, converted to space by firefox
695 ' ' => "\xc2\xa0",
696 ' ' => "\xc2\xa0",
703 * Get a message from the MediaWiki namespace, with caching. The key must
704 * first be converted to two-part lang/msg form if necessary.
706 * Unlike self::get(), this function doesn't resolve fallback chains, and
707 * some callers require this behavior. LanguageConverter::parseCachedTable()
708 * and self::get() are some examples in core.
710 * @param string $title Message cache key with initial uppercase letter.
711 * @param string $code code denoting the language to try.
713 * @return string|bool False on failure
715 function getMsgFromNamespace( $title, $code ) {
716 $this->load( $code );
717 if ( isset( $this->mCache
[$code][$title] ) ) {
718 $entry = $this->mCache
[$code][$title];
719 if ( substr( $entry, 0, 1 ) === ' ' ) {
720 return substr( $entry, 1 );
721 } elseif ( $entry === '!NONEXISTENT' ) {
723 } elseif( $entry === '!TOO BIG' ) {
724 // Fall through and try invididual message cache below
727 // XXX: This is not cached in process cache, should it?
729 wfRunHooks( 'MessagesPreLoad', array( $title, &$message ) );
730 if ( $message !== false ) {
737 # Try the individual message cache
738 $titleKey = wfMemcKey( 'messages', 'individual', $title );
739 $entry = $this->mMemc
->get( $titleKey );
741 if ( substr( $entry, 0, 1 ) === ' ' ) {
742 $this->mCache
[$code][$title] = $entry;
743 return substr( $entry, 1 );
744 } elseif ( $entry === '!NONEXISTENT' ) {
745 $this->mCache
[$code][$title] = '!NONEXISTENT';
748 # Corrupt/obsolete entry, delete it
749 $this->mMemc
->delete( $titleKey );
753 # Try loading it from the database
754 $revision = Revision
::newFromTitle(
755 Title
::makeTitle( NS_MEDIAWIKI
, $title ), false, Revision
::READ_LATEST
758 $content = $revision->getContent();
760 // A possibly temporary loading failure.
761 wfDebugLog( 'MessageCache', __METHOD__
. ": failed to load message page text for {$title} ($code)" );
762 $message = null; // no negative caching
764 // XXX: Is this the right way to turn a Content object into a message?
765 // NOTE: $content is typically either WikitextContent, JavaScriptContent or CssContent.
766 // MessageContent is *not* used for storing messages, it's only used for wrapping them when needed.
767 $message = $content->getWikitextForTransclusion();
769 if ( $message === false ||
$message === null ) {
770 wfDebugLog( 'MessageCache', __METHOD__
. ": message content doesn't provide wikitext "
771 . "(content model: " . $content->getContentHandler() . ")" );
773 $message = false; // negative caching
775 $this->mCache
[$code][$title] = ' ' . $message;
776 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
780 $message = false; // negative caching
783 if ( $message === false ) { // negative caching
784 $this->mCache
[$code][$title] = '!NONEXISTENT';
785 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
792 * @param $message string
793 * @param $interface bool
795 * @param $title Title
798 function transform( $message, $interface = false, $language = null, $title = null ) {
799 // Avoid creating parser if nothing to transform
800 if( strpos( $message, '{{' ) === false ) {
804 if ( $this->mInParser
) {
808 $parser = $this->getParser();
810 $popts = $this->getParserOptions();
811 $popts->setInterfaceMessage( $interface );
812 $popts->setTargetLanguage( $language );
814 $userlang = $popts->setUserLang( $language );
815 $this->mInParser
= true;
816 $message = $parser->transformMsg( $message, $popts, $title );
817 $this->mInParser
= false;
818 $popts->setUserLang( $userlang );
826 function getParser() {
827 global $wgParser, $wgParserConf;
828 if ( !$this->mParser
&& isset( $wgParser ) ) {
829 # Do some initialisation so that we don't have to do it twice
830 $wgParser->firstCallInit();
831 # Clone it and store it
832 $class = $wgParserConf['class'];
833 if ( $class == 'Parser_DiffTest' ) {
835 $this->mParser
= new $class( $wgParserConf );
837 $this->mParser
= clone $wgParser;
840 return $this->mParser
;
844 * @param $text string
845 * @param $title Title
846 * @param $linestart bool
847 * @param $interface bool
849 * @return ParserOutput|string
851 public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null ) {
852 if ( $this->mInParser
) {
853 return htmlspecialchars( $text );
856 $parser = $this->getParser();
857 $popts = $this->getParserOptions();
858 $popts->setInterfaceMessage( $interface );
859 $popts->setTargetLanguage( $language );
861 wfProfileIn( __METHOD__
);
862 if ( !$title ||
!$title instanceof Title
) {
866 // Sometimes $wgTitle isn't set either...
868 # It's not uncommon having a null $wgTitle in scripts. See r80898
869 # Create a ghost title in such case
870 $title = Title
::newFromText( 'Dwimmerlaik' );
873 $this->mInParser
= true;
874 $res = $parser->parse( $text, $title, $popts, $linestart );
875 $this->mInParser
= false;
877 wfProfileOut( __METHOD__
);
882 $this->mDisable
= true;
886 $this->mDisable
= false;
890 * Clear all stored messages. Mainly used after a mass rebuild.
893 $langs = Language
::fetchLanguageNames( null, 'mw' );
894 foreach ( array_keys( $langs ) as $code ) {
896 $this->mMemc
->delete( wfMemcKey( 'messages', $code ) );
897 # Invalidate all local caches
898 $this->mMemc
->delete( wfMemcKey( 'messages', $code, 'hash' ) );
900 $this->mLoadedLanguages
= array();
907 public function figureMessage( $key ) {
908 global $wgLanguageCode;
909 $pieces = explode( '/', $key );
910 if( count( $pieces ) < 2 ) {
911 return array( $key, $wgLanguageCode );
914 $lang = array_pop( $pieces );
915 if( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
916 return array( $key, $wgLanguageCode );
919 $message = implode( '/', $pieces );
920 return array( $message, $lang );
924 * Get all message keys stored in the message cache for a given language.
925 * If $code is the content language code, this will return all message keys
926 * for which MediaWiki:msgkey exists. If $code is another language code, this
927 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
928 * @param $code string
929 * @return array of message keys (strings)
931 public function getAllMessageKeys( $code ) {
933 $this->load( $code );
934 if ( !isset( $this->mCache
[$code] ) ) {
935 // Apparently load() failed
938 $cache = $this->mCache
[$code]; // Copy the cache
939 unset( $cache['VERSION'] ); // Remove the VERSION key
940 $cache = array_diff( $cache, array( '!NONEXISTENT' ) ); // Remove any !NONEXISTENT keys
941 // Keys may appear with a capital first letter. lcfirst them.
942 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );