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