(bug 22647) Add category details in search results. Contributed by Lejonel.
[lhc/web/wiklou.git] / includes / BagOStuff.php
index 327bc5a..ac0263d 100644 (file)
@@ -32,7 +32,7 @@
  * backends for local hash array and SQL table included:
  * <code>
  *   $bag = new HashBagOStuff();
- *   $bag = new MediaWikiBagOStuff($tablename); # connect to db first
+ *   $bag = new SqlBagOStuff(); # connect to db first
  * </code>
  *
  * @ingroup Cache
@@ -54,7 +54,7 @@ abstract class BagOStuff {
        abstract public function get( $key );
 
        /**
-        * Set an item. 
+        * Set an item.
         * @param $key string
         * @param $value mixed
         * @param $exptime int Either an interval in seconds or a unix timestamp for expiry
@@ -87,9 +87,11 @@ abstract class BagOStuff {
        /* Better performance can likely be got with custom written versions */
        public function get_multi( $keys ) {
                $out = array();
+
                foreach ( $keys as $key ) {
                        $out[$key] = $this->get( $key );
                }
+
                return $out;
        }
 
@@ -140,7 +142,7 @@ abstract class BagOStuff {
        }
 
        public function decr( $key, $value = 1 ) {
-               return $this->incr( $key, -$value );
+               return $this->incr( $key, - $value );
        }
 
        public function debug( $text ) {
@@ -160,7 +162,6 @@ abstract class BagOStuff {
        }
 }
 
-
 /**
  * Functional versions!
  * This is a test of the interface, mainly. It stores things in an associative
@@ -221,10 +222,19 @@ class SqlBagOStuff extends BagOStuff {
        var $lastExpireAll = 0;
 
        protected function getDB() {
-               if ( !isset( $this->lb ) ) {
-                       $this->lb = wfGetLBFactory()->newMainLB();
-                       $this->db = $this->lb->getConnection( DB_MASTER );
-                       $this->db->clearFlag( DBO_TRX );
+               global $wgDBtype;
+               if ( !isset( $this->db ) ) {
+                       /* We must keep a separate connection to MySQL in order to avoid deadlocks
+                        * However, SQLite has an opposite behaviour.
+                        * @todo Investigate behaviour for other databases
+                        */
+                       if ( $wgDBtype == 'sqlite' ) {
+                               $this->db = wfGetDB( DB_MASTER );
+                       } else {
+                               $this->lb = wfGetLBFactory()->newMainLB();
+                               $this->db = $this->lb->getConnection( DB_MASTER );
+                               $this->db->clearFlag( DBO_TRX );
+                       }
                }
                return $this->db;
        }
@@ -245,10 +255,10 @@ class SqlBagOStuff extends BagOStuff {
                        $this->debug( "get: key has expired, deleting" );
                        try {
                                $db->begin();
-                               # Put the expiry time in the WHERE condition to avoid deleting a 
+                               # Put the expiry time in the WHERE condition to avoid deleting a
                                # newly-inserted value
-                               $db->delete( 'objectcache', 
-                                       array( 
+                               $db->delete( 'objectcache',
+                                       array(
                                                'keyname' => $key,
                                                'exptime' => $row->exptime
                                        ), __METHOD__ );
@@ -275,8 +285,8 @@ class SqlBagOStuff extends BagOStuff {
                try {
                        $db->begin();
                        $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
-                       $db->insert( 'objectcache', 
-                               array( 
+                       $db->insert( 'objectcache',
+                               array(
                                        'keyname' => $key,
                                        'value' => $db->encodeBlob( $this->serialize( $value ) ),
                                        'exptime' => $encExpiry
@@ -340,7 +350,7 @@ class SqlBagOStuff extends BagOStuff {
 
        public function keys() {
                $db = $this->getDB();
-               $res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__ );     
+               $res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__ );
                $result = array();
                foreach ( $res as $row ) {
                        $result[] = $row->keyname;
@@ -449,52 +459,7 @@ class SqlBagOStuff extends BagOStuff {
 /**
  * Backwards compatibility alias
  */
-class MediaWikiBagOStuff extends SqlBagOStuff {}
-
-/**
- * This is a wrapper for Turck MMCache's shared memory functions.
- *
- * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
- * to use a weird custom serializer that randomly segfaults. So we wrap calls
- * with serialize()/unserialize().
- *
- * The thing I noticed about the Turck serialized data was that unlike ordinary
- * serialize(), it contained the names of methods, and judging by the amount of
- * binary data, perhaps even the bytecode of the methods themselves. It may be
- * that Turck's serializer is faster, so a possible future extension would be
- * to use it for arrays but not for objects.
- *
- * @ingroup Cache
- */
-class TurckBagOStuff extends BagOStuff {
-       public function get( $key ) {
-               $val = mmcache_get( $key );
-               if ( is_string( $val ) ) {
-                       $val = unserialize( $val );
-               }
-               return $val;
-       }
-
-       public function set( $key, $value, $exptime = 0 ) {
-               mmcache_put( $key, serialize( $value ), $exptime );
-               return true;
-       }
-
-       public function delete( $key, $time = 0 ) {
-               mmcache_rm( $key );
-               return true;
-       }
-
-       public function lock( $key, $waitTimeout = 0 ) {
-               mmcache_lock( $key );
-               return true;
-       }
-
-       public function unlock( $key ) {
-               mmcache_unlock( $key );
-               return true;
-       }
-}
+class MediaWikiBagOStuff extends SqlBagOStuff { }
 
 /**
  * This is a wrapper for APC's shared memory functions
@@ -519,13 +484,22 @@ class APCBagOStuff extends BagOStuff {
                apc_delete( $key );
                return true;
        }
-}
 
+       public function keys() {
+               $info = apc_cache_info( 'user' );
+               $list = $info['cache_list'];
+               $keys = array();
+               foreach ( $list as $entry ) {
+                       $keys[] = $entry['info'];
+               }
+               return $keys;
+       }
+}
 
 /**
  * This is a wrapper for eAccelerator's shared memory functions.
  *
- * This is basically identical to the Turck MMCache version,
+ * This is basically identical to the deceased Turck MMCache version,
  * mostly because eAccelerator is based on Turck MMCache.
  *
  * @ingroup Cache
@@ -605,13 +579,12 @@ class XCacheBagOStuff extends BagOStuff {
                xcache_unset( $key );
                return true;
        }
-
 }
 
 /**
- * Cache that uses DBA as a backend. 
- * Slow due to the need to constantly open and close the file to avoid holding 
- * writer locks. Intended for development use only,  as a memcached workalike 
+ * Cache that uses DBA as a backend.
+ * Slow due to the need to constantly open and close the file to avoid holding
+ * writer locks. Intended for development use only,  as a memcached workalike
  * for systems that don't have it.
  *
  * @ingroup Cache
@@ -650,7 +623,7 @@ class DBABagOStuff extends BagOStuff {
                        return array(
                                unserialize( substr( $blob, 11 ) ),
                                intval( substr( $blob, 0, 10 ) )
-                       );
+                       );
                }
        }