2011-12-05 12:12:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2014-03-27 11:54:40 +00:00
|
|
|
* Definition of Drupal\comment\CommentStorage.
|
2011-12-05 12:12:15 +00:00
|
|
|
*/
|
|
|
|
|
2012-05-03 05:49:41 +00:00
|
|
|
namespace Drupal\comment;
|
2011-12-05 12:12:15 +00:00
|
|
|
|
2014-03-25 18:41:33 +00:00
|
|
|
use Drupal\Core\Database\Connection;
|
2012-09-12 09:18:04 +00:00
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
2014-05-15 17:26:18 +00:00
|
|
|
use Drupal\Core\Entity\EntityManagerInterface;
|
2014-03-25 18:41:33 +00:00
|
|
|
use Drupal\Core\Entity\EntityTypeInterface;
|
2014-03-27 11:54:40 +00:00
|
|
|
use Drupal\Core\Entity\ContentEntityDatabaseStorage;
|
2014-03-25 18:41:33 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2011-12-05 12:12:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the controller class for comments.
|
|
|
|
*
|
2014-03-27 11:54:40 +00:00
|
|
|
* This extends the Drupal\Core\Entity\ContentEntityDatabaseStorage class,
|
|
|
|
* adding required special handling for comment entities.
|
2011-12-05 12:12:15 +00:00
|
|
|
*/
|
2014-03-27 11:54:40 +00:00
|
|
|
class CommentStorage extends ContentEntityDatabaseStorage implements CommentStorageInterface {
|
2013-06-16 09:40:11 +00:00
|
|
|
|
2014-03-25 18:41:33 +00:00
|
|
|
/**
|
|
|
|
* The comment statistics service.
|
|
|
|
*
|
|
|
|
* @var \Drupal\comment\CommentStatisticsInterface
|
|
|
|
*/
|
|
|
|
protected $statistics;
|
|
|
|
|
|
|
|
/**
|
2014-03-27 11:54:40 +00:00
|
|
|
* Constructs a CommentStorage object.
|
2014-03-25 18:41:33 +00:00
|
|
|
*
|
|
|
|
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_info
|
|
|
|
* An array of entity info for the entity type.
|
|
|
|
* @param \Drupal\Core\Database\Connection $database
|
|
|
|
* The database connection to be used.
|
2014-05-15 17:26:18 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
|
|
|
* The entity manager.
|
2014-03-25 18:41:33 +00:00
|
|
|
* @param \Drupal\comment\CommentStatisticsInterface $comment_statistics
|
|
|
|
* The comment statistics service.
|
|
|
|
*/
|
2014-05-15 17:26:18 +00:00
|
|
|
public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, CommentStatisticsInterface $comment_statistics) {
|
|
|
|
parent::__construct($entity_info, $database, $entity_manager);
|
2014-03-25 18:41:33 +00:00
|
|
|
$this->statistics = $comment_statistics;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
|
|
|
|
return new static(
|
|
|
|
$entity_info,
|
|
|
|
$container->get('database'),
|
2014-05-15 17:26:18 +00:00
|
|
|
$container->get('entity.manager'),
|
2014-03-25 18:41:33 +00:00
|
|
|
$container->get('comment.statistics')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-05 12:12:15 +00:00
|
|
|
/**
|
2013-06-16 09:40:11 +00:00
|
|
|
* {@inheritdoc}
|
2011-12-05 12:12:15 +00:00
|
|
|
*/
|
2012-08-21 15:38:04 +00:00
|
|
|
protected function buildQuery($ids, $revision_id = FALSE) {
|
|
|
|
$query = parent::buildQuery($ids, $revision_id);
|
2013-09-27 15:34:47 +00:00
|
|
|
// Specify additional fields from the user table.
|
2011-12-05 12:12:15 +00:00
|
|
|
$query->innerJoin('users', 'u', 'base.uid = u.uid');
|
2013-01-14 10:59:47 +00:00
|
|
|
// @todo: Move to a computed 'name' field instead.
|
2011-12-05 12:12:15 +00:00
|
|
|
$query->addField('u', 'name', 'registered_name');
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-16 09:40:11 +00:00
|
|
|
* {@inheritdoc}
|
2011-12-05 12:12:15 +00:00
|
|
|
*/
|
2014-04-22 22:00:30 +00:00
|
|
|
protected function mapFromStorageRecords(array $records) {
|
2013-01-14 10:59:47 +00:00
|
|
|
// Prepare standard comment fields.
|
2014-04-22 22:00:30 +00:00
|
|
|
foreach ($records as $record) {
|
2013-01-14 10:59:47 +00:00
|
|
|
$record->name = $record->uid ? $record->registered_name : $record->name;
|
|
|
|
}
|
2014-04-22 22:00:30 +00:00
|
|
|
return parent::mapFromStorageRecords($records);
|
2013-01-14 10:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-16 09:40:11 +00:00
|
|
|
* {@inheritdoc}
|
2011-12-05 12:12:15 +00:00
|
|
|
*/
|
2013-09-27 15:34:47 +00:00
|
|
|
public function updateEntityStatistics(CommentInterface $comment) {
|
2014-03-25 18:41:33 +00:00
|
|
|
$this->statistics->update($comment);
|
2011-12-05 12:12:15 +00:00
|
|
|
}
|
2012-08-19 12:28:00 +00:00
|
|
|
|
2013-06-16 09:40:11 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getMaxThread(EntityInterface $comment) {
|
2013-09-27 15:34:47 +00:00
|
|
|
$query = $this->database->select('comment', 'c')
|
2014-02-16 07:20:53 +00:00
|
|
|
->condition('entity_id', $comment->getCommentedEntityId())
|
|
|
|
->condition('field_id', $comment->getFieldId())
|
|
|
|
->condition('entity_type', $comment->getCommentedEntityTypeId());
|
2013-09-27 15:34:47 +00:00
|
|
|
$query->addExpression('MAX(thread)', 'thread');
|
|
|
|
return $query->execute()
|
|
|
|
->fetchField();
|
2013-06-16 09:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getMaxThreadPerThread(EntityInterface $comment) {
|
2013-09-27 15:34:47 +00:00
|
|
|
$query = $this->database->select('comment', 'c')
|
2014-02-16 07:20:53 +00:00
|
|
|
->condition('entity_id', $comment->getCommentedEntityId())
|
|
|
|
->condition('field_id', $comment->getFieldId())
|
|
|
|
->condition('entity_type', $comment->getCommentedEntityTypeId())
|
|
|
|
->condition('thread', $comment->getParentComment()->getThread() . '.%', 'LIKE');
|
2013-09-27 15:34:47 +00:00
|
|
|
$query->addExpression('MAX(thread)', 'thread');
|
|
|
|
return $query->execute()
|
|
|
|
->fetchField();
|
2013-06-16 09:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getChildCids(array $comments) {
|
|
|
|
return $this->database->select('comment', 'c')
|
|
|
|
->fields('c', array('cid'))
|
|
|
|
->condition('pid', array_keys($comments))
|
|
|
|
->execute()
|
|
|
|
->fetchCol();
|
|
|
|
}
|
2013-12-22 21:11:36 +00:00
|
|
|
|
2014-06-01 20:53:43 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getSchema() {
|
|
|
|
$schema = parent::getSchema();
|
|
|
|
|
|
|
|
// Marking the respective fields as NOT NULL makes the indexes more
|
|
|
|
// performant.
|
|
|
|
$schema['comment']['fields']['pid']['not null'] = TRUE;
|
|
|
|
$schema['comment']['fields']['status']['not null'] = TRUE;
|
|
|
|
$schema['comment']['fields']['entity_id']['not null'] = TRUE;
|
|
|
|
$schema['comment']['fields']['field_id']['not null'] = TRUE;
|
|
|
|
$schema['comment']['fields']['created']['not null'] = TRUE;
|
|
|
|
$schema['comment']['fields']['thread']['not null'] = TRUE;
|
|
|
|
|
|
|
|
unset($schema['comment']['indexes']['field__pid']);
|
|
|
|
unset($schema['comment']['indexes']['field__entity_id']);
|
|
|
|
$schema['comment']['indexes'] += array(
|
|
|
|
'comment__status_pid' => array('pid', 'status'),
|
|
|
|
'comment__num_new' => array(
|
|
|
|
'entity_id',
|
|
|
|
array('entity_type', 32),
|
|
|
|
array('field_id', 32),
|
|
|
|
'status',
|
|
|
|
'created',
|
|
|
|
'cid',
|
|
|
|
'thread',
|
|
|
|
),
|
|
|
|
'comment__entity_langcode' => array(
|
|
|
|
'entity_id',
|
|
|
|
array('entity_type', 32),
|
|
|
|
array('field_id', 32),
|
|
|
|
'langcode',
|
|
|
|
),
|
|
|
|
'comment__created' => array('created'),
|
|
|
|
);
|
|
|
|
$schema['comment']['foreign keys'] += array(
|
|
|
|
'comment__author' => array(
|
|
|
|
'table' => 'users',
|
|
|
|
'columns' => array('uid' => 'uid'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $schema;
|
|
|
|
}
|
|
|
|
|
2011-12-05 12:12:15 +00:00
|
|
|
}
|