2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
*
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2005-04-12 01:29:21 +00:00
|
|
|
* @subpackage 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);
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Message cache
|
|
|
|
|
* Performs various useful MediaWiki namespace-related functions
|
2004-09-03 23:00:01 +00:00
|
|
|
*
|
|
|
|
|
* @package MediaWiki
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2005-12-04 18:27:59 +00:00
|
|
|
class MessageCache {
|
2006-05-11 22:40:38 +00:00
|
|
|
var $mCache, $mUseCache, $mDisable, $mExpiry;
|
|
|
|
|
var $mMemcKey, $mKeys, $mParserOptions, $mParser;
|
|
|
|
|
var $mExtensionMessages = array();
|
|
|
|
|
var $mInitialised = false;
|
|
|
|
|
var $mDeferred = true;
|
2004-09-25 02:23:04 +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-09-25 02:23:04 +00:00
|
|
|
|
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-09-24 13:10:38 +00:00
|
|
|
|
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-09-24 13:10:38 +00:00
|
|
|
|
2004-12-09 05:51:20 +00:00
|
|
|
# When we first get asked for a message,
|
|
|
|
|
# then we'll fill up the cache. If we
|
|
|
|
|
# can return a cache hit, this saves
|
|
|
|
|
# some extra milliseconds
|
|
|
|
|
$this->mDeferred = true;
|
2005-07-07 03:08:58 +00:00
|
|
|
|
2004-08-19 08:44:13 +00:00
|
|
|
wfProfileOut( $fname );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-07 13:09:30 +00:00
|
|
|
/**
|
2005-11-08 11:54:04 +00:00
|
|
|
* Try to load the cache from a local file
|
|
|
|
|
*/
|
|
|
|
|
function loadFromLocal( $hash ) {
|
|
|
|
|
global $wgLocalMessageCache, $wgDBname;
|
|
|
|
|
|
|
|
|
|
$this->mCache = false;
|
|
|
|
|
if ( $wgLocalMessageCache === false ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-11-08 11:54:04 +00:00
|
|
|
$filename = "$wgLocalMessageCache/messages-$wgDBname";
|
|
|
|
|
|
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 ) {
|
|
|
|
|
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, 1000000 );
|
|
|
|
|
$this->mCache = unserialize( $serialized );
|
|
|
|
|
}
|
|
|
|
|
fclose( $file );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save the cache to a local file
|
|
|
|
|
*/
|
|
|
|
|
function saveToLocal( $serialized, $hash ) {
|
|
|
|
|
global $wgLocalMessageCache, $wgDBname;
|
|
|
|
|
|
|
|
|
|
if ( $wgLocalMessageCache === false ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-11-08 11:54:04 +00:00
|
|
|
$filename = "$wgLocalMessageCache/messages-$wgDBname";
|
2006-01-04 23:24:25 +00:00
|
|
|
$oldUmask = umask( 0 );
|
2005-11-08 11:54:04 +00:00
|
|
|
wfMkdirParents( $wgLocalMessageCache, 0777 );
|
2006-01-04 23:24:25 +00:00
|
|
|
umask( $oldUmask );
|
2005-11-08 11:54:04 +00:00
|
|
|
|
|
|
|
|
$file = fopen( $filename, 'w' );
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2006-05-28 18:29:42 +00:00
|
|
|
function loadFromScript( $hash ) {
|
|
|
|
|
global $wgLocalMessageCache, $wgDBname;
|
|
|
|
|
if ( $wgLocalMessageCache === false ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$filename = "$wgLocalMessageCache/messages-$wgDBname";
|
|
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$file = fopen( $filename, 'r' );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$file ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$localHash=substr(fread($file,40),8);
|
|
|
|
|
fclose($file);
|
|
|
|
|
if ($hash!=$localHash) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
require("$wgLocalMessageCache/messages-$wgDBname");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function saveToScript($array, $hash) {
|
|
|
|
|
global $wgLocalMessageCache, $wgDBname;
|
|
|
|
|
if ( $wgLocalMessageCache === false ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$filename = "$wgLocalMessageCache/messages-$wgDBname";
|
|
|
|
|
$oldUmask = umask( 0 );
|
|
|
|
|
wfMkdirParents( $wgLocalMessageCache, 0777 );
|
|
|
|
|
umask( $oldUmask );
|
|
|
|
|
$file = fopen( $filename.'.tmp', 'w');
|
|
|
|
|
fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
|
|
|
|
|
|
|
|
|
|
foreach ($array as $key => $message) {
|
2006-05-29 05:49:03 +00:00
|
|
|
fwrite($file, "'". $this->escapeForScript($key).
|
|
|
|
|
"' => '" . $this->escapeForScript($message).
|
|
|
|
|
"',\n");
|
2006-05-28 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
fwrite($file,");\n?>");
|
|
|
|
|
fclose($file);
|
|
|
|
|
rename($filename.'.tmp',$filename);
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +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
|
|
|
|
|
*/
|
2003-12-14 14:32:19 +00:00
|
|
|
function load() {
|
2006-05-28 18:29:42 +00:00
|
|
|
global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
if ( $this->mDisable ) {
|
2005-05-28 11:07:55 +00:00
|
|
|
static $shownDisabled = false;
|
|
|
|
|
if ( !$shownDisabled ) {
|
|
|
|
|
wfDebug( "MessageCache::load(): disabled\n" );
|
|
|
|
|
$shownDisabled = true;
|
|
|
|
|
}
|
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;
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
if ( $this->mUseCache ) {
|
2005-11-08 11:54:04 +00:00
|
|
|
$this->mCache = false;
|
|
|
|
|
|
|
|
|
|
# Try local cache
|
|
|
|
|
wfProfileIn( $fname.'-fromlocal' );
|
|
|
|
|
$hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
|
|
|
|
|
if ( $hash ) {
|
2006-05-28 18:29:42 +00:00
|
|
|
if ($wgLocalMessageCacheSerialized) {
|
|
|
|
|
$this->loadFromLocal( $hash );
|
|
|
|
|
} else {
|
|
|
|
|
$this->loadFromScript( $hash );
|
|
|
|
|
}
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
wfProfileOut( $fname.'-fromlocal' );
|
|
|
|
|
|
|
|
|
|
# Try memcached
|
|
|
|
|
if ( !$this->mCache ) {
|
|
|
|
|
wfProfileIn( $fname.'-fromcache' );
|
|
|
|
|
$this->mCache = $this->mMemc->get( $this->mMemcKey );
|
|
|
|
|
|
|
|
|
|
# Save to local cache
|
2006-01-07 13:09:30 +00:00
|
|
|
if ( $wgLocalMessageCache !== false ) {
|
2005-11-08 11:54:04 +00:00
|
|
|
$serialized = serialize( $this->mCache );
|
|
|
|
|
if ( !$hash ) {
|
|
|
|
|
$hash = md5( $serialized );
|
|
|
|
|
$this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
|
|
|
|
|
}
|
2006-05-28 18:29:42 +00:00
|
|
|
if ($wgLocalMessageCacheSerialized) {
|
|
|
|
|
$this->saveToLocal( $serialized,$hash );
|
|
|
|
|
} else {
|
|
|
|
|
$this->saveToScript( $this->mCache, $hash );
|
|
|
|
|
}
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
wfProfileOut( $fname.'-fromcache' );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-20 14:55:25 +00:00
|
|
|
|
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.
|
2005-07-07 03:08:58 +00:00
|
|
|
$success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
|
2004-07-18 08:48:43 +00:00
|
|
|
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' );
|
2005-11-08 11:54:04 +00:00
|
|
|
|
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' );
|
2005-07-07 03:08:58 +00:00
|
|
|
for ($i=0; $i<20 &&
|
|
|
|
|
!$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
|
|
|
|
|
$i++ ) {
|
2004-07-18 08:48:43 +00:00
|
|
|
usleep(mt_rand(500000,1500000));
|
|
|
|
|
}
|
2005-11-08 11:54:04 +00:00
|
|
|
|
|
|
|
|
# Save to local cache
|
2006-01-07 13:09:30 +00:00
|
|
|
if ( $wgLocalMessageCache !== false ) {
|
2005-11-08 11:54:04 +00:00
|
|
|
$serialized = serialize( $this->mCache );
|
|
|
|
|
$hash = md5( $serialized );
|
|
|
|
|
$this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
|
2006-05-28 18:29:42 +00:00
|
|
|
if ($wgLocalMessageCacheSerialized) {
|
|
|
|
|
$this->saveToLocal( $serialized,$hash );
|
|
|
|
|
} else {
|
|
|
|
|
$this->saveToScript( $this->mCache, $hash );
|
|
|
|
|
}
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileOut( $fname.'-save' );
|
2004-07-18 08:48:43 +00:00
|
|
|
if ( $i == 20 ) {
|
2005-07-07 03:08:58 +00:00
|
|
|
$this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
|
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();
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
if ( !is_array( $this->mCache ) ) {
|
2004-12-19 08:00:50 +00:00
|
|
|
wfDebug( "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-09-20 14:55:25 +00:00
|
|
|
# Causing too much DB load, disabling -- TS
|
2004-07-10 01:15:23 +00:00
|
|
|
$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 );
|
2004-12-09 05:51:20 +00:00
|
|
|
$this->mDeferred = false;
|
2003-12-14 14:32:19 +00:00
|
|
|
return $success;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-04 20:36:11 +00:00
|
|
|
* Loads all or main part of cacheable messages from the database
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
|
|
|
|
function loadFromDB() {
|
2005-09-11 14:50:47 +00:00
|
|
|
global $wgAllMessagesEn, $wgLang;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-10-04 20:36:11 +00:00
|
|
|
$fname = 'MessageCache::loadFromDB';
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbr =& wfGetDB( DB_SLAVE );
|
2005-08-14 07:21:43 +00:00
|
|
|
if ( !$dbr ) {
|
2006-06-07 06:40:24 +00:00
|
|
|
throw new MWException( 'Invalid database object' );
|
2005-08-14 07:21:43 +00:00
|
|
|
}
|
2005-07-07 03:08:58 +00:00
|
|
|
$conditions = array( 'page_is_redirect' => 0,
|
2004-12-19 08:00:50 +00:00
|
|
|
'page_namespace' => NS_MEDIAWIKI);
|
2005-03-28 10:47:12 +00:00
|
|
|
$res = $dbr->select( array( 'page', 'revision', 'text' ),
|
2004-12-19 08:00:50 +00:00
|
|
|
array( 'page_title', 'old_text', 'old_flags' ),
|
2005-03-28 10:47:12 +00:00
|
|
|
'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
|
2004-12-19 08:00:50 +00:00
|
|
|
$fname
|
|
|
|
|
);
|
2004-09-20 14:55:25 +00:00
|
|
|
|
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 ) ) {
|
2004-12-19 12:21:29 +00:00
|
|
|
$this->mCache[$row->page_title] = Revision::getRevisionText( $row );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
2005-09-11 14:50:47 +00:00
|
|
|
# Negative caching
|
2006-01-07 13:09:30 +00:00
|
|
|
# Go through the language array and the extension array and make a note of
|
2005-09-11 14:50:47 +00:00
|
|
|
# any keys missing from the cache
|
|
|
|
|
foreach ( $wgAllMessagesEn as $key => $value ) {
|
|
|
|
|
$uckey = $wgLang->ucfirst( $key );
|
|
|
|
|
if ( !array_key_exists( $uckey, $this->mCache ) ) {
|
|
|
|
|
$this->mCache[$uckey] = false;
|
|
|
|
|
}
|
2005-03-19 10:40:41 +00:00
|
|
|
}
|
2006-06-25 08:38:17 +00:00
|
|
|
|
|
|
|
|
# Make sure all extension messages are available
|
|
|
|
|
wfLoadAllExtensions();
|
|
|
|
|
|
|
|
|
|
# Add them to the cache
|
2005-09-11 14:50:47 +00:00
|
|
|
foreach ( $this->mExtensionMessages as $key => $value ) {
|
|
|
|
|
$uckey = $wgLang->ucfirst( $key );
|
|
|
|
|
if ( !array_key_exists( $uckey, $this->mCache ) ) {
|
|
|
|
|
$this->mCache[$uckey] = false;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
2005-09-11 14:50:47 +00:00
|
|
|
|
|
|
|
|
$dbr->freeResult( $res );
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Not really needed anymore
|
|
|
|
|
*/
|
2003-12-14 14:32:19 +00:00
|
|
|
function getKeys() {
|
2004-09-25 02:23:04 +00:00
|
|
|
global $wgAllMessagesEn, $wgContLang;
|
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-12-22 07:28:11 +00:00
|
|
|
$title = $wgContLang->ucfirst( $key );
|
2004-12-18 08:11:32 +00:00
|
|
|
array_push( $this->mKeys, $title );
|
2004-01-25 08:49:45 +00:00
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
return $this->mKeys;
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2005-06-26 15:30:50 +00:00
|
|
|
* @deprecated
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-12-14 14:32:19 +00:00
|
|
|
function isCacheable( $key ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
return true;
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function replace( $title, $text ) {
|
2006-05-28 18:29:42 +00:00
|
|
|
global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
|
2005-11-08 11:54:04 +00:00
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
$this->lock();
|
|
|
|
|
$this->load();
|
2006-01-14 22:13:43 +00:00
|
|
|
$parserMemc->delete("$wgDBname:sidebar");
|
2004-03-20 15:03:26 +00:00
|
|
|
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 );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-11-08 11:54:04 +00:00
|
|
|
# Save to local cache
|
2006-01-07 13:09:30 +00:00
|
|
|
if ( $wgLocalMessageCache !== false ) {
|
2005-11-08 11:54:04 +00:00
|
|
|
$serialized = serialize( $this->mCache );
|
|
|
|
|
$hash = md5( $serialized );
|
|
|
|
|
$this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
|
2006-05-28 18:29:42 +00:00
|
|
|
if ($wgLocalMessageCacheSerialized) {
|
|
|
|
|
$this->saveToLocal( $serialized,$hash );
|
|
|
|
|
} else {
|
|
|
|
|
$this->saveToScript( $this->mCache, $hash );
|
|
|
|
|
}
|
2005-11-08 11:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Returns success
|
|
|
|
|
* Represents a write lock on the messages key
|
|
|
|
|
*/
|
2003-12-14 14:32:19 +00:00
|
|
|
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);
|
|
|
|
|
}
|
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
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
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
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2004-12-29 01:07:43 +00:00
|
|
|
function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
|
2004-10-11 02:15:55 +00:00
|
|
|
global $wgContLanguageCode;
|
2004-11-21 13:56:04 +00:00
|
|
|
if( $forcontent ) {
|
2004-10-11 02:15:55 +00:00
|
|
|
global $wgContLang;
|
2004-11-21 13:56:04 +00:00
|
|
|
$lang =& $wgContLang;
|
2004-09-25 04:15:47 +00:00
|
|
|
$langcode = $wgContLanguageCode;
|
2004-11-21 13:56:04 +00:00
|
|
|
} else {
|
2004-09-25 04:15:47 +00:00
|
|
|
global $wgLang, $wgLanguageCode;
|
2004-11-21 13:56:04 +00:00
|
|
|
$lang =& $wgLang;
|
2004-09-25 04:15:47 +00:00
|
|
|
$langcode = $wgLanguageCode;
|
|
|
|
|
}
|
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-12-09 05:51:20 +00:00
|
|
|
# If cache initialization was deferred, start it now.
|
2005-08-14 07:21:43 +00:00
|
|
|
if( $this->mDeferred && !$this->mDisable && $useDB ) {
|
2004-12-09 05:51:20 +00:00
|
|
|
$this->load();
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
$message = false;
|
2004-11-21 13:56:04 +00:00
|
|
|
if( !$this->mDisable && $useDB ) {
|
2004-12-22 07:28:11 +00:00
|
|
|
$title = $lang->ucfirst( $key );
|
2004-12-29 01:07:43 +00:00
|
|
|
if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
|
2004-11-21 13:56:04 +00:00
|
|
|
$title .= '/' . $langcode;
|
|
|
|
|
}
|
2005-03-13 11:50:54 +00:00
|
|
|
$message = $this->getFromCache( $title );
|
2004-05-15 03:36:39 +00:00
|
|
|
}
|
|
|
|
|
# Try the extension array
|
2005-09-11 14:50:47 +00:00
|
|
|
if( $message === false && array_key_exists( $key, $this->mExtensionMessages ) ) {
|
|
|
|
|
$message = $this->mExtensionMessages[$key];
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-24 00:09:42 +00:00
|
|
|
# Try the array in the language object
|
2005-09-11 14:50:47 +00:00
|
|
|
if( $message === false ) {
|
2004-09-20 14:55:25 +00:00
|
|
|
wfSuppressWarnings();
|
2004-09-25 04:15:47 +00:00
|
|
|
$message = $lang->getMessage( $key );
|
2004-09-20 14:55:25 +00:00
|
|
|
wfRestoreWarnings();
|
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
|
|
|
|
|
|
|
|
# Try the English array
|
2005-09-11 14:50:47 +00:00
|
|
|
if( $message === false && $langcode != 'en' ) {
|
2004-09-20 14:55:25 +00:00
|
|
|
wfSuppressWarnings();
|
2003-12-14 14:32:19 +00:00
|
|
|
$message = Language::getMessage( $key );
|
2004-09-20 14:55:25 +00:00
|
|
|
wfRestoreWarnings();
|
2005-10-22 20:52:30 +00:00
|
|
|
if ( is_null( $message ) ) {
|
|
|
|
|
$message = false;
|
|
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +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 &&
|
|
|
|
|
!$isfullkey && ($langcode != $wgContLanguageCode) ) {
|
|
|
|
|
$message = $this->getFromCache( $lang->ucfirst( $key ) );
|
|
|
|
|
}
|
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
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2005-07-07 03:08:58 +00:00
|
|
|
|
2005-03-13 11:50:54 +00:00
|
|
|
function getFromCache( $title ) {
|
|
|
|
|
$message = false;
|
2005-07-07 03:08:58 +00:00
|
|
|
|
2005-03-13 11:50:54 +00:00
|
|
|
# Try the cache
|
|
|
|
|
if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
|
2005-09-11 14:50:47 +00:00
|
|
|
return $this->mCache[$title];
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2005-09-11 14:50:47 +00:00
|
|
|
# Try individual message cache
|
|
|
|
|
if ( $this->mUseCache ) {
|
2005-03-13 11:50:54 +00:00
|
|
|
$message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
|
2005-09-11 17:33:25 +00:00
|
|
|
if ( $message == '###NONEXISTENT###' ) {
|
|
|
|
|
return false;
|
|
|
|
|
} elseif( !is_null( $message ) ) {
|
2005-03-13 11:50:54 +00:00
|
|
|
$this->mCache[$title] = $message;
|
2005-09-11 14:50:47 +00:00
|
|
|
return $message;
|
|
|
|
|
} else {
|
|
|
|
|
$message = false;
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-11 11:41:46 +00:00
|
|
|
# Call message Hooks, in case they are defined
|
|
|
|
|
wfRunHooks('MessagesPreLoad',array($title,&$message));
|
|
|
|
|
|
2005-03-13 11:50:54 +00:00
|
|
|
# If it wasn't in the cache, load each message from the DB individually
|
2005-09-11 14:50:47 +00:00
|
|
|
$revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
|
|
|
|
|
if( $revision ) {
|
|
|
|
|
$message = $revision->getText();
|
|
|
|
|
if ($this->mUseCache) {
|
|
|
|
|
$this->mCache[$title]=$message;
|
|
|
|
|
/* individual messages may be often
|
|
|
|
|
recached until proper purge code exists
|
|
|
|
|
*/
|
|
|
|
|
$this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
|
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
|
|
|
|
|
$this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
|
2005-03-13 11:50:54 +00:00
|
|
|
}
|
2005-07-07 03:08:58 +00:00
|
|
|
|
2005-03-13 11:50:54 +00:00
|
|
|
return $message;
|
|
|
|
|
}
|
2004-03-01 05:51:55 +00:00
|
|
|
|
2004-04-05 10:38:40 +00:00
|
|
|
function transform( $message ) {
|
2004-09-20 14:55:25 +00:00
|
|
|
if( !$this->mDisableTransform ) {
|
2004-11-21 13:56:04 +00:00
|
|
|
if( strpos( $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-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; }
|
2004-05-03 10:21:26 +00:00
|
|
|
function disableTransform() { $this->mDisableTransform = true; }
|
2004-09-09 22:22:17 +00:00
|
|
|
function enableTransform() { $this->mDisableTransform = false; }
|
2005-11-28 01:14:12 +00:00
|
|
|
function setTransform( $x ) { $this->mDisableTransform = $x; }
|
|
|
|
|
function getTransform() { return $this->mDisableTransform; }
|
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
|
|
|
|
|
*/
|
2004-05-15 03:36:39 +00:00
|
|
|
function addMessage( $key, $value ) {
|
|
|
|
|
$this->mExtensionMessages[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
2004-05-15 03:36:39 +00:00
|
|
|
function addMessages( $messages ) {
|
|
|
|
|
foreach ( $messages as $key => $value ) {
|
2005-07-22 20:35:08 +00:00
|
|
|
$this->addMessage( $key, $value );
|
2004-05-15 03:36:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
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 ) {
|
|
|
|
|
$this->mMemc->delete( $this->mMemcKey );
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
|
|
|
|
?>
|