c0a28d21ed4cf6c4c4e6221c203e4e71e5677622
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace wcf\system\moderation\queue;
4
5 use wcf\data\comment\Comment;
6 use wcf\data\comment\response\CommentResponse;
7 use wcf\data\comment\response\CommentResponseAction;
8 use wcf\data\comment\response\ViewableCommentResponse;
9 use wcf\data\moderation\queue\ModerationQueue;
10 use wcf\data\moderation\queue\ViewableModerationQueue;
11 use wcf\system\cache\runtime\CommentResponseRuntimeCache;
12 use wcf\system\cache\runtime\CommentRuntimeCache;
13 use wcf\system\cache\runtime\UserProfileRuntimeCache;
14 use wcf\system\comment\manager\ICommentPermissionManager;
15 use wcf\system\database\util\PreparedStatementConditionBuilder;
16 use wcf\system\WCF;
17
18 /**
19 * An implementation of IModerationQueueHandler for comment responses.
20 *
21 * @author Alexander Ebert
22 * @copyright 2001-2019 WoltLab GmbH
23 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
24 */
25 class AbstractCommentResponseModerationQueueHandler extends AbstractCommentCommentModerationQueueHandler
26 {
27 /**
28 * @inheritDoc
29 */
30 protected $className = CommentResponse::class;
31
32 /**
33 * @inheritDoc
34 */
35 protected $objectType = 'com.woltlab.wcf.comment.response';
36
37 /**
38 * @inheritDoc
39 */
40 public function assignQueues(array $queues)
41 {
42 $assignments = [];
43
44 // read comments and responses
45 $responseIDs = [];
46 foreach ($queues as $queue) {
47 $responseIDs[] = $queue->objectID;
48 }
49
50 $conditions = new PreparedStatementConditionBuilder();
51 $conditions->add("comment_response.responseID IN (?)", [$responseIDs]);
52
53 $sql = "SELECT comment_response.responseID, comment.commentID, comment.objectTypeID, comment.objectID
54 FROM wcf" . WCF_N . "_comment_response comment_response
55 LEFT JOIN wcf" . WCF_N . "_comment comment
56 ON comment.commentID = comment_response.commentID
57 " . $conditions;
58 $statement = WCF::getDB()->prepareStatement($sql);
59 $statement->execute($conditions->getParameters());
60 $comments = $responses = [];
61 while ($row = $statement->fetchArray()) {
62 $comments[$row['commentID']] = new Comment(null, $row);
63 $responses[$row['responseID']] = new CommentResponse(null, $row);
64 }
65
66 $orphanedQueueIDs = [];
67 foreach ($queues as $queue) {
68 $assignUser = false;
69
70 if (!isset($responses[$queue->objectID]) || !isset($comments[$responses[$queue->objectID]->commentID])) {
71 $orphanedQueueIDs[] = $queue->queueID;
72 continue;
73 }
74
75 $comment = $comments[$responses[$queue->objectID]->commentID];
76 if ($this->getCommentManager($comment)->canModerate($comment->objectTypeID, $comment->objectID)) {
77 $assignUser = true;
78 }
79
80 $assignments[$queue->queueID] = $assignUser;
81 }
82
83 ModerationQueueManager::getInstance()->removeOrphans($orphanedQueueIDs);
84 ModerationQueueManager::getInstance()->setAssignment($assignments);
85 }
86
87 /**
88 * @inheritDoc
89 */
90 public function getRelatedContent(ViewableModerationQueue $queue)
91 {
92 WCF::getTPL()->assign([
93 'message' => ViewableCommentResponse::getResponse($queue->objectID),
94 ]);
95
96 return WCF::getTPL()->fetch('moderationComment');
97 }
98
99 /**
100 * @inheritDoc
101 */
102 public function isValid($objectID)
103 {
104 if ($this->getResponse($objectID) === null) {
105 return false;
106 }
107
108 return true;
109 }
110
111 /**
112 * Returns a comment response object by response id or null if response id is invalid.
113 *
114 * @param int $objectID
115 * @return CommentResponse|null
116 */
117 protected function getResponse($objectID)
118 {
119 return CommentResponseRuntimeCache::getInstance()->getObject($objectID);
120 }
121
122 /**
123 * @inheritDoc
124 */
125 public function populate(array $queues)
126 {
127 $objectIDs = [];
128 foreach ($queues as $object) {
129 $objectIDs[] = $object->objectID;
130 }
131
132 $responses = CommentResponseRuntimeCache::getInstance()->getObjects($objectIDs);
133
134 $commentIDs = [];
135 foreach ($responses as $response) {
136 if ($response !== null) {
137 $commentIDs[] = $response->commentID;
138 }
139 }
140
141 $comments = [];
142 if (!empty($commentIDs)) {
143 $comments = CommentRuntimeCache::getInstance()->getObjects($commentIDs);
144 }
145
146 foreach ($queues as $object) {
147 if ($responses[$object->objectID] !== null) {
148 $response = $responses[$object->objectID];
149 $response->setComment($comments[$response->commentID]);
150
151 $object->setAffectedObject($response);
152 } else {
153 $object->setIsOrphaned();
154 }
155 }
156 }
157
158 /**
159 * @inheritDoc
160 */
161 public function removeContent(ModerationQueue $queue, $message)
162 {
163 if ($this->isValid($queue->objectID)) {
164 $responseAction = new CommentResponseAction([$this->getResponse($queue->objectID)], 'delete');
165 $responseAction->executeAction();
166 }
167 }
168
169 #[\Override]
170 public function isAffectedUser(ModerationQueue $queue, $userID)
171 {
172 if (!AbstractModerationQueueHandler::isAffectedUser($queue, $userID)) {
173 return false;
174 }
175 $response = $this->getResponse($queue->objectID);
176 if ($response === null) {
177 return false;
178 }
179 $comment = $this->getComment($response->commentID);
180 if ($comment === null) {
181 return false;
182 }
183 $manager = $this->getCommentManager($comment);
184 if (!($manager instanceof ICommentPermissionManager)) {
185 return false;
186 }
187
188 return $manager->canModerateObject(
189 $comment->objectTypeID,
190 $comment->objectID,
191 UserProfileRuntimeCache::getInstance()->getObject($userID)
192 );
193 }
194 }