2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2003-12-14 14:32:19 +00:00
|
|
|
|
|
|
|
|
# Message cache
|
|
|
|
|
# Performs various useful MediaWiki namespace-related functions
|
|
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
define( 'MSG_LOAD_TIMEOUT', 60);
|
|
|
|
|
define( 'MSG_LOCK_TIMEOUT', 10);
|
|
|
|
|
define( 'MSG_WAIT_TIMEOUT', 10);
|
2003-12-14 14:32:19 +00:00
|
|
|
|
|
|
|
|
class MessageCache
|
|
|
|
|
{
|
|
|
|
|
var $mCache, $mUseCache, $mDisable, $mExpiry;
|
2004-04-05 10:38:40 +00:00
|
|
|
var $mMemcKey, $mKeys, $mParserOptions, $mParser;
|
2004-05-15 03:36:39 +00:00
|
|
|
var $mExtensionMessages;
|
|
|
|
|
|
2004-01-07 13:05:27 +00:00
|
|
|
var $mInitialised = false;
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2004-05-09 05:12:55 +00:00
|
|
|
function initialise( &$memCached, $useDB, $expiry, $memcPrefix ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
$fname = 'MessageCache::initialise';
|
2004-08-19 08:44:13 +00:00
|
|
|
wfProfileIn( $fname );
|
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;
|
2004-08-22 17:24:50 +00:00
|
|
|
$this->mMemcKey = $memcPrefix.':messages';
|
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;
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileIn( $fname.'-parseropt' );
|
2004-04-05 10:38:40 +00:00
|
|
|
$this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileOut( $fname.'-parseropt' );
|
|
|
|
|
wfProfileIn( $fname.'-parser' );
|
2004-04-05 10:38:40 +00:00
|
|
|
$this->mParser = new Parser;
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileOut( $fname.'-parser' );
|
2004-04-05 10:38:40 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
$this->load();
|
2004-08-19 08:44:13 +00:00
|
|
|
wfProfileOut( $fname );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Loads messages either from memcached or the database, if not disabled
|
|
|
|
|
# On error, quietly switches to a fallback mode
|
|
|
|
|
# Returns false for a reportable error, true otherwise
|
|
|
|
|
function load() {
|
2004-05-09 05:12:55 +00:00
|
|
|
global $wgAllMessagesEn;
|
2003-12-14 14:32:19 +00:00
|
|
|
|
|
|
|
|
if ( $this->mDisable ) {
|
2004-07-10 03:09:26 +00:00
|
|
|
wfDebug( "MessageCache::load(): disabled\n" );
|
2003-12-14 14:32:19 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2004-08-22 17:24:50 +00:00
|
|
|
$fname = 'MessageCache::load';
|
2004-08-19 08:44:13 +00:00
|
|
|
wfProfileIn( $fname );
|
2003-12-14 14:32:19 +00:00
|
|
|
$success = true;
|
|
|
|
|
|
|
|
|
|
if ( $this->mUseCache ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileIn( $fname.'-fromcache' );
|
2004-05-09 05:12:55 +00:00
|
|
|
$this->mCache = $this->mMemc->get( $this->mMemcKey );
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileOut( $fname.'-fromcache' );
|
2003-12-14 14:32:19 +00:00
|
|
|
|
|
|
|
|
# If there's nothing in memcached, load all the messages from the database
|
|
|
|
|
if ( !$this->mCache ) {
|
2004-07-10 03:09:26 +00:00
|
|
|
wfDebug( "MessageCache::load(): loading all messages\n" );
|
2003-12-14 14:32:19 +00:00
|
|
|
$this->lock();
|
|
|
|
|
# Other threads don't need to load the messages if another thread is doing it.
|
2004-07-18 08:48:43 +00:00
|
|
|
$success = $this->mMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
|
|
|
|
|
if ( $success ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileIn( $fname.'-load' );
|
2004-07-18 08:48:43 +00:00
|
|
|
$this->loadFromDB();
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileOut( $fname.'-load' );
|
2004-07-18 08:48:43 +00:00
|
|
|
# Save in memcached
|
|
|
|
|
# Keep trying if it fails, this is kind of important
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileIn( $fname.'-save' );
|
2004-07-18 08:48:43 +00:00
|
|
|
for ( $i=0; $i<20 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
|
|
|
|
|
usleep(mt_rand(500000,1500000));
|
|
|
|
|
}
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileOut( $fname.'-save' );
|
2004-07-18 08:48:43 +00:00
|
|
|
if ( $i == 20 ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
$this->mMemc->set( $this->mMemcKey, 'error', 86400 );
|
2004-07-18 08:48:43 +00:00
|
|
|
wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
|
|
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
$this->unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_array( $this->mCache ) ) {
|
2004-07-10 03:09:26 +00:00
|
|
|
wfMsg( "MessageCache::load(): individual message mode\n" );
|
2003-12-14 14:32:19 +00:00
|
|
|
# If it is 'loading' or 'error', switch to individual message mode, otherwise disable
|
2004-07-10 01:15:23 +00:00
|
|
|
# Causing too much DB load, disabling -- TS
|
|
|
|
|
$this->mDisable = true;
|
|
|
|
|
/*
|
2003-12-14 14:32:19 +00:00
|
|
|
if ( $this->mCache == "loading" ) {
|
|
|
|
|
$this->mUseCache = false;
|
|
|
|
|
} elseif ( $this->mCache == "error" ) {
|
|
|
|
|
$this->mUseCache = false;
|
|
|
|
|
$success = false;
|
|
|
|
|
} else {
|
|
|
|
|
$this->mDisable = true;
|
|
|
|
|
$success = false;
|
2004-07-10 01:15:23 +00:00
|
|
|
}*/
|
2003-12-14 14:32:19 +00:00
|
|
|
$this->mCache = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-08-19 08:44:13 +00:00
|
|
|
wfProfileOut( $fname );
|
2003-12-14 14:32:19 +00:00
|
|
|
return $success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Loads all cacheable messages from the database
|
|
|
|
|
function loadFromDB()
|
|
|
|
|
{
|
2004-08-22 17:24:50 +00:00
|
|
|
$fname = 'MessageCache::loadFromDB';
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbr =& wfGetDB( DB_SLAVE );
|
2004-07-10 03:09:26 +00:00
|
|
|
$res = $dbr->select( 'cur',
|
|
|
|
|
array( 'cur_title', 'cur_text' ),
|
|
|
|
|
array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI ),
|
|
|
|
|
$fname
|
|
|
|
|
);
|
|
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
$this->mCache = array();
|
2004-07-10 03:09:26 +00:00
|
|
|
for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
|
2003-12-14 14:32:19 +00:00
|
|
|
$this->mCache[$row->cur_title] = $row->cur_text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-10 03:09:26 +00:00
|
|
|
$dbr->freeResult( $res );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
# Not really needed anymore
|
2003-12-14 14:32:19 +00:00
|
|
|
function getKeys() {
|
2004-01-25 05:57:02 +00:00
|
|
|
global $wgAllMessagesEn, $wgLang;
|
2003-12-14 14:32:19 +00:00
|
|
|
if ( !$this->mKeys ) {
|
2004-01-25 08:49:45 +00:00
|
|
|
$this->mKeys = array();
|
|
|
|
|
foreach ( $wgAllMessagesEn as $key => $value ) {
|
2004-01-25 12:38:44 +00:00
|
|
|
array_push( $this->mKeys, $wgLang->ucfirst( $key ) );
|
2004-01-25 08:49:45 +00:00
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
return $this->mKeys;
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
# Obsolete
|
2003-12-14 14:32:19 +00:00
|
|
|
function isCacheable( $key ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
return true;
|
|
|
|
|
/*
|
2004-01-25 05:57:02 +00:00
|
|
|
global $wgAllMessagesEn, $wgLang;
|
|
|
|
|
return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
|
2003-12-14 14:32:19 +00:00
|
|
|
array_key_exists( $key, $wgAllMessagesEn );
|
2004-03-20 15:03:26 +00:00
|
|
|
*/
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function replace( $title, $text ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
$this->lock();
|
|
|
|
|
$this->load();
|
|
|
|
|
if ( is_array( $this->mCache ) ) {
|
|
|
|
|
$this->mCache[$title] = $text;
|
2004-05-09 05:12:55 +00:00
|
|
|
$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2004-03-20 15:03:26 +00:00
|
|
|
$this->unlock();
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Returns success
|
|
|
|
|
# Represents a write lock on the messages key
|
|
|
|
|
function lock() {
|
|
|
|
|
if ( !$this->mUseCache ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
$lockKey = $this->mMemcKey . '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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $i >= MSG_WAIT_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unlock() {
|
|
|
|
|
if ( !$this->mUseCache ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
$lockKey = $this->mMemcKey . 'lock';
|
2004-05-09 05:12:55 +00:00
|
|
|
$this->mMemc->delete( $lockKey );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get( $key, $useDB ) {
|
|
|
|
|
global $wgLang, $wgLanguageCode;
|
2004-01-07 13:05:27 +00:00
|
|
|
|
|
|
|
|
# If uninitialised, someone is trying to call this halfway through Setup.php
|
|
|
|
|
if ( !$this->mInitialised ) {
|
|
|
|
|
return "<$key>";
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
$message = false;
|
2004-08-30 05:11:21 +00:00
|
|
|
if ( !$this->mDisable && $useDB ) {
|
2004-05-15 03:36:39 +00:00
|
|
|
$title = $wgLang->ucfirst( $key );
|
|
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2004-05-15 03:36:39 +00:00
|
|
|
# Try the cache
|
|
|
|
|
if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
|
|
|
|
|
$message = $this->mCache[$title];
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2004-05-15 03:36:39 +00:00
|
|
|
|
|
|
|
|
# If it wasn't in the cache, load each message from the DB individually
|
2004-08-30 05:11:21 +00:00
|
|
|
if ( !$message ) {
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbr =& wfGetDB( DB_SLAVE );
|
2004-08-22 17:24:50 +00:00
|
|
|
$result = $dbr->getArray( 'cur', array('cur_text'),
|
|
|
|
|
array( 'cur_namespace' => NS_MEDIAWIKI, 'cur_title' => $title ),
|
|
|
|
|
'MessageCache::get' );
|
2004-05-15 03:36:39 +00:00
|
|
|
if ( $result ) {
|
|
|
|
|
$message = $result->cur_text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# Try the extension array
|
|
|
|
|
if ( !$message ) {
|
|
|
|
|
$message = @$this->mExtensionMessages[$key];
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Try the array in $wgLang
|
|
|
|
|
if ( !$message ) {
|
|
|
|
|
$message = $wgLang->getMessage( $key );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Try the English array
|
2004-08-22 17:24:50 +00:00
|
|
|
if ( !$message && $wgLanguageCode != 'en' ) {
|
2003-12-14 14:32:19 +00:00
|
|
|
$message = Language::getMessage( $key );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Final fallback
|
|
|
|
|
if ( !$message ) {
|
|
|
|
|
$message = "<$key>";
|
|
|
|
|
}
|
2004-04-05 10:38:40 +00:00
|
|
|
|
|
|
|
|
# Replace brace tags
|
|
|
|
|
$message = $this->transform( $message );
|
2003-12-14 14:32:19 +00:00
|
|
|
return $message;
|
|
|
|
|
}
|
2004-03-01 05:51:55 +00:00
|
|
|
|
2004-04-05 10:38:40 +00:00
|
|
|
function transform( $message ) {
|
2004-05-03 10:21:26 +00:00
|
|
|
if( !$this->mDisableTransform ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
if ( strstr( $message, '{{' ) !== false ) {
|
2004-05-03 10:21:26 +00:00
|
|
|
$message = $this->mParser->transformMsg( $message, $this->mParserOptions );
|
|
|
|
|
}
|
2004-04-05 10:38:40 +00:00
|
|
|
}
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-01 05:51:55 +00:00
|
|
|
function disable() { $this->mDisable = true; }
|
|
|
|
|
function enable() { $this->mDisable = false; }
|
2004-05-03 10:21:26 +00:00
|
|
|
function disableTransform() { $this->mDisableTransform = true; }
|
2004-03-01 05:51:55 +00:00
|
|
|
|
2004-05-15 03:36:39 +00:00
|
|
|
function addMessage( $key, $value ) {
|
|
|
|
|
$this->mExtensionMessages[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addMessages( $messages ) {
|
|
|
|
|
foreach ( $messages as $key => $value ) {
|
|
|
|
|
$this->mExtensionMessages[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-08-11 02:31:47 +00:00
|
|
|
|
|
|
|
|
# Clear all stored messages. Mainly used after a mass rebuild.
|
|
|
|
|
function clear() {
|
|
|
|
|
if( $this->mUseCache ) {
|
|
|
|
|
$this->mMemc->delete( $this->mMemcKey );
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
?>
|