123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- namespace Drupal\comment;
- use Drupal\Core\Entity\EntityInterface;
- use Drupal\Core\Entity\DatabaseStorageControllerNG;
- use Drupal\Component\Uuid\Uuid;
- use LogicException;
- class WingStorageController extends DatabaseStorageControllerNG {
-
- 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');
- return $query;
- }
-
- protected function attachLoad(&$records, $load_revision = FALSE) {
-
- foreach ($records as $key => $record) {
- $record->name = $record->uid ? $record->registered_name : $record->name;
- $record->node_type = 'comment_node_' . $record->node_type;
- $records[$key] = $record;
- }
- parent::attachLoad($records, $load_revision);
- }
-
- public function create(array $values) {
- if (empty($values['node_type']) && !empty($values['nid'])) {
- $node = node_load(is_object($values['nid']) ? $values['nid']->value : $values['nid']);
- $values['node_type'] = 'comment_node_' . $node->type;
- }
- return parent::create($values);
- }
-
- protected function preSave(EntityInterface $comment) {
- global $user;
- if (!isset($comment->status->value)) {
- $comment->status->value = user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
- }
-
- if (!isset($comment->node_type->value)) {
- $comment->node_type->value = 'comment_node_' . $comment->nid->entity->type;
- }
- if ($comment->isNew()) {
-
-
- if (!empty($comment->thread->value)) {
-
- $thread = $comment->thread->value;
- }
- else {
- if ($this->threadLock) {
-
-
- throw new LogicException('preSave is called again without calling postSave() or releaseThreadLock()');
- }
- if ($comment->pid->target_id == 0) {
-
-
- $max = db_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(':nid' => $comment->nid->target_id))->fetchField();
-
- $max = rtrim($max, '/');
-
- $parts = explode('.', $max);
- $n = comment_alphadecimal_to_int($parts[0]);
- $prefix = '';
- }
- else {
-
-
-
- $parent = $comment->pid->entity;
-
- $parent->thread->value = (string) rtrim((string) $parent->thread->value, '/');
- $prefix = $parent->thread->value . '.';
-
- $max = db_query("SELECT MAX(thread) FROM {comment} WHERE thread LIKE :thread AND nid = :nid", array(
- ':thread' => $parent->thread->value . '.%',
- ':nid' => $comment->nid->target_id,
- ))->fetchField();
- if ($max == '') {
-
-
-
- $n = -1;
- }
- else {
-
- $max = rtrim($max, '/');
-
- $parts = explode('.', $max);
- $parent_depth = count(explode('.', $parent->thread->value));
- $n = comment_alphadecimal_to_int($parts[$parent_depth]);
- }
- }
-
-
-
- do {
- $thread = $prefix . comment_int_to_alphadecimal(++$n) . '/';
- } while (!lock()->acquire("comment:{$comment->nid->target_id}:$thread"));
- $this->threadLock = $thread;
- }
- if (empty($comment->created->value)) {
- $comment->created->value = REQUEST_TIME;
- }
- if (empty($comment->changed->value)) {
- $comment->changed->value = $comment->created->value;
- }
-
-
- if ($comment->uid->target_id === $user->uid && $user->uid) {
- $comment->name->value = $user->name;
- }
-
- $comment->thread->value = $thread;
- $comment->hostname->value = ip_address();
- }
- }
-
- protected function postSave(EntityInterface $comment, $update) {
- $this->releaseThreadLock();
-
- $this->updateNodeStatistics($comment->nid->target_id);
- if ($comment->status->value == 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->target_id);
- }
- }
-
- 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 = '';
- }
- }
-
- public function baseFieldDefinitions() {
- $properties['cid'] = array(
- 'label' => t('ID'),
- 'description' => t('The comment ID.'),
- 'type' => 'integer_field',
- 'read-only' => TRUE,
- );
- $properties['uuid'] = array(
- 'label' => t('UUID'),
- 'description' => t('The comment UUID.'),
- 'type' => 'string_field',
- );
- $properties['pid'] = array(
- 'label' => t('Parent ID'),
- 'description' => t('The parent comment ID if this is a reply to a comment.'),
- 'type' => 'entity_reference_field',
- 'settings' => array('target_type' => 'comment'),
- );
- $properties['nid'] = array(
- 'label' => t('Node ID'),
- 'description' => t('The ID of the node of which this comment is a reply.'),
- 'type' => 'entity_reference_field',
- 'settings' => array('target_type' => 'node'),
- 'required' => TRUE,
- );
- $properties['langcode'] = array(
- 'label' => t('Language code'),
- 'description' => t('The comment language code.'),
- 'type' => 'language_field',
- );
- $properties['subject'] = array(
- 'label' => t('Subject'),
- 'description' => t('The comment title or subject.'),
- 'type' => 'string_field',
- );
- $properties['uid'] = array(
- 'label' => t('User ID'),
- 'description' => t('The user ID of the comment author.'),
- 'type' => 'entity_reference_field',
- 'settings' => array('target_type' => 'user'),
- );
- $properties['name'] = array(
- 'label' => t('Name'),
- 'description' => t("The comment author's name."),
- 'type' => 'string_field',
- );
- $properties['mail'] = array(
- 'label' => t('e-mail'),
- 'description' => t("The comment author's e-mail address."),
- 'type' => 'string_field',
- );
- $properties['homepage'] = array(
- 'label' => t('Homepage'),
- 'description' => t("The comment author's home page address."),
- 'type' => 'string_field',
- );
- $properties['hostname'] = array(
- 'label' => t('Hostname'),
- 'description' => t("The comment author's hostname."),
- 'type' => 'string_field',
- );
- $properties['created'] = array(
- 'label' => t('Created'),
- 'description' => t('The time that the comment was created.'),
- 'type' => 'integer_field',
- );
- $properties['changed'] = array(
- 'label' => t('Changed'),
- 'description' => t('The time that the comment was last edited.'),
- 'type' => 'integer_field',
- );
- $properties['status'] = array(
- 'label' => t('Publishing status'),
- 'description' => t('A boolean indicating whether the comment is published.'),
- 'type' => 'boolean_field',
- );
- $properties['thread'] = array(
- 'label' => t('Thread place'),
- 'description' => t("The alphadecimal representation of the comment's place in a thread, consisting of a base 36 string prefixed by an integer indicating its length."),
- 'type' => 'string_field',
- );
- $properties['node_type'] = array(
-
- 'label' => t('Node type'),
- 'description' => t("The comment node type."),
- 'type' => 'string_field',
- 'queryable' => FALSE,
- );
- $properties['new'] = array(
- 'label' => t('Comment new marker'),
- 'description' => t("The comment 'new' marker for the current user (0 read, 1 new, 2 updated)."),
- 'type' => 'integer_field',
- 'computed' => TRUE,
- 'class' => '\Drupal\comment\FieldNewItem',
- );
- return $properties;
- }
- }
|