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 $hash String: 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 $this->lock( $cacheKey );
331 # Limit the concurrency of loadFromDB to a single process
332 # This prevents the site from going down when the cache expires
333 $statusKey = wfMemcKey( 'messages', $code, 'status' );
334 $success = $this->mMemc
->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT
);
335 if ( $success ) { // acquired lock
336 $cache = $this->loadFromDB( $code );
337 $success = $this->setCache( $cache, $code );
338 if ( $success ) { // messages loaded
339 $success = $this->saveToCaches( $cache, true, $code );
341 $this->mMemc
->delete( $statusKey );
343 $this->mMemc
->set( $statusKey, 'error', 60 * 5 );
344 wfDebug( __METHOD__
. ": set() error: restart memcached server!\n" );
345 $exception = new MWException( "Could not save cache for '$code'." );
348 $this->mMemc
->delete( $statusKey );
349 $exception = new MWException( "Could not load cache from DB for '$code'." );
352 $exception = new MWException( "Could not acquire '$statusKey' lock." );
354 $this->unlock( $cacheKey );
358 $this->mDisable
= true;
359 $this->mCache
= false;
360 // This used to go on, but that led to lots of nasty side
361 // effects like gadgets and sidebar getting cached with their
363 if ( $exception instanceof Exception
) {
366 throw new MWException( "MessageCache failed to load messages" );
369 # All good, just record the success
370 $info = implode( ', ', $where );
371 wfDebug( __METHOD__
. ": Loading $code... $info\n" );
372 $this->mLoadedLanguages
[$code] = true;
374 wfProfileOut( __METHOD__
);
379 * Loads cacheable messages from the database. Messages bigger than
380 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
381 * on-demand from the database later.
383 * @param $code String: language code.
384 * @return Array: loaded messages for storing in caches.
386 function loadFromDB( $code ) {
387 wfProfileIn( __METHOD__
);
388 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
389 $dbr = wfGetDB( DB_SLAVE
);
394 'page_is_redirect' => 0,
395 'page_namespace' => NS_MEDIAWIKI
,
399 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
400 if ( !isset( $this->mCache
[$wgLanguageCode] ) ) {
401 $this->load( $wgLanguageCode );
403 $mostused = array_keys( $this->mCache
[$wgLanguageCode] );
404 foreach ( $mostused as $key => $value ) {
405 $mostused[$key] = "$value/$code";
409 if ( count( $mostused ) ) {
410 $conds['page_title'] = $mostused;
411 } elseif ( $code !== $wgLanguageCode ) {
412 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
414 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
415 # other than language code.
416 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
419 # Conditions to fetch oversized pages to ignore them
421 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
423 # Load titles for all oversized pages in the MediaWiki namespace
424 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__
. "($code)-big" );
425 foreach ( $res as $row ) {
426 $cache[$row->page_title
] = '!TOO BIG';
429 # Conditions to load the remaining pages with their contents
430 $smallConds = $conds;
431 $smallConds[] = 'page_latest=rev_id';
432 $smallConds[] = 'rev_text_id=old_id';
433 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
436 array( 'page', 'revision', 'text' ),
437 array( 'page_title', 'old_text', 'old_flags' ),
439 __METHOD__
. "($code)-small"
442 foreach ( $res as $row ) {
443 $text = Revision
::getRevisionText( $row );
444 if( $text === false ) {
445 // Failed to fetch data; possible ES errors?
446 // Store a marker to fetch on-demand as a workaround...
448 wfDebugLog( 'MessageCache', __METHOD__
. ": failed to load message page text for {$row->page_title} ($code)" );
450 $entry = ' ' . $text;
452 $cache[$row->page_title
] = $entry;
455 $cache['VERSION'] = MSG_CACHE_VERSION
;
456 wfProfileOut( __METHOD__
);
461 * Updates cache as necessary when message page is changed
463 * @param $title String: name of the page changed.
464 * @param $text Mixed: new contents of the page.
466 public function replace( $title, $text ) {
467 global $wgMaxMsgCacheEntrySize;
468 wfProfileIn( __METHOD__
);
470 if ( $this->mDisable
) {
471 wfProfileOut( __METHOD__
);
475 list( $msg, $code ) = $this->figureMessage( $title );
477 $cacheKey = wfMemcKey( 'messages', $code );
478 $this->load( $code );
479 $this->lock( $cacheKey );
481 $titleKey = wfMemcKey( 'messages', 'individual', $title );
483 if ( $text === false ) {
484 # Article was deleted
485 $this->mCache
[$code][$title] = '!NONEXISTENT';
486 $this->mMemc
->delete( $titleKey );
487 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
489 $this->mCache
[$code][$title] = '!TOO BIG';
490 $this->mMemc
->set( $titleKey, ' ' . $text, $this->mExpiry
);
492 $this->mCache
[$code][$title] = ' ' . $text;
493 $this->mMemc
->delete( $titleKey );
497 $this->saveToCaches( $this->mCache
[$code], true, $code );
498 $this->unlock( $cacheKey );
500 // Also delete cached sidebar... just in case it is affected
501 $codes = array( $code );
502 if ( $code === 'en' ) {
503 // Delete all sidebars, like for example on action=purge on the
505 $codes = array_keys( Language
::fetchLanguageNames() );
509 foreach ( $codes as $code ) {
510 $sidebarKey = wfMemcKey( 'sidebar', $code );
511 $wgMemc->delete( $sidebarKey );
514 // Update the message in the message blob store
516 MessageBlobStore
::updateMessage( $wgContLang->lcfirst( $msg ) );
518 wfRunHooks( 'MessageCacheReplace', array( $title, $text ) );
520 wfProfileOut( __METHOD__
);
524 * Shortcut to update caches.
526 * @param $cache Array: cached messages with a version.
527 * @param $memc Bool: Wether to update or not memcache.
528 * @param $code String: Language code.
529 * @return bool on somekind of error.
531 protected function saveToCaches( $cache, $memc = true, $code = false ) {
532 wfProfileIn( __METHOD__
);
533 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
535 $cacheKey = wfMemcKey( 'messages', $code );
538 $success = $this->mMemc
->set( $cacheKey, $cache, $this->mExpiry
);
543 # Save to local cache
544 if ( $wgUseLocalMessageCache ) {
545 $serialized = serialize( $cache );
546 $hash = md5( $serialized );
547 $this->mMemc
->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry
);
548 if ($wgLocalMessageCacheSerialized) {
549 $this->saveToLocal( $serialized, $hash, $code );
551 $this->saveToScript( $cache, $hash, $code );
555 wfProfileOut( __METHOD__
);
560 * Represents a write lock on the messages key
564 * @return Boolean: success
566 function lock( $key ) {
567 $lockKey = $key . ':lock';
568 for ( $i = 0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
572 return $i >= MSG_WAIT_TIMEOUT
;
575 function unlock( $key ) {
576 $lockKey = $key . ':lock';
577 $this->mMemc
->delete( $lockKey );
581 * Get a message from either the content language or the user language.
583 * @param $key String: the message cache key
584 * @param $useDB Boolean: get the message from the DB, false to use only
586 * @param bool|string $langcode Code of the language to get the message for, if
587 * it is a valid code create a language for that language,
588 * if it is a string but not a valid code then make a basic
589 * language object, if it is a false boolean then use the
590 * current users language (as a fallback for the old
591 * parameter functionality), or if it is a true boolean
592 * then use the wikis content language (also as a
594 * @param $isFullKey Boolean: specifies whether $key is a two part key
597 * @throws MWException
598 * @return string|bool
600 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
601 global $wgLanguageCode, $wgContLang;
603 if ( is_int( $key ) ) {
604 // "Non-string key given" exception sometimes happens for numerical strings that become ints somewhere on their way here
605 $key = strval( $key );
608 if ( !is_string( $key ) ) {
609 throw new MWException( 'Non-string key given' );
612 if ( strval( $key ) === '' ) {
613 # Shortcut: the empty key is always missing
617 $lang = wfGetLangObj( $langcode );
619 throw new MWException( "Bad lang code $langcode given" );
622 $langcode = $lang->getCode();
626 # Normalise title-case input (with some inlining)
627 $lckey = str_replace( ' ', '_', $key );
628 if ( ord( $key ) < 128 ) {
629 $lckey[0] = strtolower( $lckey[0] );
630 $uckey = ucfirst( $lckey );
632 $lckey = $wgContLang->lcfirst( $lckey );
633 $uckey = $wgContLang->ucfirst( $lckey );
636 # Try the MediaWiki namespace
637 if( !$this->mDisable
&& $useDB ) {
639 if( !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
640 $title .= '/' . $langcode;
642 $message = $this->getMsgFromNamespace( $title, $langcode );
645 # Try the array in the language object
646 if ( $message === false ) {
647 $message = $lang->getMessage( $lckey );
648 if ( is_null( $message ) ) {
653 # Try the array of another language
654 if( $message === false ) {
655 $parts = explode( '/', $lckey );
656 # We may get calls for things that are http-urls from sidebar
657 # Let's not load nonexistent languages for those
658 # They usually have more than one slash.
659 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
660 $message = Language
::getMessageFor( $parts[0], $parts[1] );
661 if ( is_null( $message ) ) {
667 # Is this a custom message? Try the default language in the db...
668 if( ( $message === false ||
$message === '-' ) &&
669 !$this->mDisable
&& $useDB &&
670 !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
671 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
675 if( $message === false ) {
680 $message = strtr( $message,
682 # Fix for trailing whitespace, removed by textarea
684 # Fix for NBSP, converted to space by firefox
685 ' ' => "\xc2\xa0",
686 ' ' => "\xc2\xa0",
693 * Get a message from the MediaWiki namespace, with caching. The key must
694 * first be converted to two-part lang/msg form if necessary.
696 * @param $title String: Message cache key with initial uppercase letter.
697 * @param $code String: code denoting the language to try.
699 * @return string|bool False on failure
701 function getMsgFromNamespace( $title, $code ) {
702 $this->load( $code );
703 if ( isset( $this->mCache
[$code][$title] ) ) {
704 $entry = $this->mCache
[$code][$title];
705 if ( substr( $entry, 0, 1 ) === ' ' ) {
706 return substr( $entry, 1 );
707 } elseif ( $entry === '!NONEXISTENT' ) {
709 } elseif( $entry === '!TOO BIG' ) {
710 // Fall through and try invididual message cache below
713 // XXX: This is not cached in process cache, should it?
715 wfRunHooks( 'MessagesPreLoad', array( $title, &$message ) );
716 if ( $message !== false ) {
723 # Try the individual message cache
724 $titleKey = wfMemcKey( 'messages', 'individual', $title );
725 $entry = $this->mMemc
->get( $titleKey );
727 if ( substr( $entry, 0, 1 ) === ' ' ) {
728 $this->mCache
[$code][$title] = $entry;
729 return substr( $entry, 1 );
730 } elseif ( $entry === '!NONEXISTENT' ) {
731 $this->mCache
[$code][$title] = '!NONEXISTENT';
734 # Corrupt/obsolete entry, delete it
735 $this->mMemc
->delete( $titleKey );
739 # Try loading it from the database
740 $revision = Revision
::newFromTitle(
741 Title
::makeTitle( NS_MEDIAWIKI
, $title ), false, Revision
::READ_LATEST
744 $content = $revision->getContent();
746 // A possibly temporary loading failure.
747 wfDebugLog( 'MessageCache', __METHOD__
. ": failed to load message page text for {$title} ($code)" );
748 $message = null; // no negative caching
750 // XXX: Is this the right way to turn a Content object into a message?
751 // NOTE: $content is typically either WikitextContent, JavaScriptContent or CssContent.
752 // MessageContent is *not* used for storing messages, it's only used for wrapping them when needed.
753 $message = $content->getWikitextForTransclusion();
755 if ( $message === false ||
$message === null ) {
756 wfDebugLog( 'MessageCache', __METHOD__
. ": message content doesn't provide wikitext "
757 . "(content model: " . $content->getContentHandler() . ")" );
759 $message = false; // negative caching
761 $this->mCache
[$code][$title] = ' ' . $message;
762 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
766 $message = false; // negative caching
769 if ( $message === false ) { // negative caching
770 $this->mCache
[$code][$title] = '!NONEXISTENT';
771 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
778 * @param $message string
779 * @param $interface bool
781 * @param $title Title
784 function transform( $message, $interface = false, $language = null, $title = null ) {
785 // Avoid creating parser if nothing to transform
786 if( strpos( $message, '{{' ) === false ) {
790 if ( $this->mInParser
) {
794 $parser = $this->getParser();
796 $popts = $this->getParserOptions();
797 $popts->setInterfaceMessage( $interface );
798 $popts->setTargetLanguage( $language );
800 $userlang = $popts->setUserLang( $language );
801 $this->mInParser
= true;
802 $message = $parser->transformMsg( $message, $popts, $title );
803 $this->mInParser
= false;
804 $popts->setUserLang( $userlang );
812 function getParser() {
813 global $wgParser, $wgParserConf;
814 if ( !$this->mParser
&& isset( $wgParser ) ) {
815 # Do some initialisation so that we don't have to do it twice
816 $wgParser->firstCallInit();
817 # Clone it and store it
818 $class = $wgParserConf['class'];
819 if ( $class == 'Parser_DiffTest' ) {
821 $this->mParser
= new $class( $wgParserConf );
823 $this->mParser
= clone $wgParser;
826 return $this->mParser
;
830 * @param $text string
831 * @param $title Title
832 * @param $linestart bool
833 * @param $interface bool
835 * @return ParserOutput|string
837 public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null ) {
838 if ( $this->mInParser
) {
839 return htmlspecialchars( $text );
842 $parser = $this->getParser();
843 $popts = $this->getParserOptions();
844 $popts->setInterfaceMessage( $interface );
845 $popts->setTargetLanguage( $language );
847 wfProfileIn( __METHOD__
);
848 if ( !$title ||
!$title instanceof Title
) {
852 // Sometimes $wgTitle isn't set either...
854 # It's not uncommon having a null $wgTitle in scripts. See r80898
855 # Create a ghost title in such case
856 $title = Title
::newFromText( 'Dwimmerlaik' );
859 $this->mInParser
= true;
860 $res = $parser->parse( $text, $title, $popts, $linestart );
861 $this->mInParser
= false;
863 wfProfileOut( __METHOD__
);
868 $this->mDisable
= true;
872 $this->mDisable
= false;
876 * Clear all stored messages. Mainly used after a mass rebuild.
879 $langs = Language
::fetchLanguageNames( null, 'mw' );
880 foreach ( array_keys( $langs ) as $code ) {
882 $this->mMemc
->delete( wfMemcKey( 'messages', $code ) );
883 # Invalidate all local caches
884 $this->mMemc
->delete( wfMemcKey( 'messages', $code, 'hash' ) );
886 $this->mLoadedLanguages
= array();
893 public function figureMessage( $key ) {
894 global $wgLanguageCode;
895 $pieces = explode( '/', $key );
896 if( count( $pieces ) < 2 ) {
897 return array( $key, $wgLanguageCode );
900 $lang = array_pop( $pieces );
901 if( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
902 return array( $key, $wgLanguageCode );
905 $message = implode( '/', $pieces );
906 return array( $message, $lang );
910 * Get all message keys stored in the message cache for a given language.
911 * If $code is the content language code, this will return all message keys
912 * for which MediaWiki:msgkey exists. If $code is another language code, this
913 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
914 * @param $code string
915 * @return array of message keys (strings)
917 public function getAllMessageKeys( $code ) {
919 $this->load( $code );
920 if ( !isset( $this->mCache
[$code] ) ) {
921 // Apparently load() failed
924 $cache = $this->mCache
[$code]; // Copy the cache
925 unset( $cache['VERSION'] ); // Remove the VERSION key
926 $cache = array_diff( $cache, array( '!NONEXISTENT' ) ); // Remove any !NONEXISTENT keys
927 // Keys may appear with a capital first letter. lcfirst them.
928 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );