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
|
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
|
|
|
*/
|
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;
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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() {
|
2004-05-09 05:12:55 +00:00
|
|
|
global $wgAllMessagesEn;
|
2004-09-20 14:55:25 +00:00
|
|
|
|
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;
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
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' );
|
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.
|
2004-10-11 04:13:43 +00:00
|
|
|
$success = $this->mMemc->add( $this->mMemcKey, "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' );
|
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();
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
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-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 );
|
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() {
|
2004-10-04 20:36:11 +00:00
|
|
|
global $wgPartialMessageCache;
|
|
|
|
|
$fname = 'MessageCache::loadFromDB';
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbr =& wfGetDB( DB_SLAVE );
|
2004-10-04 20:36:11 +00:00
|
|
|
$conditions = array( 'cur_is_redirect' => 0,
|
|
|
|
|
'cur_namespace' => NS_MEDIAWIKI);
|
|
|
|
|
if ($wgPartialMessageCache) {
|
|
|
|
|
if (is_array($wgPartialMessageCache)) {
|
|
|
|
|
$conditions['cur_title']=$wgPartialMessageCache;
|
|
|
|
|
} else {
|
|
|
|
|
require_once("MessageCacheHints.php");
|
|
|
|
|
$conditions['cur_title']=MessageCacheHints::get();
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
$res = $dbr->select( 'cur',
|
2004-10-04 20:36:11 +00:00
|
|
|
array( 'cur_title', 'cur_text' ), $conditions, $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 ) ) {
|
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-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-09-25 02:23:04 +00:00
|
|
|
array_push( $this->mKeys, $wgContLang->ucfirst( $key ) );
|
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
|
|
|
/**
|
|
|
|
|
* 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;
|
2004-09-20 14:55:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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-09-25 04:15:47 +00:00
|
|
|
function get( $key, $useDB, $forcontent=true ) {
|
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 ) {
|
2004-01-07 13:05:27 +00:00
|
|
|
return "<$key>";
|
|
|
|
|
}
|
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-10-11 02:15:55 +00:00
|
|
|
$title = $lang->ucfirst( $key );
|
2004-11-21 13:56:04 +00:00
|
|
|
if( $langcode != $wgContLanguageCode ) {
|
|
|
|
|
$title .= '/' . $langcode;
|
|
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
|
2004-05-15 03:36:39 +00:00
|
|
|
# Try the cache
|
2004-12-08 17:43:09 +00:00
|
|
|
if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
|
2004-05-15 03:36:39 +00:00
|
|
|
$message = $this->mCache[$title];
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2004-10-05 11:18:43 +00:00
|
|
|
if ( !$message && $this->mUseCache ) {
|
2004-11-21 13:56:04 +00:00
|
|
|
$message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
|
|
|
|
|
if( $message ) {
|
|
|
|
|
$this->mCache[$title] = $message;
|
2004-10-05 11:18:43 +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-10-24 07:10:33 +00:00
|
|
|
$result = $dbr->selectRow( 'cur', array('cur_text'),
|
2004-08-22 17:24:50 +00:00
|
|
|
array( 'cur_namespace' => NS_MEDIAWIKI, 'cur_title' => $title ),
|
|
|
|
|
'MessageCache::get' );
|
2004-05-15 03:36:39 +00:00
|
|
|
if ( $result ) {
|
|
|
|
|
$message = $result->cur_text;
|
2004-11-21 13:56:04 +00:00
|
|
|
if( $this->mUseCache ) {
|
|
|
|
|
$this->mCache[$title] = $message;
|
2004-10-05 11:18:43 +00:00
|
|
|
/* individual messages may be often
|
|
|
|
|
recached until proper purge code exists
|
|
|
|
|
*/
|
2004-11-21 13:56:04 +00:00
|
|
|
$this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
|
2004-10-05 11:18:43 +00:00
|
|
|
}
|
2004-05-15 03:36:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# Try the extension array
|
2004-11-21 13:56:04 +00:00
|
|
|
if( !$message ) {
|
2004-05-15 03:36:39 +00:00
|
|
|
$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
|
2004-11-21 13:56:04 +00:00
|
|
|
if( !$message ) {
|
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();
|
|
|
|
|
}
|
2003-12-14 14:32:19 +00:00
|
|
|
|
|
|
|
|
# Try the English array
|
2004-11-21 13:56:04 +00:00
|
|
|
if( !$message && $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();
|
2003-12-14 14:32:19 +00:00
|
|
|
}
|
2004-09-20 14:55:25 +00:00
|
|
|
|
2003-12-14 14:32:19 +00:00
|
|
|
# Final fallback
|
2004-11-21 13:56:04 +00:00
|
|
|
if( !$message ) {
|
2003-12-14 14:32:19 +00:00
|
|
|
$message = "<$key>";
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
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; }
|
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-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
|
|
|
}
|
|
|
|
|
?>
|