Reverting r16861; incompatible change to message texts, breaks a lot of toggle displa...
[lhc/web/wiklou.git] / includes / BagOStuff.php
index 7e48fb0..1dc93a2 100644 (file)
@@ -15,7 +15,7 @@
 #
 # You should have received a copy of the GNU General Public License along
 # with this program; if not, write to the Free Software Foundation, Inc.,
-# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 # http://www.gnu.org/copyleft/gpl.html
 /**
  *
@@ -33,7 +33,6 @@
  * $bag = new MysqlBagOStuff($tablename); # connect to db first
  *
  * @package MediaWiki
- * @abstract
  */
 class BagOStuff {
        var $debugmode;
@@ -147,6 +146,17 @@ class BagOStuff {
                if($this->debugmode)
                        wfDebug("BagOStuff debug: $text\n");
        }
+
+       /**
+        * Convert an optionally relative time to an absolute time
+        */
+       static function convertExpiry( $exptime ) {
+               if(($exptime != 0) && ($exptime < 3600*24*30)) {
+                       return time() + $exptime;
+               } else {
+                       return $exptime;
+               }
+       }
 }
 
 
@@ -184,9 +194,7 @@ class HashBagOStuff extends BagOStuff {
        }
 
        function set($key,$value,$exptime=0) {
-               if(($exptime != 0) && ($exptime < 3600*24*30))
-                       $exptime = time() + $exptime;
-               $this->bag[$key] = array( $value, $exptime );
+               $this->bag[$key] = array( $value, BagOStuff::convertExpiry( $exptime ) );
        }
 
        function delete($key,$time=0) {
@@ -212,7 +220,7 @@ CREATE TABLE objectcache (
  * @abstract
  * @package MediaWiki
  */
-class SqlBagOStuff extends BagOStuff {
+abstract class SqlBagOStuff extends BagOStuff {
        var $table;
        var $lastexpireall = 0;
 
@@ -232,7 +240,7 @@ class SqlBagOStuff extends BagOStuff {
                }
                if($row=$this->_fetchobject($res)) {
                        $this->_debug("get: retrieved data; exp time is " . $row->exptime);
-                       return $this->_unserialize($row->value);
+                       return $this->_unserialize($this->_blobdecode($row->value));
                } else {
                        $this->_debug('get: no matching rows');
                }
@@ -292,16 +300,12 @@ class SqlBagOStuff extends BagOStuff {
        function _blobencode($str) {
                return $str;
        }
-       function _doinsert($table, $vals) {
-               die( 'abstract function SqlBagOStuff::_doinsert() must be defined' );
-       }
-       function _doquery($sql) {
-               die( 'abstract function SqlBagOStuff::_doquery() must be defined' );
+       function _blobdecode($str) {
+               return $str;
        }
 
-       function _fetchrow($res) {
-               die( 'abstract function SqlBagOStuff::_fetchrow() must be defined' );
-       }
+       abstract function _doinsert($table, $vals);
+       abstract function _doquery($sql);
 
        function _freeresult($result) {
                /* stub */
@@ -313,13 +317,8 @@ class SqlBagOStuff extends BagOStuff {
                return 'unknown error';
        }
 
-       function _maxdatetime() {
-               die( 'abstract function SqlBagOStuff::_maxdatetime() must be defined' );
-       }
-
-       function _fromunixtime() {
-               die( 'abstract function SqlBagOStuff::_fromunixtime() must be defined' );
-       }
+       abstract function _maxdatetime();
+       abstract function _fromunixtime($ts);
 
        function garbageCollect() {
                /* Ignore 99% of requests */
@@ -373,7 +372,6 @@ class SqlBagOStuff extends BagOStuff {
                                $serial = $decomp;
                        }
                }
-               wfdebug("serial: [$serial]\n");
                $ret = unserialize( $serial );
                return $ret;
        }
@@ -422,13 +420,17 @@ class MediaWikiBagOStuff extends SqlBagOStuff {
                $dbw =& wfGetDB( DB_MASTER );
                return $dbw->encodeBlob($s);
        }
+       function _blobdecode($s) {
+               $dbw =& wfGetDB( DB_MASTER );
+               return $dbw->decodeBlob($s);
+       }
        function getTableName() {
                if ( !$this->tableInitialised ) {
                        $dbw =& wfGetDB( DB_MASTER );
-                       /* This is actually a hack, we should be able 
+                       /* This is actually a hack, we should be able
                           to use Language classes here... or not */
                        if (!$dbw)
-                               die("Could not connect to database");
+                               throw new MWException("Could not connect to database");
                        $this->table = $dbw->tableName( $this->table );
                        $this->tableInitialised = true;
                }
@@ -481,6 +483,30 @@ class TurckBagOStuff extends BagOStuff {
        }
 }
 
+/**
+ * This is a wrapper for APC's shared memory functions
+ *
+ * @package MediaWiki
+ */
+
+class APCBagOStuff extends BagOStuff {
+       function get($key) {
+               $val = apc_fetch($key);
+               return $val;
+       }
+       
+       function set($key, $value, $exptime=0) {
+               apc_store($key, $value, $exptime);
+               return true;
+       }
+       
+       function delete($key, $time=0) {
+               apc_delete($key);
+               return true;
+       }
+}
+
+
 /**
  * This is a wrapper for eAccelerator's shared memory functions.
  *
@@ -518,4 +544,136 @@ class eAccelBagOStuff extends BagOStuff {
                return true;
        }
 }
+
+class DBABagOStuff extends BagOStuff {
+       var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
+       
+       function __construct( $handler = 'db3', $dir = false ) {
+               if ( $dir === false ) {
+                       global $wgTmpDirectory;
+                       $dir = $wgTmpDirectory;
+               }
+               $this->mFile = "$dir/mw-cache-" . wfWikiID();
+               $this->mFile .= '.db';
+               $this->mHandler = $handler;
+       }
+
+       /**
+        * Encode value and expiry for storage
+        */
+       function encode( $value, $expiry ) {
+               # Convert to absolute time
+               $expiry = BagOStuff::convertExpiry( $expiry );
+               return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
+       }
+
+       /**
+        * @return list containing value first and expiry second
+        */
+       function decode( $blob ) {
+               if ( !is_string( $blob ) ) {
+                       return array( null, 0 );
+               } else {
+                       return array( 
+                               unserialize( substr( $blob, 11 ) ),
+                               intval( substr( $blob, 0, 10 ) )
+                       );
+               }
+       }
+
+       function getReader() {
+               if ( file_exists( $this->mFile ) ) {
+                       $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
+               } else {
+                       $handle = $this->getWriter();
+               }
+               if ( !$handle ) {
+                       wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
+               }
+               return $handle;
+       }
+
+       function getWriter() {
+               $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
+               if ( !$handle ) {
+                       wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
+               }
+               return $handle;
+       }
+
+       function get( $key ) {
+               wfProfileIn( __METHOD__ );
+               wfDebug( __METHOD__."($key)\n" );
+               $handle = $this->getReader();
+               if ( !$handle ) {
+                       return null;
+               }
+               $val = dba_fetch( $key, $handle );
+               list( $val, $expiry ) = $this->decode( $val );
+               # Must close ASAP because locks are held
+               dba_close( $handle );
+
+               if ( !is_null( $val ) && $expiry && $expiry < time() ) {
+                       # Key is expired, delete it
+                       $handle = $this->getWriter();
+                       dba_delete( $key, $handle );
+                       dba_close( $handle );
+                       wfDebug( __METHOD__.": $key expired\n" );
+                       $val = null;
+               }
+               wfProfileOut( __METHOD__ );
+               return $val;
+       }
+
+       function set( $key, $value, $exptime=0 ) {
+               wfProfileIn( __METHOD__ );
+               wfDebug( __METHOD__."($key)\n" );
+               $blob = $this->encode( $value, $exptime );
+               $handle = $this->getWriter();
+               if ( !$handle ) {
+                       return false;
+               }
+               $ret = dba_replace( $key, $blob, $handle );
+               dba_close( $handle );
+               wfProfileOut( __METHOD__ );
+               return $ret;
+       }
+
+       function delete( $key, $time = 0 ) {
+               wfProfileIn( __METHOD__ );
+               $handle = $this->getWriter();
+               if ( !$handle ) {
+                       return false;
+               }
+               $ret = dba_delete( $key, $handle );
+               dba_close( $handle );
+               wfProfileOut( __METHOD__ );
+               return $ret;
+       }
+
+       function add( $key, $value, $exptime = 0 ) {
+               wfProfileIn( __METHOD__ );
+               $blob = $this->encode( $value, $exptime );
+               $handle = $this->getWriter();
+               if ( !$handle ) {
+                       return false;
+               }
+               $ret = dba_insert( $key, $blob, $handle );
+               # Insert failed, check to see if it failed due to an expired key
+               if ( !$ret ) {
+                       list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
+                       if ( $expiry < time() ) {
+                               # Yes expired, delete and try again
+                               dba_delete( $key, $handle );
+                               $ret = dba_insert( $key, $blob, $handle );
+                               # This time if it failed then it will be handled by the caller like any other race
+                       }
+               }
+
+               dba_close( $handle );
+               wfProfileOut( __METHOD__ );
+               return $ret;
+       }
+}
+       
 ?>