-<?
+<?php
# Message cache
# Performs various useful MediaWiki namespace-related functions
class MessageCache
{
var $mCache, $mUseCache, $mDisable, $mExpiry;
- var $mMemcKey, $mKeys;
-
- function MessageCache( $useMemCached, $useDB, $expiry, $memcPrefix ) {
- $this->initialise( $useMemCached, $useDB, $expiry, $memcPrefix );
- }
+ var $mMemcKey, $mKeys, $mParserOptions, $mParser;
+
+ var $mInitialised = false;
function initialise( $useMemCached, $useDB, $expiry, $memcPrefix ) {
$this->mUseCache = $useMemCached;
$this->mExpiry = $expiry;
$this->mMemcKey = "$memcPrefix:messages";
$this->mKeys = false; # initialised on demand
+ $this->mInitialised = true;
+ $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
+ $this->mParser = new Parser;
$this->load();
}
# Loads all cacheable messages from the database
function loadFromDB()
{
+ $fname = "MessageCache::loadFromDB";
$sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
- $sql .= " AND cur_title IN ('";
- $sql .= implode( "','", $this->getKeys() ) . "')";
- wfDebug( "$sql\n" );
-
$res = wfQuery( $sql, DB_READ, $fname );
$this->mCache = array();
$this->mCache[$row->cur_title] = $row->cur_text;
}
- wfDebug( var_export( $this->mCache, true ) );
-
wfFreeResult( $res );
}
+ # Not really needed anymore
function getKeys() {
- global $wgAllMessagesEn;
+ global $wgAllMessagesEn, $wgLang;
if ( !$this->mKeys ) {
- $this->mKeys = array_map( 'ucfirst', array_keys( $wgAllMessagesEn ) );
+ $this->mKeys = array();
+ foreach ( $wgAllMessagesEn as $key => $value ) {
+ array_push( $this->mKeys, $wgLang->ucfirst( $key ) );
+ }
}
return $this->mKeys;
}
+ # Obsolete
function isCacheable( $key ) {
- global $wgAllMessagesEn;
- return array_key_exists( lcfirst( $key ), $wgAllMessagesEn ) ||
+ return true;
+ /*
+ global $wgAllMessagesEn, $wgLang;
+ return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
array_key_exists( $key, $wgAllMessagesEn );
+ */
}
function replace( $title, $text ) {
global $wgMemc;
- if ( $this->isCacheable( $title ) ) {
- $this->lock();
- $this->load();
- if ( is_array( $this->mCache ) ) {
- $this->mCache[$title] = $text;
- $wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
- }
- $this->unlock();
+ $this->lock();
+ $this->load();
+ if ( is_array( $this->mCache ) ) {
+ $this->mCache[$title] = $text;
+ $wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
}
+ $this->unlock();
}
# Returns success
function get( $key, $useDB ) {
global $wgLang, $wgLanguageCode;
-
+
+ # If uninitialised, someone is trying to call this halfway through Setup.php
+ if ( !$this->mInitialised ) {
+ return "<$key>";
+ }
+
if ( $this->mDisable ) {
- return $wgLang->getMessage( $key );
+ return $this->transform( $wgLang->getMessage( $key ) );
}
- $title = ucfirst( $key );
+ $title = $wgLang->ucfirst( $key );
$message = false;
# Try the cache
- if ( $this->mUseCache && $this->mCache ) {
+ if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
$message = $this->mCache[$title];
}
# If it wasn't in the cache, load each message from the DB individually
if ( !$message && $useDB) {
- $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI .
- " AND cur_title='$title'";
- $res = wfQuery( $sql, DB_READ, $fname );
-
- if ( wfNumRows( $res ) ) {
- $obj = wfFetchObject( $res );
- $message = $obj->cur_text;
- wfFreeResult( $res );
+ $result = wfGetArray( "cur", array("cur_text"),
+ array( "cur_namespace" => NS_MEDIAWIKI, "cur_title" => $title ),
+ "MessageCache::get" );
+ if ( $result ) {
+ $message = $result->cur_text;
}
}
if ( !$message ) {
$message = "<$key>";
}
+
+ # Replace brace tags
+ $message = $this->transform( $message );
+
return $message;
}
-}
-function lcfirst( $s ) {
- return strtolower( $s{0} ). substr( $s, 1 );
+ function transform( $message ) {
+ if ( strstr( $message, "{{" ) !== false ) {
+ $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
+ }
+ return $message;
+ }
+
+ function disable() { $this->mDisable = true; }
+ function enable() { $this->mDisable = false; }
+
}
?>