From 994ad779ce1eba6462c5a3fe59968b278e1b6f6c Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 26 Mar 2016 17:46:55 +0100 Subject: [PATCH] Use runtime caches in moderation queue report handlers --- .../CommentResponseRuntimeCache.class.php | 25 +++++ .../cache/runtime/UserRuntimeCache.class.php | 25 +++++ ...mentModerationQueueReportHandler.class.php | 91 +++++++------------ ...onseModerationQueueReportHandler.class.php | 81 +++++++---------- ...UserModerationQueueReportHandler.class.php | 65 +++++-------- 5 files changed, 138 insertions(+), 149 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/cache/runtime/CommentResponseRuntimeCache.class.php create mode 100644 wcfsetup/install/files/lib/system/cache/runtime/UserRuntimeCache.class.php diff --git a/wcfsetup/install/files/lib/system/cache/runtime/CommentResponseRuntimeCache.class.php b/wcfsetup/install/files/lib/system/cache/runtime/CommentResponseRuntimeCache.class.php new file mode 100644 index 0000000000..90a46f05bf --- /dev/null +++ b/wcfsetup/install/files/lib/system/cache/runtime/CommentResponseRuntimeCache.class.php @@ -0,0 +1,25 @@ + + * @package com.woltlab.wcf + * @subpackage system.cache.runtime + * @category Community Framework + * @since 2.2 + * + * @method CommentResponse getObject($objectID) + * @method CommentResponse[] getObjects(array $objectIDs) + */ +class CommentResponseRuntimeCache extends AbstractRuntimeCache { + /** + * @inheritDoc + */ + protected $listClassName = CommentResponseList::class; +} diff --git a/wcfsetup/install/files/lib/system/cache/runtime/UserRuntimeCache.class.php b/wcfsetup/install/files/lib/system/cache/runtime/UserRuntimeCache.class.php new file mode 100644 index 0000000000..ea6d931d95 --- /dev/null +++ b/wcfsetup/install/files/lib/system/cache/runtime/UserRuntimeCache.class.php @@ -0,0 +1,25 @@ + + * @package com.woltlab.wcf + * @subpackage system.cache.runtime + * @category Community Framework + * @since 2.2 + * + * @method User getObject($objectID) + * @method User[] getObjects(array $objectIDs) + */ +class UserRuntimeCache extends AbstractRuntimeCache { + /** + * @inheritDoc + */ + protected $listClassName = UserList::class; +} diff --git a/wcfsetup/install/files/lib/system/moderation/queue/report/CommentCommentModerationQueueReportHandler.class.php b/wcfsetup/install/files/lib/system/moderation/queue/report/CommentCommentModerationQueueReportHandler.class.php index dd6138204a..8abc4562bc 100644 --- a/wcfsetup/install/files/lib/system/moderation/queue/report/CommentCommentModerationQueueReportHandler.class.php +++ b/wcfsetup/install/files/lib/system/moderation/queue/report/CommentCommentModerationQueueReportHandler.class.php @@ -7,7 +7,8 @@ use wcf\data\comment\ViewableComment; use wcf\data\moderation\queue\ModerationQueue; use wcf\data\moderation\queue\ViewableModerationQueue; use wcf\data\object\type\ObjectTypeCache; -use wcf\system\database\util\PreparedStatementConditionBuilder; +use wcf\system\cache\runtime\CommentRuntimeCache; +use wcf\system\comment\manager\ICommentManager; use wcf\system\moderation\queue\AbstractModerationQueueHandler; use wcf\system\moderation\queue\ModerationQueueManager; use wcf\system\WCF; @@ -16,7 +17,7 @@ use wcf\system\WCF; * An implementation of IModerationQueueReportHandler for comments. * * @author Alexander Ebert - * @copyright 2001-2015 WoltLab GmbH + * @copyright 2001-2016 WoltLab GmbH * @license GNU Lesser General Public License * @package com.woltlab.wcf * @subpackage system.moderation.queue @@ -24,62 +25,45 @@ use wcf\system\WCF; */ class CommentCommentModerationQueueReportHandler extends AbstractModerationQueueHandler implements IModerationQueueReportHandler { /** - * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$className + * @inheritDoc */ - protected $className = 'wcf\data\comment\Comment'; + protected $className = Comment::class; /** - * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$definitionName + * @inheritDoc */ protected $definitionName = 'com.woltlab.wcf.moderation.report'; /** - * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$objectType + * @inheritDoc */ protected $objectType = 'com.woltlab.wcf.comment.comment'; - /** - * list of comments - * @var array<\wcf\data\comment\Comment> - */ - protected static $comments = array(); - /** * list of comment managers - * @var array<\wcf\system\comment\manager\ICommentManager> + * @var ICommentManager[] */ - protected static $commentManagers = array(); + protected static $commentManagers = []; /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::assignQueues() + * @inheritDoc */ public function assignQueues(array $queues) { - $assignments = array(); + $assignments = []; // read comments - $commentIDs = array(); + $commentIDs = []; foreach ($queues as $queue) { $commentIDs[] = $queue->objectID; } - $conditions = new PreparedStatementConditionBuilder(); - $conditions->add("commentID IN (?)", array($commentIDs)); - - $sql = "SELECT commentID, objectTypeID, objectID - FROM wcf".WCF_N."_comment - ".$conditions; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute($conditions->getParameters()); - $comments = array(); - while ($row = $statement->fetchArray()) { - $comments[$row['commentID']] = new Comment(null, $row); - } + $comments = CommentRuntimeCache::getInstance()->getObjects($commentIDs); - $orphanedQueueIDs = array(); + $orphanedQueueIDs = []; foreach ($queues as $queue) { $assignUser = false; - if (!isset($comments[$queue->objectID])) { + if ($comments[$queue->objectID] === null) { $orphanedQueueIDs[] = $queue->queueID; continue; } @@ -97,7 +81,7 @@ class CommentCommentModerationQueueReportHandler extends AbstractModerationQueue } /** - * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::canReport() + * @inheritDoc */ public function canReport($objectID) { if (!$this->isValid($objectID)) { @@ -113,32 +97,32 @@ class CommentCommentModerationQueueReportHandler extends AbstractModerationQueue } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::getContainerID() + * @inheritDoc */ public function getContainerID($objectID) { return 0; } /** - * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedContent() + * @inheritDoc */ public function getReportedContent(ViewableModerationQueue $queue) { - WCF::getTPL()->assign(array( + WCF::getTPL()->assign([ 'message' => ViewableComment::getComment($queue->objectID) - )); + ]); return WCF::getTPL()->fetch('moderationComment'); } /** - * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedObject() + * @inheritDoc */ public function getReportedObject($objectID) { return $this->getComment($objectID); } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::isValid() + * @inheritDoc */ public function isValid($objectID) { if ($this->getComment($objectID) === null) { @@ -152,24 +136,17 @@ class CommentCommentModerationQueueReportHandler extends AbstractModerationQueue * Returns a comment object by comment id or null if comment id is invalid. * * @param integer $objectID - * @return \wcf\data\comment\Comment + * @return Comment|null */ protected function getComment($objectID) { - if (!array_key_exists($objectID, self::$comments)) { - self::$comments[$objectID] = new Comment($objectID); - if (!self::$comments[$objectID]->commentID) { - self::$comments[$objectID] = null; - } - } - - return self::$comments[$objectID]; + return CommentRuntimeCache::getInstance()->getObject($objectID); } /** * Returns a comment manager for given comment. * - * @param \wcf\data\comment\Comment $comment - * @return \wcf\system\comment\manager\ICommentManager + * @param Comment $comment + * @return ICommentManager */ protected function getCommentManager(Comment $comment) { if (!isset(self::$commentManagers[$comment->objectTypeID])) { @@ -180,22 +157,18 @@ class CommentCommentModerationQueueReportHandler extends AbstractModerationQueue } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::populate() + * @inheritDoc */ public function populate(array $queues) { - $objectIDs = array(); + $objectIDs = []; foreach ($queues as $object) { $objectIDs[] = $object->objectID; } // fetch comments - $commentList = new CommentList(); - $commentList->setObjectIDs($objectIDs); - $commentList->readObjects(); - $comments = $commentList->getObjects(); - + $comments = CommentRuntimeCache::getInstance()->getObjects($objectIDs); foreach ($queues as $object) { - if (isset($comments[$object->objectID])) { + if ($comments[$object->objectID] !== null) { $object->setAffectedObject($comments[$object->objectID]); } else { @@ -205,11 +178,11 @@ class CommentCommentModerationQueueReportHandler extends AbstractModerationQueue } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::removeContent() + * @inheritDoc */ public function removeContent(ModerationQueue $queue, $message) { if ($this->isValid($queue->objectID)) { - $commentAction = new CommentAction(array($this->getComment($queue->objectID)), 'delete'); + $commentAction = new CommentAction([$this->getComment($queue->objectID)], 'delete'); $commentAction->executeAction(); } } diff --git a/wcfsetup/install/files/lib/system/moderation/queue/report/CommentResponseModerationQueueReportHandler.class.php b/wcfsetup/install/files/lib/system/moderation/queue/report/CommentResponseModerationQueueReportHandler.class.php index af4e75a645..225dd4f703 100644 --- a/wcfsetup/install/files/lib/system/moderation/queue/report/CommentResponseModerationQueueReportHandler.class.php +++ b/wcfsetup/install/files/lib/system/moderation/queue/report/CommentResponseModerationQueueReportHandler.class.php @@ -8,6 +8,8 @@ use wcf\data\comment\Comment; use wcf\data\comment\CommentList; use wcf\data\moderation\queue\ModerationQueue; use wcf\data\moderation\queue\ViewableModerationQueue; +use wcf\system\cache\runtime\CommentResponseRuntimeCache; +use wcf\system\cache\runtime\CommentRuntimeCache; use wcf\system\database\util\PreparedStatementConditionBuilder; use wcf\system\moderation\queue\ModerationQueueManager; use wcf\system\WCF; @@ -16,7 +18,7 @@ use wcf\system\WCF; * An implementation of IModerationQueueReportHandler for comment responses. * * @author Alexander Ebert - * @copyright 2001-2015 WoltLab GmbH + * @copyright 2001-2016 WoltLab GmbH * @license GNU Lesser General Public License * @package com.woltlab.wcf * @subpackage system.moderation.queue @@ -24,35 +26,29 @@ use wcf\system\WCF; */ class CommentResponseModerationQueueReportHandler extends CommentCommentModerationQueueReportHandler { /** - * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$className + * @inheritDoc */ - protected $className = 'wcf\data\comment\response\CommentResponse'; + protected $className = CommentResponse::class; /** - * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$objectType + * @inheritDoc */ protected $objectType = 'com.woltlab.wcf.comment.response'; /** - * list of comment responses - * @var array<\wcf\data\comment\response\CommentResponse> - */ - protected static $responses = array(); - - /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::assignQueues() + * @inheritDoc */ public function assignQueues(array $queues) { - $assignments = array(); + $assignments = []; // read comments and responses - $responseIDs = array(); + $responseIDs = []; foreach ($queues as $queue) { $responseIDs[] = $queue->objectID; } $conditions = new PreparedStatementConditionBuilder(); - $conditions->add("comment_response.responseID IN (?)", array($responseIDs)); + $conditions->add("comment_response.responseID IN (?)", [$responseIDs]); $sql = "SELECT comment_response.responseID, comment.commentID, comment.objectTypeID, comment.objectID FROM wcf".WCF_N."_comment_response comment_response @@ -61,13 +57,13 @@ class CommentResponseModerationQueueReportHandler extends CommentCommentModerati ".$conditions; $statement = WCF::getDB()->prepareStatement($sql); $statement->execute($conditions->getParameters()); - $comments = $responses = array(); + $comments = $responses = []; while ($row = $statement->fetchArray()) { $comments[$row['commentID']] = new Comment(null, $row); $responses[$row['responseID']] = new CommentResponse(null, $row); } - $orphanedQueueIDs = array(); + $orphanedQueueIDs = []; foreach ($queues as $queue) { $assignUser = false; @@ -89,7 +85,7 @@ class CommentResponseModerationQueueReportHandler extends CommentCommentModerati } /** - * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::canReport() + * @inheritDoc */ public function canReport($objectID) { if (!$this->isValid($objectID)) { @@ -106,32 +102,32 @@ class CommentResponseModerationQueueReportHandler extends CommentCommentModerati } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::getContainerID() + * @inheritDoc */ public function getContainerID($objectID) { return 0; } /** - * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedContent() + * @inheritDoc */ public function getReportedContent(ViewableModerationQueue $queue) { - WCF::getTPL()->assign(array( + WCF::getTPL()->assign([ 'message' => ViewableCommentResponse::getResponse($queue->objectID) - )); + ]); return WCF::getTPL()->fetch('moderationComment'); } /** - * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedObject() + * @inheritDoc */ public function getReportedObject($objectID) { return $this->getResponse($objectID); } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::isValid() + * @inheritDoc */ public function isValid($objectID) { if ($this->getResponse($objectID) === null) { @@ -145,49 +141,36 @@ class CommentResponseModerationQueueReportHandler extends CommentCommentModerati * Returns a comment response object by response id or null if response id is invalid. * * @param integer $objectID - * @return \wcf\data\comment\response\CommentResponse + * @return CommentResponse|null */ protected function getResponse($objectID) { - if (!array_key_exists($objectID, self::$responses)) { - self::$responses[$objectID] = new CommentResponse($objectID); - if (!self::$responses[$objectID]->responseID) { - self::$responses[$objectID] = null; - } - } - - return self::$responses[$objectID]; + return CommentResponseRuntimeCache::getInstance()->getObject($objectID); } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::populate() + * @inheritDoc */ public function populate(array $queues) { - $objectIDs = array(); + $objectIDs = []; foreach ($queues as $object) { $objectIDs[] = $object->objectID; } - // fetch responses - $responseList = new CommentResponseList(); - $responseList->setObjectIDs($objectIDs); - $responseList->readObjects(); - $responses = $responseList->getObjects(); + $responses = CommentResponseRuntimeCache::getInstance()->getObjects($objectIDs); - // fetch comments - $commentIDs = array(); + $commentIDs = []; foreach ($responses as $response) { - $commentIDs[] = $response->commentID; + if ($response !== null) { + $commentIDs[] = $response->commentID; + } } if (!empty($commentIDs)) { - $commentList = new CommentList(); - $commentList->setObjectIDs($commentIDs); - $commentList->readObjects(); - $comments = $commentList->getObjects(); + $comments = CommentRuntimeCache::getInstance()->getObjects($commentIDs); } foreach ($queues as $object) { - if (isset($responses[$object->objectID])) { + if ($responses[$object->objectID] !== null) { $response = $responses[$object->objectID]; $response->setComment($comments[$response->commentID]); @@ -200,11 +183,11 @@ class CommentResponseModerationQueueReportHandler extends CommentCommentModerati } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::removeContent() + * @inheritDoc */ public function removeContent(ModerationQueue $queue, $message) { if ($this->isValid($queue->objectID)) { - $responseAction = new CommentResponseAction(array($this->getResponse($queue->objectID)), 'delete'); + $responseAction = new CommentResponseAction([$this->getResponse($queue->objectID)], 'delete'); $responseAction->executeAction(); } } diff --git a/wcfsetup/install/files/lib/system/moderation/queue/report/UserModerationQueueReportHandler.class.php b/wcfsetup/install/files/lib/system/moderation/queue/report/UserModerationQueueReportHandler.class.php index 9c89be0103..833d4d1012 100644 --- a/wcfsetup/install/files/lib/system/moderation/queue/report/UserModerationQueueReportHandler.class.php +++ b/wcfsetup/install/files/lib/system/moderation/queue/report/UserModerationQueueReportHandler.class.php @@ -5,6 +5,7 @@ use wcf\data\moderation\queue\ViewableModerationQueue; use wcf\data\user\User; use wcf\data\user\UserList; use wcf\data\user\UserProfile; +use wcf\system\cache\runtime\UserRuntimeCache; use wcf\system\exception\SystemException; use wcf\system\moderation\queue\AbstractModerationQueueHandler; use wcf\system\moderation\queue\ModerationQueueManager; @@ -14,7 +15,7 @@ use wcf\system\WCF; * An implementation of IModerationQueueReportHandler for user profiles. * * @author Marcel Werk - * @copyright 2001-2015 WoltLab GmbH + * @copyright 2001-2016 WoltLab GmbH * @license GNU Lesser General Public License * @package com.woltlab.wcf * @subpackage system.moderation.queue @@ -22,37 +23,31 @@ use wcf\system\WCF; */ class UserModerationQueueReportHandler extends AbstractModerationQueueHandler implements IModerationQueueReportHandler { /** - * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$className + * @inheritDoc */ - protected $className = 'wcf\data\user\User'; + protected $className = User::class; /** - * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$definitionName + * @inheritDoc */ protected $definitionName = 'com.woltlab.wcf.moderation.report'; /** - * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$objectType + * @inheritDoc */ protected $objectType = 'com.woltlab.wcf.user'; /** - * list of users - * @var array<\wcf\data\user\User> - */ - protected static $users = array(); - - /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::assignQueues() + * @inheritDoc */ public function assignQueues(array $queues) { - $assignments = array(); + $assignments = []; foreach ($queues as $queue) { $assignUser = false; if (WCF::getSession()->getPermission('mod.general.canUseModeration')) { $assignUser = true; } - + $assignments[$queue->queueID] = $assignUser; } @@ -60,7 +55,7 @@ class UserModerationQueueReportHandler extends AbstractModerationQueueHandler im } /** - * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::canReport() + * @inheritDoc */ public function canReport($objectID) { if (!$this->isValid($objectID)) { @@ -71,25 +66,25 @@ class UserModerationQueueReportHandler extends AbstractModerationQueueHandler im } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::getContainerID() + * @inheritDoc */ public function getContainerID($objectID) { return 0; } /** - * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedContent() + * @inheritDoc */ public function getReportedContent(ViewableModerationQueue $queue) { - WCF::getTPL()->assign(array( + WCF::getTPL()->assign([ 'user' => new UserProfile($queue->getAffectedObject()) - )); + ]); return WCF::getTPL()->fetch('moderationUser'); } /** - * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedObject() + * @inheritDoc */ public function getReportedObject($objectID) { if ($this->isValid($objectID)) { @@ -100,7 +95,7 @@ class UserModerationQueueReportHandler extends AbstractModerationQueueHandler im } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::isValid() + * @inheritDoc */ public function isValid($objectID) { if ($this->getUser($objectID) === null) { @@ -114,36 +109,24 @@ class UserModerationQueueReportHandler extends AbstractModerationQueueHandler im * Returns a user object by user id or null if user id is invalid. * * @param integer $objectID - * @return \wcf\data\user\User + * @return User|null */ protected function getUser($objectID) { - if (!array_key_exists($objectID, self::$users)) { - self::$users[$objectID] = new User($objectID); - if (!self::$users[$objectID]->userID) { - self::$users[$objectID] = null; - } - } - - return self::$users[$objectID]; + return UserRuntimeCache::getInstance()->getObject($objectID); } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::populate() + * @inheritDoc */ public function populate(array $queues) { - $objectIDs = array(); + $objectIDs = []; foreach ($queues as $object) { $objectIDs[] = $object->objectID; } - // fetch users - $userList = new UserList(); - $userList->setObjectIDs($objectIDs); - $userList->readObjects(); - $users = $userList->getObjects(); - + $users = UserRuntimeCache::getInstance()->getObjects($objectIDs); foreach ($queues as $object) { - if (isset($users[$object->objectID])) { + if ($users[$object->objectID] !== null) { $object->setAffectedObject($users[$object->objectID]); } else { @@ -153,14 +136,14 @@ class UserModerationQueueReportHandler extends AbstractModerationQueueHandler im } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::canRemoveContent() + * @inheritDoc */ public function canRemoveContent(ModerationQueue $queue) { return false; } /** - * @see \wcf\system\moderation\queue\IModerationQueueHandler::removeContent() + * @inheritDoc */ public function removeContent(ModerationQueue $queue, $message) { throw new SystemException("it's not allowed to delete users using the moderation"); -- 2.20.1