c929154ee91b1f57abe2fb8b4660ee00a3fd6d7e
[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-2013 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' => new ViewableComment($queue->getAffectedObject())
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 if ($this->isValid($objectID)) {
138 return $this->getComment($objectID);
139 }
140
141 return null;
142 }
143
144 /**
145 * @see \wcf\system\moderation\queue\IModerationQueueHandler::isValid()
146 */
147 public function isValid($objectID) {
148 if ($this->getComment($objectID) === null) {
149 return false;
150 }
151
152 return true;
153 }
154
155 /**
156 * Returns a comment object by comment id or null if comment id is invalid.
157 *
158 * @param integer $objectID
159 * @return \wcf\data\comment\Comment
160 */
161 protected function getComment($objectID) {
162 if (!array_key_exists($objectID, self::$comments)) {
163 self::$comments[$objectID] = new Comment($objectID);
164 if (!self::$comments[$objectID]->commentID) {
165 self::$comments[$objectID] = null;
166 }
167 }
168
169 return self::$comments[$objectID];
170 }
171
172 /**
173 * Returns a comment manager for given comment.
174 *
175 * @param \wcf\data\comment\Comment $comment
176 * @return \wcf\system\comment\manager\ICommentManager
177 */
178 protected function getCommentManager(Comment $comment) {
179 if (!isset(self::$commentManagers[$comment->objectTypeID])) {
180 self::$commentManagers[$comment->objectTypeID] = ObjectTypeCache::getInstance()->getObjectType($comment->objectTypeID)->getProcessor();
181 }
182
183 return self::$commentManagers[$comment->objectTypeID];
184 }
185
186 /**
187 * @see \wcf\system\moderation\queue\IModerationQueueHandler::populate()
188 */
189 public function populate(array $queues) {
190 $objectIDs = array();
191 foreach ($queues as $object) {
192 $objectIDs[] = $object->objectID;
193 }
194
195 // fetch comments
196 $commentList = new CommentList();
197 $commentList->getConditionBuilder()->add("comment.commentID IN (?)", array($objectIDs));
198 $commentList->readObjects();
199 $comments = $commentList->getObjects();
200
201 foreach ($queues as $object) {
202 if (isset($comments[$object->objectID])) {
203 $object->setAffectedObject($comments[$object->objectID]);
204 }
205 else {
206 $object->setIsOrphaned();
207 }
208 }
209 }
210
211 /**
212 * @see \wcf\system\moderation\queue\IModerationQueueHandler::removeContent()
213 */
214 public function removeContent(ModerationQueue $queue, $message) {
215 if ($this->isValid($queue->objectID)) {
216 $commentAction = new CommentAction(array($this->getComment($queue->objectID)), 'delete');
217 $commentAction->executeAction();
218 }
219 }
220 }