Revert r78000 (removing PHP version from environment checks), never got a chance...
[lhc/web/wiklou.git] / includes / memcached-client.php
index 26f2383..53f0324 100644 (file)
@@ -1,40 +1,41 @@
 <?php
-//
-// +---------------------------------------------------------------------------+
-// | memcached client, PHP                                                     |
-// +---------------------------------------------------------------------------+
-// | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net>                 |
-// | All rights reserved.                                                      |
-// |                                                                           |
-// | Redistribution and use in source and binary forms, with or without        |
-// | modification, are permitted provided that the following conditions        |
-// | are met:                                                                  |
-// |                                                                           |
-// | 1. Redistributions of source code must retain the above copyright         |
-// |    notice, this list of conditions and the following disclaimer.          |
-// | 2. Redistributions in binary form must reproduce the above copyright      |
-// |    notice, this list of conditions and the following disclaimer in the    |
-// |    documentation and/or other materials provided with the distribution.   |
-// |                                                                           |
-// | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      |
-// | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
-// | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   |
-// | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          |
-// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  |
-// | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
-// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |
-// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |
-// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  |
-// | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         |
-// +---------------------------------------------------------------------------+
-// | Author: Ryan T. Dean <rtdean@cytherianage.net>                            |
-// | Heavily influenced by the Perl memcached client by Brad Fitzpatrick.      |
-// |   Permission granted by Brad Fitzpatrick for relicense of ported Perl     |
-// |   client logic under 2-clause BSD license.                                |
-// +---------------------------------------------------------------------------+
-//
-// $TCAnet$
-//
+/**
+ * +---------------------------------------------------------------------------+
+ * | memcached client, PHP                                                     |
+ * +---------------------------------------------------------------------------+
+ * | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net>                 |
+ * | All rights reserved.                                                      |
+ * |                                                                           |
+ * | Redistribution and use in source and binary forms, with or without        |
+ * | modification, are permitted provided that the following conditions        |
+ * | are met:                                                                  |
+ * |                                                                           |
+ * | 1. Redistributions of source code must retain the above copyright         |
+ * |    notice, this list of conditions and the following disclaimer.          |
+ * | 2. Redistributions in binary form must reproduce the above copyright      |
+ * |    notice, this list of conditions and the following disclaimer in the    |
+ * |    documentation and/or other materials provided with the distribution.   |
+ * |                                                                           |
+ * | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      |
+ * | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
+ * | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   |
+ * | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          |
+ * | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  |
+ * | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+ * | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |
+ * | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |
+ * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  |
+ * | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         |
+ * +---------------------------------------------------------------------------+
+ * | Author: Ryan T. Dean <rtdean@cytherianage.net>                            |
+ * | Heavily influenced by the Perl memcached client by Brad Fitzpatrick.      |
+ * |   Permission granted by Brad Fitzpatrick for relicense of ported Perl     |
+ * |   client logic under 2-clause BSD license.                                |
+ * +---------------------------------------------------------------------------+
+ *
+ * @file
+ * $TCAnet$
+ */
 
 /**
  * This is the PHP client for memcached - a distributed memory cache daemon.
@@ -239,17 +240,17 @@ class MWMemcached {
        /**
         * Memcache initializer
         *
-        * @param $args Associative array of settings
+        * @param $args Array Associative array of settings
         *
         * @return  mixed
         */
        public function __construct( $args ) {
                global $wgMemCachedTimeout;
-               $this->set_servers( @$args['servers'] );
-               $this->_debug = @$args['debug'];
+               $this->set_servers( isset( $args['servers'] ) ? $args['servers'] : array() );
+               $this->_debug = isset( $args['debug'] ) ? $args['debug'] : false;
                $this->stats = array();
-               $this->_compress_threshold = @$args['compress_threshold'];
-               $this->_persistant = array_key_exists( 'persistant', $args ) ? ( @$args['persistant'] ) : false;
+               $this->_compress_threshold = isset( $args['compress_threshold'] ) ? $args['compress_threshold'] : 0;
+               $this->_persistant = isset( $args['persistant'] ) ? $args['persistant'] : false;
                $this->_compress_enable = true;
                $this->_have_zlib = function_exists( 'gzcompress' );
 
@@ -272,7 +273,11 @@ class MWMemcached {
         *
         * @param $key String: key to set with data
         * @param $val Mixed: value to store
-        * @param $exp Integer: (optional) time to expire data at
+        * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
+        * to cache for (up to 30 days inclusive).  Any timespans of 30 days + 1 second or
+        * longer must be the timestamp of the time at which the mapping should expire. It
+        * is safe to use timestamps in all cases, regardless of exipration
+        * eg: strtotime("+3 hour")
         *
         * @return Boolean
         */
@@ -284,10 +289,10 @@ class MWMemcached {
        // {{{ decr()
 
        /**
-        * Decriment a value stored on the memcache server
+        * Decrease a value stored on the memcache server
         *
-        * @param $key String: key to decriment
-        * @param $amt Integer: (optional) amount to decriment
+        * @param $key String: key to decrease
+        * @param $amt Integer: (optional) amount to decrease
         *
         * @return Mixed: FALSE on failure, value on success
         */
@@ -318,7 +323,11 @@ class MWMemcached {
 
                $key = is_array( $key ) ? $key[1] : $key;
 
-               @$this->stats['delete']++;
+               if ( isset( $this->stats['delete'] ) ) {
+                       $this->stats['delete']++;
+               } else {
+                       $this->stats['delete'] = 1;
+               }
                $cmd = "delete $key $time\r\n";
                if( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
                        $this->_dead_sock( $sock );
@@ -401,7 +410,11 @@ class MWMemcached {
                        return false;
                }
 
-               @$this->stats['get']++;
+               if ( isset( $this->stats['get'] ) ) {
+                       $this->stats['get']++;
+               } else {
+                       $this->stats['get'] = 1;
+               }
 
                $cmd = "get $key\r\n";
                if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
@@ -438,7 +451,11 @@ class MWMemcached {
                        return false;
                }
 
-               @$this->stats['get_multi']++;
+               if ( isset( $this->stats['get_multi'] ) ) {
+                       $this->stats['get_multi']++;
+               } else {
+                       $this->stats['get_multi'] = 1;
+               }
                $sock_keys = array();
 
                foreach ( $keys as $key ) {
@@ -493,7 +510,9 @@ class MWMemcached {
         * @param $key String: key to increment
         * @param $amt Integer: (optional) amount to increment
         *
-        * @return Integer: new key value?
+        * @return Integer: null if the key does not exist yet (this does NOT
+        * create new mappings if the key does not exist). If the key does
+        * exist, this returns the new value for that key.
         */
        public function incr( $key, $amt = 1 ) {
                return $this->_incrdecr( 'incr', $key, $amt );
@@ -507,7 +526,11 @@ class MWMemcached {
         *
         * @param $key String: key to set value as
         * @param $value Mixed: value to store
-        * @param $exp Integer: (optional) experiation time
+        * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
+        * to cache for (up to 30 days inclusive).  Any timespans of 30 days + 1 second or
+        * longer must be the timestamp of the time at which the mapping should expire. It
+        * is safe to use timestamps in all cases, regardless of exipration
+        * eg: strtotime("+3 hour")
         *
         * @return Boolean
         */
@@ -564,7 +587,11 @@ class MWMemcached {
         *
         * @param $key String: key to set value as
         * @param $value Mixed: value to set
-        * @param $exp Integer: (optional) Experiation time
+        * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
+        * to cache for (up to 30 days inclusive).  Any timespans of 30 days + 1 second or
+        * longer must be the timestamp of the time at which the mapping should expire. It
+        * is safe to use timestamps in all cases, regardless of exipration
+        * eg: strtotime("+3 hour")
         *
         * @return Boolean: TRUE on success
         */
@@ -667,11 +694,13 @@ class MWMemcached {
                $timeout = $this->_connect_timeout;
                $errno = $errstr = null;
                for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
+                       wfSuppressWarnings();
                        if ( $this->_persistant == 1 ) {
-                               $sock = @pfsockopen( $ip, $port, $errno, $errstr, $timeout );
+                               $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
                        } else {
-                               $sock = @fsockopen( $ip, $port, $errno, $errstr, $timeout );
+                               $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
                        }
+                       wfRestoreWarnings();
                }
                if ( !$sock ) {
                        if ( $this->_debug ) {
@@ -702,7 +731,8 @@ class MWMemcached {
        }
 
        function _dead_host( $host ) {
-               @list( $ip, /* $port */) = explode( ':', $host );
+               $parts = explode( ':', $host );
+               $ip = $parts[0];
                $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
                $this->_host_dead[$host] = $this->_host_dead[$ip];
                unset( $this->_cache_sock[$host] );
@@ -801,7 +831,11 @@ class MWMemcached {
                }
 
                $key = is_array( $key ) ? $key[1] : $key;
-               @$this->stats[$cmd]++;
+               if ( isset( $this->stats[$cmd] ) ) {
+                       $this->stats[$cmd]++;
+               } else {
+                       $this->stats[$cmd] = 1;
+               }
                if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
                        return $this->_dead_sock( $sock );
                }
@@ -843,7 +877,11 @@ class MWMemcached {
                                        }
                                        $offset += $n;
                                        $bneed -= $n;
-                                       @$ret[$rkey] .= $data;
+                                       if ( isset( $ret[$rkey] ) ) {
+                                               $ret[$rkey] .= $data;
+                                       } else {
+                                               $ret[$rkey] = $data;
+                                       }
                                }
 
                                if ( $offset != $len + 2 ) {
@@ -883,7 +921,11 @@ class MWMemcached {
         * @param $cmd String: command to perform
         * @param $key String: key to act on
         * @param $val Mixed: what we need to store
-        * @param $exp Integer: when it should expire
+        * @param $exp Integer: (optional) Expiration time. This can be a number of seconds
+        * to cache for (up to 30 days inclusive).  Any timespans of 30 days + 1 second or
+        * longer must be the timestamp of the time at which the mapping should expire. It
+        * is safe to use timestamps in all cases, regardless of exipration
+        * eg: strtotime("+3 hour")
         *
         * @return Boolean
         * @access private
@@ -898,7 +940,11 @@ class MWMemcached {
                        return false;
                }
 
-               @$this->stats[$cmd]++;
+               if ( isset( $this->stats[$cmd] ) ) {
+                       $this->stats[$cmd]++;
+               } else {
+                       $this->stats[$cmd] = 1;
+               }
 
                $flags = 0;