dd6138204ac738100fe4581dd53c80c16d7b7a2e
[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\database\util\PreparedStatementConditionBuilder;
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-2015 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 * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$className
28 */
29 protected $className = 'wcf\data\comment\Comment';
30
31 /**
32 * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$definitionName
33 */
34 protected $definitionName = 'com.woltlab.wcf.moderation.report';
35
36 /**
37 * @see \wcf\system\moderation\queue\AbstractModerationQueueHandler::$objectType
38 */
39 protected $objectType = 'com.woltlab.wcf.comment.comment';
40
41 /**
42 * list of comments
43 * @var array<\wcf\data\comment\Comment>
44 */
45 protected static $comments = array();
46
47 /**
48 * list of comment managers
49 * @var array<\wcf\system\comment\manager\ICommentManager>
50 */
51 protected static $commentManagers = array();
52
53 /**
54 * @see \wcf\system\moderation\queue\IModerationQueueHandler::assignQueues()
55 */
56 public function assignQueues(array $queues) {
57 $assignments = array();
58
59 // read comments
60 $commentIDs = array();
61 foreach ($queues as $queue) {
62 $commentIDs[] = $queue->objectID;
63 }
64
65 $conditions = new PreparedStatementConditionBuilder();
66 $conditions->add("commentID IN (?)", array($commentIDs));
67
68 $sql = "SELECT commentID, objectTypeID, objectID
69 FROM wcf".WCF_N."_comment
70 ".$conditions;
71 $statement = WCF::getDB()->prepareStatement($sql);
72 $statement->execute($conditions->getParameters());
73 $comments = array();
74 while ($row = $statement->fetchArray()) {
75 $comments[$row['commentID']] = new Comment(null, $row);
76 }
77
78 $orphanedQueueIDs = array();
79 foreach ($queues as $queue) {
80 $assignUser = false;
81
82 if (!isset($comments[$queue->objectID])) {
83 $orphanedQueueIDs[] = $queue->queueID;
84 continue;
85 }
86
87 $comment = $comments[$queue->objectID];
88 if ($this->getCommentManager($comment)->canModerate($comment->objectTypeID, $comment->objectID)) {
89 $assignUser = true;
90 }
91
92 $assignments[$queue->queueID] = $assignUser;
93 }
94
95 ModerationQueueManager::getInstance()->removeOrphans($orphanedQueueIDs);
96 ModerationQueueManager::getInstance()->setAssignment($assignments);
97 }
98
99 /**
100 * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::canReport()
101 */
102 public function canReport($objectID) {
103 if (!$this->isValid($objectID)) {
104 return false;
105 }
106
107 $comment = $this->getComment($objectID);
108 if (!$this->getCommentManager($comment)->isAccessible($comment->objectID)) {
109 return false;
110 }
111
112 return true;
113 }
114
115 /**
116 * @see \wcf\system\moderation\queue\IModerationQueueHandler::getContainerID()
117 */
118 public function getContainerID($objectID) {
119 return 0;
120 }
121
122 /**
123 * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedContent()
124 */
125 public function getReportedContent(ViewableModerationQueue $queue) {
126 WCF::getTPL()->assign(array(
127 'message' => ViewableComment::getComment($queue->objectID)
128 ));
129
130 return WCF::getTPL()->fetch('moderationComment');
131 }
132
133 /**
134 * @see \wcf\system\moderation\queue\report\IModerationQueueReportHandler::getReportedObject()
135 */
136 public function getReportedObject($objectID) {
137 return $this->getComment($objectID);
138 }
139
140 /**
141 * @see \wcf\system\moderation\queue\IModerationQueueHandler::isValid()
142 */
143 public function isValid($objectID) {
144 if ($this->getComment($objectID) === null) {
145 return false;
146 }
147
148 return true;
149 }
150
151 /**
152 * Returns a comment object by comment id or null if comment id is invalid.
153 *
154 * @param integer $objectID
155 * @return \wcf\data\comment\Comment
156 */
157 protected function getComment($objectID) {
158 if (!array_key_exists($objectID, self::$comments)) {
159 self::$comments[$objectID] = new Comment($objectID);
160 if (!self::$comments[$objectID]->commentID) {
161 self::$comments[$objectID] = null;
162 }
163 }
164
165 return self::$comments[$objectID];
166 }
167
168 /**
169 * Returns a comment manager for given comment.
170 *
171 * @param \wcf\data\comment\Comment $comment
172 * @return \wcf\system\comment\manager\ICommentManager
173 */
174 protected function getCommentManager(Comment $comment) {
175 if (!isset(self::$commentManagers[$comment->objectTypeID])) {
176 self::$commentManagers[$comment->objectTypeID] = ObjectTypeCache::getInstance()->getObjectType($comment->objectTypeID)->getProcessor();
177 }
178
179 return self::$commentManagers[$comment->objectTypeID];
180 }
181
182 /**
183 * @see \wcf\system\moderation\queue\IModerationQueueHandler::populate()
184 */
185 public function populate(array $queues) {
186 $objectIDs = array();
187 foreach ($queues as $object) {
188 $objectIDs[] = $object->objectID;
189 }
190
191 // fetch comments
192 $commentList = new CommentList();
193 $commentList->setObjectIDs($objectIDs);
194 $commentList->readObjects();
195 $comments = $commentList->getObjects();
196
197 foreach ($queues as $object) {
198 if (isset($comments[$object->objectID])) {
199 $object->setAffectedObject($comments[$object->objectID]);
200 }
201 else {
202 $object->setIsOrphaned();
203 }
204 }
205 }
206
207 /**
208 * @see \wcf\system\moderation\queue\IModerationQueueHandler::removeContent()
209 */
210 public function removeContent(ModerationQueue $queue, $message) {
211 if ($this->isValid($queue->objectID)) {
212 $commentAction = new CommentAction(array($this->getComment($queue->objectID)), 'delete');
213 $commentAction->executeAction();
214 }
215 }
216 }