Merge "Slight improvements to FormSpecialPage behavior."
[lhc/web/wiklou.git] / includes / objectcache / MemcachedClient.php
index 441c02b..e5f60b5 100644 (file)
@@ -55,9 +55,9 @@
  *              'compress_threshold' => 10240,
  *              'persistent' => true));
  *
- * $mc->add('key', array('some', 'array'));
- * $mc->replace('key', 'some random string');
- * $val = $mc->get('key');
+ * $mc->add( 'key', array( 'some', 'array' ) );
+ * $mc->replace( 'key', 'some random string' );
+ * $val = $mc->get( 'key' );
  *
  * @author  Ryan T. Dean <rtdean@cytherianage.net>
  * @version 0.1.2
@@ -241,7 +241,7 @@ class MWMemcached {
        /**
         * Memcache initializer
         *
-        * @param $args Array Associative array of settings
+        * @param array $args Associative array of settings
         *
         * @return  mixed
         */
@@ -271,7 +271,7 @@ class MWMemcached {
         * Adds a key/value to the memcache server if one isn't already set with
         * that key
         *
-        * @param $key String: key to set with data
+        * @param string $key key to set with data
         * @param $val Mixed: value to store
         * @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
@@ -291,7 +291,7 @@ class MWMemcached {
        /**
         * Decrease a value stored on the memcache server
         *
-        * @param $key String: key to decrease
+        * @param string $key key to decrease
         * @param $amt Integer: (optional) amount to decrease
         *
         * @return Mixed: FALSE on failure, value on success
@@ -306,7 +306,7 @@ class MWMemcached {
        /**
         * Deletes a key from the server, optionally after $time
         *
-        * @param $key String: key to delete
+        * @param string $key key to delete
         * @param $time Integer: (optional) how long to wait before deleting
         *
         * @return Boolean: TRUE on success, FALSE on failure
@@ -329,7 +329,7 @@ class MWMemcached {
                        $this->stats['delete'] = 1;
                }
                $cmd = "delete $key $time\r\n";
-               if( !$this->_fwrite( $sock, $cmd ) ) {
+               if ( !$this->_fwrite( $sock, $cmd ) ) {
                        return false;
                }
                $res = $this->_fgets( $sock );
@@ -406,7 +406,7 @@ class MWMemcached {
        /**
         * Retrieves the value associated with the key from the memcache server
         *
-        * @param $key array|string key to retrieve
+        * @param array|string $key key to retrieve
         * @param $casToken[optional] Float
         *
         * @return Mixed
@@ -466,7 +466,7 @@ class MWMemcached {
        /**
         * Get multiple keys from the server(s)
         *
-        * @param $keys Array: keys to retrieve
+        * @param array $keys keys to retrieve
         *
         * @return Array
         */
@@ -489,17 +489,17 @@ class MWMemcached {
                        }
                        $key = is_array( $key ) ? $key[1] : $key;
                        if ( !isset( $sock_keys[$sock] ) ) {
-                               $sock_keys[ intval( $sock ) ] = array();
+                               $sock_keys[intval( $sock )] = array();
                                $socks[] = $sock;
                        }
-                       $sock_keys[ intval( $sock ) ][] = $key;
+                       $sock_keys[intval( $sock )][] = $key;
                }
 
                $gather = array();
                // Send out the requests
                foreach ( $socks as $sock ) {
                        $cmd = 'gets';
-                       foreach ( $sock_keys[ intval( $sock ) ] as $key ) {
+                       foreach ( $sock_keys[intval( $sock )] as $key ) {
                                $cmd .= ' ' . $key;
                        }
                        $cmd .= "\r\n";
@@ -530,7 +530,7 @@ class MWMemcached {
        /**
         * Increments $key (optionally) by $amt
         *
-        * @param $key String: key to increment
+        * @param string $key key to increment
         * @param $amt Integer: (optional) amount to increment
         *
         * @return Integer: null if the key does not exist yet (this does NOT
@@ -547,7 +547,7 @@ class MWMemcached {
        /**
         * Overwrites an existing value for key; only works if key is already set
         *
-        * @param $key String: key to set value as
+        * @param string $key key to set value as
         * @param $value Mixed: value to store
         * @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
@@ -569,7 +569,7 @@ class MWMemcached {
         * output as an array (null array if no output)
         *
         * @param $sock Resource: socket to send command on
-        * @param $cmd String: command to run
+        * @param string $cmd command to run
         *
         * @return Array: output array
         */
@@ -603,7 +603,7 @@ class MWMemcached {
         * Unconditionally sets a key to a given value in the memcache.  Returns true
         * if set successfully.
         *
-        * @param $key String: key to set value as
+        * @param string $key key to set value as
         * @param $value Mixed: value to set
         * @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
@@ -625,7 +625,7 @@ class MWMemcached {
         * to a known, given value.  Returns true if set successfully.
         *
         * @param $casToken Float: current known value
-        * @param $key String: key to set value as
+        * @param string $key key to set value as
         * @param $value Mixed: value to set
         * @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
@@ -671,7 +671,7 @@ class MWMemcached {
        /**
         * Sets the server list to distribute key gets and puts between
         *
-        * @param $list Array of servers to connect to
+        * @param array $list of servers to connect to
         *
         * @see     MWMemcached::__construct()
         */
@@ -706,7 +706,7 @@ class MWMemcached {
        /**
         * Close the specified socket
         *
-        * @param $sock String: socket to close
+        * @param string $sock socket to close
         *
         * @access  private
         */
@@ -723,7 +723,7 @@ class MWMemcached {
         * Connects $sock to $host, timing out after $timeout
         *
         * @param $sock Integer: socket to connect
-        * @param $host String: Host:IP to connect to
+        * @param string $host Host:IP to connect to
         *
         * @return  boolean
         * @access  private
@@ -733,7 +733,7 @@ class MWMemcached {
                $sock = false;
                $timeout = $this->_connect_timeout;
                $errno = $errstr = null;
-               for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
+               for ( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
                        wfSuppressWarnings();
                        if ( $this->_persistent == 1 ) {
                                $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
@@ -765,7 +765,7 @@ class MWMemcached {
        /**
         * Marks a host as dead until 30-40 seconds in the future
         *
-        * @param $sock String: socket to mark as dead
+        * @param string $sock socket to mark as dead
         *
         * @access  private
         */
@@ -791,7 +791,7 @@ class MWMemcached {
        /**
         * get_sock
         *
-        * @param $key String: key to retrieve value for;
+        * @param string $key key to retrieve value for;
         *
         * @return Mixed: resource on success, false on failure
         * @access private
@@ -810,7 +810,7 @@ class MWMemcached {
                        $bu = array();
                        foreach ( $this->_servers as $v ) {
                                if ( is_array( $v ) ) {
-                                       for( $i = 0; $i < $v[1]; $i++ ) {
+                                       for ( $i = 0; $i < $v[1]; $i++ ) {
                                                $bu[] = $v[0];
                                        }
                                } else {
@@ -822,7 +822,7 @@ class MWMemcached {
                }
 
                $realkey = is_array( $key ) ? $key[1] : $key;
-               for( $tries = 0; $tries < 20; $tries++ ) {
+               for ( $tries = 0; $tries < 20; $tries++ ) {
                        $host = $this->_buckets[$hv % $this->_bucketcount];
                        $sock = $this->sock_to_host( $host );
                        if ( is_resource( $sock ) ) {
@@ -840,7 +840,7 @@ class MWMemcached {
        /**
         * Creates a hash integer based on the $key
         *
-        * @param $key String: key to hash
+        * @param string $key key to hash
         *
         * @return Integer: hash value
         * @access private
@@ -858,8 +858,8 @@ class MWMemcached {
        /**
         * Perform increment/decriment on $key
         *
-        * @param $cmd String command to perform
-        * @param $key String|array key to perform it on
+        * @param string $cmd command to perform
+        * @param string|array $key key to perform it on
         * @param $amt Integer amount to adjust
         *
         * @return Integer: new value of $key
@@ -900,7 +900,7 @@ class MWMemcached {
         * Load items into $ret from $sock
         *
         * @param $sock Resource: socket to read from
-        * @param $ret Array: returned values
+        * @param array $ret returned values
         * @param $casToken[optional] Float
         * @return boolean True for success, false for failure
         *
@@ -911,10 +911,21 @@ class MWMemcached {
 
                while ( 1 ) {
                        $decl = $this->_fgets( $sock );
-                       if( $decl === false ) {
+
+                       if ( $decl === false ) {
+                               /*
+                                * If nothing can be read, something is wrong because we know exactly when
+                                * to stop reading (right after "END") and we return right after that.
+                                */
                                return false;
                        } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+) (\d+)$/', $decl, $match ) ) {
-
+                               /*
+                                * Read all data returned. This can be either one or multiple values.
+                                * Save all that data (in an array) to be processed later: we'll first
+                                * want to continue reading until "END" before doing anything else,
+                                * to make sure that we don't leave our client in a state where it's
+                                * output is not yet fully read.
+                                */
                                $results[] = array(
                                        $match[1], // rkey
                                        $match[2], // flags
@@ -927,6 +938,10 @@ class MWMemcached {
                                        return false;
                                }
 
+                               /**
+                                * All data has been read, time to process the data and build
+                                * meaningful return values.
+                                */
                                foreach ( $results as $vars ) {
                                        list( $rkey, $flags, $len, $casToken, $data ) = $vars;
 
@@ -942,6 +957,15 @@ class MWMemcached {
                                                $ret[$rkey] = gzuncompress( $ret[$rkey] );
                                        }
 
+                                       /*
+                                        * This unserialize is the exact reason that we only want to
+                                        * process data after having read until "END" (instead of doing
+                                        * this right away): "unserialize" can trigger outside code:
+                                        * in the event that $ret[$rkey] is a serialized object,
+                                        * unserializing it will trigger __wakeup() if present. If that
+                                        * function attempted to read from memcached (while we did not
+                                        * yet read "END"), these 2 calls would collide.
+                                        */
                                        if ( $flags & self::SERIALIZED ) {
                                                $ret[$rkey] = unserialize( $ret[$rkey] );
                                        }
@@ -961,8 +985,8 @@ class MWMemcached {
        /**
         * Performs the requested storage operation to the memcache server
         *
-        * @param $cmd String: command to perform
-        * @param $key String: key to act on
+        * @param string $cmd command to perform
+        * @param string $key key to act on
         * @param $val Mixed: what we need to store
         * @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
@@ -1044,7 +1068,7 @@ class MWMemcached {
        /**
         * Returns the socket for the host
         *
-        * @param $host String: Host:IP to get socket for
+        * @param string $host Host:IP to get socket for
         *
         * @return Mixed: IO Stream or false
         * @access private
@@ -1099,7 +1123,7 @@ class MWMemcached {
        function _fwrite( $sock, $buf ) {
                $bytesWritten = 0;
                $bufSize = strlen( $buf );
-               while ( $bytesWritten < $bufSize  ) {
+               while ( $bytesWritten < $bufSize ) {
                        $result = fwrite( $sock, $buf );
                        $data = stream_get_meta_data( $sock );
                        if ( $data['timed_out'] ) {