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