From 46028d805b40f9ed20e31e57455b631b1b087507 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Thu, 13 Nov 2014 20:00:41 +0100 Subject: [PATCH] Add UserStorageHandler::getField() method --- .../files/lib/data/notice/Notice.class.php | 7 ++- .../files/lib/data/user/User.class.php | 18 +++----- .../files/lib/data/user/UserProfile.class.php | 43 +++++++------------ .../queue/ModerationQueueManager.class.php | 6 +-- .../UserCollapsibleContentHandler.class.php | 10 ++--- .../UserNotificationHandler.class.php | 8 +--- .../user/storage/UserStorageHandler.class.php | 33 ++++++++++++++ .../visitTracker/VisitTracker.class.php | 10 ++--- 8 files changed, 66 insertions(+), 69 deletions(-) diff --git a/wcfsetup/install/files/lib/data/notice/Notice.class.php b/wcfsetup/install/files/lib/data/notice/Notice.class.php index 8465a801f1..5491ab8fb6 100644 --- a/wcfsetup/install/files/lib/data/notice/Notice.class.php +++ b/wcfsetup/install/files/lib/data/notice/Notice.class.php @@ -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); } } diff --git a/wcfsetup/install/files/lib/data/user/User.class.php b/wcfsetup/install/files/lib/data/user/User.class.php index 658b098a7b..f50a81ff44 100644 --- a/wcfsetup/install/files/lib/data/user/User.class.php +++ b/wcfsetup/install/files/lib/data/user/User.class.php @@ -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) { diff --git a/wcfsetup/install/files/lib/data/user/UserProfile.class.php b/wcfsetup/install/files/lib/data/user/UserProfile.class.php index ca900ed7d6..8a65a1cd12 100644 --- a/wcfsetup/install/files/lib/data/user/UserProfile.class.php +++ b/wcfsetup/install/files/lib/data/user/UserProfile.class.php @@ -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); } } } diff --git a/wcfsetup/install/files/lib/system/moderation/queue/ModerationQueueManager.class.php b/wcfsetup/install/files/lib/system/moderation/queue/ModerationQueueManager.class.php index 4ec7d9dd22..4864e93bdf 100644 --- a/wcfsetup/install/files/lib/system/moderation/queue/ModerationQueueManager.class.php +++ b/wcfsetup/install/files/lib/system/moderation/queue/ModerationQueueManager.class.php @@ -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) { diff --git a/wcfsetup/install/files/lib/system/user/collapsible/content/UserCollapsibleContentHandler.class.php b/wcfsetup/install/files/lib/system/user/collapsible/content/UserCollapsibleContentHandler.class.php index cc526bc8f3..b68d958c77 100644 --- a/wcfsetup/install/files/lib/system/user/collapsible/content/UserCollapsibleContentHandler.class.php +++ b/wcfsetup/install/files/lib/system/user/collapsible/content/UserCollapsibleContentHandler.class.php @@ -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 { diff --git a/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php b/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php index 72e618ea08..4c0feaa8cf 100644 --- a/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php +++ b/wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php @@ -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); } } } diff --git a/wcfsetup/install/files/lib/system/user/storage/UserStorageHandler.class.php b/wcfsetup/install/files/lib/system/user/storage/UserStorageHandler.class.php index e8693d9f43..7310719392 100644 --- a/wcfsetup/install/files/lib/system/user/storage/UserStorageHandler.class.php +++ b/wcfsetup/install/files/lib/system/user/storage/UserStorageHandler.class.php @@ -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. * diff --git a/wcfsetup/install/files/lib/system/visitTracker/VisitTracker.class.php b/wcfsetup/install/files/lib/system/visitTracker/VisitTracker.class.php index 9d4a66a920..93d678361f 100644 --- a/wcfsetup/install/files/lib/system/visitTracker/VisitTracker.class.php +++ b/wcfsetup/install/files/lib/system/visitTracker/VisitTracker.class.php @@ -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 { -- 2.20.1