b127b6621d897413a95d93fdc1ebbba97396fd91
[GitHub/WoltLab/WCF.git] /
1 <?php
2 declare(strict_types=1);
3 namespace wcf\system\moderation\queue;
4 use wcf\data\comment\response\CommentResponse;
5 use wcf\data\comment\response\CommentResponseAction;
6 use wcf\data\comment\response\ViewableCommentResponse;
7 use wcf\data\comment\Comment;
8 use wcf\data\moderation\queue\ModerationQueue;
9 use wcf\data\moderation\queue\ViewableModerationQueue;
10 use wcf\system\cache\runtime\CommentResponseRuntimeCache;
11 use wcf\system\cache\runtime\CommentRuntimeCache;
12 use wcf\system\database\util\PreparedStatementConditionBuilder;
13 use wcf\system\WCF;
14
15 /**
16 * An implementation of IModerationQueueHandler for comment responses.
17 *
18 * @author Alexander Ebert
19 * @copyright 2001-2018 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 AbstractCommentResponseModerationQueueHandler extends AbstractCommentCommentModerationQueueHandler {
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 getRelatedContent(ViewableModerationQueue $queue) {
87 WCF::getTPL()->assign([
88 'message' => ViewableCommentResponse::getResponse($queue->objectID)
89 ]);
90
91 return WCF::getTPL()->fetch('moderationComment');
92 }
93
94 /**
95 * @inheritDoc
96 */
97 public function isValid($objectID) {
98 if ($this->getResponse($objectID) === null) {
99 return false;
100 }
101
102 return true;
103 }
104
105 /**
106 * Returns a comment response object by response id or null if response id is invalid.
107 *
108 * @param integer $objectID
109 * @return CommentResponse|null
110 */
111 protected function getResponse($objectID) {
112 return CommentResponseRuntimeCache::getInstance()->getObject($objectID);
113 }
114
115 /**
116 * @inheritDoc
117 */
118 public function populate(array $queues) {
119 $objectIDs = [];
120 foreach ($queues as $object) {
121 $objectIDs[] = $object->objectID;
122 }
123
124 $responses = CommentResponseRuntimeCache::getInstance()->getObjects($objectIDs);
125
126 $commentIDs = [];
127 foreach ($responses as $response) {
128 if ($response !== null) {
129 $commentIDs[] = $response->commentID;
130 }
131 }
132
133 $comments = [];
134 if (!empty($commentIDs)) {
135 $comments = CommentRuntimeCache::getInstance()->getObjects($commentIDs);
136 }
137
138 foreach ($queues as $object) {
139 if ($responses[$object->objectID] !== null) {
140 $response = $responses[$object->objectID];
141 $response->setComment($comments[$response->commentID]);
142
143 $object->setAffectedObject($response);
144 }
145 else {
146 $object->setIsOrphaned();
147 }
148 }
149 }
150
151 /**
152 * @inheritDoc
153 */
154 public function removeContent(ModerationQueue $queue, $message) {
155 if ($this->isValid($queue->objectID)) {
156 $responseAction = new CommentResponseAction([$this->getResponse($queue->objectID)], 'delete');
157 $responseAction->executeAction();
158 }
159 }
160 }