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