class CommentStorageController extends DatabaseStorageController {
protected $threadLock = '';
protected function buildQuery($ids, $revision_id = FALSE) {
$query = parent::buildQuery($ids, $revision_id);
$query
->innerJoin('node', 'n', 'base.nid = n.nid');
$query
->addField('n', 'type', 'node_type');
$query
->innerJoin('users', 'u', 'base.uid = u.uid');
$query
->addField('u', 'name', 'registered_name');
$query
->fields('u', array(
'uid',
'signature',
'signature_format',
));
return $query;
}
protected function attachLoad(&$comments, $load_revision = FALSE) {
foreach ($comments as $key => $comment) {
$comment->name = $comment->uid ? $comment->registered_name : $comment->name;
$comment->new = node_mark($comment->nid, $comment->changed);
$comment->node_type = 'comment_node_' . $comment->node_type;
$comments[$key] = $comment;
}
parent::attachLoad($comments, $load_revision);
}
protected function preSave(EntityInterface $comment) {
global $user;
if (!isset($comment->status)) {
$comment->status = user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
}
if (!isset($comment->node_type)) {
$node = node_load($comment->nid);
$comment->node_type = 'comment_node_' . $node->type;
}
if (!$comment->cid) {
if (!empty($comment->thread)) {
$thread = $comment->thread;
}
else {
if ($this->threadLock) {
throw new LogicException('preSave is called again without calling postSave() or releaseThreadLock()');
}
if ($comment->pid == 0) {
$max = db_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(
':nid' => $comment->nid,
))
->fetchField();
$max = rtrim($max, '/');
$parts = explode('.', $max);
$n = comment_alphadecimal_to_int($parts[0]);
$prefix = '';
}
else {
$parent = comment_load($comment->pid);
$parent->thread = (string) rtrim((string) $parent->thread, '/');
$prefix = $parent->thread . '.';
$max = db_query("SELECT MAX(thread) FROM {comment} WHERE thread LIKE :thread AND nid = :nid", array(
':thread' => $parent->thread . '.%',
':nid' => $comment->nid,
))
->fetchField();
if ($max == '') {
$n = -1;
}
else {
$max = rtrim($max, '/');
$parts = explode('.', $max);
$parent_depth = count(explode('.', $parent->thread));
$n = comment_alphadecimal_to_int($parts[$parent_depth]);
}
}
do {
$thread = $prefix . comment_int_to_alphadecimal(++$n) . '/';
} while (!lock()
->acquire("comment:{$comment->nid}:{$thread}"));
$this->threadLock = $thread;
}
if (empty($comment->created)) {
$comment->created = REQUEST_TIME;
}
if (empty($comment->changed)) {
$comment->changed = $comment->created;
}
if ($comment->uid === $user->uid && isset($user->name)) {
$comment->name = $user->name;
}
$comment->thread = $thread;
$comment->hostname = ip_address();
}
}
protected function postSave(EntityInterface $comment, $update) {
$this
->releaseThreadLock();
$this
->updateNodeStatistics($comment->nid);
if ($comment->status == COMMENT_PUBLISHED) {
module_invoke_all('comment_publish', $comment);
}
}
protected function postDelete($comments) {
$query = db_select('comment', 'c')
->fields('c', array(
'cid',
))
->condition('pid', array(
array_keys($comments),
), 'IN');
$child_cids = $query
->execute()
->fetchCol();
comment_delete_multiple($child_cids);
foreach ($comments as $comment) {
$this
->updateNodeStatistics($comment->nid);
}
}
protected function updateNodeStatistics($nid) {
if (!variable_get('comment_maintain_node_statistics', TRUE)) {
return;
}
$count = db_query('SELECT COUNT(cid) FROM {comment} WHERE nid = :nid AND status = :status', array(
':nid' => $nid,
':status' => COMMENT_PUBLISHED,
))
->fetchField();
if ($count > 0) {
$last_reply = db_query_range('SELECT cid, name, changed, uid FROM {comment} WHERE nid = :nid AND status = :status ORDER BY cid DESC', 0, 1, array(
':nid' => $nid,
':status' => COMMENT_PUBLISHED,
))
->fetchObject();
db_update('node_comment_statistics')
->fields(array(
'cid' => $last_reply->cid,
'comment_count' => $count,
'last_comment_timestamp' => $last_reply->changed,
'last_comment_name' => $last_reply->uid ? '' : $last_reply->name,
'last_comment_uid' => $last_reply->uid,
))
->condition('nid', $nid)
->execute();
}
else {
$node = db_query('SELECT uid, created FROM {node} WHERE nid = :nid', array(
':nid' => $nid,
))
->fetchObject();
db_update('node_comment_statistics')
->fields(array(
'cid' => 0,
'comment_count' => 0,
'last_comment_timestamp' => $node->created,
'last_comment_name' => '',
'last_comment_uid' => $node->uid,
))
->condition('nid', $nid)
->execute();
}
}
protected function releaseThreadLock() {
if ($this->threadLock) {
lock()
->release($this->threadLock);
$this->threadLock = '';
}
}
}