<?php
/**
- *
- * @addtogroup Cache
+ * @file
+ * @ingroup Cache
*/
/**
/**
* Message cache
* Performs various MediaWiki namespace-related functions
- *
+ * @ingroup Cache
*/
class MessageCache {
var $mCache, $mUseCache, $mDisable, $mExpiry;
$localHash = fread( $file, 32 );
if ( $hash === $localHash ) {
// All good, get the rest of it
- $serialized = fread( $file, 20000000 );
+ $serialized = '';
+ while ( !feof( $file ) ) {
+ $serialized .= fread( $file, 100000 );
+ }
$this->setCache( unserialize( $serialized ) );
}
fclose( $file );
}
function loadFromScript( $hash ) {
- trigger_error( 'Use of ' . __METHOD__ . ' is deprecated', E_USER_NOTICE );
+ wfDeprecated( __METHOD__ );
$this->loadFromLocal( $hash );
}
-
+
function saveToScript($array, $hash) {
global $wgLocalMessageCache;
if ( $wgLocalMessageCache === false ) {
umask( $oldUmask );
$file = fopen( $filename.'.tmp', 'w');
fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
-
+
foreach ($array as $key => $message) {
fwrite($file, "'". $this->escapeForScript($key).
- "' => '" . $this->escapeForScript($message).
+ "' => '" . $this->escapeForScript($message).
"',\n");
}
fwrite($file,");\n?>");
# Load titles for all oversized pages in the MediaWiki namespace
$res = $dbr->select( 'page', 'page_title',
- array(
+ array(
'page_len > ' . intval( $wgMaxMsgCacheEntrySize ),
'page_is_redirect' => 0,
'page_namespace' => NS_MEDIAWIKI,
- ),
+ ),
__METHOD__ );
while ( $row = $dbr->fetchObject( $res ) ) {
$this->mCache[$row->page_title] = '!TOO BIG';
# Load text for the remaining pages
$res = $dbr->select( array( 'page', 'revision', 'text' ),
array( 'page_title', 'old_text', 'old_flags' ),
- array(
+ array(
'page_is_redirect' => 0,
'page_namespace' => NS_MEDIAWIKI,
'page_latest=rev_id',
'rev_text_id=old_id',
- 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ),
+ 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ),
__METHOD__ );
for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
wfProfileOut( __METHOD__ );
}
- /**
- * Not really needed anymore
- */
- function getKeys() {
- global $wgContLang;
- if ( !$this->mKeys ) {
- $this->mKeys = array();
- $allMessages = Language::getMessagesFor( 'en' );
- foreach ( $allMessages as $key => $unused ) {
- $title = $wgContLang->ucfirst( $key );
- array_push( $this->mKeys, $title );
- }
- }
- return $this->mKeys;
- }
-
function replace( $title, $text ) {
global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
global $wgMaxMsgCacheEntrySize;
*
* @param string $key The message cache key
* @param bool $useDB Get the message from the DB, false to use only the localisation
- * @param bool $forContent Get the message from the content language rather than the
- * user language
+ * @param string $langcode Code of the language to get the message for, if
+ * it is a valid code create a language for that
+ * language, if it is a string but not a valid code
+ * then make a basic language object, if it is a
+ * false boolean then use the current users
+ * language (as a fallback for the old parameter
+ * functionality), or if it is a true boolean then
+ * use the wikis content language (also as a
+ * fallback).
* @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
*/
- function get( $key, $useDB = true, $forContent = true, $isFullKey = false ) {
+ function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
global $wgContLanguageCode, $wgContLang, $wgLang;
- if( $forContent ) {
+
+ # Identify which language to get or create a language object for.
+ if( $langcode === $wgContLang->getCode() || $langcode === true ) {
+ # $langcode is the language code of the wikis content language object.
+ # or it is a boolean and value is true
$lang =& $wgContLang;
- } else {
+ } elseif( $langcode === $wgLang->getCode() || $langcode === false ) {
+ # $langcode is the language code of user language object.
+ # or it was a boolean and value is false
$lang =& $wgLang;
+ } else {
+ $validCodes = array_keys( Language::getLanguageNames() );
+ if( in_array( $langcode, $validCodes ) ) {
+ # $langcode corresponds to a valid language.
+ $lang = Language::factory( $langcode );
+ } else {
+ # $langcode is a string, but not a valid language code; use content language.
+ $lang =& $wgContLang;
+ wfDebug( 'Invalid language code passed to MessageCache::get, falling back to content language.' );
+ }
}
+
$langcode = $lang->getCode();
+
# If uninitialised, someone is trying to call this halfway through Setup.php
if( !$this->mInitialised ) {
return '<' . htmlspecialchars($key) . '>';
}
/**
- * Get a message from the MediaWiki namespace, with caching. The key must
+ * Get a message from the MediaWiki namespace, with caching. The key must
* first be converted to two-part lang/msg form if necessary.
*
* @param string $title Message cache key with initial uppercase letter
function enable() { $this->mDisable = false; }
/** @deprecated */
- function disableTransform() {}
- function enableTransform() {}
- function setTransform( $x ) {}
- function getTransform() { return false; }
+ function disableTransform(){
+ wfDeprecated( __METHOD__ );
+ }
+ function enableTransform() {
+ wfDeprecated( __METHOD__ );
+ }
+ function setTransform( $x ) {
+ wfDeprecated( __METHOD__ );
+ }
+ function getTransform() {
+ wfDeprecated( __METHOD__ );
+ return false;
+ }
/**
* Add a message to the cache
wfRunHooks( 'LoadAllMessages' );
# Some register their messages in $wgExtensionMessagesFiles
foreach ( $wgExtensionMessagesFiles as $name => $file ) {
- if ( $file ) {
- $this->loadMessagesFile( $file );
- $wgExtensionMessagesFiles[$name] = false;
- }
+ wfLoadExtensionMessages( $name );
}
# Still others will respond to neither, they are EVIL. We sometimes need to know!
}
/**
* Load messages from a given file
+ *
+ * @param string $filename Filename of file to load.
+ * @param string $langcode Language to load messages for, or false for
+ * default behvaiour (en, content language and user
+ * language).
*/
- function loadMessagesFile( $filename ) {
+ function loadMessagesFile( $filename, $langcode = false ) {
global $wgLang, $wgContLang;
$messages = $magicWords = false;
require( $filename );
- /*
- * Load only languages that are usually used, and merge all fallbacks,
- * except English.
- */
- $langs = array_unique( array( 'en', $wgContLang->getCode(), $wgLang->getCode() ) );
- foreach( $langs as $code ) {
- $fbcode = $code;
- $mergedMessages = array();
- do {
- if ( isset($messages[$fbcode]) ) {
- $mergedMessages += $messages[$fbcode];
- }
- $fbcode = Language::getFallbackfor( $fbcode );
- } while( $fbcode && $fbcode !== 'en' );
-
- if ( !empty($mergedMessages) )
- $this->addMessages( $mergedMessages, $code );
+ $validCodes = Language::getLanguageNames();
+ if( is_string( $langcode ) && array_key_exists( $langcode, $validCodes ) ) {
+ # Load messages for given language code.
+ $this->processMessagesArray( $messages, $langcode );
+ } elseif( is_string( $langcode ) && !array_key_exists( $langcode, $validCodes ) ) {
+ wfDebug( "Invalid language '$langcode' code passed to MessageCache::loadMessagesFile()" );
+ } else {
+ # Load only languages that are usually used, and merge all
+ # fallbacks, except English.
+ $langs = array_unique( array( 'en', $wgContLang->getCode(), $wgLang->getCode() ) );
+ foreach( $langs as $code ) {
+ $this->processMessagesArray( $messages, $code );
+ }
}
if ( $magicWords !== false ) {
$wgContLang->addMagicWordsByLang( $magicWords );
}
}
+
+ /**
+ * Process an array of messages, loading it into the message cache.
+ *
+ * @param array $messages Messages array.
+ * @param string $langcode Language code to process.
+ */
+ function processMessagesArray( $messages, $langcode ) {
+ $fallbackCode = $langcode;
+ $mergedMessages = array();
+ do {
+ if ( isset($messages[$fallbackCode]) ) {
+ $mergedMessages += $messages[$fallbackCode];
+ }
+ $fallbackCode = Language::getFallbackfor( $fallbackCode );
+ } while( $fallbackCode && $fallbackCode !== 'en' );
+
+ if ( !empty($mergedMessages) )
+ $this->addMessages( $mergedMessages, $langcode );
+ }
+
}