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