Play safe and clear the article object internals, too (eg. mTouched).
[lhc/web/wiklou.git] / includes / BagOStuff.php
index 563127d..7f40001 100644 (file)
@@ -1,30 +1,33 @@
 <?php
-#
-# Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
-# http://www.mediawiki.org/
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# 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.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# http://www.gnu.org/copyleft/gpl.html
-
 /**
- * @defgroup Cache Cache
+ * Classes to cache objects in PHP accelerators, SQL database or DBA files
+ *
+ * Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
+ * http://www.mediawiki.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
  *
  * @file
  * @ingroup Cache
  */
 
+/**
+ * @defgroup Cache Cache
+ */
+
 /**
  * interface is intended to be more or less compatible with
  * the PHP memcached client.
@@ -102,8 +105,9 @@ abstract class BagOStuff {
        }
 
        public function add( $key, $value, $exptime = 0 ) {
-               if ( $this->get( $key ) == false ) {
+               if ( !$this->get( $key ) ) {
                        $this->set( $key, $value, $exptime );
+
                        return true;
                }
        }
@@ -126,18 +130,24 @@ abstract class BagOStuff {
                }
        }
 
+       /**
+        * @param $key String: Key to increase
+        * @param $value Integer: Value to add to $key (Default 1)
+        * @return null if lock is not possible else $key value increased by $value
+        */
        public function incr( $key, $value = 1 ) {
                if ( !$this->lock( $key ) ) {
-                       return false;
+                       return null;
                }
+
                $value = intval( $value );
 
-               $n = false;
                if ( ( $n = $this->get( $key ) ) !== false ) {
                        $n += $value;
                        $this->set( $key, $n ); // exptime?
                }
                $this->unlock( $key );
+
                return $n;
        }
 
@@ -146,8 +156,9 @@ abstract class BagOStuff {
        }
 
        public function debug( $text ) {
-               if ( $this->debugMode )
+               if ( $this->debugMode ) {
                        wfDebug( "BagOStuff debug: $text\n" );
+               }
        }
 
        /**
@@ -178,10 +189,13 @@ class HashBagOStuff extends BagOStuff {
 
        protected function expire( $key ) {
                $et = $this->bag[$key][1];
+
                if ( ( $et == 0 ) || ( $et > time() ) ) {
                        return false;
                }
+
                $this->delete( $key );
+
                return true;
        }
 
@@ -189,9 +203,11 @@ class HashBagOStuff extends BagOStuff {
                if ( !isset( $this->bag[$key] ) ) {
                        return false;
                }
+
                if ( $this->expire( $key ) ) {
                        return false;
                }
+
                return $this->bag[$key][0];
        }
 
@@ -203,7 +219,9 @@ class HashBagOStuff extends BagOStuff {
                if ( !isset( $this->bag[$key] ) ) {
                        return false;
                }
+
                unset( $this->bag[$key] );
+
                return true;
        }
 
@@ -222,13 +240,12 @@ class SqlBagOStuff extends BagOStuff {
        var $lastExpireAll = 0;
 
        protected function getDB() {
-               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' ) {
+                       if ( wfGetDB( DB_MASTER )->getType() == 'sqlite' ) {
                                $this->db = wfGetDB( DB_MASTER );
                        } else {
                                $this->lb = wfGetLBFactory()->newMainLB();
@@ -236,6 +253,7 @@ class SqlBagOStuff extends BagOStuff {
                                $this->db->clearFlag( DBO_TRX );
                        }
                }
+
                return $this->db;
        }
 
@@ -245,12 +263,14 @@ class SqlBagOStuff extends BagOStuff {
                $db = $this->getDB();
                $row = $db->selectRow( 'objectcache', array( 'value', 'exptime' ),
                        array( 'keyname' => $key ), __METHOD__ );
+
                if ( !$row ) {
                        $this->debug( 'get: no matching rows' );
                        return false;
                }
 
                $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
+
                if ( $this->isExpired( $row->exptime ) ) {
                        $this->debug( "get: key has expired, deleting" );
                        try {
@@ -266,26 +286,35 @@ class SqlBagOStuff extends BagOStuff {
                        } catch ( DBQueryError $e ) {
                                $this->handleWriteError( $e );
                        }
+
                        return false;
                }
+
                return $this->unserialize( $db->decodeBlob( $row->value ) );
        }
 
        public function set( $key, $value, $exptime = 0 ) {
                $db = $this->getDB();
                $exptime = intval( $exptime );
-               if ( $exptime < 0 ) $exptime = 0;
+
+               if ( $exptime < 0 ) {
+                       $exptime = 0;
+               }
+
                if ( $exptime == 0 ) {
                        $encExpiry = $this->getMaxDateTime();
                } else {
-                       if ( $exptime < 3.16e8 ) # ~10 years
+                       if ( $exptime < 3.16e8 ) # ~10 years
                                $exptime += time();
+                       }
+
                        $encExpiry = $db->timestamp( $exptime );
                }
                try {
                        $db->begin();
-                       $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
-                       $db->insert( 'objectcache',
+                       // (bug 24425) use a replace if the db supports it instead of
+                       // delete/insert to avoid clashes with conflicting keynames
+                       $db->replace( 'objectcache', array( 'keyname' ),
                                array(
                                        'keyname' => $key,
                                        'value' => $db->encodeBlob( $this->serialize( $value ) ),
@@ -294,21 +323,26 @@ class SqlBagOStuff extends BagOStuff {
                        $db->commit();
                } catch ( DBQueryError $e ) {
                        $this->handleWriteError( $e );
+
                        return false;
                }
+
                return true;
        }
 
        public function delete( $key, $time = 0 ) {
                $db = $this->getDB();
+
                try {
                        $db->begin();
                        $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
                        $db->commit();
                } catch ( DBQueryError $e ) {
                        $this->handleWriteError( $e );
+
                        return false;
                }
+
                return true;
        }
 
@@ -323,13 +357,15 @@ class SqlBagOStuff extends BagOStuff {
                        if ( $row === false ) {
                                // Missing
                                $db->commit();
-                               return false;
+
+                               return null;
                        }
                        $db->delete( 'objectcache', array( 'keyname' => $key ), __METHOD__ );
                        if ( $this->isExpired( $row->exptime ) ) {
                                // Expired, do not reinsert
                                $db->commit();
-                               return false;
+
+                               return null;
                        }
 
                        $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
@@ -343,8 +379,10 @@ class SqlBagOStuff extends BagOStuff {
                        $db->commit();
                } catch ( DBQueryError $e ) {
                        $this->handleWriteError( $e );
-                       return false;
+
+                       return null;
                }
+
                return $newValue;
        }
 
@@ -352,9 +390,11 @@ class SqlBagOStuff extends BagOStuff {
                $db = $this->getDB();
                $res = $db->select( 'objectcache', array( 'keyname' ), false, __METHOD__ );
                $result = array();
+
                foreach ( $res as $row ) {
                        $result[] = $row->keyname;
                }
+
                return $result;
        }
 
@@ -385,6 +425,7 @@ class SqlBagOStuff extends BagOStuff {
        public function expireAll() {
                $db = $this->getDB();
                $now = $db->timestamp();
+
                try {
                        $db->begin();
                        $db->delete( 'objectcache', array( 'exptime < ' . $db->addQuotes( $now ) ), __METHOD__ );
@@ -396,6 +437,7 @@ class SqlBagOStuff extends BagOStuff {
 
        public function deleteAll() {
                $db = $this->getDB();
+
                try {
                        $db->begin();
                        $db->delete( 'objectcache', '*', __METHOD__ );
@@ -415,6 +457,7 @@ class SqlBagOStuff extends BagOStuff {
         */
        protected function serialize( &$data ) {
                $serial = serialize( $data );
+
                if ( function_exists( 'gzdeflate' ) ) {
                        return gzdeflate( $serial );
                } else {
@@ -430,11 +473,14 @@ class SqlBagOStuff extends BagOStuff {
        protected function unserialize( $serial ) {
                if ( function_exists( 'gzinflate' ) ) {
                        $decomp = @gzinflate( $serial );
+
                        if ( false !== $decomp ) {
                                $serial = $decomp;
                        }
                }
+
                $ret = unserialize( $serial );
+
                return $ret;
        }
 
@@ -444,13 +490,16 @@ class SqlBagOStuff extends BagOStuff {
         */
        protected function handleWriteError( $exception ) {
                $db = $this->getDB();
+
                if ( !$db->wasReadOnlyError() ) {
                        throw $exception;
                }
+
                try {
                        $db->rollback();
                } catch ( DBQueryError $e ) {
                }
+
                wfDebug( __METHOD__ . ": ignoring query error\n" );
                $db->ignoreErrors( false );
        }
@@ -469,19 +518,23 @@ class MediaWikiBagOStuff extends SqlBagOStuff { }
 class APCBagOStuff extends BagOStuff {
        public function get( $key ) {
                $val = apc_fetch( $key );
+
                if ( is_string( $val ) ) {
                        $val = unserialize( $val );
                }
+
                return $val;
        }
 
        public function set( $key, $value, $exptime = 0 ) {
                apc_store( $key, serialize( $value ), $exptime );
+
                return true;
        }
 
        public function delete( $key, $time = 0 ) {
                apc_delete( $key );
+
                return true;
        }
 
@@ -489,9 +542,11 @@ class APCBagOStuff extends BagOStuff {
                $info = apc_cache_info( 'user' );
                $list = $info['cache_list'];
                $keys = array();
+
                foreach ( $list as $entry ) {
                        $keys[] = $entry['info'];
                }
+
                return $keys;
        }
 }
@@ -507,29 +562,35 @@ class APCBagOStuff extends BagOStuff {
 class eAccelBagOStuff extends BagOStuff {
        public function get( $key ) {
                $val = eaccelerator_get( $key );
+
                if ( is_string( $val ) ) {
                        $val = unserialize( $val );
                }
+
                return $val;
        }
 
        public function set( $key, $value, $exptime = 0 ) {
                eaccelerator_put( $key, serialize( $value ), $exptime );
+
                return true;
        }
 
        public function delete( $key, $time = 0 ) {
                eaccelerator_rm( $key );
+
                return true;
        }
 
        public function lock( $key, $waitTimeout = 0 ) {
                eaccelerator_lock( $key );
+
                return true;
        }
 
        public function unlock( $key ) {
                eaccelerator_unlock( $key );
+
                return true;
        }
 }
@@ -541,7 +602,6 @@ class eAccelBagOStuff extends BagOStuff {
  * @ingroup Cache
  */
 class XCacheBagOStuff extends BagOStuff {
-
        /**
         * Get a value from the XCache object cache
         *
@@ -550,8 +610,11 @@ class XCacheBagOStuff extends BagOStuff {
         */
        public function get( $key ) {
                $val = xcache_get( $key );
-               if ( is_string( $val ) )
+
+               if ( is_string( $val ) ) {
                        $val = unserialize( $val );
+               }
+
                return $val;
        }
 
@@ -565,6 +628,7 @@ class XCacheBagOStuff extends BagOStuff {
         */
        public function set( $key, $value, $expire = 0 ) {
                xcache_set( $key, serialize( $value ), $expire );
+
                return true;
        }
 
@@ -577,6 +641,7 @@ class XCacheBagOStuff extends BagOStuff {
         */
        public function delete( $key, $time = 0 ) {
                xcache_unset( $key );
+
                return true;
        }
 }
@@ -594,10 +659,12 @@ class DBABagOStuff extends BagOStuff {
 
        public function __construct( $dir = false ) {
                global $wgDBAhandler;
+
                if ( $dir === false ) {
                        global $wgTmpDirectory;
                        $dir = $wgTmpDirectory;
                }
+
                $this->mFile = "$dir/mw-cache-" . wfWikiID();
                $this->mFile .= '.db';
                wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
@@ -610,6 +677,7 @@ class DBABagOStuff extends BagOStuff {
        function encode( $value, $expiry ) {
                # Convert to absolute time
                $expiry = $this->convertExpiry( $expiry );
+
                return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
        }
 
@@ -633,29 +701,36 @@ class DBABagOStuff extends BagOStuff {
                } 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 );
 
@@ -667,6 +742,7 @@ class DBABagOStuff extends BagOStuff {
                        wfDebug( __METHOD__ . ": $key expired\n" );
                        $val = null;
                }
+
                wfProfileOut( __METHOD__ );
                return $val;
        }
@@ -674,13 +750,17 @@ class DBABagOStuff extends BagOStuff {
        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;
        }
@@ -688,27 +768,36 @@ class DBABagOStuff extends BagOStuff {
        function delete( $key, $time = 0 ) {
                wfProfileIn( __METHOD__ );
                wfDebug( __METHOD__ . "($key)\n" );
+
                $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 );
@@ -718,6 +807,7 @@ class DBABagOStuff extends BagOStuff {
                }
 
                dba_close( $handle );
+
                wfProfileOut( __METHOD__ );
                return $ret;
        }
@@ -725,13 +815,17 @@ class DBABagOStuff extends BagOStuff {
        function keys() {
                $reader = $this->getReader();
                $k1 = dba_firstkey( $reader );
+
                if ( !$k1 ) {
                        return array();
                }
+
                $result[] = $k1;
+
                while ( $key = dba_nextkey( $reader ) ) {
                        $result[] = $key;
                }
+
                return $result;
        }
 }
@@ -752,8 +846,11 @@ class WinCacheBagOStuff extends BagOStuff {
         */
        public function get( $key ) {
                $val = wincache_ucache_get( $key );
-               if ( is_string( $val ) )
+
+               if ( is_string( $val ) ) {
                        $val = unserialize( $val );
+               }
+
                return $val;
        }
 
@@ -766,8 +863,11 @@ class WinCacheBagOStuff extends BagOStuff {
         * @return bool
         */
        public function set( $key, $value, $expire = 0 ) {
-               wincache_ucache_set( $key, serialize( $value ), $expire );
-               return true;
+               $result = wincache_ucache_set( $key, serialize( $value ), $expire );
+
+               /* wincache_ucache_set returns an empty array on success if $value
+                  was an array, bool otherwise */
+               return ( is_array( $result ) && $result === array() ) || $result;
        }
 
        /**
@@ -779,6 +879,7 @@ class WinCacheBagOStuff extends BagOStuff {
         */
        public function delete( $key, $time = 0 ) {
                wincache_ucache_delete( $key );
+
                return true;
        }
 
@@ -786,9 +887,15 @@ class WinCacheBagOStuff extends BagOStuff {
                $info = wincache_ucache_info();
                $list = $info['ucache_entries'];
                $keys = array();
+
+               if ( is_null( $list ) ) {
+                       return array();
+               }
+
                foreach ( $list as $entry ) {
                        $keys[] = $entry['key_name'];
                }
+
                return $keys;
        }
 }