diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index f47151f..8bf0c24 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -40,7 +40,6 @@ function comment_enable() { $query->addField('n', 'uid', 'last_comment_uid'); $query->addField('n', 'nid'); $query->addExpression('0', 'comment_count'); - $query->addExpression('NULL', 'last_comment_name'); $query->isNull('ncs.comment_count'); db_insert('node_comment_statistics') @@ -157,24 +156,6 @@ function comment_schema() { 'not null' => TRUE, 'description' => "The alphadecimal representation of the comment's place in a thread, consisting of a base 36 string prefixed by an integer indicating its length.", ), - 'name' => array( - 'type' => 'varchar', - 'length' => 60, - 'not null' => FALSE, - 'description' => "The comment author's name. Uses {users}.name if the user is logged in, otherwise uses the value typed into the comment form.", - ), - 'mail' => array( - 'type' => 'varchar', - 'length' => 64, - 'not null' => FALSE, - 'description' => "The comment author's e-mail address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on.", - ), - 'homepage' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => FALSE, - 'description' => "The comment author's home page address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on.", - ), 'langcode' => array( 'description' => 'The {language}.langcode of this comment.', 'type' => 'varchar', @@ -228,12 +209,6 @@ function comment_schema() { 'default' => 0, 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.', ), - 'last_comment_name' => array( - 'type' => 'varchar', - 'length' => 60, - 'not null' => FALSE, - 'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.', - ), 'last_comment_uid' => array( 'type' => 'int', 'unsigned' => TRUE, @@ -382,6 +357,50 @@ function comment_update_8003(&$sandbox) { } /** + * Drop fields from comment table after anonymous user api has gone in + */ +function comment_update_8004(&$sandbox) { + if (!isset($sandbox['progress'])) { + $sandbox['progress'] = 0; + $sandbox['last'] = 0; + $sandbox['count'] = 0; + $sandbox['max'] = db_query('SELECT COUNT(cid) FROM {comment} WHERE uid = 0')->fetchField(); + } + + $comments = db_query_range('SELECT * FROM {comment} WHERE cid > :cid AND uid = 0 ORDER BY cid ASC', 0 , 10, array(':cid' => $sandbox['last']))->fetchAllAssoc('cid'); + + foreach ($comments as $comment) { + $values = array( + 'name' => $comment->name, + 'mail' => $comment->mail, + 'created' => $comment->created, + 'homepage' => $comment->homepage, + ); + + $user = entity_create('user', $values); + $user->save(); + + db_query('UPDATE {comment} SET uid = :uid WHERE cid = :cid', array(':cid' => $comment->cid, ':uid' => $user->uid)); + db_query('UPDATE {node_comment_statistics} SET last_comment_uid = :uid WHERE cid = :cid', array(':cid' => $comment->cid, ':uid' => $user->uid)); + + $sandbox['last'] = $comment->cid; + $sandbox['count']++; + } + + $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['count'] / $sandbox['max']); + + if ($sandbox['#finished']) { + // Drop comment fields + db_drop_field('comment', 'name'); + db_drop_field('comment', 'mail'); + db_drop_field('comment', 'homepage'); + + // Drop comment stats fields + db_drop_field('node_comment_stats', 'last_comment_name'); + } +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index df0a33a..f1ecd1a 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1208,7 +1208,6 @@ function comment_node_load($nodes, $types) { else { $node->cid = 0; $node->last_comment_timestamp = $node->created; - $node->last_comment_name = ''; $node->last_comment_uid = $node->uid; $node->comment_count = 0; } @@ -1216,11 +1215,10 @@ function comment_node_load($nodes, $types) { // For nodes with comments enabled, fetch information from the database. if (!empty($comments_enabled)) { - $result = db_query('SELECT nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count FROM {node_comment_statistics} WHERE nid IN (:comments_enabled)', array(':comments_enabled' => $comments_enabled)); + $result = db_query('SELECT nid, cid, last_comment_timestamp, last_comment_uid, comment_count FROM {node_comment_statistics} WHERE nid IN (:comments_enabled)', array(':comments_enabled' => $comments_enabled)); foreach ($result as $record) { $nodes[$record->nid]->cid = $record->cid; $nodes[$record->nid]->last_comment_timestamp = $record->last_comment_timestamp; - $nodes[$record->nid]->last_comment_name = $record->last_comment_name; $nodes[$record->nid]->last_comment_uid = $record->last_comment_uid; $nodes[$record->nid]->comment_count = $record->comment_count; } @@ -1248,7 +1246,6 @@ function comment_node_insert(Node $node) { 'nid' => $node->nid, 'cid' => 0, 'last_comment_timestamp' => $node->changed, - 'last_comment_name' => NULL, 'last_comment_uid' => $node->uid, 'comment_count' => 0, )) @@ -1572,35 +1569,10 @@ function comment_edit_page(Comment $comment) { * @param Drupal\comment\Comment $comment */ function comment_preview(Comment $comment) { - global $user; $preview_build = array(); if (!form_get_errors()) { - $comment_body = field_get_items('comment', $comment, 'comment_body'); - $comment->format = $comment_body[0]['format']; - // Attach the user and time information. - if (!empty($comment->name)) { - $account = user_load_by_name($comment->name); - } - elseif ($user->uid && empty($comment->is_anonymous)) { - $account = $user; - } - - if (!empty($account->uid)) { - $comment->uid = $account->uid; - $comment->name = check_plain($account->name); - $comment->signature = $account->signature; - $comment->signature_format = $account->signature_format; - $comment->picture = $account->picture; - } - elseif (empty($comment->name)) { - $comment->name = config('user.settings')->get('anonymous'); - } - - $comment->created = !empty($comment->created) ? $comment->created : REQUEST_TIME; - $comment->changed = REQUEST_TIME; - $comment->in_preview = TRUE; - $comment_build = comment_view($comment); + $comment_build = comment_view($comment, $node); $comment_build['#weight'] = -100; $preview_build['comment_preview'] = $comment_build; @@ -1642,13 +1614,13 @@ function template_preprocess_comment(&$variables) { $node = $variables['elements']['#node']; $variables['comment'] = $comment; $variables['node'] = $node; - $variables['author'] = theme('username', array('account' => $comment)); + $variables['author'] = theme('username', array('account' => $comment->author)); $variables['created'] = format_date($comment->created); $variables['changed'] = format_date($comment->changed); $variables['new'] = !empty($comment->new) ? t('new') : ''; $variables['user_picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', array('account' => $comment)) : ''; - $variables['signature'] = $comment->signature; + $variables['signature'] = $comment->author->signature; $uri = $comment->uri(); $uri['options'] += array('attributes' => array('class' => 'permalink', 'rel' => 'bookmark')); @@ -1681,7 +1653,7 @@ function template_preprocess_comment(&$variables) { if ($variables['new']) { $variables['attributes']['class'][] = 'new'; } - if (!$comment->uid) { + if ($comment->author->anonymous) { $variables['attributes']['class'][] = 'by-anonymous'; } else { diff --git a/core/modules/comment/lib/Drupal/comment/Comment.php b/core/modules/comment/lib/Drupal/comment/Comment.php index ef6c16a..d74a39e 100644 --- a/core/modules/comment/lib/Drupal/comment/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Comment.php @@ -64,33 +64,6 @@ class Comment extends Entity implements ContentEntityInterface { public $uid = 0; /** - * The comment author's name. - * - * For anonymous authors, this is the value as typed in the comment form. - * - * @var string - */ - public $name = ''; - - /** - * The comment author's e-mail address. - * - * For anonymous authors, this is the value as typed in the comment form. - * - * @var string - */ - public $mail; - - /** - * The comment author's home page address. - * - * For anonymous authors, this is the value as typed in the comment form. - * - * @var string - */ - public $homepage; - - /** * Implements Drupal\Core\Entity\EntityInterface::id(). */ public function id() { diff --git a/core/modules/comment/lib/Drupal/comment/CommentFormController.php b/core/modules/comment/lib/Drupal/comment/CommentFormController.php index cead0cd..e8063ac 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFormController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFormController.php @@ -61,7 +61,7 @@ public function form(array $form, array &$form_state, EntityInterface $comment) // Prepare default values for form elements. if ($is_admin) { - $author = (!$comment->uid && $comment->name ? $comment->name : $comment->registered_name); + $author = user_label('user', $comment->author); $status = (isset($comment->status) ? $comment->status : COMMENT_NOT_PUBLISHED); $date = (!empty($comment->date) ? $comment->date : format_date($comment->created, 'custom', 'Y-m-d H:i O')); } @@ -70,7 +70,7 @@ public function form(array $form, array &$form_state, EntityInterface $comment) $author = $user->name; } else { - $author = ($comment->name ? $comment->name : ''); + $author = !empty($comment->author) ? user_label('user', $comment->author) : ''; } $status = (user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED); $date = ''; @@ -115,7 +115,7 @@ public function form(array $form, array &$form_state, EntityInterface $comment) $form['author']['mail'] = array( '#type' => 'email', '#title' => t('E-mail'), - '#default_value' => $comment->mail, + '#default_value' => !empty($comment->author->mail) ? $comment->author->mail : '', '#required' => (!$user->uid && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT), '#maxlength' => 64, '#size' => 30, @@ -123,15 +123,6 @@ public function form(array $form, array &$form_state, EntityInterface $comment) '#access' => $is_admin || (!$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), ); - $form['author']['homepage'] = array( - '#type' => 'url', - '#title' => t('Homepage'), - '#default_value' => $comment->homepage, - '#maxlength' => 255, - '#size' => 30, - '#access' => $is_admin || (!$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), - ); - // Add administrative comment publishing options. $form['author']['date'] = array( '#type' => 'textfield', @@ -164,7 +155,7 @@ public function form(array $form, array &$form_state, EntityInterface $comment) // Used for conditional validation of author fields. $form['is_anonymous'] = array( '#type' => 'value', - '#value' => ($comment->cid ? !$comment->uid : !$user->uid), + '#value' => ($comment->cid ? $comment->author->anonymous : !$user->uid), ); // Add internal comment properties. @@ -249,14 +240,14 @@ public function validate(array $form, array &$form_state) { // user with this name exists. if ($form_state['values']['name']) { $query = db_select('users', 'u'); - $query->addField('u', 'uid', 'uid'); - $taken = $query + $account = $query + ->fields('u', array('uid', 'mail', 'anonymous')) ->condition('name', db_like($form_state['values']['name']), 'LIKE') - ->countQuery() ->execute() - ->fetchField(); - if ($taken) { - form_set_error('name', t('The name you used belongs to a registered user.')); + ->fetchObject(); + + if ($account && ($account->mail != $form_state['values']['mail'] || !$account->anonymous)) { + form_set_error('name', t('The name you used is already in use.')); } } } @@ -278,12 +269,34 @@ public function submit(array $form, array &$form_state) { // @todo Too fragile. Should be prepared and stored in comment_form() // already. if (!$comment->is_anonymous && !empty($comment->name) && ($account = user_load_by_name($comment->name))) { - $comment->uid = $account->uid; + $comment->author = $account; } - // If the comment was posted by an anonymous user and no author name was - // required, use "Anonymous" by default. - if ($comment->is_anonymous && (!isset($comment->name) || $comment->name === '')) { - $comment->name = config('user.settings')->get('anonymous'); + // If the comment was posted by an anonymous user create a user, or find one with a matching email. + if ($comment->is_anonymous) { + // If the comment is anonymous and we have a matching username and email + // then we'll hook them up. Otherwise we create a user a new user. The + // case where a user with the name existing but different email address + // has been dealt with in validation. + $account = db_select('users', 'u') + ->fields('u', array('uid', 'mail', 'anonymous')) + ->condition('name', db_like($form_state['values']['name']), 'LIKE') + ->condition('mail', $form_state['values']['mail']) + ->condition('anonymous', 1) + ->execute() + ->fetchObject(); + + if ($account) { + $comment->uid = $account->uid; + } + else { + $account = entity_create('user', array( + 'name' => $form_state['values']['name'], + 'mail' => $form_state['values']['mail'], + 'anonymous' => 1, + )); + + $comment->author = $account; + } } // Validate the comment's subject. If not specified, extract from comment @@ -338,7 +351,13 @@ public function save(array $form, array &$form_state) { if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_OPEN)) { // Save the anonymous user information to a cookie for reuse. if (user_is_anonymous()) { - user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage')))); + user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail')))); + } + + // Save the user if we need to. + if (empty($comment->uid) && !empty($comment->author)) { + $comment->author->save(); + $comment->uid = $comment->author->uid; } comment_save($comment); diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php index 4d447a5..86eb240 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php @@ -32,9 +32,6 @@ protected function buildQuery($ids, $revision_id = FALSE) { // Specify additional fields from the user and node tables. $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', 'picture')); return $query; } @@ -44,7 +41,7 @@ protected function buildQuery($ids, $revision_id = FALSE) { protected function attachLoad(&$comments, $load_revision = FALSE) { // Set up standard comment properties. foreach ($comments as $key => $comment) { - $comment->name = $comment->uid ? $comment->registered_name : $comment->name; + $comment->author = user_load($comment->uid); $comment->new = node_mark($comment->nid, $comment->changed); $comment->node_type = 'comment_node_' . $comment->node_type; $comments[$key] = $comment; @@ -137,11 +134,7 @@ protected function preSave(EntityInterface $comment) { if (empty($comment->changed)) { $comment->changed = $comment->created; } - // We test the value with '===' because we need to modify anonymous - // users as well. - if ($comment->uid === $user->uid && isset($user->name)) { - $comment->name = $user->name; - } + // Add the values which aren't passed into the function. $comment->thread = $thread; $comment->hostname = ip_address(); @@ -206,7 +199,7 @@ protected function updateNodeStatistics($nid) { if ($count > 0) { // Comments exist. - $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( + $last_reply = db_query_range('SELECT cid, changed, uid FROM {comment} WHERE nid = :nid AND status = :status ORDER BY cid DESC', 0, 1, array( ':nid' => $nid, ':status' => COMMENT_PUBLISHED, ))->fetchObject(); @@ -215,7 +208,6 @@ protected function updateNodeStatistics($nid) { '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) @@ -229,7 +221,6 @@ protected function updateNodeStatistics($nid) { 'cid' => 0, 'comment_count' => 0, 'last_comment_timestamp' => $node->created, - 'last_comment_name' => '', 'last_comment_uid' => $node->uid, )) ->condition('nid', $nid)