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