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
25 * MediaWiki message cache structure version.
26 * Bump this whenever the message cache format has changed.
28 define( 'MSG_CACHE_VERSION', 2 );
31 * Memcached timeout when loading a key.
32 * See MessageCache::load()
34 define( 'MSG_LOAD_TIMEOUT', 60 );
37 * Memcached timeout when locking a key for a writing operation.
38 * See MessageCache::lock()
40 define( 'MSG_LOCK_TIMEOUT', 30 );
42 * Number of times we will try to acquire a lock from Memcached.
43 * This comes in addition to MSG_LOCK_TIMEOUT.
45 define( 'MSG_WAIT_TIMEOUT', 30 );
49 * Performs various MediaWiki namespace-related functions
53 const FOR_UPDATE
= 1; // force message reload
56 * Process local cache of loaded messages that are defined in
57 * MediaWiki namespace. First array level is a language code,
58 * second level is message key and the values are either message
59 * content prefixed with space, or !NONEXISTENT for negative
66 * Should mean that database cannot be used, but check
72 * Lifetime for cache, used by object caching.
73 * Set on construction, see __construct().
78 * Message cache has its own parser which it uses to transform
81 protected $mParserOptions, $mParser;
84 * Variable for tracking which variables are already loaded
85 * @var array $mLoadedLanguages
87 protected $mLoadedLanguages = array();
90 * @var bool $mInParser
92 protected $mInParser = false;
96 /** @var WANObjectCache */
102 * @var MessageCache $instance
104 private static $instance;
107 * Get the signleton instance of this class
110 * @return MessageCache
112 public static function singleton() {
113 if ( is_null( self
::$instance ) ) {
114 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
115 self
::$instance = new self(
116 wfGetMessageCacheStorage(),
117 $wgUseDatabaseMessages,
122 return self
::$instance;
126 * Destroy the singleton instance
130 public static function destroyInstance() {
131 self
::$instance = null;
135 * Normalize message key input
137 * @param string $key Input message key to be normalized
138 * @return string Normalized message key
140 public static function normalizeKey( $key ) {
142 $lckey = strtr( $key, ' ', '_' );
143 if ( ord( $lckey ) < 128 ) {
144 $lckey[0] = strtolower( $lckey[0] );
146 $lckey = $wgContLang->lcfirst( $lckey );
153 * @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
155 * @param int $expiry Lifetime for cache. @see $mExpiry.
157 function __construct( $memCached, $useDB, $expiry ) {
158 global $wgUseLocalMessageCache;
161 $memCached = wfGetCache( CACHE_NONE
);
164 $this->mMemc
= $memCached;
165 $this->mDisable
= !$useDB;
166 $this->mExpiry
= $expiry;
168 if ( $wgUseLocalMessageCache ) {
169 $this->localCache
= ObjectCache
::newAccelerator( array(), CACHE_NONE
);
171 $this->localCache
= wfGetCache( CACHE_NONE
);
174 $this->wanCache
= ObjectCache
::getMainWANInstance();
178 * ParserOptions is lazy initialised.
180 * @return ParserOptions
182 function getParserOptions() {
183 if ( !$this->mParserOptions
) {
184 $this->mParserOptions
= new ParserOptions
;
185 $this->mParserOptions
->setEditSection( false );
188 return $this->mParserOptions
;
192 * Try to load the cache from APC.
194 * @param string $code Optional language code, see documenation of load().
195 * @return array|bool The cache array, or false if not in cache.
197 protected function getLocalCache( $code ) {
198 $cacheKey = wfMemcKey( __CLASS__
, $code );
199 return $this->localCache
->get( $cacheKey );
203 * Save the cache to APC.
205 * @param string $code
206 * @param array $cache The cache array
208 protected function saveToLocalCache( $code, $cache ) {
209 $cacheKey = wfMemcKey( __CLASS__
, $code );
210 $this->localCache
->set( $cacheKey, $cache );
214 * Loads messages from caches or from database in this order:
215 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
217 * (3) from the database.
219 * When succesfully loading from (2) or (3), all higher level caches are
220 * updated for the newest version.
222 * Nothing is loaded if member variable mDisable is true, either manually
223 * set by calling code or if message loading fails (is this possible?).
225 * Returns true if cache is already populated or it was succesfully populated,
226 * or false if populating empty cache fails. Also returns true if MessageCache
229 * @param bool|string $code Language to which load messages
230 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
231 * @throws MWException
234 function load( $code = false, $mode = null ) {
235 global $wgUseLocalMessageCache;
237 if ( !is_string( $code ) ) {
238 # This isn't really nice, so at least make a note about it and try to
240 wfDebug( __METHOD__
. " called without providing a language code\n" );
244 # Don't do double loading...
245 if ( isset( $this->mLoadedLanguages
[$code] ) && $mode != self
::FOR_UPDATE
) {
249 # 8 lines of code just to say (once) that message cache is disabled
250 if ( $this->mDisable
) {
251 static $shownDisabled = false;
252 if ( !$shownDisabled ) {
253 wfDebug( __METHOD__
. ": disabled\n" );
254 $shownDisabled = true;
260 # Loading code starts
261 $success = false; # Keep track of success
262 $staleCache = false; # a cache array with expired data, or false if none has been loaded
263 $where = array(); # Debug info, delayed to avoid spamming debug log too much
265 # Hash of the contents is stored in memcache, to detect if data-center cache
266 # or local cache goes out of date (e.g. due to replace() on some other server)
267 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
269 if ( $wgUseLocalMessageCache && $hash ) {
270 # Try the local cache and check against the cluster hash key...
271 $cache = $this->getLocalCache( $code );
273 $where[] = 'local cache is empty';
274 } elseif ( !isset( $cache['HASH'] ) ||
$cache['HASH'] !== $hash ) {
275 $where[] = 'local cache has the wrong hash';
276 $staleCache = $cache;
277 } elseif ( $this->isCacheExpired( $cache ) ) {
278 $where[] = 'local cache is expired';
279 $staleCache = $cache;
280 } elseif ( $hashVolatile ) {
281 $where[] = 'local cache validation key is expired/volatile';
282 $staleCache = $cache;
284 $where[] = 'got from local cache';
286 $this->mCache
[$code] = $cache;
291 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
292 # Try the global cache. If it is empty, try to acquire a lock. If
293 # the lock can't be acquired, wait for the other thread to finish
294 # and then try the global cache a second time.
295 for ( $failedAttempts = 0; $failedAttempts < 2; $failedAttempts++
) {
296 if ( $hashVolatile && $staleCache ) {
297 # Do not bother fetching the whole cache blob to avoid I/O.
298 # Instead, just try to get the non-blocking $statusKey lock
299 # below, and use the local stale value if it was not acquired.
300 $where[] = 'global cache is presumed expired';
302 $cache = $this->mMemc
->get( $cacheKey );
304 $where[] = 'global cache is empty';
305 } elseif ( $this->isCacheExpired( $cache ) ) {
306 $where[] = 'global cache is expired';
307 $staleCache = $cache;
308 } elseif ( $hashVolatile ) {
309 # DB results are slave lag prone until the holdoff TTL passes.
310 # By then, updates should be reflected in loadFromDBWithLock().
311 # One thread renerates the cache while others use old values.
312 $where[] = 'global cache is expired/volatile';
313 $staleCache = $cache;
315 $where[] = 'got from global cache';
316 $this->mCache
[$code] = $cache;
317 $this->saveToCaches( $cache, 'local-only', $code );
323 # Done, no need to retry
327 # We need to call loadFromDB. Limit the concurrency to one process.
328 # This prevents the site from going down when the cache expires.
329 # Note that the slam-protection lock here is non-blocking.
330 if ( $this->loadFromDBWithLock( $code, $where ) ) {
333 } elseif ( $staleCache ) {
334 # Use the stale cache while some other thread constructs the new one
335 $where[] = 'using stale cache';
336 $this->mCache
[$code] = $staleCache;
339 } elseif ( $failedAttempts > 0 ) {
340 # Already retried once, still failed, so don't do another lock/unlock cycle
341 # This case will typically be hit if memcached is down, or if
342 # loadFromDB() takes longer than MSG_WAIT_TIMEOUT
343 $where[] = "could not acquire status key.";
346 $statusKey = wfMemcKey( 'messages', $code, 'status' );
347 $status = $this->mMemc
->get( $statusKey );
348 if ( $status === 'error' ) {
352 # Wait for the other thread to finish, then retry
353 $where[] = 'waited for other thread to complete';
354 $this->lock( $cacheKey );
355 $this->unlock( $cacheKey );
362 $where[] = 'loading FAILED - cache is disabled';
363 $this->mDisable
= true;
364 $this->mCache
= false;
365 # This used to throw an exception, but that led to nasty side effects like
366 # the whole wiki being instantly down if the memcached server died
368 # All good, just record the success
369 $this->mLoadedLanguages
[$code] = true;
372 $info = implode( ', ', $where );
373 wfDebugLog( 'MessageCache', __METHOD__
. ": Loading $code... $info\n" );
379 * @param string $code
380 * @param array $where List of wfDebug() comments
381 * @return bool Lock acquired and loadFromDB() called
383 protected function loadFromDBWithLock( $code, array &$where ) {
384 global $wgUseLocalMessageCache;
386 $memCache = $this->mMemc
;
388 $statusKey = wfMemcKey( 'messages', $code, 'status' );
389 if ( !$memCache->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT
) ) {
390 return false; // could not acquire lock
393 # Unlock the status key if there is an exception
394 $statusUnlocker = new ScopedCallback( function () use ( $memCache, $statusKey ) {
395 $memCache->delete( $statusKey );
398 # Now let's regenerate
399 $where[] = 'loading from database';
401 $cacheKey = wfMemcKey( 'messages', $code );
402 # Lock the cache to prevent conflicting writes
403 # If this lock fails, it doesn't really matter, it just means the
404 # write is potentially non-atomic, e.g. the results of a replace()
406 if ( $this->lock( $cacheKey ) ) {
408 $mainUnlocker = new ScopedCallback( function () use ( $that, $cacheKey ) {
409 $that->unlock( $cacheKey );
412 $mainUnlocker = null;
413 $where[] = 'could not acquire main lock';
416 $cache = $this->loadFromDB( $code );
417 $this->mCache
[$code] = $cache;
418 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
421 ScopedCallback
::consume( $mainUnlocker );
422 ScopedCallback
::consume( $statusUnlocker );
424 if ( !$saveSuccess ) {
425 # Cache save has failed.
426 # There are two main scenarios where this could be a problem:
428 # - The cache is more than the maximum size (typically
431 # - Memcached has no space remaining in the relevant slab
432 # class. This is unlikely with recent versions of
435 # Either way, if there is a local cache, nothing bad will
436 # happen. If there is no local cache, disabling the message
437 # cache for all requests avoids incurring a loadFromDB()
438 # overhead on every request, and thus saves the wiki from
439 # complete downtime under moderate traffic conditions.
440 if ( !$wgUseLocalMessageCache ) {
441 $memCache->set( $statusKey, 'error', 60 * 5 );
442 $where[] = 'could not save cache, disabled globally for 5 minutes';
444 $where[] = "could not save global cache";
452 * Loads cacheable messages from the database. Messages bigger than
453 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
454 * on-demand from the database later.
456 * @param string $code Language code.
457 * @return array Loaded messages for storing in caches.
459 function loadFromDB( $code ) {
460 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
462 $dbr = wfGetDB( DB_SLAVE
);
467 'page_is_redirect' => 0,
468 'page_namespace' => NS_MEDIAWIKI
,
472 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
473 if ( !isset( $this->mCache
[$wgLanguageCode] ) ) {
474 $this->load( $wgLanguageCode );
476 $mostused = array_keys( $this->mCache
[$wgLanguageCode] );
477 foreach ( $mostused as $key => $value ) {
478 $mostused[$key] = "$value/$code";
482 if ( count( $mostused ) ) {
483 $conds['page_title'] = $mostused;
484 } elseif ( $code !== $wgLanguageCode ) {
485 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
487 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
488 # other than language code.
489 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
492 # Conditions to fetch oversized pages to ignore them
494 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
496 # Load titles for all oversized pages in the MediaWiki namespace
497 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__
. "($code)-big" );
498 foreach ( $res as $row ) {
499 $cache[$row->page_title
] = '!TOO BIG';
502 # Conditions to load the remaining pages with their contents
503 $smallConds = $conds;
504 $smallConds[] = 'page_latest=rev_id';
505 $smallConds[] = 'rev_text_id=old_id';
506 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
509 array( 'page', 'revision', 'text' ),
510 array( 'page_title', 'old_text', 'old_flags' ),
512 __METHOD__
. "($code)-small"
515 foreach ( $res as $row ) {
516 $text = Revision
::getRevisionText( $row );
517 if ( $text === false ) {
518 // Failed to fetch data; possible ES errors?
519 // Store a marker to fetch on-demand as a workaround...
524 . ": failed to load message page text for {$row->page_title} ($code)"
527 $entry = ' ' . $text;
529 $cache[$row->page_title
] = $entry;
532 $cache['VERSION'] = MSG_CACHE_VERSION
;
534 $cache['HASH'] = md5( serialize( $cache ) );
535 $cache['EXPIRY'] = wfTimestamp( TS_MW
, time() +
$this->mExpiry
);
541 * Updates cache as necessary when message page is changed
543 * @param string $title Name of the page changed.
544 * @param mixed $text New contents of the page.
546 public function replace( $title, $text ) {
547 global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
549 if ( $this->mDisable
) {
553 list( $msg, $code ) = $this->figureMessage( $title );
554 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
555 # Content language overrides do not use the /<code> suffix
559 $cacheKey = wfMemcKey( 'messages', $code );
560 $this->lock( $cacheKey );
561 $this->load( $code, self
::FOR_UPDATE
);
563 $titleKey = wfMemcKey( 'messages', 'individual', $title );
565 if ( $text === false ) {
566 # Article was deleted
567 $this->mCache
[$code][$title] = '!NONEXISTENT';
568 $this->mMemc
->delete( $titleKey );
569 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
571 $this->mCache
[$code][$title] = '!TOO BIG';
572 $this->mMemc
->set( $titleKey, ' ' . $text, $this->mExpiry
);
574 $this->mCache
[$code][$title] = ' ' . $text;
575 $this->mMemc
->delete( $titleKey );
579 $this->saveToCaches( $this->mCache
[$code], 'all', $code );
580 $this->unlock( $cacheKey );
581 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
583 // Also delete cached sidebar... just in case it is affected
584 $codes = array( $code );
585 if ( $code === 'en' ) {
586 // Delete all sidebars, like for example on action=purge on the
588 $codes = array_keys( Language
::fetchLanguageNames() );
591 foreach ( $codes as $code ) {
592 $sidebarKey = wfMemcKey( 'sidebar', $code );
593 $this->wanCache
->delete( $sidebarKey, 5 );
596 // Update the message in the message blob store
597 $blobStore = new MessageBlobStore();
598 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
600 Hooks
::run( 'MessageCacheReplace', array( $title, $text ) );
604 * Is the given cache array expired due to time passing or a version change?
606 * @param array $cache
609 protected function isCacheExpired( $cache ) {
610 if ( !isset( $cache['VERSION'] ) ||
!isset( $cache['EXPIRY'] ) ) {
613 if ( $cache['VERSION'] != MSG_CACHE_VERSION
) {
616 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
624 * Shortcut to update caches.
626 * @param array $cache Cached messages with a version.
627 * @param string $dest Either "local-only" to save to local caches only
628 * or "all" to save to all caches.
629 * @param string|bool $code Language code (default: false)
632 protected function saveToCaches( $cache, $dest, $code = false ) {
633 global $wgUseLocalMessageCache;
635 if ( $dest === 'all' ) {
636 $cacheKey = wfMemcKey( 'messages', $code );
637 $success = $this->mMemc
->set( $cacheKey, $cache );
642 $this->setValidationHash( $code, $cache['HASH'] );
644 # Save to local cache
645 if ( $wgUseLocalMessageCache ) {
646 $this->saveToLocalCache( $code, $cache );
653 * Get the md5 used to validate the local disk cache
655 * @param string $code
656 * @return array (hash or false, bool expiry/volatility status)
658 protected function getValidationHash( $code ) {
660 $value = $this->wanCache
->get(
661 wfMemcKey( 'messages', $code, 'hash' ),
663 array( wfMemcKey( 'messages', $code ) )
665 $expired = ( $curTTL === null ||
$curTTL < 0 );
667 return array( $value, $expired );
671 * Set the md5 used to validate the local disk cache
673 * @param string $code
674 * @param string $hash
676 protected function setValidationHash( $code, $hash ) {
677 $this->wanCache
->set(
678 wfMemcKey( 'messages', $code, 'hash' ),
680 WANObjectCache
::TTL_NONE
685 * Represents a write lock on the messages key.
687 * Will retry MessageCache::MSG_WAIT_TIMEOUT times, each operations having
688 * a timeout of MessageCache::MSG_LOCK_TIMEOUT.
691 * @return bool Success
693 function lock( $key ) {
694 $lockKey = $key . ':lock';
697 for ( $i = 0; $i < MSG_WAIT_TIMEOUT
&& !$acquired; $i++
) {
698 $acquired = $this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
);
703 # Fail fast if memcached is totally down
706 if ( !$this->mMemc
->set( wfMemcKey( 'test' ), 'test', 1 ) ) {
716 function unlock( $key ) {
717 $lockKey = $key . ':lock';
718 $this->mMemc
->delete( $lockKey );
722 * Get a message from either the content language or the user language.
724 * First, assemble a list of languages to attempt getting the message from. This
725 * chain begins with the requested language and its fallbacks and then continues with
726 * the content language and its fallbacks. For each language in the chain, the following
727 * process will occur (in this order):
728 * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
729 * Note: for the content language, there is no /lang subpage.
730 * 2. Fetch from the static CDB cache.
731 * 3. If available, check the database for fallback language overrides.
733 * This process provides a number of guarantees. When changing this code, make sure all
734 * of these guarantees are preserved.
735 * * If the requested language is *not* the content language, then the CDB cache for that
736 * specific language will take precedence over the root database page ([[MW:msg]]).
737 * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
738 * the message is available *anywhere* in the language for which it is a fallback.
740 * @param string $key The message key
741 * @param bool $useDB If true, look for the message in the DB, false
742 * to use only the compiled l10n cache.
743 * @param bool|string|object $langcode Code of the language to get the message for.
744 * - If string and a valid code, will create a standard language object
745 * - If string but not a valid code, will create a basic language object
746 * - If boolean and false, create object from the current users language
747 * - If boolean and true, create object from the wikis content language
748 * - If language object, use it as given
749 * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
751 * @throws MWException When given an invalid key
752 * @return string|bool False if the message doesn't exist, otherwise the
753 * message (which can be empty)
755 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
758 if ( is_int( $key ) ) {
759 // Fix numerical strings that somehow become ints
762 } elseif ( !is_string( $key ) ) {
763 throw new MWException( 'Non-string key given' );
764 } elseif ( $key === '' ) {
765 // Shortcut: the empty key is always missing
769 // For full keys, get the language code from the key
770 $pos = strrpos( $key, '/' );
771 if ( $isFullKey && $pos !== false ) {
772 $langcode = substr( $key, $pos +
1 );
773 $key = substr( $key, 0, $pos );
776 // Normalise title-case input (with some inlining)
777 $lckey = MessageCache
::normalizeKey( $key );
779 Hooks
::run( 'MessageCache::get', array( &$lckey ) );
781 if ( ord( $lckey ) < 128 ) {
782 $uckey = ucfirst( $lckey );
784 $uckey = $wgContLang->ucfirst( $lckey );
787 // Loop through each language in the fallback list until we find something useful
788 $lang = wfGetLangObj( $langcode );
789 $message = $this->getMessageFromFallbackChain(
793 !$this->mDisable
&& $useDB
796 // If we still have no message, maybe the key was in fact a full key so try that
797 if ( $message === false ) {
798 $parts = explode( '/', $lckey );
799 // We may get calls for things that are http-urls from sidebar
800 // Let's not load nonexistent languages for those
801 // They usually have more than one slash.
802 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
803 $message = Language
::getMessageFor( $parts[0], $parts[1] );
804 if ( $message === null ) {
810 // Post-processing if the message exists
811 if ( $message !== false ) {
813 $message = str_replace(
815 # Fix for trailing whitespace, removed by textarea
817 # Fix for NBSP, converted to space by firefox
834 * Given a language, try and fetch a message from that language, then the
835 * fallbacks of that language, then the site language, then the fallbacks for the
838 * @param Language $lang Requested language
839 * @param string $lckey Lowercase key for the message
840 * @param string $uckey Uppercase key for the message
841 * @param bool $useDB Whether to use the database
843 * @see MessageCache::get
844 * @return string|bool The message, or false if not found
846 protected function getMessageFromFallbackChain( $lang, $lckey, $uckey, $useDB ) {
847 global $wgLanguageCode, $wgContLang;
849 $langcode = $lang->getCode();
852 // First try the requested language.
854 if ( $langcode === $wgLanguageCode ) {
855 // Messages created in the content language will not have the /lang extension
856 $message = $this->getMsgFromNamespace( $uckey, $langcode );
858 $message = $this->getMsgFromNamespace( "$uckey/$langcode", $langcode );
862 if ( $message !== false ) {
866 // Check the CDB cache
867 $message = $lang->getMessage( $lckey );
868 if ( $message !== null ) {
872 list( $fallbackChain, $siteFallbackChain ) =
873 Language
::getFallbacksIncludingSiteLanguage( $langcode );
875 // Next try checking the database for all of the fallback languages of the requested language.
877 foreach ( $fallbackChain as $code ) {
878 if ( $code === $wgLanguageCode ) {
879 // Messages created in the content language will not have the /lang extension
880 $message = $this->getMsgFromNamespace( $uckey, $code );
882 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
885 if ( $message !== false ) {
886 // Found the message.
892 // Now try checking the site language.
894 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
895 if ( $message !== false ) {
900 $message = $wgContLang->getMessage( $lckey );
901 if ( $message !== null ) {
905 // Finally try the DB for the site language's fallbacks.
907 foreach ( $siteFallbackChain as $code ) {
908 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
909 if ( $message === false && $code === $wgLanguageCode ) {
910 // Messages created in the content language will not have the /lang extension
911 $message = $this->getMsgFromNamespace( $uckey, $code );
914 if ( $message !== false ) {
915 // Found the message.
925 * Get a message from the MediaWiki namespace, with caching. The key must
926 * first be converted to two-part lang/msg form if necessary.
928 * Unlike self::get(), this function doesn't resolve fallback chains, and
929 * some callers require this behavior. LanguageConverter::parseCachedTable()
930 * and self::get() are some examples in core.
932 * @param string $title Message cache key with initial uppercase letter.
933 * @param string $code Code denoting the language to try.
934 * @return string|bool The message, or false if it does not exist or on error
936 function getMsgFromNamespace( $title, $code ) {
937 $this->load( $code );
938 if ( isset( $this->mCache
[$code][$title] ) ) {
939 $entry = $this->mCache
[$code][$title];
940 if ( substr( $entry, 0, 1 ) === ' ' ) {
941 // The message exists, so make sure a string
943 return (string)substr( $entry, 1 );
944 } elseif ( $entry === '!NONEXISTENT' ) {
946 } elseif ( $entry === '!TOO BIG' ) {
947 // Fall through and try invididual message cache below
950 // XXX: This is not cached in process cache, should it?
952 Hooks
::run( 'MessagesPreLoad', array( $title, &$message ) );
953 if ( $message !== false ) {
960 # Try the individual message cache
961 $titleKey = wfMemcKey( 'messages', 'individual', $title );
962 $entry = $this->mMemc
->get( $titleKey );
964 if ( substr( $entry, 0, 1 ) === ' ' ) {
965 $this->mCache
[$code][$title] = $entry;
967 // The message exists, so make sure a string
969 return (string)substr( $entry, 1 );
970 } elseif ( $entry === '!NONEXISTENT' ) {
971 $this->mCache
[$code][$title] = '!NONEXISTENT';
975 # Corrupt/obsolete entry, delete it
976 $this->mMemc
->delete( $titleKey );
980 # Try loading it from the database
981 $revision = Revision
::newFromTitle(
982 Title
::makeTitle( NS_MEDIAWIKI
, $title ), false, Revision
::READ_LATEST
985 $content = $revision->getContent();
987 // A possibly temporary loading failure.
990 __METHOD__
. ": failed to load message page text for {$title} ($code)"
992 $message = null; // no negative caching
994 // XXX: Is this the right way to turn a Content object into a message?
995 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
996 // CssContent. MessageContent is *not* used for storing messages, it's
997 // only used for wrapping them when needed.
998 $message = $content->getWikitextForTransclusion();
1000 if ( $message === false ||
$message === null ) {
1003 __METHOD__
. ": message content doesn't provide wikitext "
1004 . "(content model: " . $content->getContentHandler() . ")"
1007 $message = false; // negative caching
1009 $this->mCache
[$code][$title] = ' ' . $message;
1010 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
1014 $message = false; // negative caching
1017 if ( $message === false ) { // negative caching
1018 $this->mCache
[$code][$title] = '!NONEXISTENT';
1019 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
1026 * @param string $message
1027 * @param bool $interface
1028 * @param string $language Language code
1029 * @param Title $title
1032 function transform( $message, $interface = false, $language = null, $title = null ) {
1033 // Avoid creating parser if nothing to transform
1034 if ( strpos( $message, '{{' ) === false ) {
1038 if ( $this->mInParser
) {
1042 $parser = $this->getParser();
1044 $popts = $this->getParserOptions();
1045 $popts->setInterfaceMessage( $interface );
1046 $popts->setTargetLanguage( $language );
1048 $userlang = $popts->setUserLang( $language );
1049 $this->mInParser
= true;
1050 $message = $parser->transformMsg( $message, $popts, $title );
1051 $this->mInParser
= false;
1052 $popts->setUserLang( $userlang );
1061 function getParser() {
1062 global $wgParser, $wgParserConf;
1063 if ( !$this->mParser
&& isset( $wgParser ) ) {
1064 # Do some initialisation so that we don't have to do it twice
1065 $wgParser->firstCallInit();
1066 # Clone it and store it
1067 $class = $wgParserConf['class'];
1068 if ( $class == 'ParserDiffTest' ) {
1070 $this->mParser
= new $class( $wgParserConf );
1072 $this->mParser
= clone $wgParser;
1076 return $this->mParser
;
1080 * @param string $text
1081 * @param Title $title
1082 * @param bool $linestart Whether or not this is at the start of a line
1083 * @param bool $interface Whether this is an interface message
1084 * @param string $language Language code
1085 * @return ParserOutput|string
1087 public function parse( $text, $title = null, $linestart = true,
1088 $interface = false, $language = null
1090 if ( $this->mInParser
) {
1091 return htmlspecialchars( $text );
1094 $parser = $this->getParser();
1095 $popts = $this->getParserOptions();
1096 $popts->setInterfaceMessage( $interface );
1097 $popts->setTargetLanguage( $language );
1099 if ( !$title ||
!$title instanceof Title
) {
1101 wfDebugLog( 'GlobalTitleFail', __METHOD__
. ' called by ' .
1102 wfGetAllCallers( 5 ) . ' with no title set.' );
1105 // Sometimes $wgTitle isn't set either...
1107 # It's not uncommon having a null $wgTitle in scripts. See r80898
1108 # Create a ghost title in such case
1109 $title = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/title not set in ' . __METHOD__
);
1112 $this->mInParser
= true;
1113 $res = $parser->parse( $text, $title, $popts, $linestart );
1114 $this->mInParser
= false;
1119 function disable() {
1120 $this->mDisable
= true;
1124 $this->mDisable
= false;
1128 * Clear all stored messages. Mainly used after a mass rebuild.
1131 $langs = Language
::fetchLanguageNames( null, 'mw' );
1132 foreach ( array_keys( $langs ) as $code ) {
1133 # Global and local caches
1134 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
1137 $this->mLoadedLanguages
= array();
1141 * @param string $key
1144 public function figureMessage( $key ) {
1145 global $wgLanguageCode;
1147 $pieces = explode( '/', $key );
1148 if ( count( $pieces ) < 2 ) {
1149 return array( $key, $wgLanguageCode );
1152 $lang = array_pop( $pieces );
1153 if ( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
1154 return array( $key, $wgLanguageCode );
1157 $message = implode( '/', $pieces );
1159 return array( $message, $lang );
1163 * Get all message keys stored in the message cache for a given language.
1164 * If $code is the content language code, this will return all message keys
1165 * for which MediaWiki:msgkey exists. If $code is another language code, this
1166 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1167 * @param string $code Language code
1168 * @return array Array of message keys (strings)
1170 public function getAllMessageKeys( $code ) {
1172 $this->load( $code );
1173 if ( !isset( $this->mCache
[$code] ) ) {
1174 // Apparently load() failed
1177 // Remove administrative keys
1178 $cache = $this->mCache
[$code];
1179 unset( $cache['VERSION'] );
1180 unset( $cache['EXPIRY'] );
1181 // Remove any !NONEXISTENT keys
1182 $cache = array_diff( $cache, array( '!NONEXISTENT' ) );
1184 // Keys may appear with a capital first letter. lcfirst them.
1185 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );