diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index d641f34..114bef4 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -566,6 +566,22 @@ function comment_update_8006(&$sandbox) { foreach ($node_types as $node_type) { // COMMENT_OPEN $default_value = variable_get('comment_' . $node_type, 2); + // Skip to create field when comments disabled or hidden for the given type + // and there's no allowed comments to be posted for. + $comments_exists = (bool) db_query_range("SELECT 1 FROM {comment} c WHERE c.entity_type = 'node' AND c.entity_id IN (SELECT nid FROM {node} WHERE type = :type)", 0, 1, array(':type' => $node_type))->fetchField(); + $commenting_enabled = (bool) db_query_range("SELECT 1 FROM {node} WHERE type = :type and comment = 2", 0, 1, array(':type' => $node_type)); + if (!($comments_exists || $commenting_enabled || $default_value == 2)) { + // Mark deleted all field instances for the comment bundle to cleanup + // unused data in database. + db_update('field_config_instance') + ->fields(array( + 'deleted' => 1, + )) + ->condition('entity_type', 'comment') + ->condition('bundle', 'comment_node_' . $node_type) + ->execute(); + continue; + } // Add a default comment field for existing node comments. $field = array( 'cardinality' => '1', @@ -706,58 +722,67 @@ function comment_update_8007(&$sandbox) { ->fetchAllAssoc('nid'); if (count($nodes) > 0) { - $insert = db_insert('field_data_comment_node_' . $sandbox['node_type']) - ->fields(array( - 'entity_type', - 'bundle', - 'entity_id', - 'revision_id', - 'langcode', - 'delta', - 'comment_node_' . $sandbox['node_type'] . '_comment', - )); - $revision = db_insert('field_revision_comment_node_' . $sandbox['node_type']) - ->fields(array( - 'entity_type', - 'bundle', - 'entity_id', - 'revision_id', - 'langcode', - 'delta', - 'comment_node_' . $sandbox['node_type'] . '_comment', - )); - - // Update the field name to match the node type. - db_update('comment') - ->fields(array( - 'field_name' => 'comment_node_' . $sandbox['node_type'], - )) - ->condition('entity_id', array_keys($nodes)) - ->execute(); - foreach ($nodes as $nid => $node) { - $insert->values(array( - 'entity_type' => 'node', - 'bundle' => $sandbox['node_type'], - 'entity_id' => $nid, - 'revision_id' => $node->vid, - 'langcode' => $node->langcode, - 'delta' => 0, - 'comment_node_' . $sandbox['node_type'] . '_comment' => $node->comment, - )); - $revision->values(array( - 'entity_type' => 'node', - 'bundle' => $sandbox['node_type'], - 'entity_id' => $nid, - 'revision_id' => $node->vid, - 'langcode' => $node->langcode, - 'delta' => 0, - 'comment_node_' . $sandbox['node_type'] . '_comment' => $node->comment, - )); - $sandbox['progress']++; - $sandbox['current_nid'] = $nid; + // Do not try update data if no comment fields were added. + if (db_table_exists('field_data_comment_node_' . $sandbox['node_type'])) { + $insert = db_insert('field_data_comment_node_' . $sandbox['node_type']) + ->fields(array( + 'entity_type', + 'bundle', + 'entity_id', + 'revision_id', + 'langcode', + 'delta', + 'comment_node_' . $sandbox['node_type'] . '_comment', + )); + $revision = db_insert('field_revision_comment_node_' . $sandbox['node_type']) + ->fields(array( + 'entity_type', + 'bundle', + 'entity_id', + 'revision_id', + 'langcode', + 'delta', + 'comment_node_' . $sandbox['node_type'] . '_comment', + )); + + // Update the field name to match the node type. + db_update('comment') + ->fields(array( + 'field_name' => 'comment_node_' . $sandbox['node_type'], + )) + ->condition('entity_id', array_keys($nodes)) + ->execute(); + foreach ($nodes as $nid => $node) { + $insert->values(array( + 'entity_type' => 'node', + 'bundle' => $sandbox['node_type'], + 'entity_id' => $nid, + 'revision_id' => $node->vid, + 'langcode' => $node->langcode, + 'delta' => 0, + 'comment_node_' . $sandbox['node_type'] . '_comment' => $node->comment, + )); + $revision->values(array( + 'entity_type' => 'node', + 'bundle' => $sandbox['node_type'], + 'entity_id' => $nid, + 'revision_id' => $node->vid, + 'langcode' => $node->langcode, + 'delta' => 0, + 'comment_node_' . $sandbox['node_type'] . '_comment' => $node->comment, + )); + $sandbox['progress']++; + $sandbox['current_nid'] = $nid; + } + $insert->execute(); + $revision->execute(); + } + else { + // Skip the nodes to show progress. + $sandbox['progress'] += count($nodes); + end($nodes); + $sandbox['current_nid'] = key($nodes); } - $insert->execute(); - $revision->execute(); } else { // Move to the next node type. diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/CommentUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/CommentUpgradePathTest.php index 2e8e3c9..3339e52 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/CommentUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/CommentUpgradePathTest.php @@ -28,6 +28,8 @@ public function setUp() { drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.filled.standard_all.database.php.gz', // Language dataset includes nodes with comments so can be reused. drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.language.database.php', + // Comment dataset includes comment and variable for poll node type. + drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.comment.database.php', ); parent::setUp(); } @@ -40,15 +42,21 @@ public function testCommentUpgrade() { // Check that comments display on the node. $this->drupalGet('node/50'); - $node = node_load(50); $this->assertText('Node title 50', 'Node 50 displayed after update.'); $this->assertText('First test comment', 'Comment 1 displayed after update.'); $this->assertText('Reply to first test comment', 'Comment 2 displayed after update.'); + // Check that comment statistics updated properly. + $node = node_load(37); + debug($node, 'node 37'); + $node = node_load(50); + debug($node, 'node 50'); + // Check one instance exists for each node type. $types = node_type_get_types(); foreach (array_keys($types) as $type) { $instance = field_info_instance('node', 'comment_node_' . $type, $type); + debug($instance, $type); $this->assertTrue($instance, format_string('Comment field found for the %type node type', array( '%type' => $type ))); diff --git a/core/modules/system/tests/upgrade/drupal-7.comment.database.php b/core/modules/system/tests/upgrade/drupal-7.comment.database.php new file mode 100644 index 0000000..f4d6bff --- /dev/null +++ b/core/modules/system/tests/upgrade/drupal-7.comment.database.php @@ -0,0 +1,281 @@ +fields(array( + 'cid', + 'pid', + 'nid', + 'uid', + 'subject', + 'hostname', + 'created', + 'changed', + 'status', + 'thread', + 'name', + 'mail', + 'homepage', + 'language', + )) + ->values(array( + 'cid' => '3', + 'pid' => '0', + 'nid' => '37', + 'uid' => '6', + 'subject' => 'First test comment', + 'hostname' => '127.0.0.1', + 'created' => '1314997642', + 'changed' => '1314997642', + 'status' => '1', + 'thread' => '01/', + 'name' => 'test user 4', + 'mail' => '', + 'homepage' => '', + 'language' => 'und', + )) + ->execute(); + +// Add the comment body. +db_insert('field_data_comment_body') + ->fields(array( + 'entity_type', + 'bundle', + 'deleted', + 'entity_id', + 'revision_id', + 'language', + 'delta', + 'comment_body_value', + 'comment_body_format', + )) + ->values(array( + 'entity_type' => 'comment', + 'bundle' => 'comment_node_poll', + 'deleted' => '0', + 'entity_id' => '3', + 'revision_id' => '3', + 'language' => 'und', + 'delta' => '0', + 'comment_body_value' => 'Poll comment body', + 'comment_body_format' => 'filtered_html', + )) + ->execute(); + +// Add revision for comment body. +db_insert('field_revision_comment_body') + ->fields(array( + 'entity_type', + 'bundle', + 'deleted', + 'entity_id', + 'revision_id', + 'language', + 'delta', + 'comment_body_value', + 'comment_body_format', + )) + ->values(array( + 'entity_type' => 'comment', + 'bundle' => 'comment_node_poll', + 'deleted' => '0', + 'entity_id' => '3', + 'revision_id' => '3', + 'language' => 'und', + 'delta' => '0', + 'comment_body_value' => 'Poll comment body', + 'comment_body_format' => 'filtered_html', + )) + ->execute(); + +// Update comment statistics for node 37. +db_update('node_comment_statistics') + ->fields(array( + 'cid' => '3', + 'last_comment_timestamp' => '1314997642', + 'last_comment_name' => 'test user 4', + 'last_comment_uid' => '6', + 'comment_count' => '1', + )) + ->condition('nid', '37') + ->execute(); + +// Update comment statistics for node 50. +db_update('node_comment_statistics') + ->fields(array( + 'cid' => '2', + 'last_comment_timestamp' => '1314997642', + 'last_comment_name' => 'test user 4', + 'last_comment_uid' => '6', + 'comment_count' => '2', + )) + ->condition('nid', '50') + ->execute(); + +// Add book node. +db_insert('node') + ->fields(array( + 'nid', + 'vid', + 'type', + 'language', + 'title', + 'uid', + 'status', + 'created', + 'changed', + 'comment', + 'promote', + 'sticky', + 'tnid', + 'translate', + )) + ->values(array( + 'nid' => '54', + 'vid' => '90', + 'type' => 'book', + 'language' => 'und', + 'title' => 'Node title 54', + 'uid' => '6', + 'status' => '1', + 'created' => '1263769200', + 'changed' => '1314997642', + 'comment' => '2', + 'promote' => '0', + 'sticky' => '0', + 'tnid' => '0', + 'translate' => '0', + )) + ->execute(); + +// Add node revision information. +db_insert('node_revision') + ->fields(array( + 'nid', + 'vid', + 'uid', + 'title', + 'log', + 'timestamp', + 'status', + 'comment', + 'promote', + 'sticky', + )) + ->values(array( + 'nid' => '54', + 'vid' => '90', + 'uid' => '6', + 'title' => 'Node title 54', + 'log' => 'Added a LANGUAGE_NOT_SPECIFIED node to comment on.', + 'timestamp' => '1314997642', + 'status' => '1', + 'comment' => '2', + 'promote' => '0', + 'sticky' => '0', + )) + ->execute(); + +// Add the body field value. +db_insert('field_data_body') + ->fields(array( + 'entity_type', + 'bundle', + 'deleted', + 'entity_id', + 'revision_id', + 'language', + 'delta', + 'body_value', + 'body_summary', + 'body_format', + )) + ->values(array( + 'entity_type' => 'node', + 'bundle' => 'book', + 'deleted' => '0', + 'entity_id' => '54', + 'revision_id' => '90', + 'language' => 'und', + 'delta' => '0', + 'body_value' => 'Node body', + 'body_summary' => 'Node body', + 'body_format' => 'filtered_html', + )) + ->execute(); + +// Add revision information for the body field value. +db_insert('field_revision_body') + ->fields(array( + 'entity_type', + 'bundle', + 'deleted', + 'entity_id', + 'revision_id', + 'language', + 'delta', + 'body_value', + 'body_summary', + 'body_format', + )) + ->values(array( + 'entity_type' => 'node', + 'bundle' => 'book', + 'deleted' => '0', + 'entity_id' => '54', + 'revision_id' => '90', + 'language' => 'und', + 'delta' => '0', + 'body_value' => 'Node body', + 'body_summary' => 'Node body', + 'body_format' => 'filtered_html', + )) + ->execute(); + + +// Add node comment statistics for the book node. +db_insert('node_comment_statistics') + ->fields(array( + 'nid', + 'cid', + 'last_comment_timestamp', + 'last_comment_name', + 'last_comment_uid', + 'comment_count', + )) + ->values(array( + 'nid' => '54', + 'cid' => '0', + 'last_comment_timestamp' => '1314997642', + 'last_comment_name' => NULL, + 'last_comment_uid' => '6', + 'comment_count' => '0', + )) + ->execute(); + + +// Disable commenting on poll and book node types. +db_insert('variable') + ->fields(array( + 'name', + 'value', + )) + ->values(array( + 'name' => 'comment_poll', + 'value' => 'i:1;', + )) + ->values(array( + 'name' => 'comment_book', + 'value' => 'i:1;', + )) + ->execute();