diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index f390f57..d738cd4 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -157,24 +157,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 +210,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 +358,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 27ff7c3..ebf5f7c 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1061,7 +1061,6 @@ function comment_build_content(Comment $comment, Node $node, $view_mode = 'full' field_attach_prepare_view('comment', array($comment->cid => $comment), $view_mode, $langcode); entity_prepare_view('comment', array($comment->cid => $comment), $langcode); $comment->content += field_attach_view('comment', $comment, $view_mode, $langcode); - $comment->content['links'] = array( '#theme' => 'links__comment', '#pre_render' => array('drupal_pre_render_links'), @@ -1321,7 +1320,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; } @@ -1329,11 +1327,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; } @@ -1361,7 +1358,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, )) @@ -1685,35 +1681,10 @@ function comment_edit_page(Comment $comment) { * @param Drupal\comment\Comment $comment */ function comment_preview(Comment $comment) { - global $user; $preview_build = array(); $node = node_load($comment->nid); 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, $node); $comment_build['#weight'] = -100; @@ -1756,13 +1727,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['user_picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', array('account' => $comment->author)) : ''; + $variables['signature'] = $comment->author->signature; $uri = $comment->uri(); $uri['options'] += array('attributes' => array('class' => 'permalink', 'rel' => 'bookmark')); @@ -1795,7 +1766,7 @@ function template_preprocess_comment(&$variables) { if ($variables['new']) { $variables['attributes']['class'][] = 'new'; } - if (!$comment->uid) { + if (!account_uid_load($comment->uid)) { $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 9b507e7..2937fa8 100644 --- a/core/modules/comment/lib/Drupal/comment/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Comment.php @@ -50,7 +50,6 @@ class Comment extends Entity implements ContentEntityInterface { */ public $subject; - /** * The comment author ID. * @@ -59,33 +58,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..5146731 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, @@ -126,7 +126,7 @@ public function form(array $form, array &$form_state, EntityInterface $comment) $form['author']['homepage'] = array( '#type' => 'url', '#title' => t('Homepage'), - '#default_value' => $comment->homepage, + '#default_value' => '', '#maxlength' => 255, '#size' => 30, '#access' => $is_admin || (!$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), @@ -164,7 +164,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 ? !account_uid_load($comment->uid) : !$user->uid), ); // Add internal comment properties. @@ -243,23 +243,6 @@ public function validate(array $form, array &$form_state) { form_set_error('name', t('You have to specify a valid author.')); } } - elseif ($form_state['values']['is_anonymous']) { - // Validate anonymous comment author fields (if given). If the (original) - // author of this comment was an anonymous user, verify that no registered - // user with this name exists. - if ($form_state['values']['name']) { - $query = db_select('users', 'u'); - $query->addField('u', 'uid', 'uid'); - $taken = $query - ->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.')); - } - } - } } /** @@ -279,11 +262,16 @@ public function submit(array $form, array &$form_state) { // 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. + if ($comment->is_anonymous) { + $user = entity_create('user', array( + 'name' => empty($form_state['values']['name']) ? config('user.settings')->get('anonymous') : $form_state['values']['name'], + 'mail' => $form_state['values']['mail'], + )); + + $comment->author = $user; } // Validate the comment's subject. If not specified, extract from comment @@ -341,6 +329,12 @@ public function save(array $form, array &$form_state) { user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage')))); } + // 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); $form_state['values']['cid'] = $comment->cid; 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)