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