MediaWiki REL1_28
MessageCache Class Reference

Message cache Performs various MediaWiki namespace-related functions. More...

Collaboration diagram for MessageCache:

Public Member Functions

 __construct ( $memCached, $useDB, $expiry)
 
 clear ()
 Clear all stored messages.
 
 disable ()
 
 enable ()
 
 figureMessage ( $key)
 
 get ( $key, $useDB=true, $langcode=true, $isFullKey=false)
 Get a message from either the content language or the user language.
 
 getAllMessageKeys ( $code)
 Get all message keys stored in the message cache for a given language.
 
 getMsgFromNamespace ( $title, $code)
 Get a message from the MediaWiki namespace, with caching.
 
 getParser ()
 
 getParserOptions ()
 ParserOptions is lazy initialised.
 
 isDisabled ()
 Whether DB/cache usage is disabled for determining messages.
 
 loadFromDB ( $code, $mode=null)
 Loads cacheable messages from the database.
 
 parse ( $text, $title=null, $linestart=true, $interface=false, $language=null)
 
 replace ( $title, $text)
 Updates cache as necessary when message page is changed.
 
 transform ( $message, $interface=false, $language=null, $title=null)
 

Static Public Member Functions

static destroyInstance ()
 Destroy the singleton instance.
 
static normalizeKey ( $key)
 Normalize message key input.
 
static singleton ()
 Get the signleton instance of this class.
 

Public Attributes

const FOR_UPDATE = 1
 
const LOCK_TTL = 30
 How long memcached locks last.
 
const WAIT_SEC = 15
 How long to wait for memcached locks.
 

Protected Member Functions

 getLocalCache ( $code)
 Try to load the cache from APC.
 
 getMessageFromFallbackChain ( $lang, $lckey, $useDB)
 Given a language, try and fetch messages from that language.
 
 getReentrantScopedLock ( $key, $timeout=self::WAIT_SEC)
 
 getValidationHash ( $code)
 Get the md5 used to validate the local APC cache.
 
 isCacheExpired ( $cache)
 Is the given cache array expired due to time passing or a version change?
 
 load ( $code, $mode=null)
 Loads messages from caches or from database in this order: (1) local message cache (if $wgUseLocalMessageCache is enabled) (2) memcached (3) from the database.
 
 loadFromDBWithLock ( $code, array &$where, $mode=null)
 
 saveToCaches (array $cache, $dest, $code=false)
 Shortcut to update caches.
 
 saveToLocalCache ( $code, $cache)
 Save the cache to APC.
 
 setValidationHash ( $code, array $cache)
 Set the md5 used to validate the local disk cache.
 

Protected Attributes

 $mCache
 Process local cache of loaded messages that are defined in MediaWiki namespace.
 
 $mDisable
 Should mean that database cannot be used, but check.
 
 $mExpiry
 Lifetime for cache, used by object caching.
 
 $mInParser = false
 
 $mLoadedLanguages = []
 Variable for tracking which variables are already loaded.
 
BagOStuff $mMemc
 
 $mParser
 
 $mParserOptions
 Message cache has its own parser which it uses to transform messages.
 
WANObjectCache $wanCache
 

Private Member Functions

 getMessageForLang ( $lang, $lckey, $useDB, &$alreadyTried)
 Given a language, try and fetch messages from that language and its fallbacks.
 
 getMessagePageName ( $langcode, $uckey)
 Get the message page name for a given language.
 

Static Private Attributes

static $instance
 Singleton instance.
 

Detailed Description

Message cache Performs various MediaWiki namespace-related functions.

Definition at line 37 of file MessageCache.php.

Constructor & Destructor Documentation

◆ __construct()

MessageCache::__construct (   $memCached,
  $useDB,
  $expiry 
)
Parameters
BagOStuff$memCachedA cache instance. If none, fall back to CACHE_NONE.
bool$useDB
int$expiryLifetime for cache.
See also
$mExpiry.

Definition at line 147 of file MessageCache.php.

References $wgUseLocalMessageCache, CACHE_NONE, global, and wfGetCache().

Member Function Documentation

◆ clear()

MessageCache::clear ( )

Clear all stored messages.

Mainly used after a mass rebuild.

Definition at line 1162 of file MessageCache.php.

References $code, as, and wfMemcKey().

◆ destroyInstance()

static MessageCache::destroyInstance ( )
static

Destroy the singleton instance.

Since
1.18

Definition at line 120 of file MessageCache.php.

Referenced by ParserTestRunner\staticSetup(), and MessageTest\testRawHtmlInMsg().

◆ disable()

MessageCache::disable ( )

Definition at line 1135 of file MessageCache.php.

◆ enable()

MessageCache::enable ( )

Definition at line 1139 of file MessageCache.php.

◆ figureMessage()

MessageCache::figureMessage (   $key)
Parameters
string$key
Returns
array

Definition at line 1176 of file MessageCache.php.

References $lang, $wgLanguageCode, and global.

Referenced by replace().

◆ get()

MessageCache::get (   $key,
  $useDB = true,
  $langcode = true,
  $isFullKey = false 
)

Get a message from either the content language or the user language.

First, assemble a list of languages to attempt getting the message from. This chain begins with the requested language and its fallbacks and then continues with the content language and its fallbacks. For each language in the chain, the following process will occur (in this order):

  1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that. Note: for the content language, there is no /lang subpage.
  2. Fetch from the static CDB cache.
  3. If available, check the database for fallback language overrides.

This process provides a number of guarantees. When changing this code, make sure all of these guarantees are preserved.

  • If the requested language is not the content language, then the CDB cache for that specific language will take precedence over the root database page ([[MW:msg]]).
  • Fallbacks will be just that: fallbacks. A fallback language will never be reached if the message is available anywhere in the language for which it is a fallback.
Parameters
string$keyThe message key
bool$useDBIf true, look for the message in the DB, false to use only the compiled l10n cache.
bool | string | object$langcodeCode of the language to get the message for.
  • If string and a valid code, will create a standard language object
  • If string but not a valid code, will create a basic language object
  • If boolean and false, create object from the current users language
  • If boolean and true, create object from the wikis content language
  • If language object, use it as given
bool$isFullKeySpecifies whether $key is a two part key "msg/lang".
Exceptions
MWExceptionWhen given an invalid key
Returns
string|bool False if the message doesn't exist, otherwise the message (which can be empty)

Definition at line 742 of file MessageCache.php.

References $lang, by, false, getMessageFromFallbackChain(), normalizeKey(), string, and wfGetLangObj().

◆ getAllMessageKeys()

MessageCache::getAllMessageKeys (   $code)

Get all message keys stored in the message cache for a given language.

If $code is the content language code, this will return all message keys for which MediaWiki:msgkey exists. If $code is another language code, this will ONLY return message keys for which MediaWiki:msgkey/$code exists.

Parameters
string$codeLanguage code
Returns
array Array of message keys (strings)

Definition at line 1202 of file MessageCache.php.

References $cache, $code, $wgContLang, global, and load.

◆ getLocalCache()

MessageCache::getLocalCache (   $code)
protected

Try to load the cache from APC.

Parameters
string$codeOptional language code, see documenation of load().
Returns
array|bool The cache array, or false if not in cache.

Definition at line 203 of file MessageCache.php.

References $code, and wfMemcKey().

Referenced by load().

◆ getMessageForLang()

MessageCache::getMessageForLang (   $lang,
  $lckey,
  $useDB,
$alreadyTried 
)
private

Given a language, try and fetch messages from that language and its fallbacks.

See also
MessageCache::get
Parameters
Language | StubObject$langPreferred language
string$lckeyLowercase key for the message (as for localisation cache)
bool$useDBWhether to include messages from the wiki database
bool[]$alreadyTriedContains true for each language that has been tried already
Returns
string|bool The message, or false if not found

Definition at line 851 of file MessageCache.php.

References $code, $lang, $wgContLang, as, getMessagePageName(), getMsgFromNamespace(), and global.

Referenced by getMessageFromFallbackChain().

◆ getMessageFromFallbackChain()

MessageCache::getMessageFromFallbackChain (   $lang,
  $lckey,
  $useDB 
)
protected

Given a language, try and fetch messages from that language.

Will also consider fallbacks of that language, the site language, and fallbacks for the site language.

See also
MessageCache::get
Parameters
Language | StubObject$langPreferred language
string$lckeyLowercase key for the message (as for localisation cache)
bool$useDBWhether to include messages from the wiki database
Returns
string|bool The message, or false if not found

Definition at line 825 of file MessageCache.php.

References $lang, $wgContLang, getMessageForLang(), and global.

Referenced by get().

◆ getMessagePageName()

MessageCache::getMessagePageName (   $langcode,
  $uckey 
)
private

Get the message page name for a given language.

Parameters
string$langcode
string$uckeyUppercase key for the message
Returns
string The page name

Definition at line 909 of file MessageCache.php.

References $wgLanguageCode, and global.

Referenced by getMessageForLang().

◆ getMsgFromNamespace()

MessageCache::getMsgFromNamespace (   $title,
  $code 
)

Get a message from the MediaWiki namespace, with caching.

The key must first be converted to two-part lang/msg form if necessary.

Unlike self::get(), this function doesn't resolve fallback chains, and some callers require this behavior. LanguageConverter::parseCachedTable() and self::get() are some examples in core.

Parameters
string$titleMessage cache key with initial uppercase letter.
string$codeCode denoting the language to try.
Returns
string|bool The message, or false if it does not exist or on error

Definition at line 931 of file MessageCache.php.

References $code, $content, $dbr, $title, DB_REPLICA, load, Revision\newKnownCurrent(), NS_MEDIAWIKI, wfDebugLog(), wfGetDB(), and wfMemcKey().

Referenced by getMessageForLang().

◆ getParser()

MessageCache::getParser ( )
Returns
Parser

Definition at line 1073 of file MessageCache.php.

References $mParser, $wgParser, $wgParserConf, and global.

Referenced by parse(), and transform().

◆ getParserOptions()

MessageCache::getParserOptions ( )

ParserOptions is lazy initialised.

Returns
ParserOptions

Definition at line 172 of file MessageCache.php.

References $mParserOptions, $wgUser, global, ParserOptions\newFromAnon(), and ParserOptions\setEditSection().

Referenced by parse(), and transform().

◆ getReentrantScopedLock()

MessageCache::getReentrantScopedLock (   $key,
  $timeout = self::WAIT_SEC 
)
protected
Parameters
string$keyA language message cache key that stores blobs
integer$timeoutWait timeout in seconds
Returns
null|ScopedCallback

Definition at line 704 of file MessageCache.php.

Referenced by load(), loadFromDBWithLock(), and replace().

◆ getValidationHash()

MessageCache::getValidationHash (   $code)
protected

Get the md5 used to validate the local APC cache.

Parameters
string$code
Returns
array (hash or false, bool expiry/volatility status)

Definition at line 653 of file MessageCache.php.

References $code, $value, and wfMemcKey().

Referenced by load().

◆ isCacheExpired()

MessageCache::isCacheExpired (   $cache)
protected

Is the given cache array expired due to time passing or a version change?

Parameters
array$cache
Returns
bool

Definition at line 610 of file MessageCache.php.

References $cache, MSG_CACHE_VERSION, and wfTimestampNow().

Referenced by load().

◆ isDisabled()

MessageCache::isDisabled ( )

Whether DB/cache usage is disabled for determining messages.

If so, this typically indicates either:

  • a) load() failed to find a cached copy nor query the DB
  • b) we are in a special context or error mode that cannot use the DB If the DB is ignored, any derived HTML output or cached objects may be wrong. To avoid long-term cache pollution, TTLs can be adjusted accordingly.
Returns
bool
Since
1.27

Definition at line 1155 of file MessageCache.php.

References $mDisable.

◆ load()

MessageCache::load (   $code,
  $mode = null 
)
protected

Loads messages from caches or from database in this order: (1) local message cache (if $wgUseLocalMessageCache is enabled) (2) memcached (3) from the database.

When succesfully loading from (2) or (3), all higher level caches are updated for the newest version.

Nothing is loaded if member variable mDisable is true, either manually set by calling code or if message loading fails (is this possible?).

Returns true if cache is already populated or it was succesfully populated, or false if populating empty cache fails. Also returns true if MessageCache is disabled.

Parameters
string$codeLanguage to which load messages
integer$modeUse MessageCache::FOR_UPDATE to skip process cache [optional]
Exceptions
MWException
Returns
bool

Definition at line 241 of file MessageCache.php.

References $cache, $code, $success, array(), cache, data, getLocalCache(), getReentrantScopedLock(), getValidationHash(), in, info, isCacheExpired(), list, loadFromDBWithLock(), messages, of, or, saveToCaches(), too, wfDebug(), wfDebugLog(), and wfMemcKey().

◆ loadFromDB()

MessageCache::loadFromDB (   $code,
  $mode = null 
)

Loads cacheable messages from the database.

Messages bigger than $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded on-demand from the database later.

Parameters
string$codeLanguage code
integer$modeUse MessageCache::FOR_UPDATE to skip process cache
Returns
array Loaded messages for storing in caches

Definition at line 446 of file MessageCache.php.

References $cache, $code, $dbr, $res, $value, $wgAdaptiveMessageCache, $wgLanguageCode, $wgMaxMsgCacheEntrySize, as, DB_MASTER, DB_REPLICA, Revision\getRevisionText(), global, load, MSG_CACHE_VERSION, NS_MEDIAWIKI, serialize(), TS_MW, wfDebugLog(), wfGetDB(), and wfTimestamp().

Referenced by loadFromDBWithLock().

◆ loadFromDBWithLock()

MessageCache::loadFromDBWithLock (   $code,
array $where,
  $mode = null 
)
protected
Parameters
string$code
array$whereList of wfDebug() comments
integer$modeUse MessageCache::FOR_UPDATE to use DB_MASTER
Returns
bool|string True on success or one of ("cantacquire", "disabled")

Definition at line 382 of file MessageCache.php.

References $cache, $code, $status, $wgUseLocalMessageCache, getReentrantScopedLock(), global, loadFromDB(), saveToCaches(), and wfMemcKey().

Referenced by load().

◆ normalizeKey()

static MessageCache::normalizeKey (   $key)
static

Normalize message key input.

Parameters
string$keyInput message key to be normalized
Returns
string Normalized message key

Definition at line 130 of file MessageCache.php.

References $wgContLang, and global.

Referenced by ApiQueryAllMessages\execute(), get(), and MessageCacheTest\testNormalizeKey().

◆ parse()

MessageCache::parse (   $text,
  $title = null,
  $linestart = true,
  $interface = false,
  $language = null 
)
Parameters
string$text
Title$title
bool$linestartWhether or not this is at the start of a line
bool$interfaceWhether this is an interface message
Language | string$languageLanguage code
Returns
ParserOutput|string

Definition at line 1099 of file MessageCache.php.

References $parser, $res, $title, $wgTitle, getParser(), getParserOptions(), global, NS_SPECIAL, wfDebugLog(), and wfGetAllCallers().

◆ replace()

MessageCache::replace (   $title,
  $text 
)

Updates cache as necessary when message page is changed.

Parameters
string | bool$titleName of the page changed (false if deleted)
mixed$textNew contents of the page.

Definition at line 534 of file MessageCache.php.

References $code, $title, $wgContLang, $wgLanguageCode, $wgMaxMsgCacheEntrySize, as, figureMessage(), RequestContext\getMain(), getReentrantScopedLock(), global, list, load, saveToCaches(), and wfMemcKey().

◆ saveToCaches()

MessageCache::saveToCaches ( array  $cache,
  $dest,
  $code = false 
)
protected

Shortcut to update caches.

Parameters
array$cacheCached messages with a version.
string$destEither "local-only" to save to local caches only or "all" to save to all caches.
string | bool$codeLanguage code (default: false)
Returns
bool

Definition at line 633 of file MessageCache.php.

References $cache, $code, $success, saveToLocalCache(), setValidationHash(), and wfMemcKey().

Referenced by load(), loadFromDBWithLock(), and replace().

◆ saveToLocalCache()

MessageCache::saveToLocalCache (   $code,
  $cache 
)
protected

Save the cache to APC.

Parameters
string$code
array$cacheThe cache array

Definition at line 215 of file MessageCache.php.

References $cache, $code, and wfMemcKey().

Referenced by saveToCaches().

◆ setValidationHash()

MessageCache::setValidationHash (   $code,
array  $cache 
)
protected

Set the md5 used to validate the local disk cache.

If $cache has a 'LATEST' UNIX timestamp key, then the hash will not be treated as "volatile" by getValidationHash() for the next few seconds

Parameters
string$code
array$cacheCached messages with a version

Definition at line 688 of file MessageCache.php.

References $cache, $code, and wfMemcKey().

Referenced by saveToCaches().

◆ singleton()

◆ transform()

MessageCache::transform (   $message,
  $interface = false,
  $language = null,
  $title = null 
)
Parameters
string$message
bool$interface
string$languageLanguage code
Title$title
Returns
string

Definition at line 1044 of file MessageCache.php.

References $parser, $title, getParser(), and getParserOptions().

Member Data Documentation

◆ $instance

MessageCache MessageCache::$instance
staticprivate

Singleton instance.

Definition at line 94 of file MessageCache.php.

Referenced by singleton().

◆ $mCache

array MessageCache::$mCache
protected

Process local cache of loaded messages that are defined in MediaWiki namespace.

First array level is a language code, second level is message key and the values are either message content prefixed with space, or !NONEXISTENT for negative caching.

Definition at line 53 of file MessageCache.php.

◆ $mDisable

bool MessageCache::$mDisable
protected

Should mean that database cannot be used, but check.

Definition at line 59 of file MessageCache.php.

Referenced by isDisabled().

◆ $mExpiry

MessageCache::$mExpiry
protected

Lifetime for cache, used by object caching.

Set on construction, see __construct().

Definition at line 65 of file MessageCache.php.

◆ $mInParser

bool MessageCache::$mInParser = false
protected

Definition at line 82 of file MessageCache.php.

◆ $mLoadedLanguages

array MessageCache::$mLoadedLanguages = []
protected

Variable for tracking which variables are already loaded.

Definition at line 77 of file MessageCache.php.

◆ $mMemc

BagOStuff MessageCache::$mMemc
protected

Definition at line 85 of file MessageCache.php.

◆ $mParser

MessageCache::$mParser
protected

Definition at line 71 of file MessageCache.php.

Referenced by getParser().

◆ $mParserOptions

MessageCache::$mParserOptions
protected

Message cache has its own parser which it uses to transform messages.

Definition at line 71 of file MessageCache.php.

Referenced by getParserOptions().

◆ $wanCache

WANObjectCache MessageCache::$wanCache
protected

Definition at line 87 of file MessageCache.php.

◆ FOR_UPDATE

const MessageCache::FOR_UPDATE = 1

Definition at line 38 of file MessageCache.php.

◆ LOCK_TTL

const MessageCache::LOCK_TTL = 30

How long memcached locks last.

Definition at line 43 of file MessageCache.php.

◆ WAIT_SEC

const MessageCache::WAIT_SEC = 15

How long to wait for memcached locks.

Definition at line 41 of file MessageCache.php.


The documentation for this class was generated from the following file: