2 namespace wcf\system\comment
;
3 use wcf\data\comment\response\CommentResponseList
;
4 use wcf\data\comment\CommentEditor
;
5 use wcf\data\comment\CommentList
;
6 use wcf\data\comment\StructuredCommentList
;
7 use wcf\data\
object\type\ObjectTypeCache
;
8 use wcf\system\comment\manager\ICommentManager
;
9 use wcf\system\exception\NamedUserException
;
10 use wcf\system\exception\SystemException
;
11 use wcf\system\like\LikeHandler
;
12 use wcf\system\user\activity\event\UserActivityEventHandler
;
13 use wcf\system\user\notification\UserNotificationHandler
;
14 use wcf\system\SingletonFactory
;
18 * Provides methods for comment object handling.
20 * @author Alexander Ebert
21 * @copyright 2001-2014 WoltLab GmbH
22 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
23 * @package com.woltlab.wcf
24 * @subpackage system.comment
25 * @category Community Framework
27 class CommentHandler
extends SingletonFactory
{
32 protected $cache = null;
35 * @see \wcf\system\SingletonFactory::init()
37 protected function init() {
39 'objectTypes' => array(),
40 'objectTypeIDs' => array()
43 $cache = ObjectTypeCache
::getInstance()->getObjectTypes('com.woltlab.wcf.comment.commentableContent');
44 foreach ($cache as $objectType) {
45 $this->cache
['objectTypes'][$objectType->objectTypeID
] = $objectType;
46 $this->cache
['objectTypeIDs'][$objectType->objectType
] = $objectType->objectTypeID
;
51 * Returns the object type id for a given object type.
53 * @param string $objectType
56 public function getObjectTypeID($objectType) {
57 if (isset($this->cache
['objectTypeIDs'][$objectType])) {
58 return $this->cache
['objectTypeIDs'][$objectType];
65 * Returns the object type for a given object type id.
67 * @param integer $objectTypeID
68 * @return \wcf\data\object\type\ObjectType
70 public function getObjectType($objectTypeID) {
71 if (isset($this->cache
['objectTypes'][$objectTypeID])) {
72 return $this->cache
['objectTypes'][$objectTypeID];
79 * Returns comment manager object for given object type.
81 * @param string $objectType
82 * @return \wcf\system\comment\manager\ICommentManager
84 public function getCommentManager($objectType) {
85 $objectTypeID = $this->getObjectTypeID($objectType);
86 if ($objectTypeID === null) {
87 throw new SystemException("Unable to find object type for '".$objectType."'");
90 return $this->getObjectType($objectTypeID)->getProcessor();
95 * Returns a comment list for a given object type and object id.
97 * @param \wcf\data\comment\manager\ICommentManager $commentManager
98 * @param integer $objectTypeID
99 * @param integer $objectID
100 * @param boolean $readObjects
101 * @return \wcf\data\comment\StructuredCommentList
103 public function getCommentList(ICommentManager
$commentManager, $objectTypeID, $objectID, $readObjects = true) {
104 $commentList = new StructuredCommentList($commentManager, $objectTypeID, $objectID);
106 $commentList->readObjects();
113 * Removes all comments for given objects.
115 * @param string $objectType
116 * @param array<integer> $objectIDs
118 public function deleteObjects($objectType, array $objectIDs) {
119 $objectTypeID = $this->getObjectTypeID($objectType);
120 $objectTypeObj = $this->getObjectType($objectTypeID);
123 $commentList = new CommentList();
124 $commentList->getConditionBuilder()->add('comment.objectTypeID = ?', array($objectTypeID));
125 $commentList->getConditionBuilder()->add('comment.objectID IN (?)', array($objectIDs));
126 $commentList->readObjectIDs();
127 $commentIDs = $commentList->getObjectIDs();
129 // no comments -> skip
130 if (empty($commentIDs)) return;
133 $responseList = new CommentResponseList();
134 $responseList->getConditionBuilder()->add('comment_response.commentID IN (?)', array($commentIDs));
135 $responseList->readObjectIDs();
136 $responseIDs = $responseList->getObjectIDs();
139 LikeHandler
::getInstance()->removeLikes('com.woltlab.wcf.comment', $commentIDs);
141 // delete activity events
142 if (UserActivityEventHandler
::getInstance()->getObjectTypeID($objectTypeObj->objectType
.'.recentActivityEvent')) {
143 UserActivityEventHandler
::getInstance()->removeEvents($objectTypeObj->objectType
.'.recentActivityEvent', $commentIDs);
145 // delete notifications
146 if (UserNotificationHandler
::getInstance()->getObjectTypeID($objectTypeObj->objectType
.'.notification')) {
147 UserNotificationHandler
::getInstance()->deleteNotifications('comment', $objectTypeObj->objectType
.'.notification', array(), $commentIDs);
150 if (!empty($responseIDs)) {
151 // delete likes (for responses)
152 LikeHandler
::getInstance()->removeLikes('com.woltlab.wcf.comment.response', $responseIDs);
154 // delete activity events (for responses)
155 if (UserActivityEventHandler
::getInstance()->getObjectTypeID($objectTypeObj->objectType
.'.response.recentActivityEvent')) {
156 UserActivityEventHandler
::getInstance()->removeEvents($objectTypeObj->objectType
.'.response.recentActivityEvent', $responseIDs);
158 // delete notifications (for responses)
159 if (UserNotificationHandler
::getInstance()->getObjectTypeID($objectTypeObj->objectType
.'.response.notification')) {
160 UserNotificationHandler
::getInstance()->deleteNotifications('commentResponse', $objectTypeObj->objectType
.'.response.notification', array(), $responseIDs);
161 UserNotificationHandler
::getInstance()->deleteNotifications('commentResponseOwner', $objectTypeObj->objectType
.'.response.notification', array(), $responseIDs);
165 // delete comments / responses
166 CommentEditor
::deleteAll($commentIDs);
170 * Enforces the flood control.
172 public static function enforceFloodControl() {
173 if (!WCF
::getSession()->getPermission('user.comment.floodControlTime')) {
177 // flood control for guests is session based
178 if (!WCF
::getUser()->userID
) {
179 $lastCommentTime = WCF
::getSession()->getVar('lastCommentTime');
181 if ($lastCommentTime && $lastCommentTime + WCF
::getSession()->getPermission('user.comment.floodControlTime') > TIME_NOW
) {
182 throw new NamedUserException(WCF
::getLanguage()->getDynamicVariable('wcf.comment.error.floodControl', array(
183 'lastCommentTime' => $lastCommentTime
190 // check for comments
192 FROM wcf".WCF_N
."_comment
196 $statement = WCF
::getDB()->prepareStatement($sql, 1);
197 $statement->execute(array(
198 WCF
::getUser()->userID
,
199 (TIME_NOW
- WCF
::getSession()->getPermission('user.comment.floodControlTime'))
201 if (($row = $statement->fetchArray()) !== false) {
202 throw new NamedUserException(WCF
::getLanguage()->getDynamicVariable('wcf.comment.error.floodControl', array('lastCommentTime' => $row['time'])));
205 // check for comment response
207 FROM wcf".WCF_N
."_comment_response
211 $statement = WCF
::getDB()->prepareStatement($sql, 1);
212 $statement->execute(array(
213 WCF
::getUser()->userID
,
214 (TIME_NOW
- WCF
::getSession()->getPermission('user.comment.floodControlTime'))
216 if (($row = $statement->fetchArray()) !== false) {
217 throw new NamedUserException(WCF
::getLanguage()->getDynamicVariable('wcf.comment.error.floodControl', array('lastCommentTime' => $row['time'])));