Add UserStorageHandler::getField() method
authorMatthias Schmidt <gravatronics@live.com>
Thu, 13 Nov 2014 19:00:41 +0000 (20:00 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Thu, 13 Nov 2014 19:00:41 +0000 (20:00 +0100)
wcfsetup/install/files/lib/data/notice/Notice.class.php
wcfsetup/install/files/lib/data/user/User.class.php
wcfsetup/install/files/lib/data/user/UserProfile.class.php
wcfsetup/install/files/lib/system/moderation/queue/ModerationQueueManager.class.php
wcfsetup/install/files/lib/system/user/collapsible/content/UserCollapsibleContentHandler.class.php
wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php
wcfsetup/install/files/lib/system/user/storage/UserStorageHandler.class.php
wcfsetup/install/files/lib/system/visitTracker/VisitTracker.class.php

index 8465a801f1bda887df49ae49d6128e44241acbce..5491ab8fb6b3f9a6023f9f848eebbc6f188a1377 100644 (file)
@@ -59,9 +59,8 @@ class Notice extends DatabaseObject implements IRouteController {
                
                if ($this->isDismissed === null) {
                        if (WCF::getUser()->userID) {
-                               UserStorageHandler::getInstance()->loadStorage(array(WCF::getUser()->userID));
-                               $dismissedNotices = UserStorageHandler::getInstance()->getStorage(array(WCF::getUser()->userID), 'dismissedNotices');
-                               if ($dismissedNotices[WCF::getUser()->userID] === null) {
+                               $dismissedNotices = UserStorageHandler::getInstance()->getField('dismissedNotices');
+                               if ($dismissedNotices === null) {
                                        $sql = "SELECT  noticeID
                                                FROM    wcf".WCF_N."_notice_dismissed
                                                WHERE   userID = ?";
@@ -82,7 +81,7 @@ class Notice extends DatabaseObject implements IRouteController {
                                        UserStorageHandler::getInstance()->update(WCF::getUser()->userID, 'dismissedNotices', serialize($noticeIDs));
                                }
                                else {
-                                       $dismissedNoticeIDs = @unserialize($dismissedNotices[WCF::getUser()->userID]);
+                                       $dismissedNoticeIDs = @unserialize($dismissedNotices);
                                        $this->isDismissed = in_array($this->noticeID, $dismissedNoticeIDs);
                                }
                        }
index 658b098a7bb768e75bac9b48bfe96c6ac5d70585..f50a81ff4489f1813460b68054cc9de23c87782d 100644 (file)
@@ -154,14 +154,11 @@ final class User extends DatabaseObject implements IRouteController, IUserConten
                                $this->groupIDs = UserGroup::getGroupIDsByType(array(UserGroup::GUESTS, UserGroup::EVERYONE));
                        }
                        else {
-                               // load storage data
-                               UserStorageHandler::getInstance()->loadStorage(array($this->userID));
-                               
                                // get group ids
-                               $data = UserStorageHandler::getInstance()->getStorage(array($this->userID), 'groupIDs');
+                               $data = UserStorageHandler::getInstance()->getField('groupIDs', $this->userID);
                                
                                // cache does not exist or is outdated
-                               if ($data[$this->userID] === null || $skipCache) {
+                               if ($data === null || $skipCache) {
                                        $this->groupIDs = array();
                                        $sql = "SELECT  groupID
                                                FROM    wcf".WCF_N."_user_to_group
@@ -178,7 +175,7 @@ final class User extends DatabaseObject implements IRouteController, IUserConten
                                        }
                                }
                                else {
-                                       $this->groupIDs = unserialize($data[$this->userID]);
+                                       $this->groupIDs = unserialize($data);
                                }
                        }
                        
@@ -198,14 +195,11 @@ final class User extends DatabaseObject implements IRouteController, IUserConten
                        $this->languageIDs = array();
                        
                        if ($this->userID) {
-                               // load storage data
-                               UserStorageHandler::getInstance()->loadStorage(array($this->userID));
-                               
                                // get language ids
-                               $data = UserStorageHandler::getInstance()->getStorage(array($this->userID), 'languageIDs');
+                               $data = UserStorageHandler::getInstance()->getField('languageIDs', $this->userID);
                                
                                // cache does not exist or is outdated
-                               if ($data[$this->userID] === null) {
+                               if ($data === null) {
                                        $sql = "SELECT  languageID
                                                FROM    wcf".WCF_N."_user_to_language
                                                WHERE   userID = ?";
@@ -219,7 +213,7 @@ final class User extends DatabaseObject implements IRouteController, IUserConten
                                        UserStorageHandler::getInstance()->update($this->userID, 'languageIDs', serialize($this->languageIDs));
                                }
                                else {
-                                       $this->languageIDs = unserialize($data[$this->userID]);
+                                       $this->languageIDs = unserialize($data);
                                }
                        }
                        else if (!WCF::getSession()->spiderID) {
index ca900ed7d6a73d10e07f9fd7a02488463abccf88..8a65a1cd12a5c679461845ca49e7eafda6dd57fd 100644 (file)
@@ -114,14 +114,11 @@ class UserProfile extends DatabaseObjectDecorator implements IBreadcrumbProvider
                        $this->followingUserIDs = array();
                        
                        if ($this->userID) {
-                               // load storage data
-                               UserStorageHandler::getInstance()->loadStorage(array($this->userID));
-                               
                                // get ids
-                               $data = UserStorageHandler::getInstance()->getStorage(array($this->userID), 'followingUserIDs');
+                               $data = UserStorageHandler::getInstance()->getField('followingUserIDs', $this->userID);
                                
                                // cache does not exist or is outdated
-                               if ($data[$this->userID] === null) {
+                               if ($data === null) {
                                        $sql = "SELECT  followUserID
                                                FROM    wcf".WCF_N."_user_follow
                                                WHERE   userID = ?";
@@ -135,7 +132,7 @@ class UserProfile extends DatabaseObjectDecorator implements IBreadcrumbProvider
                                        UserStorageHandler::getInstance()->update($this->userID, 'followingUserIDs', serialize($this->followingUserIDs));
                                }
                                else {
-                                       $this->followingUserIDs = unserialize($data[$this->userID]);
+                                       $this->followingUserIDs = unserialize($data);
                                }
                        }
                }
@@ -153,14 +150,11 @@ class UserProfile extends DatabaseObjectDecorator implements IBreadcrumbProvider
                        $this->followerUserIDs = array();
                        
                        if ($this->userID) {
-                               // load storage data
-                               UserStorageHandler::getInstance()->loadStorage(array($this->userID));
-                               
                                // get ids
-                               $data = UserStorageHandler::getInstance()->getStorage(array($this->userID), 'followerUserIDs');
+                               $data = UserStorageHandler::getInstance()->getField('followerUserIDs', $this->userID);
                                
                                // cache does not exist or is outdated
-                               if ($data[$this->userID] === null) {
+                               if ($data === null) {
                                        $sql = "SELECT  userID
                                                FROM    wcf".WCF_N."_user_follow
                                                WHERE   followUserID = ?";
@@ -174,7 +168,7 @@ class UserProfile extends DatabaseObjectDecorator implements IBreadcrumbProvider
                                        UserStorageHandler::getInstance()->update($this->userID, 'followerUserIDs', serialize($this->followerUserIDs));
                                }
                                else {
-                                       $this->followerUserIDs = unserialize($data[$this->userID]);
+                                       $this->followerUserIDs = unserialize($data);
                                }
                        }
                }
@@ -192,14 +186,11 @@ class UserProfile extends DatabaseObjectDecorator implements IBreadcrumbProvider
                        $this->ignoredUserIDs = array();
                        
                        if ($this->userID) {
-                               // load storage data
-                               UserStorageHandler::getInstance()->loadStorage(array($this->userID));
-                               
                                // get ids
-                               $data = UserStorageHandler::getInstance()->getStorage(array($this->userID), 'ignoredUserIDs');
+                               $data = UserStorageHandler::getInstance()->getField('ignoredUserIDs', $this->userID);
                                
                                // cache does not exist or is outdated
-                               if ($data[$this->userID] === null) {
+                               if ($data === null) {
                                        $sql = "SELECT  ignoreUserID
                                                FROM    wcf".WCF_N."_user_ignore
                                                WHERE   userID = ?";
@@ -213,7 +204,7 @@ class UserProfile extends DatabaseObjectDecorator implements IBreadcrumbProvider
                                        UserStorageHandler::getInstance()->update($this->userID, 'ignoredUserIDs', serialize($this->ignoredUserIDs));
                                }
                                else {
-                                       $this->ignoredUserIDs = unserialize($data[$this->userID]);
+                                       $this->ignoredUserIDs = unserialize($data);
                                }
                        }
                }
@@ -262,16 +253,13 @@ class UserProfile extends DatabaseObjectDecorator implements IBreadcrumbProvider
                                if ($this->canSeeAvatar()) {
                                        if ($this->avatarID) {
                                                if (!$this->fileHash) {
-                                                       // load storage data
-                                                       UserStorageHandler::getInstance()->loadStorage(array($this->userID));
-                                                       $data = UserStorageHandler::getInstance()->getStorage(array($this->userID), 'avatar');
-                                                       
-                                                       if ($data[$this->userID] === null) {
+                                                       $data = UserStorageHandler::getInstance()->getField('avatar', $this->userID);
+                                                       if ($data === null) {
                                                                $this->avatar = new UserAvatar($this->avatarID);
                                                                UserStorageHandler::getInstance()->update($this->userID, 'avatar', serialize($this->avatar));
                                                        }
                                                        else {
-                                                               $this->avatar = unserialize($data[$this->userID]);
+                                                               $this->avatar = unserialize($data);
                                                        }
                                                }
                                                else {
@@ -616,15 +604,14 @@ class UserProfile extends DatabaseObjectDecorator implements IBreadcrumbProvider
                                }
                                else {
                                        // load storage data
-                                       UserStorageHandler::getInstance()->loadStorage(array($this->userID));
-                                       $data = UserStorageHandler::getInstance()->getStorage(array($this->userID), 'userRank');
+                                       $data = UserStorageHandler::getInstance()->getField('userRank', $this->userID);
                                        
-                                       if ($data[$this->userID] === null) {
+                                       if ($data === null) {
                                                $this->rank = new UserRank($this->rankID);
                                                UserStorageHandler::getInstance()->update($this->userID, 'userRank', serialize($this->rank));
                                        }
                                        else {
-                                               $this->rank = unserialize($data[$this->userID]);
+                                               $this->rank = unserialize($data);
                                        }
                                }
                        }
index 4ec7d9dd22571157cb10ed814da960a4d7a8da03..4864e93bdf3b7257385c75cee0f0019036233b2f 100644 (file)
@@ -208,12 +208,8 @@ class ModerationQueueManager extends SingletonFactory {
         * @return      integer
         */
        public function getOutstandingModerationCount() {
-               // load storage data
-               UserStorageHandler::getInstance()->loadStorage(array(WCF::getUser()->userID));
-               
                // get count
-               $data = UserStorageHandler::getInstance()->getStorage(array(WCF::getUser()->userID), 'outstandingModerationCount');
-               $count = $data[WCF::getUser()->userID];
+               $count = UserStorageHandler::getInstance()->getField('outstandingModerationCount');
                
                // cache does not exist or is outdated
                if ($count === null) {
index cc526bc8f38480ccb46ba1cdee746e95bde3a5ae..b68d958c77ac0af15c5dee9df89046e66a82c061 100644 (file)
@@ -87,14 +87,10 @@ class UserCollapsibleContentHandler extends SingletonFactory {
                        $this->collapsedContent[$objectTypeID] = array();
                        
                        if (WCF::getUser()->userID) {
-                               // get data from storage
-                               UserStorageHandler::getInstance()->loadStorage(array(WCF::getUser()->userID));
-                               
-                               // get ids
-                               $data = UserStorageHandler::getInstance()->getStorage(array(WCF::getUser()->userID), 'collapsedContent-'.$objectTypeID);
+                               $data = UserStorageHandler::getInstance()->getField('collapsedContent-'.$objectTypeID);
                                
                                // cache does not exist or is outdated
-                               if ($data[WCF::getUser()->userID] === null) {
+                               if ($data === null) {
                                        $sql = "SELECT  objectID
                                                FROM    wcf".WCF_N."_user_collapsible_content
                                                WHERE   objectTypeID = ?
@@ -112,7 +108,7 @@ class UserCollapsibleContentHandler extends SingletonFactory {
                                        UserStorageHandler::getInstance()->update(WCF::getUser()->userID, 'collapsedContent-'.$objectTypeID, serialize($this->collapsedContent[$objectTypeID]));
                                }
                                else {
-                                       $this->collapsedContent[$objectTypeID] = @unserialize($data[WCF::getUser()->userID]);
+                                       $this->collapsedContent[$objectTypeID] = @unserialize($data);
                                }
                        }
                        else {
index 72e618ea08334cfd513e228b1727c203aa134b62..4c0feaa8cfab7e5d105756f337feea254578a3d8 100644 (file)
@@ -239,11 +239,7 @@ class UserNotificationHandler extends SingletonFactory {
                        $this->notificationCount = 0;
                        
                        if (WCF::getUser()->userID) {
-                               // load storage data
-                               UserStorageHandler::getInstance()->loadStorage(array(WCF::getUser()->userID));
-                               
-                               // get ids
-                               $data = UserStorageHandler::getInstance()->getStorage(array(WCF::getUser()->userID), 'userNotificationCount');
+                               $data = UserStorageHandler::getInstance()->getField('userNotificationCount');
                                
                                // cache does not exist or is outdated
                                if ($data[WCF::getUser()->userID] === null || $skipCache) {
@@ -264,7 +260,7 @@ class UserNotificationHandler extends SingletonFactory {
                                        UserStorageHandler::getInstance()->update(WCF::getUser()->userID, 'userNotificationCount', serialize($this->notificationCount));
                                }
                                else {
-                                       $this->notificationCount = unserialize($data[WCF::getUser()->userID]);
+                                       $this->notificationCount = unserialize($data);
                                }
                        }
                }
index e8693d9f43d0a9db5375ea3480a577a16707d80f..731071939278ce22c131b1137d3b0d3bfb146b28 100644 (file)
@@ -86,6 +86,39 @@ class UserStorageHandler extends SingletonFactory {
                return $data;
        }
        
+       /**
+        * Returns the value of the given field for a certain user or null if no
+        * such value exists. If no userID is given, the id of the current user
+        * is used.
+        * 
+        * In contrast to getStorage(), this method calls loadStorage() if no stored
+        * data for the user has been loaded yet!
+        * 
+        * @param       string          $field
+        * @param       integer         $userID
+        * @return      mixed
+        */
+       public function getField($field, $userID = null) {
+               if ($userID === null) {
+                       $userID = WCF::getUser()->userID;
+               }
+               
+               if (!$userID) {
+                       return null;
+               }
+               
+               // make sure stored data is loaded
+               if (!isset($this->cache[$userID])) {
+                       $this->loadStorage(array($userID));
+               }
+               
+               if (isset($this->cache[$userID][$field])) {
+                       return $this->cache[$userID][$field];
+               }
+               
+               return null;
+       }
+       
        /**
         * Inserts new data records into database.
         * 
index 9d4a66a920d336d423768fa0213c64eca8c2a9a5..93d678361f44ec79cca6e29d40af25933924b852 100644 (file)
@@ -68,14 +68,10 @@ class VisitTracker extends SingletonFactory {
                
                if ($this->userVisits === null) {
                        if (WCF::getUser()->userID) {
-                               // get data from storage
-                               UserStorageHandler::getInstance()->loadStorage(array(WCF::getUser()->userID));
-                               
-                               // get ids
-                               $data = UserStorageHandler::getInstance()->getStorage(array(WCF::getUser()->userID), 'trackedUserVisits');
+                               $data = UserStorageHandler::getInstance()->getField('trackedUserVisits');
                                
                                // cache does not exist or is outdated
-                               if ($data[WCF::getUser()->userID] === null) {
+                               if ($data === null) {
                                        $this->userVisits = array();
                                        $sql = "SELECT  objectTypeID, visitTime
                                                FROM    wcf".WCF_N."_tracked_visit_type
@@ -90,7 +86,7 @@ class VisitTracker extends SingletonFactory {
                                        UserStorageHandler::getInstance()->update(WCF::getUser()->userID, 'trackedUserVisits', serialize($this->userVisits));
                                }
                                else {
-                                       $this->userVisits = @unserialize($data[WCF::getUser()->userID]);
+                                       $this->userVisits = @unserialize($data);
                                }
                        }
                        else {