f24e8bcde4380b3a462511958d32d1fd05658d65
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. The fallback
582 * language order is the users language fallback union the content language fallback.
583 * This list is then applied to find keys in the following order
584 * 1) MediaWiki:$key/$langcode (for every language except the content language where
585 * we look at MediaWiki:$key)
586 * 2) Built-in messages via the l10n cache which is also in fallback order
588 * @param $key String: the message cache key
589 * @param $useDB Boolean: If true will look for the message in the DB, false only
590 * get the message from the DB, false to use only the compiled l10n cache.
591 * @param bool|string|object $langcode Code of the language to get the message for.
592 * - If string and a valid code, will create a standard language object
593 * - If string but not a valid code, will create a basic language object
594 * - If boolean and false, create object from the current users language
595 * - If boolean and true, create object from the wikis content language
596 * - If language object, use it as given
597 * @param $isFullKey Boolean: specifies whether $key is a two part key
600 * @throws MWException
601 * @return string|bool False if the message doesn't exist, otherwise the message
603 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
604 global $wgLanguageCode, $wgContLang;
606 wfProfileIn( __METHOD__
);
608 if ( is_int( $key ) ) {
609 // "Non-string key given" exception sometimes happens for numerical strings that become ints somewhere on their way here
610 $key = strval( $key );
613 if ( !is_string( $key ) ) {
614 wfProfileOut( __METHOD__
);
615 throw new MWException( 'Non-string key given' );
618 if ( strval( $key ) === '' ) {
619 # Shortcut: the empty key is always missing
620 wfProfileOut( __METHOD__
);
625 # Obtain the initial language object
627 $keyParts = explode( '/', $key );
628 if ( count( $keyParts ) < 2 ) {
629 throw new MWException( "Message key '$key' does not appear to be a full key." );
632 $langcode = array_pop( $keyParts );
633 $key = implode( '/', $keyParts );
636 # Obtain a language object for the requested language from the passed language code
637 # Note that the language code could in fact be a language object already but we assume
638 # it's a string further below.
639 $requestedLangObj = wfGetLangObj( $langcode );
640 if ( !$requestedLangObj ) {
641 wfProfileOut( __METHOD__
);
642 throw new MWException( "Bad lang code $langcode given" );
644 $langcode = $requestedLangObj->getCode();
646 # Normalise title-case input (with some inlining)
647 $lckey = str_replace( ' ', '_', $key );
648 if ( ord( $key ) < 128 ) {
649 $lckey[0] = strtolower( $lckey[0] );
650 $uckey = ucfirst( $lckey );
652 $lckey = $wgContLang->lcfirst( $lckey );
653 $uckey = $wgContLang->ucfirst( $lckey );
656 # Loop through each language in the fallback list until we find something useful
659 # Try the MediaWiki namespace
660 if ( !$this->mDisable
&& $useDB ) {
661 $fallbackChain = Language
::getFallbacksIncludingSiteLanguage( $langcode );
662 array_unshift( $fallbackChain, $langcode );
664 foreach ( $fallbackChain as $langcode ) {
665 if ( $langcode === $wgLanguageCode ) {
666 # Messages created in the content language will not have the /lang extension
667 $message = $this->getMsgFromNamespace( $uckey, $langcode );
669 $message = $this->getMsgFromNamespace( "$uckey/$langcode", $langcode );
672 if ( $message !== false ) {
678 # Try the array in the language object
679 if ( $message === false ) {
680 $message = $requestedLangObj->getMessage( $lckey );
681 if ( is_null ( $message ) ) {
686 # If we still have no message, maybe the key was in fact a full key so try that
687 if( $message === false ) {
688 $parts = explode( '/', $lckey );
689 # We may get calls for things that are http-urls from sidebar
690 # Let's not load nonexistent languages for those
691 # They usually have more than one slash.
692 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
693 $message = Language
::getMessageFor( $parts[0], $parts[1] );
694 if ( is_null( $message ) ) {
701 if( $message === false ) {
702 wfProfileOut( __METHOD__
);
707 $message = strtr( $message,
709 # Fix for trailing whitespace, removed by textarea
711 # Fix for NBSP, converted to space by firefox
712 ' ' => "\xc2\xa0",
713 ' ' => "\xc2\xa0",
716 wfProfileOut( __METHOD__
);
721 * Get a message from the MediaWiki namespace, with caching. The key must
722 * first be converted to two-part lang/msg form if necessary.
724 * @param $title String: Message cache key with initial uppercase letter.
725 * @param $code String: code denoting the language to try.
727 * @return string|bool False on failure
729 function getMsgFromNamespace( $title, $code ) {
730 $this->load( $code );
731 if ( isset( $this->mCache
[$code][$title] ) ) {
732 $entry = $this->mCache
[$code][$title];
733 if ( substr( $entry, 0, 1 ) === ' ' ) {
734 return substr( $entry, 1 );
735 } elseif ( $entry === '!NONEXISTENT' ) {
737 } elseif( $entry === '!TOO BIG' ) {
738 // Fall through and try invididual message cache below
741 // XXX: This is not cached in process cache, should it?
743 wfRunHooks( 'MessagesPreLoad', array( $title, &$message ) );
744 if ( $message !== false ) {
751 # Try the individual message cache
752 $titleKey = wfMemcKey( 'messages', 'individual', $title );
753 $entry = $this->mMemc
->get( $titleKey );
755 if ( substr( $entry, 0, 1 ) === ' ' ) {
756 $this->mCache
[$code][$title] = $entry;
757 return substr( $entry, 1 );
758 } elseif ( $entry === '!NONEXISTENT' ) {
759 $this->mCache
[$code][$title] = '!NONEXISTENT';
762 # Corrupt/obsolete entry, delete it
763 $this->mMemc
->delete( $titleKey );
767 # Try loading it from the database
768 $revision = Revision
::newFromTitle(
769 Title
::makeTitle( NS_MEDIAWIKI
, $title ), false, Revision
::READ_LATEST
772 $content = $revision->getContent();
774 // A possibly temporary loading failure.
775 wfDebugLog( 'MessageCache', __METHOD__
. ": failed to load message page text for {$title} ($code)" );
776 $message = null; // no negative caching
778 // XXX: Is this the right way to turn a Content object into a message?
779 // NOTE: $content is typically either WikitextContent, JavaScriptContent or CssContent.
780 // MessageContent is *not* used for storing messages, it's only used for wrapping them when needed.
781 $message = $content->getWikitextForTransclusion();
783 if ( $message === false ||
$message === null ) {
784 wfDebugLog( 'MessageCache', __METHOD__
. ": message content doesn't provide wikitext "
785 . "(content model: " . $content->getContentHandler() . ")" );
787 $message = false; // negative caching
789 $this->mCache
[$code][$title] = ' ' . $message;
790 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
794 $message = false; // negative caching
797 if ( $message === false ) { // negative caching
798 $this->mCache
[$code][$title] = '!NONEXISTENT';
799 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
806 * @param $message string
807 * @param $interface bool
809 * @param $title Title
812 function transform( $message, $interface = false, $language = null, $title = null ) {
813 // Avoid creating parser if nothing to transform
814 if( strpos( $message, '{{' ) === false ) {
818 if ( $this->mInParser
) {
822 $parser = $this->getParser();
824 $popts = $this->getParserOptions();
825 $popts->setInterfaceMessage( $interface );
826 $popts->setTargetLanguage( $language );
828 $userlang = $popts->setUserLang( $language );
829 $this->mInParser
= true;
830 $message = $parser->transformMsg( $message, $popts, $title );
831 $this->mInParser
= false;
832 $popts->setUserLang( $userlang );
840 function getParser() {
841 global $wgParser, $wgParserConf;
842 if ( !$this->mParser
&& isset( $wgParser ) ) {
843 # Do some initialisation so that we don't have to do it twice
844 $wgParser->firstCallInit();
845 # Clone it and store it
846 $class = $wgParserConf['class'];
847 if ( $class == 'Parser_DiffTest' ) {
849 $this->mParser
= new $class( $wgParserConf );
851 $this->mParser
= clone $wgParser;
854 return $this->mParser
;
858 * @param $text string
859 * @param $title Title
860 * @param $linestart bool
861 * @param $interface bool
863 * @return ParserOutput|string
865 public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null ) {
866 if ( $this->mInParser
) {
867 return htmlspecialchars( $text );
870 $parser = $this->getParser();
871 $popts = $this->getParserOptions();
872 $popts->setInterfaceMessage( $interface );
873 $popts->setTargetLanguage( $language );
875 wfProfileIn( __METHOD__
);
876 if ( !$title ||
!$title instanceof Title
) {
880 // Sometimes $wgTitle isn't set either...
882 # It's not uncommon having a null $wgTitle in scripts. See r80898
883 # Create a ghost title in such case
884 $title = Title
::newFromText( 'Dwimmerlaik' );
887 $this->mInParser
= true;
888 $res = $parser->parse( $text, $title, $popts, $linestart );
889 $this->mInParser
= false;
891 wfProfileOut( __METHOD__
);
896 $this->mDisable
= true;
900 $this->mDisable
= false;
904 * Clear all stored messages. Mainly used after a mass rebuild.
907 $langs = Language
::fetchLanguageNames( null, 'mw' );
908 foreach ( array_keys( $langs ) as $code ) {
910 $this->mMemc
->delete( wfMemcKey( 'messages', $code ) );
911 # Invalidate all local caches
912 $this->mMemc
->delete( wfMemcKey( 'messages', $code, 'hash' ) );
914 $this->mLoadedLanguages
= array();
921 public function figureMessage( $key ) {
922 global $wgLanguageCode;
923 $pieces = explode( '/', $key );
924 if( count( $pieces ) < 2 ) {
925 return array( $key, $wgLanguageCode );
928 $lang = array_pop( $pieces );
929 if( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
930 return array( $key, $wgLanguageCode );
933 $message = implode( '/', $pieces );
934 return array( $message, $lang );
938 * Get all message keys stored in the message cache for a given language.
939 * If $code is the content language code, this will return all message keys
940 * for which MediaWiki:msgkey exists. If $code is another language code, this
941 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
942 * @param $code string
943 * @return array of message keys (strings)
945 public function getAllMessageKeys( $code ) {
947 $this->load( $code );
948 if ( !isset( $this->mCache
[$code] ) ) {
949 // Apparently load() failed
952 $cache = $this->mCache
[$code]; // Copy the cache
953 unset( $cache['VERSION'] ); // Remove the VERSION key
954 $cache = array_diff( $cache, array( '!NONEXISTENT' ) ); // Remove any !NONEXISTENT keys
955 // Keys may appear with a capital first letter. lcfirst them.
956 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );