[FEATURE] Use update-query to write fe_session data
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Wed, 15 Jun 2011 22:50:45 +0000 (00:50 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sun, 31 Jul 2011 11:59:25 +0000 (13:59 +0200)
Use an update-query to write the front end user session data
to the database instead of doing a delete- and insert-query.

Using only one update-query saves resources and avoids
an inconsistent state between the delete- and insert-query
(since no transactions were used).

Original patch by Netresearch.

Change-Id: I58624215b65bced7473923eb6ce1966650c02860
Resolves: #19286
Reviewed-on: http://review.typo3.org/2761
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
Reviewed-by: Xavier Perseguers
Reviewed-by: Markus Klein
Reviewed-by: Mario Rimann
Reviewed-by: Jo Hasenau
Reviewed-by: Georg Ringer
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/cms/tslib/class.tslib_feuserauth.php

index c7754a1..b047c08 100644 (file)
@@ -83,7 +83,7 @@ class tslib_feUserAuth extends t3lib_userAuth {
        var $sesData = Array();
        var $sesData_change = 0;
        var $userData_change = 0;
-       protected $sessionDataTimestamp;
+       protected $sessionDataTimestamp = NULL;
 
        /**
         * Default constructor.
@@ -386,7 +386,8 @@ class tslib_feUserAuth extends t3lib_userAuth {
        /**
         * Will write UC and session data.
         * If the flag $this->userData_change has been set, the function ->writeUC is called (which will save persistent user session data)
-        * If the flag $this->sesData_change has been set, the fe_session_data table is updated with the content of $this->sesData (deleting any old record, inserting new)
+        * If the flag $this->sesData_change has been set, the fe_session_data table is updated with the content of $this->sesData
+        * If the $this->sessionDataTimestamp is NULL there was no session record yet, so we need to insert it into the database
         *
         * @return      void
         * @see fetchSessionData(), getKey(), setKey()
@@ -396,15 +397,25 @@ class tslib_feUserAuth extends t3lib_userAuth {
                if ($this->userData_change)     {
                        $this->writeUC('');
                }
-               if ($this->sesData_change)      {
-                       if ($this->id)  {
-                               $insertFields = array (
+
+               if ($this->sesData_change && $this->id) {
+                       if ($this->sessionDataTimestamp === NULL) {
+                                       // Write new session-data
+                               $insertFields = array(
                                        'hash' => $this->id,
                                        'content' => serialize($this->sesData),
-                                       'tstamp' => $GLOBALS['EXEC_TIME'],
+                                       'tstamp' => $GLOBALS['EXEC_TIME']
                                );
-                               $this->removeSessionData();
+                               $this->sessionDataTimestamp = $GLOBALS['EXEC_TIME'];
                                $GLOBALS['TYPO3_DB']->exec_INSERTquery('fe_session_data', $insertFields);
+                       } else {
+                                       // Update session data
+                               $updateFields = array(
+                                       'content' => serialize($this->sesData),
+                                       'tstamp' => $GLOBALS['EXEC_TIME'],
+                               );
+                               $this->sessionDataTimestamp = $GLOBALS['EXEC_TIME'];
+                               $GLOBALS['TYPO3_DB']->exec_UPDATEquery('fe_session_data', 'hash=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, 'fe_session_data'), $updateFields);
                        }
                }
        }
@@ -561,7 +572,7 @@ class tslib_feUserAuth extends t3lib_userAuth {
                        // Check if there are any fe_session_data records for the session ID the client claims to have
                if ($count == FALSE) {
                        $statement = $GLOBALS['TYPO3_DB']->prepare_SELECTquery(
-                               'content',
+                               'content,tstamp',
                                'fe_session_data',
                                'hash = :hash'
                        );
@@ -570,6 +581,7 @@ class tslib_feUserAuth extends t3lib_userAuth {
                                if ($sesDataRow = $statement->fetch()) {
                                        $count = TRUE;
                                        $this->sesData = unserialize($sesDataRow['content']);
+                                       $this->sessionDataTimestamp = $sesDataRow['tstamp'];
                                }
                                $statement->free();
                        }