c09110b346234177909cefdeb77cd6d2b59f7372
[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\CommentAction;
7 use wcf\data\comment\ViewableComment;
8 use wcf\data\moderation\queue\ModerationQueue;
9 use wcf\data\moderation\queue\ViewableModerationQueue;
10 use wcf\data\object\type\ObjectTypeCache;
11 use wcf\system\cache\runtime\CommentRuntimeCache;
12 use wcf\system\cache\runtime\UserProfileRuntimeCache;
13 use wcf\system\comment\manager\ICommentManager;
14 use wcf\system\WCF;
15
16 /**
17 * An abstract implementation of IModerationQueueHandler for comments.
18 *
19 * @author Alexander Ebert
20 * @copyright 2001-2019 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 */
23 class AbstractCommentCommentModerationQueueHandler extends AbstractModerationQueueHandler
24 {
25 /**
26 * @inheritDoc
27 */
28 protected $className = Comment::class;
29
30 /**
31 * @inheritDoc
32 */
33 protected $objectType = 'com.woltlab.wcf.comment.comment';
34
35 /**
36 * list of comment managers
37 * @var ICommentManager[]
38 */
39 protected static $commentManagers = [];
40
41 /**
42 * @inheritDoc
43 */
44 public function assignQueues(array $queues)
45 {
46 $assignments = [];
47
48 // read comments
49 $commentIDs = [];
50 foreach ($queues as $queue) {
51 $commentIDs[] = $queue->objectID;
52 }
53
54 $comments = CommentRuntimeCache::getInstance()->getObjects($commentIDs);
55
56 $orphanedQueueIDs = [];
57 foreach ($queues as $queue) {
58 $assignUser = false;
59
60 if ($comments[$queue->objectID] === null) {
61 $orphanedQueueIDs[] = $queue->queueID;
62 continue;
63 }
64
65 $comment = $comments[$queue->objectID];
66 if ($this->getCommentManager($comment)->canModerate($comment->objectTypeID, $comment->objectID)) {
67 $assignUser = true;
68 }
69
70 $assignments[$queue->queueID] = $assignUser;
71 }
72
73 ModerationQueueManager::getInstance()->removeOrphans($orphanedQueueIDs);
74 ModerationQueueManager::getInstance()->setAssignment($assignments);
75 }
76
77 /**
78 * @inheritDoc
79 */
80 public function getContainerID($objectID)
81 {
82 return 0;
83 }
84
85 /**
86 * @inheritDoc
87 */
88 public function isValid($objectID)
89 {
90 if ($this->getComment($objectID) === null) {
91 return false;
92 }
93
94 return true;
95 }
96
97 /**
98 * Returns a comment object by comment id or null if comment id is invalid.
99 *
100 * @param int $objectID
101 * @return Comment|null
102 */
103 protected function getComment($objectID)
104 {
105 return CommentRuntimeCache::getInstance()->getObject($objectID);
106 }
107
108 /**
109 * Returns a comment manager for given comment.
110 *
111 * @param Comment $comment
112 * @return ICommentManager
113 */
114 protected function getCommentManager(Comment $comment)
115 {
116 if (!isset(self::$commentManagers[$comment->objectTypeID])) {
117 self::$commentManagers[$comment->objectTypeID] = ObjectTypeCache::getInstance()
118 ->getObjectType($comment->objectTypeID)
119 ->getProcessor();
120 }
121
122 return self::$commentManagers[$comment->objectTypeID];
123 }
124
125 /**
126 * @inheritDoc
127 */
128 public function populate(array $queues)
129 {
130 $objectIDs = [];
131 foreach ($queues as $object) {
132 $objectIDs[] = $object->objectID;
133 }
134
135 // fetch comments
136 $comments = CommentRuntimeCache::getInstance()->getObjects($objectIDs);
137 foreach ($queues as $object) {
138 if ($comments[$object->objectID] !== null) {
139 $object->setAffectedObject($comments[$object->objectID]);
140 } else {
141 $object->setIsOrphaned();
142 }
143 }
144 }
145
146 /**
147 * @inheritDoc
148 */
149 public function removeContent(ModerationQueue $queue, $message)
150 {
151 if ($this->isValid($queue->objectID)) {
152 $commentAction = new CommentAction([$this->getComment($queue->objectID)], 'delete');
153 $commentAction->executeAction();
154 }
155 }
156
157 /**
158 * Returns the parsed template for the target comment.
159 *
160 * @param ViewableModerationQueue $queue
161 * @return string
162 */
163 protected function getRelatedContent(ViewableModerationQueue $queue)
164 {
165 WCF::getTPL()->assign([
166 'message' => ViewableComment::getComment($queue->objectID),
167 ]);
168
169 return WCF::getTPL()->fetch('moderationComment');
170 }
171
172 #[\Override]
173 public function isAffectedUser(ModerationQueue $queue, $userID)
174 {
175 if (!parent::isAffectedUser($queue, $userID)) {
176 return false;
177 }
178 $comment = $this->getComment($queue->objectID);
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 }