2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2004-08-22 17:24:50 +00:00
|
|
|
define( 'MSG_LOAD_TIMEOUT', 60);
|
|
|
|
|
define( 'MSG_LOCK_TIMEOUT', 10);
|
|
|
|
|
define( 'MSG_WAIT_TIMEOUT', 10);
|
2007-01-05 18:08:29 +00:00
|
|
|
define( 'MSG_CACHE_VERSION', 1 );
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Message cache
|
2007-01-05 18:08:29 +00:00
|
|
|
* Performs various MediaWiki namespace-related functions
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Cache
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2005-12-04 18:27:59 +00:00
|
|
|
class MessageCache {
|
2008-07-05 15:39:10 +00:00
|
|
|
// Holds loaded messages that are defined in MediaWiki namespace.
|
|
|
|
|
var $mCache;
|
|
|
|
|
|
|
|
|
|
var $mUseCache, $mDisable, $mExpiry;
|
|
|
|
|
var $mKeys, $mParserOptions, $mParser;
|
2006-05-11 22:40:38 +00:00
|
|
|
var $mExtensionMessages = array();
|
|
|
|
|
var $mInitialised = false;
|
2008-07-05 15:39:10 +00:00
|
|
|
var $mAllMessagesLoaded; // Extension messages
|
2004-09-25 02:23:04 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
// Variable for tracking which variables are loaded
|
|
|
|
|
var $mLoadedLanguages = array();
|
2004-09-25 02:23:04 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
function __construct( &$memCached, $useDB, $expiry, /*ignored*/ $memcPrefix ) {
|
2004-05-09 05:12:55 +00:00
|
|
|
$this->mUseCache = !is_null( $memCached );
|
|
|
|
|
$this->mMemc = &$memCached;
|
2003-12-14 14:32:19 +00:00
|
|
|
$this->mDisable = !$useDB;
|
|
|
|
|
$this->mExpiry = $expiry;
|
2004-05-03 10:21:26 +00:00
|
|
|
$this->mDisableTransform = false;
|
2003-12-14 14:32:19 +00:00
|
|
|
$this->mKeys = false; # initialised on demand
|
2004-01-07 13:05:27 +00:00
|
|
|
$this->mInitialised = true;
|
2006-07-03 11:17:27 +00:00
|
|
|
$this->mParser = null;
|
2008-06-30 03:02:06 +00:00
|
|
|
}
|
2008-06-03 20:41:57 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ParserOptions is lazy initialised.
|
|
|
|
|
* Access should probably be protected.
|
|
|
|
|
*/
|
2006-07-26 07:15:39 +00:00
|
|
|
function getParserOptions() {
|
|
|
|
|
if ( !$this->mParserOptions ) {
|
|
|
|
|
$this->mParserOptions = new ParserOptions;
|
|
|
|
|
}
|
|
|
|
|
return $this->mParserOptions;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-07 13:09:30 +00:00
|
|
|
/**
|
2008-07-05 15:39:10 +00:00
|
|
|
* Try to load the cache from a local file.
|
|
|
|
|
* Actual format of the file depends on the $wgLocalMessageCacheSerialized
|
|
|
|
|
* setting.
|
|
|
|
|
*
|
|
|
|
|
* @param $hash String: the hash of contents, to check validity.
|
|
|
|
|
* @param $code Mixed: Optional language code, see documenation of load().
|
|
|
|
|
* @return false on failure.
|
2005-11-08 11:54:04 +00:00
|
|
|
*/
|
2008-07-05 15:39:10 +00:00
|
|
|
function loadFromLocal( $hash, $code ) {
|
2008-02-02 18:15:57 +00:00
|
|
|
global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
|
2005-11-08 11:54:04 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
$filename = "$wgLocalMessageCache/messages-" . wfWikiID() . "-$code";
|
2005-11-08 11:54:04 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# Check file existence
|
2005-12-30 08:50:59 +00:00
|
|
|
wfSuppressWarnings();
|
2005-11-08 11:54:04 +00:00
|
|
|
$file = fopen( $filename, 'r' );
|
2005-12-30 08:50:59 +00:00
|
|
|
wfRestoreWarnings();
|
2005-11-08 11:54:04 +00:00
|
|
|
if ( !$file ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
return false; // No cache file
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
2008-02-02 18:15:57 +00:00
|
|
|
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
|
2008-02-13 05:46:43 +00:00
|
|
|
$serialized = '';
|
|
|
|
|
while ( !feof( $file ) ) {
|
|
|
|
|
$serialized .= fread( $file, 100000 );
|
|
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
fclose( $file );
|
|
|
|
|
return $this->setCache( unserialize( $serialized ), $code );
|
|
|
|
|
} else {
|
|
|
|
|
fclose( $file );
|
|
|
|
|
return false; // Wrong hash
|
2008-02-02 18:15:57 +00:00
|
|
|
}
|
2008-02-10 14:29:17 +00:00
|
|
|
} else {
|
|
|
|
|
$localHash=substr(fread($file,40),8);
|
|
|
|
|
fclose($file);
|
|
|
|
|
if ($hash!=$localHash) {
|
2008-07-05 15:39:10 +00:00
|
|
|
return false; // Wrong hash
|
2008-02-10 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# Require overwrites the member variable or just shadows it?
|
|
|
|
|
require( $filename );
|
|
|
|
|
return $this->setCache( $this->mCache, $code );
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-07-05 15:39:10 +00:00
|
|
|
* Save the cache to a local file.
|
2005-11-08 11:54:04 +00:00
|
|
|
*/
|
2008-07-05 15:39:10 +00:00
|
|
|
function saveToLocal( $serialized, $hash, $code ) {
|
|
|
|
|
global $wgLocalMessageCache;
|
2008-05-23 22:14:32 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
$filename = "$wgLocalMessageCache/messages-" . wfWikiID() . "-$code";
|
|
|
|
|
wfMkdirParents( $wgLocalMessageCache, 0777 ); // might fail
|
2005-11-08 11:54:04 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
wfSuppressWarnings();
|
2005-11-08 11:54:04 +00:00
|
|
|
$file = fopen( $filename, 'w' );
|
2008-07-05 15:39:10 +00:00
|
|
|
wfRestoreWarnings();
|
|
|
|
|
|
2005-11-08 11:54:04 +00:00
|
|
|
if ( !$file ) {
|
|
|
|
|
wfDebug( "Unable to open local cache file for writing\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fwrite( $file, $hash . $serialized );
|
|
|
|
|
fclose( $file );
|
2006-01-04 23:24:25 +00:00
|
|
|
@chmod( $filename, 0666 );
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
function saveToScript( $array, $hash, $code ) {
|
2008-06-30 03:02:06 +00:00
|
|
|
global $wgLocalMessageCache;
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
$filename = "$wgLocalMessageCache/messages-" . wfWikiID() . "-$code";
|
|
|
|
|
$tempFilename = $filename . '.tmp';
|
|
|
|
|
wfMkdirParents( $wgLocalMessageCache, 0777 ); // might fail
|
|
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$file = fopen( $tempFilename, 'w');
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
|
|
|
|
|
if ( !$file ) {
|
|
|
|
|
wfDebug( "Unable to open local cache file for writing\n" );
|
2008-06-01 03:27:48 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2006-05-28 18:29:42 +00:00
|
|
|
|
|
|
|
|
fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-05-28 18:29:42 +00:00
|
|
|
foreach ($array as $key => $message) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$key = $this->escapeForScript($key);
|
|
|
|
|
$messages = $this->escapeForScript($message);
|
|
|
|
|
fwrite($file, "'$key' => '$message',\n");
|
2006-05-28 18:29:42 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2006-05-28 18:29:42 +00:00
|
|
|
fwrite($file,");\n?>");
|
|
|
|
|
fclose($file);
|
2008-07-05 15:39:10 +00:00
|
|
|
rename($tempFilename, $filename);
|
2006-05-28 18:29:42 +00:00
|
|
|
}
|
2005-11-08 11:54:04 +00:00
|
|
|
|
2006-05-29 05:49:03 +00:00
|
|
|
function escapeForScript($string) {
|
|
|
|
|
$string = str_replace( '\\', '\\\\', $string );
|
|
|
|
|
$string = str_replace( '\'', '\\\'', $string );
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
/**
|
|
|
|
|
* Set the cache to $cache, if it is valid. Otherwise set the cache to false.
|
|
|
|
|
*/
|
2008-07-05 15:39:10 +00:00
|
|
|
function setCache( $cache, $code ) {
|
2007-01-05 18:08:29 +00:00
|
|
|
if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$this->mCache[$code] = $cache;
|
|
|
|
|
return true;
|
2007-01-05 18:08:29 +00:00
|
|
|
} else {
|
2008-07-05 15:39:10 +00:00
|
|
|
return false;
|
2007-01-05 18:08:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2008-07-05 15:39:10 +00:00
|
|
|
* Loads messages from caches or from database in this order:
|
|
|
|
|
* (1) local message cache (if $wgLocalMessageCache 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 mDisabled 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.
|
|
|
|
|
*
|
|
|
|
|
* @param $code String: language to which load messages
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2008-07-05 15:39:10 +00:00
|
|
|
function load( $code = false ) {
|
|
|
|
|
global $wgLocalMessageCache;
|
|
|
|
|
|
|
|
|
|
if ( !$this->mUseCache ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( !is_string( $code ) ) {
|
|
|
|
|
# This isn't really nice, so at least make a note about it and try to
|
|
|
|
|
# fall back
|
|
|
|
|
wfDebug( __METHOD__ . " called without providing a language code\n" );
|
|
|
|
|
$code = 'en';
|
|
|
|
|
}
|
2008-06-03 20:41:57 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# Don't do double loading...
|
|
|
|
|
if ( isset($this->mLoadedLanguages[$code]) ) return true;
|
|
|
|
|
|
|
|
|
|
# 8 lines of code just to say (once) that message cache is disabled
|
2003-12-14 14:32:19 +00:00
|
|
|
if ( $this->mDisable ) {
|
2005-05-28 11:07:55 +00:00
|
|
|
static $shownDisabled = false;
|
|
|
|
|
if ( !$shownDisabled ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
wfDebug( __METHOD__ . ": disabled\n" );
|
2005-05-28 11:07:55 +00:00
|
|
|
$shownDisabled = true;
|
|
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# Loading code starts
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
$success = false; # Keep track of success
|
|
|
|
|
$where = array(); # Debug info, delayed to avoid spamming debug log too much
|
|
|
|
|
$cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
|
2005-11-08 11:54:04 +00:00
|
|
|
|
2008-06-01 03:27:48 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# (1) local cache
|
|
|
|
|
# Hash of the contents is stored in memcache, to detect if local cache goes
|
|
|
|
|
# out of date (due to update in other thread?)
|
2008-02-02 18:15:57 +00:00
|
|
|
if ( $wgLocalMessageCache !== false ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
wfProfileIn( __METHOD__ . '-fromlocal' );
|
|
|
|
|
|
|
|
|
|
$hash = $this->mMemc->get( wfMemcKey( 'messages', $code, 'hash' ) );
|
2008-02-02 18:15:57 +00:00
|
|
|
if ( $hash ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$success = $this->loadFromLocal( $hash, $code );
|
|
|
|
|
if ( $success ) $where[] = 'got from local cache';
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-fromlocal' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# (2) memcache
|
|
|
|
|
# Fails if nothing in cache, or in the wrong version.
|
|
|
|
|
if ( !$success ) {
|
|
|
|
|
wfProfileIn( __METHOD__ . '-fromcache' );
|
|
|
|
|
$cache = $this->mMemc->get( $cacheKey );
|
|
|
|
|
$success = $this->setCache( $cache, $code );
|
|
|
|
|
if ( $success ) {
|
|
|
|
|
$where[] = 'got from global cache';
|
|
|
|
|
$this->saveToCaches( $cache, false, $code );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-fromcache' );
|
2008-06-01 03:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# (3)
|
|
|
|
|
# Nothing in caches... so we need create one and store it in caches
|
|
|
|
|
if ( !$success ) {
|
|
|
|
|
$where[] = 'cache is empty';
|
|
|
|
|
$where[] = 'loading from database';
|
2008-06-01 03:27:48 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
$this->lock($cacheKey);
|
2008-06-01 03:27:48 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
$cache = $this->loadFromDB( $code );
|
|
|
|
|
$success = $this->setCache( $cache, $code );
|
|
|
|
|
if ( $success ) {
|
|
|
|
|
$this->saveToCaches( $cache, true, $code );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
$this->unlock($cacheKey);
|
2007-01-05 18:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( !$success ) {
|
|
|
|
|
# Bad luck... this should not happen
|
|
|
|
|
$where[] = 'loading FAILED - cache is disabled';
|
|
|
|
|
$info = implode( ', ', $where );
|
|
|
|
|
wfDebug( __METHOD__ . ": Loading $code... $info\n" );
|
2007-01-05 18:08:29 +00:00
|
|
|
$this->mDisable = true;
|
|
|
|
|
$this->mCache = false;
|
2008-07-05 15:39:10 +00:00
|
|
|
} else {
|
|
|
|
|
# All good, just record the success
|
|
|
|
|
$info = implode( ', ', $where );
|
|
|
|
|
wfDebug( __METHOD__ . ": Loading $code... $info\n" );
|
|
|
|
|
$this->mLoadedLanguages[$code] = true;
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2003-12-14 14:32:19 +00:00
|
|
|
return $success;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2008-07-05 15:39:10 +00:00
|
|
|
* Loads cacheable messages from the database. Messages bigger than
|
|
|
|
|
* $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
|
|
|
|
|
* on-demand from the database later.
|
|
|
|
|
*
|
|
|
|
|
* @param $code Optional language code, see documenation of load().
|
|
|
|
|
* @return Array: Loaded messages for storing in caches.
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2008-07-05 15:39:10 +00:00
|
|
|
function loadFromDB( $code = false ) {
|
2007-01-05 18:08:29 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2008-07-05 15:39:10 +00:00
|
|
|
global $wgMaxMsgCacheEntrySize, $wgContLanguageCode;
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2008-07-05 15:39:10 +00:00
|
|
|
$cache = array();
|
|
|
|
|
|
|
|
|
|
# Common conditions
|
|
|
|
|
$conds = array(
|
|
|
|
|
'page_is_redirect' => 0,
|
|
|
|
|
'page_namespace' => NS_MEDIAWIKI,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $code ) {
|
|
|
|
|
# Is this fast enough. Should not matter if the filtering is done in the
|
|
|
|
|
# database or in code.
|
|
|
|
|
if ( $code !== $wgContLanguageCode ) {
|
|
|
|
|
# Messages for particular language
|
|
|
|
|
$escapedCode = $dbr->escapeLike( $code );
|
|
|
|
|
$conds[] = "page_title like '%%/$escapedCode'";
|
|
|
|
|
} else {
|
|
|
|
|
# Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
|
|
|
|
|
# other than language code.
|
|
|
|
|
$conds[] = "page_title not like '%%/%%'";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Conditions to fetch oversized pages to ignore them
|
|
|
|
|
$bigConds = $conds;
|
|
|
|
|
$bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
# Load titles for all oversized pages in the MediaWiki namespace
|
2008-07-05 15:39:10 +00:00
|
|
|
$res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ );
|
2007-01-05 18:08:29 +00:00
|
|
|
while ( $row = $dbr->fetchObject( $res ) ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$cache[$row->page_title] = '!TOO BIG';
|
2005-03-19 10:40:41 +00:00
|
|
|
}
|
2007-01-05 18:08:29 +00:00
|
|
|
$dbr->freeResult( $res );
|
2006-06-25 08:38:17 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
# Conditions to load the remaining pages with their contents
|
|
|
|
|
$smallConds = $conds;
|
|
|
|
|
$smallConds[] = 'page_latest=rev_id';
|
|
|
|
|
$smallConds[] = 'rev_text_id=old_id';
|
|
|
|
|
$smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
|
|
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
$res = $dbr->select( array( 'page', 'revision', 'text' ),
|
|
|
|
|
array( 'page_title', 'old_text', 'old_flags' ),
|
2008-07-05 15:39:10 +00:00
|
|
|
$smallConds, __METHOD__ );
|
2006-06-25 08:38:17 +00:00
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$cache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
2005-09-11 14:50:47 +00:00
|
|
|
$dbr->freeResult( $res );
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
$cache['VERSION'] = MSG_CACHE_VERSION;
|
2007-01-05 18:08:29 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2008-07-05 15:39:10 +00:00
|
|
|
return $cache;
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
/**
|
|
|
|
|
* Updates cache as necessary when message page is changed
|
|
|
|
|
*
|
|
|
|
|
* @param $title String: name of the page changed.
|
|
|
|
|
* @param $text Mixed: new contents of the page.
|
|
|
|
|
*/
|
|
|
|
|
public function replace( $title, $text ) {
|
2008-06-01 03:27:48 +00:00
|
|
|
global $wgMaxMsgCacheEntrySize;
|
2008-06-30 03:02:06 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
list( , $code ) = $this->figureMessage( $title );
|
|
|
|
|
|
|
|
|
|
$cacheKey = wfMemcKey( 'messages', $code );
|
|
|
|
|
$this->load($code);
|
|
|
|
|
$this->lock($cacheKey);
|
|
|
|
|
|
|
|
|
|
if ( is_array($this->mCache[$code]) ) {
|
|
|
|
|
$titleKey = wfMemcKey( 'messages', 'individual', $title );
|
|
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
if ( $text === false ) {
|
|
|
|
|
# Article was deleted
|
2008-07-05 15:39:10 +00:00
|
|
|
unset( $this->mCache[$code][$title] );
|
|
|
|
|
$this->mMemc->delete( $titleKey );
|
|
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
} elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
# Check for size
|
|
|
|
|
$this->mCache[$code][$title] = '!TOO BIG';
|
|
|
|
|
$this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
|
|
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
} else {
|
2008-07-05 15:39:10 +00:00
|
|
|
$this->mCache[$code][$title] = ' ' . $text;
|
|
|
|
|
$this->mMemc->delete( $titleKey );
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
# Update caches
|
|
|
|
|
$this->saveToCaches( $this->mCache[$code], true, $code );
|
|
|
|
|
}
|
|
|
|
|
$this->unlock($cacheKey);
|
|
|
|
|
|
|
|
|
|
// Also delete cached sidebar... just in case it is affected
|
|
|
|
|
global $parserMemc;
|
|
|
|
|
$sidebarKey = wfMemcKey( 'sidebar', $code );
|
|
|
|
|
$parserMemc->delete( $sidebarKey );
|
|
|
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shortcut to update caches.
|
|
|
|
|
*
|
|
|
|
|
* @param $cache Array: cached messages with a version.
|
|
|
|
|
* @param $cacheKey String: Identifier for the cache.
|
|
|
|
|
* @param $memc Bool: Wether to update or not memcache.
|
|
|
|
|
* @param $code String: Language code.
|
|
|
|
|
* @return False on somekind of error.
|
|
|
|
|
*/
|
|
|
|
|
protected function saveToCaches( $cache, $memc = true, $code = false ) {
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
|
|
|
|
|
|
|
|
|
|
$cacheKey = wfMemcKey( 'messages', $code );
|
|
|
|
|
$statusKey = wfMemcKey( 'messages', $code, 'status' );
|
|
|
|
|
|
|
|
|
|
$success = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
|
|
|
|
|
if ( !$success ) return true; # Other process should be updating them now
|
|
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
|
if ( $memc ) {
|
|
|
|
|
# Save in memcached
|
|
|
|
|
# Keep trying if it fails, this is kind of important
|
|
|
|
|
|
|
|
|
|
for ($i=0; $i<20 &&
|
|
|
|
|
!$this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
|
|
|
|
|
$i++ ) {
|
|
|
|
|
usleep(mt_rand(500000,1500000));
|
2008-06-03 20:41:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
|
|
|
|
# Save to local cache
|
|
|
|
|
if ( $wgLocalMessageCache !== false ) {
|
|
|
|
|
$serialized = serialize( $cache );
|
|
|
|
|
$hash = md5( $serialized );
|
|
|
|
|
$this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry );
|
|
|
|
|
if ($wgLocalMessageCacheSerialized) {
|
|
|
|
|
$this->saveToLocal( $serialized, $hash, $code );
|
|
|
|
|
} else {
|
|
|
|
|
$this->saveToScript( $cache, $hash, $code );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $i == 20 ) {
|
|
|
|
|
$this->mMemc->set( $statusKey, 'error', 60*5 );
|
|
|
|
|
wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
|
|
|
|
|
$success = false;
|
|
|
|
|
} else {
|
|
|
|
|
$this->mMemc->delete( $statusKey );
|
|
|
|
|
$success = true;
|
|
|
|
|
}
|
2007-01-05 18:08:29 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2008-07-05 15:39:10 +00:00
|
|
|
return $success;
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Returns success
|
|
|
|
|
* Represents a write lock on the messages key
|
|
|
|
|
*/
|
2008-07-05 15:39:10 +00:00
|
|
|
function lock($key) {
|
2003-12-14 14:32:19 +00:00
|
|
|
if ( !$this->mUseCache ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
$lockKey = $key . ':lock';
|
2004-05-09 05:12:55 +00:00
|
|
|
for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
|
2003-12-14 14:32:19 +00:00
|
|
|
sleep(1);
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
return $i >= MSG_WAIT_TIMEOUT;
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
function unlock($key) {
|
2003-12-14 14:32:19 +00:00
|
|
|
if ( !$this->mUseCache ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
$lockKey = $key . ':lock';
|
2004-05-09 05:12:55 +00:00
|
|
|
$this->mMemc->delete( $lockKey );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
/**
|
|
|
|
|
* Get a message from either the content language or the user language.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key The message cache key
|
|
|
|
|
* @param bool $useDB Get the message from the DB, false to use only the localisation
|
2008-04-24 11:59:32 +00:00
|
|
|
* @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).
|
2007-01-05 18:08:29 +00:00
|
|
|
* @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
|
|
|
|
|
*/
|
2008-04-27 14:50:55 +00:00
|
|
|
function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
|
2006-07-26 08:26:51 +00:00
|
|
|
global $wgContLanguageCode, $wgContLang, $wgLang;
|
2008-04-24 11:59:32 +00:00
|
|
|
|
|
|
|
|
# Identify which language to get or create a language object for.
|
2008-04-24 13:02:32 +00:00
|
|
|
if( $langcode === $wgContLang->getCode() || $langcode === true ) {
|
2008-04-24 11:59:32 +00:00
|
|
|
# $langcode is the language code of the wikis content language object.
|
2008-04-24 13:02:32 +00:00
|
|
|
# or it is a boolean and value is true
|
2004-11-21 13:56:04 +00:00
|
|
|
$lang =& $wgContLang;
|
2008-04-24 13:02:32 +00:00
|
|
|
} elseif( $langcode === $wgLang->getCode() || $langcode === false ) {
|
2008-04-24 11:59:32 +00:00
|
|
|
# $langcode is the language code of user language object.
|
2008-04-24 13:02:32 +00:00
|
|
|
# or it was a boolean and value is false
|
2004-11-21 13:56:04 +00:00
|
|
|
$lang =& $wgLang;
|
2008-04-24 11:59:32 +00:00
|
|
|
} else {
|
2008-04-24 13:02:32 +00:00
|
|
|
$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.' );
|
|
|
|
|
}
|
2004-09-25 04:15:47 +00:00
|
|
|
}
|
2008-04-24 11:59:32 +00:00
|
|
|
|
2006-07-26 08:26:51 +00:00
|
|
|
$langcode = $lang->getCode();
|
2008-04-24 11:59:32 +00:00
|
|
|
|
2004-01-07 13:05:27 +00:00
|
|
|
# If uninitialised, someone is trying to call this halfway through Setup.php
|
2004-11-21 13:56:04 +00:00
|
|
|
if( !$this->mInitialised ) {
|
2005-05-14 05:42:29 +00:00
|
|
|
return '<' . htmlspecialchars($key) . '>';
|
2004-01-07 13:05:27 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
$message = false;
|
2007-01-05 18:08:29 +00:00
|
|
|
|
2007-01-07 14:22:32 +00:00
|
|
|
# Normalise title-case input
|
2007-01-07 15:43:23 +00:00
|
|
|
$lckey = $wgContLang->lcfirst( $key );
|
2007-01-09 00:00:56 +00:00
|
|
|
$lckey = str_replace( ' ', '_', $lckey );
|
2007-01-07 14:22:32 +00:00
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
# Try the MediaWiki namespace
|
2004-11-21 13:56:04 +00:00
|
|
|
if( !$this->mDisable && $useDB ) {
|
2007-01-09 00:21:55 +00:00
|
|
|
$title = $wgContLang->ucfirst( $lckey );
|
2007-01-05 18:08:29 +00:00
|
|
|
if(!$isFullKey && ($langcode != $wgContLanguageCode) ) {
|
2004-11-21 13:56:04 +00:00
|
|
|
$title .= '/' . $langcode;
|
|
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
$message = $this->getMsgFromNamespace( $title, $langcode );
|
2004-05-15 03:36:39 +00:00
|
|
|
}
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2004-05-15 03:36:39 +00:00
|
|
|
# Try the extension array
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( $message === false && isset( $this->mExtensionMessages[$langcode][$lckey] ) ) {
|
2007-01-07 15:43:23 +00:00
|
|
|
$message = $this->mExtensionMessages[$langcode][$lckey];
|
2007-01-05 18:08:29 +00:00
|
|
|
}
|
2007-01-07 15:43:23 +00:00
|
|
|
if ( $message === false && isset( $this->mExtensionMessages['en'][$lckey] ) ) {
|
|
|
|
|
$message = $this->mExtensionMessages['en'][$lckey];
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-24 00:09:42 +00:00
|
|
|
# Try the array in the language object
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( $message === false ) {
|
2007-01-07 15:43:23 +00:00
|
|
|
$message = $lang->getMessage( $lckey );
|
2005-10-22 20:52:30 +00:00
|
|
|
if ( is_null( $message ) ) {
|
|
|
|
|
$message = false;
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2006-08-07 12:21:06 +00:00
|
|
|
# Try the array of another language
|
2008-02-02 18:15:57 +00:00
|
|
|
$pos = strrpos( $lckey, '/' );
|
|
|
|
|
if( $message === false && $pos !== false) {
|
|
|
|
|
$mkey = substr( $lckey, 0, $pos );
|
|
|
|
|
$code = substr( $lckey, $pos+1 );
|
|
|
|
|
if ( $code ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
# We may get calls for things that are http-urls from sidebar
|
|
|
|
|
# Let's not load nonexistent languages for those
|
2008-02-02 18:15:57 +00:00
|
|
|
$validCodes = array_keys( Language::getLanguageNames() );
|
|
|
|
|
if ( in_array( $code, $validCodes ) ) {
|
|
|
|
|
$message = Language::getMessageFor( $mkey, $code );
|
|
|
|
|
if ( is_null( $message ) ) {
|
|
|
|
|
$message = false;
|
|
|
|
|
}
|
2006-08-10 09:10:06 +00:00
|
|
|
}
|
2006-08-07 12:21:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-13 11:50:54 +00:00
|
|
|
# Is this a custom message? Try the default language in the db...
|
2006-05-31 08:05:38 +00:00
|
|
|
if( ($message === false || $message === '-' ) &&
|
2005-03-13 11:50:54 +00:00
|
|
|
!$this->mDisable && $useDB &&
|
2007-01-05 18:08:29 +00:00
|
|
|
!$isFullKey && ($langcode != $wgContLanguageCode) ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $lckey ), $wgContLanguageCode );
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
2005-07-07 03:08:58 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
# Final fallback
|
2005-09-11 14:50:47 +00:00
|
|
|
if( $message === false ) {
|
2005-05-14 05:42:29 +00:00
|
|
|
return '<' . htmlspecialchars($key) . '>';
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
2005-07-07 03:08:58 +00:00
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Get a message from the MediaWiki namespace, with caching. The key must
|
2007-01-05 18:08:29 +00:00
|
|
|
* first be converted to two-part lang/msg form if necessary.
|
|
|
|
|
*
|
2008-07-05 15:39:10 +00:00
|
|
|
* @param $title String: Message cache key with initial uppercase letter.
|
|
|
|
|
* @param $code String: code denoting the language to try.
|
2007-01-05 18:08:29 +00:00
|
|
|
*/
|
2008-07-05 15:39:10 +00:00
|
|
|
function getMsgFromNamespace( $title, $code ) {
|
2008-06-30 03:02:06 +00:00
|
|
|
$type = false;
|
2008-07-05 15:39:10 +00:00
|
|
|
$message = false;
|
2005-07-07 03:08:58 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( $this->mUseCache ) {
|
|
|
|
|
$this->load( $code );
|
|
|
|
|
if (isset( $this->mCache[$code][$title] ) ) {
|
|
|
|
|
$entry = $this->mCache[$code][$title];
|
|
|
|
|
$type = substr( $entry, 0, 1 );
|
|
|
|
|
if ( $type == ' ' ) {
|
|
|
|
|
return substr( $entry, 1 );
|
|
|
|
|
}
|
2007-01-05 18:08:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Call message hooks, in case they are defined
|
|
|
|
|
wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
|
|
|
|
|
if ( $message !== false ) {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# If there is no cache entry and no placeholder, it doesn't exist
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( $type !== '!' ) {
|
2007-01-05 18:08:29 +00:00
|
|
|
return false;
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
$titleKey = wfMemcKey( 'messages', 'individual', $title );
|
2007-01-05 18:08:29 +00:00
|
|
|
|
|
|
|
|
# Try the individual message cache
|
2005-09-11 14:50:47 +00:00
|
|
|
if ( $this->mUseCache ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$entry = $this->mMemc->get( $titleKey );
|
2007-01-05 18:08:29 +00:00
|
|
|
if ( $entry ) {
|
|
|
|
|
$type = substr( $entry, 0, 1 );
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( $type === ' ' ) {
|
|
|
|
|
# Ok!
|
2007-04-27 16:39:21 +00:00
|
|
|
$message = substr( $entry, 1 );
|
2008-07-05 15:39:10 +00:00
|
|
|
$this->mCache[$code][$title] = $entry;
|
2007-01-05 18:08:29 +00:00
|
|
|
return $message;
|
2008-07-05 15:39:10 +00:00
|
|
|
} elseif ( $entry === '!NONEXISTENT' ) {
|
2007-01-05 18:08:29 +00:00
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
# Corrupt/obsolete entry, delete it
|
2008-07-05 15:39:10 +00:00
|
|
|
$this->mMemc->delete( $titleKey );
|
2007-01-05 18:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-05 18:08:29 +00:00
|
|
|
# Try loading it from the DB
|
2005-09-11 14:50:47 +00:00
|
|
|
$revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
|
|
|
|
|
if( $revision ) {
|
|
|
|
|
$message = $revision->getText();
|
|
|
|
|
if ($this->mUseCache) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$this->mCache[$code][$title] = ' ' . $message;
|
|
|
|
|
$this->mMemc->set( $titleKey, $message, $this->mExpiry );
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
2005-09-11 14:50:47 +00:00
|
|
|
} else {
|
|
|
|
|
# Negative caching
|
2005-09-11 17:33:25 +00:00
|
|
|
# Use some special text instead of false, because false gets converted to '' somewhere
|
2008-07-05 15:39:10 +00:00
|
|
|
$this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
|
|
|
|
|
$this->mCache[$code][$title] = false;
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
2004-03-01 05:51:55 +00:00
|
|
|
|
2007-10-22 19:33:46 +00:00
|
|
|
function transform( $message, $interface = false ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
// Avoid creating parser if nothing to transfrom
|
|
|
|
|
if( strpos( $message, '{{' ) === false ) {
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-03 11:08:02 +00:00
|
|
|
global $wgParser;
|
2006-07-03 11:17:27 +00:00
|
|
|
if ( !$this->mParser && isset( $wgParser ) ) {
|
2006-07-04 09:13:10 +00:00
|
|
|
# Do some initialisation so that we don't have to do it twice
|
|
|
|
|
$wgParser->firstCallInit();
|
|
|
|
|
# Clone it and store it
|
2006-07-03 11:17:27 +00:00
|
|
|
$this->mParser = clone $wgParser;
|
2008-07-05 15:39:10 +00:00
|
|
|
#wfDebug( __METHOD__ . ": following contents triggered transform: $message\n" );
|
2006-07-03 11:17:27 +00:00
|
|
|
}
|
2008-01-19 09:03:45 +00:00
|
|
|
if ( $this->mParser ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$popts = $this->getParserOptions();
|
|
|
|
|
$popts->setInterfaceMessage( $interface );
|
|
|
|
|
$message = $this->mParser->transformMsg( $message, $popts );
|
2004-04-05 10:38:40 +00:00
|
|
|
}
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2004-03-01 05:51:55 +00:00
|
|
|
function disable() { $this->mDisable = true; }
|
|
|
|
|
function enable() { $this->mDisable = false; }
|
2008-07-05 15:39:10 +00:00
|
|
|
|
2008-01-19 09:03:45 +00:00
|
|
|
/** @deprecated */
|
2008-04-02 11:49:55 +00:00
|
|
|
function disableTransform(){
|
|
|
|
|
wfDeprecated( __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
function enableTransform() {
|
|
|
|
|
wfDeprecated( __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
function setTransform( $x ) {
|
|
|
|
|
wfDeprecated( __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
function getTransform() {
|
|
|
|
|
wfDeprecated( __METHOD__ );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2004-03-01 05:51:55 +00:00
|
|
|
|
2005-06-26 15:30:50 +00:00
|
|
|
/**
|
|
|
|
|
* Add a message to the cache
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $key
|
|
|
|
|
* @param mixed $value
|
2006-06-30 20:46:54 +00:00
|
|
|
* @param string $lang The messages language, English by default
|
2005-06-26 15:30:50 +00:00
|
|
|
*/
|
2006-06-30 20:46:54 +00:00
|
|
|
function addMessage( $key, $value, $lang = 'en' ) {
|
2007-01-05 18:08:29 +00:00
|
|
|
$this->mExtensionMessages[$lang][$key] = $value;
|
2004-05-15 03:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
2005-06-26 15:30:50 +00:00
|
|
|
/**
|
|
|
|
|
* Add an associative array of message to the cache
|
|
|
|
|
*
|
|
|
|
|
* @param array $messages An associative array of key => values to be added
|
2006-06-30 20:46:54 +00:00
|
|
|
* @param string $lang The messages language, English by default
|
2005-06-26 15:30:50 +00:00
|
|
|
*/
|
2006-06-30 20:46:54 +00:00
|
|
|
function addMessages( $messages, $lang = 'en' ) {
|
2006-07-02 15:57:59 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2008-01-29 01:04:29 +00:00
|
|
|
if ( !is_array( $messages ) ) {
|
|
|
|
|
throw new MWException( __METHOD__.': Invalid message array' );
|
|
|
|
|
}
|
2007-01-05 18:08:29 +00:00
|
|
|
if ( isset( $this->mExtensionMessages[$lang] ) ) {
|
|
|
|
|
$this->mExtensionMessages[$lang] = $messages + $this->mExtensionMessages[$lang];
|
|
|
|
|
} else {
|
|
|
|
|
$this->mExtensionMessages[$lang] = $messages;
|
2004-05-15 03:36:39 +00:00
|
|
|
}
|
2006-07-02 15:57:59 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2004-05-15 03:36:39 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2007-01-06 18:29:53 +00:00
|
|
|
/**
|
|
|
|
|
* Add a 2-D array of messages by lang. Useful for extensions.
|
|
|
|
|
*
|
|
|
|
|
* @param array $messages The array to be added
|
|
|
|
|
*/
|
|
|
|
|
function addMessagesByLang( $messages ) {
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
foreach ( $messages as $key => $value ) {
|
|
|
|
|
$this->addMessages( $value, $key );
|
|
|
|
|
}
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-03 16:31:28 +00:00
|
|
|
/**
|
2008-02-02 20:33:25 +00:00
|
|
|
* Get the extension messages for a specific language. Only English, interface
|
|
|
|
|
* and content language are guaranteed to be loaded.
|
2006-09-03 16:31:28 +00:00
|
|
|
*
|
|
|
|
|
* @param string $lang The messages language, English by default
|
|
|
|
|
*/
|
|
|
|
|
function getExtensionMessagesFor( $lang = 'en' ) {
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
$messages = array();
|
2007-01-05 18:08:29 +00:00
|
|
|
if ( isset( $this->mExtensionMessages[$lang] ) ) {
|
|
|
|
|
$messages = $this->mExtensionMessages[$lang];
|
|
|
|
|
}
|
|
|
|
|
if ( $lang != 'en' ) {
|
|
|
|
|
$messages = $messages + $this->mExtensionMessages['en'];
|
2006-09-03 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $messages;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Clear all stored messages. Mainly used after a mass rebuild.
|
|
|
|
|
*/
|
2004-08-11 02:31:47 +00:00
|
|
|
function clear() {
|
|
|
|
|
if( $this->mUseCache ) {
|
2008-07-05 15:39:10 +00:00
|
|
|
$langs = Language::getLanguageNames( false );
|
|
|
|
|
foreach ( array_keys($langs) as $code ) {
|
|
|
|
|
# Global cache
|
|
|
|
|
$this->mMemc->delete( wfMemcKey( 'messages', $code ) );
|
|
|
|
|
# Invalidate all local caches
|
|
|
|
|
$this->mMemc->delete( wfMemcKey( 'messages', $code, 'hash' ) );
|
|
|
|
|
}
|
2004-08-11 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-08-24 16:58:44 +00:00
|
|
|
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
function loadAllMessages() {
|
|
|
|
|
global $wgExtensionMessagesFiles;
|
|
|
|
|
if ( $this->mAllMessagesLoaded ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->mAllMessagesLoaded = true;
|
|
|
|
|
|
2006-08-24 16:58:44 +00:00
|
|
|
# Some extensions will load their messages when you load their class file
|
|
|
|
|
wfLoadAllExtensions();
|
|
|
|
|
# Others will respond to this hook
|
|
|
|
|
wfRunHooks( 'LoadAllMessages' );
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
# Some register their messages in $wgExtensionMessagesFiles
|
|
|
|
|
foreach ( $wgExtensionMessagesFiles as $name => $file ) {
|
2008-05-05 15:03:42 +00:00
|
|
|
wfLoadExtensionMessages( $name );
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
}
|
2006-08-24 16:58:44 +00:00
|
|
|
# Still others will respond to neither, they are EVIL. We sometimes need to know!
|
|
|
|
|
}
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load messages from a given file
|
2008-04-24 11:59:32 +00:00
|
|
|
*
|
|
|
|
|
* @param string $filename Filename of file to load.
|
2008-05-05 13:29:51 +00:00
|
|
|
* @param string $langcode Language to load messages for, or false for
|
|
|
|
|
* default behvaiour (en, content language and user
|
|
|
|
|
* language).
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
*/
|
2008-05-05 13:29:51 +00:00
|
|
|
function loadMessagesFile( $filename, $langcode = false ) {
|
2008-02-02 20:33:25 +00:00
|
|
|
global $wgLang, $wgContLang;
|
2007-09-10 22:53:17 +00:00
|
|
|
$messages = $magicWords = false;
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
require( $filename );
|
2007-09-10 22:53:17 +00:00
|
|
|
|
2008-05-05 13:29:51 +00:00
|
|
|
$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()" );
|
2008-04-24 11:59:32 +00:00
|
|
|
} 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 );
|
|
|
|
|
}
|
2007-09-10 22:53:17 +00:00
|
|
|
}
|
2007-09-04 02:48:34 +00:00
|
|
|
|
|
|
|
|
if ( $magicWords !== false ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
$wgContLang->addMagicWordsByLang( $magicWords );
|
|
|
|
|
}
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
}
|
2008-04-24 11:59:32 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Process an array of messages, loading it into the message cache.
|
|
|
|
|
*
|
|
|
|
|
* @param array $messages Messages array.
|
2008-05-05 13:29:51 +00:00
|
|
|
* @param string $langcode Language code to process.
|
2008-04-24 11:59:32 +00:00
|
|
|
*/
|
2008-05-05 13:29:51 +00:00
|
|
|
function processMessagesArray( $messages, $langcode ) {
|
|
|
|
|
$fallbackCode = $langcode;
|
2008-04-24 11:59:32 +00:00
|
|
|
$mergedMessages = array();
|
|
|
|
|
do {
|
|
|
|
|
if ( isset($messages[$fallbackCode]) ) {
|
|
|
|
|
$mergedMessages += $messages[$fallbackCode];
|
|
|
|
|
}
|
|
|
|
|
$fallbackCode = Language::getFallbackfor( $fallbackCode );
|
|
|
|
|
} while( $fallbackCode && $fallbackCode !== 'en' );
|
|
|
|
|
|
|
|
|
|
if ( !empty($mergedMessages) )
|
2008-05-05 13:29:51 +00:00
|
|
|
$this->addMessages( $mergedMessages, $langcode );
|
2008-04-24 11:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
public function figureMessage( $key ) {
|
|
|
|
|
global $wgContLanguageCode;
|
|
|
|
|
$pieces = explode('/', $key, 2);
|
|
|
|
|
|
|
|
|
|
$key = $pieces[0];
|
|
|
|
|
|
|
|
|
|
# Language the user is translating to
|
|
|
|
|
$langCode = isset($pieces[1]) ? $pieces[1] : $wgContLanguageCode;
|
|
|
|
|
return array( $key, $langCode );
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|