Fixed a E_DEPRECATED
[lhc/web/wiklou.git] / includes / memcached-client.php
index b43e530..3b0ae90 100644 (file)
@@ -44,7 +44,7 @@
  *
  * require_once 'memcached.php';
  *
- * $mc = new memcached(array(
+ * $mc = new MWMemcached(array(
  *              'servers' => array('127.0.0.1:10000',
  *                                 array('192.0.0.1:10010', 2),
  *                                 '127.0.0.1:10020'),
 // {{{ requirements
 // }}}
 
-// {{{ class memcached
+// {{{ class MWMemcached
 /**
  * memcached client class implemented using (p)fsockopen()
  *
  * @author  Ryan T. Dean <rtdean@cytherianage.net>
  * @ingroup Cache
  */
-class memcached
-{
-   // {{{ properties
-   // {{{ public
+class MWMemcached {
+       // {{{ properties
+       // {{{ public
 
-               // {{{ constants
-               // {{{ flags
+       // {{{ constants
+       // {{{ flags
 
-               /**
-                * Flag: indicates data is serialized
-                */
-               const SERIALIZED = 1;
+       /**
+        * Flag: indicates data is serialized
+        */
+       const SERIALIZED = 1;
 
-               /**
-                * Flag: indicates data is compressed
-                */
-               const COMPRESSED = 2;
+       /**
+        * Flag: indicates data is compressed
+        */
+       const COMPRESSED = 2;
 
-               // }}}
+       // }}}
 
-               /**
-                * Minimum savings to store data compressed
-                */
-               const COMPRESSION_SAVINGS = 0.20;
+       /**
+        * Minimum savings to store data compressed
+        */
+       const COMPRESSION_SAVINGS = 0.20;
 
-               // }}}
+       // }}}
 
 
        /**
@@ -243,23 +242,22 @@ class memcached
         * @param   array    $args    Associative array of settings
         *
         * @return  mixed
-        * @access  public
         */
-       function memcached ($args)
-       {
-               $this->set_servers(@$args['servers']);
+       public function __construct( $args ) {
+               global $wgMemCachedTimeout;
+               $this->set_servers( @$args['servers'] );
                $this->_debug = @$args['debug'];
                $this->stats = array();
                $this->_compress_threshold = @$args['compress_threshold'];
-               $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false;
+               $this->_persistant = array_key_exists( 'persistant', $args ) ? ( @$args['persistant'] ) : false;
                $this->_compress_enable = true;
-               $this->_have_zlib = function_exists("gzcompress");
+               $this->_have_zlib = function_exists( 'gzcompress' );
 
                $this->_cache_sock = array();
                $this->_host_dead = array();
 
                $this->_timeout_seconds = 0;
-               $this->_timeout_microseconds = 50000;
+               $this->_timeout_microseconds = $wgMemCachedTimeout;
 
                $this->_connect_timeout = 0.01;
                $this->_connect_attempts = 2;
@@ -277,11 +275,9 @@ class memcached
         * @param   integer $exp     (optional) Time to expire data at
         *
         * @return  boolean
-        * @access  public
         */
-       function add ($key, $val, $exp = 0)
-       {
-               return $this->_set('add', $key, $val, $exp);
+       public function add( $key, $val, $exp = 0 ) {
+               return $this->_set( 'add', $key, $val, $exp );
        }
 
        // }}}
@@ -294,11 +290,9 @@ class memcached
         * @param   integer  $amt     (optional) Amount to decriment
         *
         * @return  mixed    FALSE on failure, value on success
-        * @access  public
         */
-       function decr ($key, $amt=1)
-       {
-               return $this->_incrdecr('decr', $key, $amt);
+       public function decr( $key, $amt = 1 ) {
+               return $this->_incrdecr( 'decr', $key, $amt );
        }
 
        // }}}
@@ -311,33 +305,34 @@ class memcached
         * @param   integer  $time    (optional) How long to wait before deleting
         *
         * @return  boolean  TRUE on success, FALSE on failure
-        * @access  public
         */
-       function delete ($key, $time = 0)
-       {
-               if (!$this->_active)
+       public function delete( $key, $time = 0 ) {
+               if ( !$this->_active ) {
                        return false;
+               }
 
-               $sock = $this->get_sock($key);
-               if (!is_resource($sock))
+               $sock = $this->get_sock( $key );
+               if ( !is_resource( $sock ) ) {
                        return false;
+               }
 
-               $key = is_array($key) ? $key[1] : $key;
+               $key = is_array( $key ) ? $key[1] : $key;
 
                @$this->stats['delete']++;
                $cmd = "delete $key $time\r\n";
-               if(!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
-               {
-                       $this->_dead_sock($sock);
+               if( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
+                       $this->_dead_sock( $sock );
                        return false;
                }
-               $res = trim(fgets($sock));
+               $res = trim( fgets( $sock ) );
 
-               if ($this->_debug)
-                       $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res));
+               if ( $this->_debug ) {
+                       $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
+               }
 
-               if ($res == "DELETED")
+               if ( $res == "DELETED" ) {
                        return true;
+               }
                return false;
        }
 
@@ -346,13 +341,11 @@ class memcached
 
        /**
         * Disconnects all connected sockets
-        *
-        * @access  public
         */
-       function disconnect_all ()
-       {
-               foreach ($this->_cache_sock as $sock)
-                       fclose($sock);
+       public function disconnect_all() {
+               foreach ( $this->_cache_sock as $sock ) {
+                       fclose( $sock );
+               }
 
                $this->_cache_sock = array();
        }
@@ -364,11 +357,8 @@ class memcached
         * Enable / Disable compression
         *
         * @param   boolean  $enable  TRUE to enable, FALSE to disable
-        *
-        * @access  public
         */
-       function enable_compress ($enable)
-       {
+       public function enable_compress( $enable ) {
                $this->_compress_enable = $enable;
        }
 
@@ -377,11 +367,8 @@ class memcached
 
        /**
         * Forget about all of the dead hosts
-        *
-        * @access  public
         */
-       function forget_dead_hosts ()
-       {
+       public function forget_dead_hosts() {
                $this->_host_dead = array();
        }
 
@@ -394,47 +381,45 @@ class memcached
         * @param  string   $key     Key to retrieve
         *
         * @return  mixed
-        * @access  public
         */
-       function get ($key)
-       {
-               $fname = 'memcached::get';
-               wfProfileIn( $fname );
+       public function get( $key ) {
+               wfProfileIn( __METHOD__ );
 
                if ( $this->_debug ) {
                        $this->_debugprint( "get($key)\n" );
                }
 
-               if (!$this->_active) {
-                       wfProfileOut( $fname );
+               if ( !$this->_active ) {
+                       wfProfileOut( __METHOD__ );
                        return false;
                }
 
-               $sock = $this->get_sock($key);
+               $sock = $this->get_sock( $key );
 
-               if (!is_resource($sock)) {
-                       wfProfileOut( $fname );
+               if ( !is_resource( $sock ) ) {
+                       wfProfileOut( __METHOD__ );
                        return false;
                }
 
                @$this->stats['get']++;
 
                $cmd = "get $key\r\n";
-               if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
-               {
-                       $this->_dead_sock($sock);
-                       wfProfileOut( $fname );
+               if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
+                       $this->_dead_sock( $sock );
+                       wfProfileOut( __METHOD__ );
                        return false;
                }
 
                $val = array();
-               $this->_load_items($sock, $val);
+               $this->_load_items( $sock, $val );
 
-               if ($this->_debug)
-                       foreach ($val as $k => $v)
-                               $this->_debugprint(sprintf("MemCache: sock %s got %s\n", serialize($sock), $k));
+               if ( $this->_debug ) {
+                       foreach ( $val as $k => $v ) {
+                               $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
+                       }
+               }
 
-               wfProfileOut( $fname );
+               wfProfileOut( __METHOD__ );
                return @$val[$key];
        }
 
@@ -447,23 +432,22 @@ class memcached
         * @param   array    $keys    Keys to retrieve
         *
         * @return  array
-        * @access  public
         */
-       function get_multi ($keys)
-       {
-               if (!$this->_active)
+       public function get_multi( $keys ) {
+               if ( !$this->_active ) {
                        return false;
+               }
 
                @$this->stats['get_multi']++;
                $sock_keys = array();
 
-               foreach ($keys as $key)
-               {
-                       $sock = $this->get_sock($key);
-                       if (!is_resource($sock)) continue;
-                       $key = is_array($key) ? $key[1] : $key;
-                       if (!isset($sock_keys[$sock]))
-                       {
+               foreach ( $keys as $key ) {
+                       $sock = $this->get_sock( $key );
+                       if ( !is_resource( $sock ) ) {
+                               continue;
+                       }
+                       $key = is_array( $key ) ? $key[1] : $key;
+                       if ( !isset( $sock_keys[$sock] ) ) {
                                $sock_keys[$sock] = array();
                                $socks[] = $sock;
                        }
@@ -471,34 +455,31 @@ class memcached
                }
 
                // Send out the requests
-               foreach ($socks as $sock)
-               {
-                       $cmd = "get";
-                       foreach ($sock_keys[$sock] as $key)
-                       {
-                               $cmd .= " ". $key;
+               foreach ( $socks as $sock ) {
+                       $cmd = 'get';
+                       foreach ( $sock_keys[$sock] as $key ) {
+                               $cmd .= ' ' . $key;
                        }
                        $cmd .= "\r\n";
 
-                       if ($this->_safe_fwrite($sock, $cmd, strlen($cmd)))
-                       {
+                       if ( $this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
                                $gather[] = $sock;
-                       } else
-                       {
-                               $this->_dead_sock($sock);
+                       } else {
+                               $this->_dead_sock( $sock );
                        }
                }
 
                // Parse responses
                $val = array();
-               foreach ($gather as $sock)
-               {
-                       $this->_load_items($sock, $val);
+               foreach ( $gather as $sock ) {
+                       $this->_load_items( $sock, $val );
                }
 
-               if ($this->_debug)
-                       foreach ($val as $k => $v)
-                               $this->_debugprint(sprintf("MemCache: got %s\n", $k));
+               if ( $this->_debug ) {
+                       foreach ( $val as $k => $v ) {
+                               $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
+                       }
+               }
 
                return $val;
        }
@@ -513,11 +494,9 @@ class memcached
         * @param   integer  $amt     (optional) amount to increment
         *
         * @return  integer  New key value?
-        * @access  public
         */
-       function incr ($key, $amt=1)
-       {
-               return $this->_incrdecr('incr', $key, $amt);
+       public function incr( $key, $amt = 1 ) {
+               return $this->_incrdecr( 'incr', $key, $amt );
        }
 
        // }}}
@@ -531,11 +510,9 @@ class memcached
         * @param   integer  $exp     (optional) Experiation time
         *
         * @return  boolean
-        * @access  public
         */
-   function replace ($key, $value, $exp=0)
-       {
-               return $this->_set('replace', $key, $value, $exp);
+       public function replace( $key, $value, $exp = 0 ) {
+               return $this->_set( 'replace', $key, $value, $exp );
        }
 
        // }}}
@@ -557,22 +534,24 @@ class memcached
         * @return  array    Output array
         * @access  public
         */
-       function run_command ($sock, $cmd)
-       {
-               if (!is_resource($sock))
+       function run_command( $sock, $cmd ) {
+               if ( !is_resource( $sock ) ) {
                        return array();
+               }
 
-               if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
+               if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
                        return array();
+               }
 
-               while (true)
-               {
-                       $res = fgets($sock);
+               while ( true ) {
+                       $res = fgets( $sock );
                        $ret[] = $res;
-                       if (preg_match('/^END/', $res))
+                       if ( preg_match( '/^END/', $res ) ) {
                                break;
-                       if (strlen($res) == 0)
+                       }
+                       if ( strlen( $res ) == 0 ) {
                                break;
+                       }
                }
                return $ret;
        }
@@ -589,11 +568,9 @@ class memcached
         * @param   integer  $exp     (optional) Experiation time
         *
         * @return  boolean  TRUE on success
-        * @access  public
         */
-   function set ($key, $value, $exp=0)
-       {
-               return $this->_set('set', $key, $value, $exp);
+       public function set( $key, $value, $exp = 0 ) {
+               return $this->_set( 'set', $key, $value, $exp );
        }
 
        // }}}
@@ -603,11 +580,8 @@ class memcached
         * Sets the compression threshold
         *
         * @param   integer  $thresh  Threshold to compress if larger than
-        *
-        * @access  public
         */
-   function set_compress_threshold ($thresh)
-       {
+       public function set_compress_threshold( $thresh ) {
                $this->_compress_threshold = $thresh;
        }
 
@@ -619,12 +593,9 @@ class memcached
         *
         * @param   boolean  $dbg     TRUE for debugging, FALSE otherwise
         *
-        * @access  public
-        *
-        * @see     memcahced::memcached
+        * @see     MWMemcached::__construct
         */
-       function set_debug ($dbg)
-       {
+       public function set_debug( $dbg ) {
                $this->_debug = $dbg;
        }
 
@@ -636,20 +607,18 @@ class memcached
         *
         * @param   array    $list    Array of servers to connect to
         *
-        * @access  public
-        *
-        * @see     memcached::memcached()
+        * @see     MWMemcached::__construct()
         */
-       function set_servers ($list)
-       {
+       public function set_servers( $list ) {
                $this->_servers = $list;
-               $this->_active = count($list);
+               $this->_active = count( $list );
                $this->_buckets = null;
                $this->_bucketcount = 0;
 
                $this->_single_sock = null;
-               if ($this->_active == 1)
+               if ( $this->_active == 1 ) {
                        $this->_single_sock = $this->_servers[0];
+               }
        }
 
        /**
@@ -657,11 +626,8 @@ class memcached
         *
         * @param   integer  $seconds Number of seconds
         * @param   integer  $microseconds  Number of microseconds
-        *
-        * @access  public
         */
-       function set_timeout ($seconds, $microseconds)
-       {
+       public function set_timeout( $seconds, $microseconds ) {
                $this->_timeout_seconds = $seconds;
                $this->_timeout_microseconds = $microseconds;
        }
@@ -678,11 +644,10 @@ class memcached
         *
         * @access  private
         */
-       function _close_sock ($sock)
-       {
-               $host = array_search($sock, $this->_cache_sock);
-               fclose($this->_cache_sock[$host]);
-               unset($this->_cache_sock[$host]);
+       function _close_sock( $sock ) {
+               $host = array_search( $sock, $this->_cache_sock );
+               fclose( $this->_cache_sock[$host] );
+               unset( $this->_cache_sock[$host] );
        }
 
        // }}}
@@ -697,29 +662,27 @@ class memcached
         * @return  boolean
         * @access  private
         */
-       function _connect_sock (&$sock, $host)
-       {
-               list ($ip, $port) = explode(":", $host);
+       function _connect_sock( &$sock, $host ) {
+               list( $ip, $port ) = explode( ':', $host );
                $sock = false;
                $timeout = $this->_connect_timeout;
                $errno = $errstr = null;
-               for ($i = 0; !$sock && $i < $this->_connect_attempts; $i++) {
-                       if ($this->_persistant == 1)
-                       {
-                               $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout);
-                       } else
-                       {
-                               $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
+               for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
+                       if ( $this->_persistant == 1 ) {
+                               $sock = @pfsockopen( $ip, $port, $errno, $errstr, $timeout );
+                       } else {
+                               $sock = @fsockopen( $ip, $port, $errno, $errstr, $timeout );
                        }
                }
-               if (!$sock) {
-                       if ($this->_debug)
+               if ( !$sock ) {
+                       if ( $this->_debug ) {
                                $this->_debugprint( "Error connecting to $host: $errstr\n" );
+                       }
                        return false;
                }
 
                // Initialise timeout
-               stream_set_timeout($sock, $this->_timeout_seconds, $this->_timeout_microseconds);
+               stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
 
                return true;
        }
@@ -734,18 +697,16 @@ class memcached
         *
         * @access  private
         */
-       function _dead_sock ($sock)
-       {
-               $host = array_search($sock, $this->_cache_sock);
-               $this->_dead_host($host);
+       function _dead_sock( $sock ) {
+               $host = array_search( $sock, $this->_cache_sock );
+               $this->_dead_host( $host );
        }
 
-       function _dead_host ($host)
-       {
-               @list ($ip, /* $port */) = explode(":", $host);
-               $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10));
+       function _dead_host( $host ) {
+               @list( $ip, /* $port */) = explode( ':', $host );
+               $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
                $this->_host_dead[$host] = $this->_host_dead[$ip];
-               unset($this->_cache_sock[$host]);
+               unset( $this->_cache_sock[$host] );
        }
 
        // }}}
@@ -759,44 +720,40 @@ class memcached
         * @return  mixed    resource on success, false on failure
         * @access  private
         */
-       function get_sock ($key)
-       {
-               if (!$this->_active)
+       function get_sock( $key ) {
+               if ( !$this->_active ) {
                        return false;
+               }
 
-               if ($this->_single_sock !== null) {
-                       $this->_flush_read_buffer($this->_single_sock);
-                       return $this->sock_to_host($this->_single_sock);
+               if ( $this->_single_sock !== null ) {
+                       $this->_flush_read_buffer( $this->_single_sock );
+                       return $this->sock_to_host( $this->_single_sock );
                }
 
-               $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key);
+               $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
 
-               if ($this->_buckets === null)
-               {
-                       foreach ($this->_servers as $v)
-                       {
-                               if (is_array($v))
-                               {
-                                       for ($i=0; $i<$v[1]; $i++)
+               if ( $this->_buckets === null ) {
+                       foreach ( $this->_servers as $v ) {
+                               if ( is_array( $v ) ) {
+                                       for( $i = 0; $i < $v[1]; $i++ ) {
                                                $bu[] = $v[0];
-                               } else
-                               {
+                                       }
+                               } else {
                                        $bu[] = $v;
                                }
                        }
                        $this->_buckets = $bu;
-                       $this->_bucketcount = count($bu);
+                       $this->_bucketcount = count( $bu );
                }
 
-               $realkey = is_array($key) ? $key[1] : $key;
-               for ($tries = 0; $tries<20; $tries++)
-               {
+               $realkey = is_array( $key ) ? $key[1] : $key;
+               for( $tries = 0; $tries < 20; $tries++ ) {
                        $host = $this->_buckets[$hv % $this->_bucketcount];
-                       $sock = $this->sock_to_host($host);
-                       if (is_resource($sock)) {
-                               $this->_flush_read_buffer($sock);
+                       $sock = $this->sock_to_host( $host );
+                       if ( is_resource( $sock ) ) {
+                               $this->_flush_read_buffer( $sock );
                                return $sock;
-                }
+                       }
                        $hv = $this->_hashfunc( $hv . $realkey );
                }
 
@@ -814,12 +771,11 @@ class memcached
         * @return  integer  Hash value
         * @access  private
         */
-       function _hashfunc ($key)
-       {
+       function _hashfunc( $key ) {
                # Hash function must on [0,0x7ffffff]
                # We take the first 31 bits of the MD5 hash, which unlike the hash
                # function used in a previous version of this client, works
-               return hexdec(substr(md5($key),0,8)) & 0x7fffffff;
+               return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
        }
 
        // }}}
@@ -835,24 +791,27 @@ class memcached
         * @return  integer     New value of $key
         * @access  private
         */
-       function _incrdecr ($cmd, $key, $amt=1)
-       {
-               if (!$this->_active)
+       function _incrdecr( $cmd, $key, $amt = 1 ) {
+               if ( !$this->_active ) {
                        return null;
+               }
 
-               $sock = $this->get_sock($key);
-               if (!is_resource($sock))
+               $sock = $this->get_sock( $key );
+               if ( !is_resource( $sock ) ) {
                        return null;
+               }
 
-               $key = is_array($key) ? $key[1] : $key;
+               $key = is_array( $key ) ? $key[1] : $key;
                @$this->stats[$cmd]++;
-               if (!$this->_safe_fwrite($sock, "$cmd $key $amt\r\n"))
-                       return $this->_dead_sock($sock);
+               if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
+                       return $this->_dead_sock( $sock );
+               }
 
-               $line = fgets($sock);
+               $line = fgets( $sock );
                $match = array();
-               if (!preg_match('/^(\d+)/', $line, $match))
+               if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
                        return null;
+               }
                return $match[1];
        }
 
@@ -867,53 +826,50 @@ class memcached
         *
         * @access  private
         */
-       function _load_items ($sock, &$ret)
-       {
-               while (1)
-               {
-                       $decl = fgets($sock);
-                       if ($decl == "END\r\n")
-                       {
+       function _load_items( $sock, &$ret ) {
+               while ( 1 ) {
+                       $decl = fgets( $sock );
+                       if ( $decl == "END\r\n" ) {
                                return true;
-                       } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match))
-                       {
-                               list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]);
-                               $bneed = $len+2;
+                       } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match ) ) {
+                               list( $rkey, $flags, $len ) = array( $match[1], $match[2], $match[3] );
+                               $bneed = $len + 2;
                                $offset = 0;
 
-                               while ($bneed > 0)
-                               {
-                                       $data = fread($sock, $bneed);
-                                       $n = strlen($data);
-                                       if ($n == 0)
+                               while ( $bneed > 0 ) {
+                                       $data = fread( $sock, $bneed );
+                                       $n = strlen( $data );
+                                       if ( $n == 0 ) {
                                                break;
+                                       }
                                        $offset += $n;
                                        $bneed -= $n;
                                        @$ret[$rkey] .= $data;
                                }
 
-                               if ($offset != $len+2)
-                               {
+                               if ( $offset != $len + 2 ) {
                                        // Something is borked!
-                                       if ($this->_debug)
-                                               $this->_debugprint(sprintf("Something is borked!  key %s expecting %d got %d length\n", $rkey, $len+2, $offset));
+                                       if ( $this->_debug ) {
+                                               $this->_debugprint( sprintf( "Something is borked!  key %s expecting %d got %d length\n", $rkey, $len + 2, $offset ) );
+                                       }
 
-                                       unset($ret[$rkey]);
-                                       $this->_close_sock($sock);
+                                       unset( $ret[$rkey] );
+                                       $this->_close_sock( $sock );
                                        return false;
                                }
 
-                               if ($this->_have_zlib && $flags & memcached::COMPRESSED)
-                                       $ret[$rkey] = gzuncompress($ret[$rkey]);
+                               if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
+                                       $ret[$rkey] = gzuncompress( $ret[$rkey] );
+                               }
 
-                               $ret[$rkey] = rtrim($ret[$rkey]);
+                               $ret[$rkey] = rtrim( $ret[$rkey] );
 
-                               if ($flags & memcached::SERIALIZED)
-                                       $ret[$rkey] = unserialize($ret[$rkey]);
+                               if ( $flags & self::SERIALIZED ) {
+                                       $ret[$rkey] = unserialize( $ret[$rkey] );
+                               }
 
-                       } else
-                       {
-                               $this->_debugprint("Error parsing memcached response\n");
+                       } else {
+                               $this->_debugprint( "Error parsing memcached response\n" );
                                return 0;
                        }
                }
@@ -933,55 +889,57 @@ class memcached
         * @return  boolean
         * @access  private
         */
-       function _set ($cmd, $key, $val, $exp)
-       {
-               if (!$this->_active)
+       function _set( $cmd, $key, $val, $exp ) {
+               if ( !$this->_active ) {
                        return false;
+               }
 
-               $sock = $this->get_sock($key);
-               if (!is_resource($sock))
+               $sock = $this->get_sock( $key );
+               if ( !is_resource( $sock ) ) {
                        return false;
+               }
 
                @$this->stats[$cmd]++;
 
                $flags = 0;
 
-               if (!is_scalar($val))
-               {
-                       $val = serialize($val);
-                       $flags |= memcached::SERIALIZED;
-                       if ($this->_debug)
-                               $this->_debugprint(sprintf("client: serializing data as it is not scalar\n"));
+               if ( !is_scalar( $val ) ) {
+                       $val = serialize( $val );
+                       $flags |= self::SERIALIZED;
+                       if ( $this->_debug ) {
+                               $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
+                       }
                }
 
-               $len = strlen($val);
+               $len = strlen( $val );
 
-               if ($this->_have_zlib && $this->_compress_enable &&
-                        $this->_compress_threshold && $len >= $this->_compress_threshold)
+               if ( $this->_have_zlib && $this->_compress_enable &&
+                        $this->_compress_threshold && $len >= $this->_compress_threshold )
                {
-                       $c_val = gzcompress($val, 9);
-                       $c_len = strlen($c_val);
+                       $c_val = gzcompress( $val, 9 );
+                       $c_len = strlen( $c_val );
 
-                       if ($c_len < $len*(1 - memcached::COMPRESSION_SAVINGS))
-                       {
-                               if ($this->_debug)
-                                       $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len));
+                       if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
+                               if ( $this->_debug ) {
+                                       $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
+                               }
                                $val = $c_val;
                                $len = $c_len;
-                               $flags |= memcached::COMPRESSED;
+                               $flags |= self::COMPRESSED;
                        }
                }
-               if (!$this->_safe_fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))
-                       return $this->_dead_sock($sock);
+               if ( !$this->_safe_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) {
+                       return $this->_dead_sock( $sock );
+               }
 
-               $line = trim(fgets($sock));
+               $line = trim( fgets( $sock ) );
 
-               if ($this->_debug)
-               {
-                       $this->_debugprint(sprintf("%s %s (%s)\n", $cmd, $key, $line));
+               if ( $this->_debug ) {
+                       $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
                }
-               if ($line == "STORED")
+               if ( $line == "STORED" ) {
                        return true;
+               }
                return false;
        }
 
@@ -996,31 +954,34 @@ class memcached
         * @return  mixed    IO Stream or false
         * @access  private
         */
-       function sock_to_host ($host)
-       {
-               if (isset($this->_cache_sock[$host]))
+       function sock_to_host( $host ) {
+               if ( isset( $this->_cache_sock[$host] ) ) {
                        return $this->_cache_sock[$host];
+               }
 
                $sock = null;
                $now = time();
-               list ($ip, /* $port */) = explode (":", $host);
-               if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now ||
-                       isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now)
+               list( $ip, /* $port */) = explode( ':', $host );
+               if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
+                       isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
+               ) {
                        return null;
+               }
 
-               if (!$this->_connect_sock($sock, $host))
-                       return $this->_dead_host($host);
+               if ( !$this->_connect_sock( $sock, $host ) ) {
+                       return $this->_dead_host( $host );
+               }
 
                // Do not buffer writes
-               stream_set_write_buffer($sock, 0);
+               stream_set_write_buffer( $sock, 0 );
 
                $this->_cache_sock[$host] = $sock;
 
                return $this->_cache_sock[$host];
        }
 
-       function _debugprint($str){
-               print($str);
+       function _debugprint( $str ) {
+               print( $str );
        }
 
        /**
@@ -1029,21 +990,21 @@ class memcached
         * @return bool false on failure, true on success
         */
        /*
-       function _safe_fwrite($f, $buf, $len = false) {
-               stream_set_blocking($f, 0);
+       function _safe_fwrite( $f, $buf, $len = false ) {
+               stream_set_blocking( $f, 0 );
 
-               if ($len === false) {
-                       wfDebug("Writing " . strlen( $buf ) . " bytes\n");
-                       $bytesWritten = fwrite($f, $buf);
+               if ( $len === false ) {
+                       wfDebug( "Writing " . strlen( $buf ) . " bytes\n" );
+                       $bytesWritten = fwrite( $f, $buf );
                } else {
-                       wfDebug("Writing $len bytes\n");
-                       $bytesWritten = fwrite($f, $buf, $len);
+                       wfDebug( "Writing $len bytes\n" );
+                       $bytesWritten = fwrite( $f, $buf, $len );
                }
-               $n = stream_select($r=NULL, $w = array($f), $e = NULL, 10, 0);
-               #   $this->_timeout_seconds, $this->_timeout_microseconds);
+               $n = stream_select( $r = null, $w = array( $f ), $e = null, 10, 0 );
+               #   $this->_timeout_seconds, $this->_timeout_microseconds );
 
-               wfDebug("stream_select returned $n\n");
-               stream_set_blocking($f, 1);
+               wfDebug( "stream_select returned $n\n" );
+               stream_set_blocking( $f, 1 );
                return $n == 1;
                return $bytesWritten;
        }*/
@@ -1051,11 +1012,11 @@ class memcached
        /**
         * Original behaviour
         */
-       function _safe_fwrite($f, $buf, $len = false) {
-               if ($len === false) {
-                       $bytesWritten = fwrite($f, $buf);
+       function _safe_fwrite( $f, $buf, $len = false ) {
+               if ( $len === false ) {
+                       $bytesWritten = fwrite( $f, $buf );
                } else {
-                       $bytesWritten = fwrite($f, $buf, $len);
+                       $bytesWritten = fwrite( $f, $buf, $len );
                }
                return $bytesWritten;
        }
@@ -1063,14 +1024,14 @@ class memcached
        /**
         * Flush the read buffer of a stream
         */
-       function _flush_read_buffer($f) {
-               if (!is_resource($f)) {
+       function _flush_read_buffer( $f ) {
+               if ( !is_resource( $f ) ) {
                        return;
                }
-               $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0);
-               while ($n == 1 && !feof($f)) {
-                       fread($f, 1024);
-                       $n = stream_select($r=array($f), $w = NULL, $e = NULL, 0, 0);
+               $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
+               while ( $n == 1 && !feof( $f ) ) {
+                       fread( $f, 1024 );
+                       $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
                }
        }
 
@@ -1082,3 +1043,9 @@ class memcached
 // vim: sts=3 sw=3 et
 
 // }}}
+
+class MemCachedClientforWiki extends MWMemcached {
+       function _debugprint( $text ) {
+               wfDebug( "memcached: $text" );
+       }
+}