summaryrefslogtreecommitdiff
path: root/includes/MessageCache.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/MessageCache.php')
-rw-r--r--includes/MessageCache.php149
1 files changed, 84 insertions, 65 deletions
diff --git a/includes/MessageCache.php b/includes/MessageCache.php
index 10c95a7e..ce717fa8 100644
--- a/includes/MessageCache.php
+++ b/includes/MessageCache.php
@@ -58,9 +58,8 @@ class MessageCache {
* Try to load the cache from a local file
*/
function loadFromLocal( $hash ) {
- global $wgLocalMessageCache;
+ global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
- $this->mCache = false;
if ( $wgLocalMessageCache === false ) {
return;
}
@@ -74,21 +73,35 @@ class MessageCache {
return;
}
- // Check to see if the file has the hash specified
- $localHash = fread( $file, 32 );
- if ( $hash == $localHash ) {
- // All good, get the rest of it
- $serialized = fread( $file, 10000000 );
- $this->setCache( unserialize( $serialized ) );
+ if ( $wgLocalMessageCacheSerialized ) {
+ // Check to see if the file has the hash specified
+ $localHash = fread( $file, 32 );
+ if ( $hash === $localHash ) {
+ // All good, get the rest of it
+ $serialized = '';
+ while ( !feof( $file ) ) {
+ $serialized .= fread( $file, 100000 );
+ }
+ $this->setCache( unserialize( $serialized ) );
+ }
+ fclose( $file );
+ } else {
+ $localHash=substr(fread($file,40),8);
+ fclose($file);
+ if ($hash!=$localHash) {
+ return;
+ }
+
+ require("$wgLocalMessageCache/messages-" . wfWikiID());
+ $this->setCache( $this->mCache);
}
- fclose( $file );
}
/**
* Save the cache to a local file
*/
function saveToLocal( $serialized, $hash ) {
- global $wgLocalMessageCache;
+ global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
if ( $wgLocalMessageCache === false ) {
return;
@@ -111,26 +124,8 @@ class MessageCache {
}
function loadFromScript( $hash ) {
- global $wgLocalMessageCache;
- if ( $wgLocalMessageCache === false ) {
- return;
- }
-
- $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
-
- wfSuppressWarnings();
- $file = fopen( $filename, 'r' );
- wfRestoreWarnings();
- if ( !$file ) {
- return;
- }
- $localHash=substr(fread($file,40),8);
- fclose($file);
- if ($hash!=$localHash) {
- return;
- }
- require("$wgLocalMessageCache/messages-" . wfWikiID());
- $this->setCache( $this->mCache);
+ trigger_error( 'Use of ' . __METHOD__ . ' is deprecated', E_USER_NOTICE );
+ $this->loadFromLocal( $hash );
}
function saveToScript($array, $hash) {
@@ -201,19 +196,17 @@ class MessageCache {
$this->mCache = false;
# Try local cache
- wfProfileIn( $fname.'-fromlocal' );
- $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
- if ( $hash ) {
- if ($wgLocalMessageCacheSerialized) {
+ if ( $wgLocalMessageCache !== false ) {
+ wfProfileIn( $fname.'-fromlocal' );
+ $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
+ if ( $hash ) {
$this->loadFromLocal( $hash );
- } else {
- $this->loadFromScript( $hash );
- }
- if ( $this->mCache ) {
- wfDebug( "MessageCache::load(): got from local cache\n" );
+ if ( $this->mCache ) {
+ wfDebug( "MessageCache::load(): got from local cache\n" );
+ }
}
+ wfProfileOut( $fname.'-fromlocal' );
}
- wfProfileOut( $fname.'-fromlocal' );
# Try memcached
if ( !$this->mCache ) {
@@ -358,7 +351,6 @@ class MessageCache {
wfProfileIn( __METHOD__ );
$this->lock();
$this->load();
- $parserMemc->delete(wfMemcKey('sidebar'));
if ( is_array( $this->mCache ) ) {
if ( $text === false ) {
# Article was deleted
@@ -386,6 +378,7 @@ class MessageCache {
}
}
$this->unlock();
+ $parserMemc->delete(wfMemcKey('sidebar'));
wfProfileOut( __METHOD__ );
}
@@ -475,17 +468,20 @@ class MessageCache {
}
# Try the array of another language
- if( $message === false && strpos( $lckey, '/' ) ) {
- $message = explode( '/', $lckey );
- if ( $message[1] ) {
- wfSuppressWarnings();
- $message = Language::getMessageFor( $message[0], $message[1] );
- wfRestoreWarnings();
- if ( is_null( $message ) ) {
- $message = false;
+ $pos = strrpos( $lckey, '/' );
+ if( $message === false && $pos !== false) {
+ $mkey = substr( $lckey, 0, $pos );
+ $code = substr( $lckey, $pos+1 );
+ if ( $code ) {
+ $validCodes = array_keys( Language::getLanguageNames() );
+ if ( in_array( $code, $validCodes ) ) {
+ $message = Language::getMessageFor( $mkey, $code );
+ if ( is_null( $message ) ) {
+ $message = false;
+ }
+ } else {
+ wfDebug( __METHOD__ . ": Invalid code $code for $mkey/$code, not trying messages array\n" );
}
- } else {
- $message = false;
}
}
@@ -500,9 +496,6 @@ class MessageCache {
if( $message === false ) {
return '<' . htmlspecialchars($key) . '>';
}
-
- # Replace brace tags
- $message = $this->transform( $message );
return $message;
}
@@ -576,7 +569,7 @@ class MessageCache {
return $message;
}
- function transform( $message ) {
+ function transform( $message, $interface = false ) {
global $wgParser;
if ( !$this->mParser && isset( $wgParser ) ) {
# Do some initialisation so that we don't have to do it twice
@@ -584,9 +577,11 @@ class MessageCache {
# Clone it and store it
$this->mParser = clone $wgParser;
}
- if ( !$this->mDisableTransform && $this->mParser ) {
+ if ( $this->mParser ) {
if( strpos( $message, '{{' ) !== false ) {
- $message = $this->mParser->transformMsg( $message, $this->getParserOptions() );
+ $popts = $this->getParserOptions();
+ $popts->setInterfaceMessage( $interface );
+ $message = $this->mParser->transformMsg( $message, $popts );
}
}
return $message;
@@ -594,10 +589,12 @@ class MessageCache {
function disable() { $this->mDisable = true; }
function enable() { $this->mDisable = false; }
- function disableTransform() { $this->mDisableTransform = true; }
- function enableTransform() { $this->mDisableTransform = false; }
- function setTransform( $x ) { $this->mDisableTransform = $x; }
- function getTransform() { return $this->mDisableTransform; }
+
+ /** @deprecated */
+ function disableTransform() {}
+ function enableTransform() {}
+ function setTransform( $x ) {}
+ function getTransform() { return false; }
/**
* Add a message to the cache
@@ -618,6 +615,9 @@ class MessageCache {
*/
function addMessages( $messages, $lang = 'en' ) {
wfProfileIn( __METHOD__ );
+ if ( !is_array( $messages ) ) {
+ throw new MWException( __METHOD__.': Invalid message array' );
+ }
if ( isset( $this->mExtensionMessages[$lang] ) ) {
$this->mExtensionMessages[$lang] = $messages + $this->mExtensionMessages[$lang];
} else {
@@ -640,7 +640,8 @@ class MessageCache {
}
/**
- * Get the extension messages for a specific language
+ * Get the extension messages for a specific language. Only English, interface
+ * and content language are guaranteed to be loaded.
*
* @param string $lang The messages language, English by default
*/
@@ -695,9 +696,28 @@ class MessageCache {
* Load messages from a given file
*/
function loadMessagesFile( $filename ) {
- $magicWords = false;
+ global $wgLang, $wgContLang;
+ $messages = $magicWords = false;
require( $filename );
- $this->addMessagesByLang( $messages );
+
+ /*
+ * 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 );
+ }
if ( $magicWords !== false ) {
global $wgContLang;
@@ -705,4 +725,3 @@ class MessageCache {
}
}
}
-