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