htaccessing some directories for dev using cvs tree as www tree ;)
[lhc/web/wiklou.git] / includes / MessageCache.php
index 3226653..0200a97 100755 (executable)
@@ -1,4 +1,4 @@
-<?
+<?php
 
 # Message cache
 # Performs various useful MediaWiki namespace-related functions
@@ -10,11 +10,9 @@ define( "MSG_WAIT_TIMEOUT", 10);
 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;
@@ -22,6 +20,9 @@ class MessageCache
                $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();
        }
@@ -76,11 +77,8 @@ class MessageCache
        # 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();
@@ -88,36 +86,40 @@ class MessageCache
                        $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
@@ -150,29 +152,31 @@ class MessageCache
        
        function get( $key, $useDB ) {
                global $wgLang, $wgLanguageCode;
-
+               
+               # If uninitialised, someone is trying to call this halfway through Setup.php
+               if ( !$this->mInitialised ) {
+                       return "&lt;$key&gt;";
+               }
+               
                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;
                        }
                }
 
@@ -190,11 +194,22 @@ class MessageCache
                if ( !$message ) {
                        $message = "&lt;$key&gt;";
                }
+               
+               # 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; }
+
 }
 ?>