diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/views/field/NodeNewComments.php b/core/modules/comment/lib/Drupal/comment/Plugin/views/field/NodeNewComments.php index edaaa00..1d23eb8 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/views/field/NodeNewComments.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/views/field/NodeNewComments.php @@ -119,7 +119,7 @@ public function preRender(&$values) { if ($nids) { $result = $this->database->query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comment} c ON n.nid = c.entity_id AND c.entity_type = 'node' - LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = :h_uid WHERE n.nid IN (:nids) + LEFT JOIN {history} h ON h.entity_id = n.nid AND h.entity_type = 'node' AND h.uid = :h_uid WHERE n.nid IN (:nids) AND c.changed > GREATEST(COALESCE(h.timestamp, :timestamp), :timestamp) AND c.status = :status GROUP BY n.nid", array( ':status' => COMMENT_PUBLISHED, ':h_uid' => $user->id(), diff --git a/core/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php index cdf9a5e..ef08337 100644 --- a/core/modules/forum/lib/Drupal/forum/ForumManager.php +++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php @@ -331,11 +331,12 @@ protected function lastVisit($nid) { if (empty($this->history[$nid])) { $result = $this->connection->select('history', 'h') - ->fields('h', array('nid', 'timestamp')) + ->fields('h', array('entity_id', 'timestamp')) ->condition('uid', $user->id()) + ->condition('entity_type', 'node') ->execute(); foreach ($result as $t) { - $this->history[$t->nid] = $t->timestamp > HISTORY_READ_LIMIT ? $t->timestamp : HISTORY_READ_LIMIT; + $this->history[$t->entity_id] = $t->timestamp > HISTORY_READ_LIMIT ? $t->timestamp : HISTORY_READ_LIMIT; } } return isset($this->history[$nid]) ? $this->history[$nid] : HISTORY_READ_LIMIT; @@ -498,7 +499,7 @@ public function checkNodeType(NodeInterface $node) { public function unreadTopics($term, $uid) { $query = $this->connection->select('node_field_data', 'n'); $query->join('forum', 'f', 'n.vid = f.vid AND f.tid = :tid', array(':tid' => $term)); - $query->leftJoin('history', 'h', 'n.nid = h.nid AND h.uid = :uid', array(':uid' => $uid)); + $query->leftJoin('history', 'h', "n.nid = h.entity_id AND h.entity_type = 'node' AND h.uid = :uid", array(':uid' => $uid)); $query->addExpression('COUNT(n.nid)', 'count'); return $query ->condition('status', 1) @@ -506,7 +507,7 @@ public function unreadTopics($term, $uid) { // field language and just fall back to the default language. ->condition('n.default_langcode', 1) ->condition('n.created', HISTORY_READ_LIMIT, '>') - ->isNull('h.nid') + ->isNull('h.entity_id') ->addTag('node_access') ->execute() ->fetchField(); diff --git a/core/modules/history/history.install b/core/modules/history/history.install index dcfd871..086c8f7 100644 --- a/core/modules/history/history.install +++ b/core/modules/history/history.install @@ -13,13 +13,20 @@ function history_schema() { 'description' => 'A record of which {users} have read which {node}s.', 'fields' => array( 'uid' => array( - 'description' => 'The {users}.uid that read the {node} nid.', + 'description' => 'The {users}.uid that read the entity_id.', 'type' => 'int', 'not null' => TRUE, 'default' => 0, ), - 'nid' => array( - 'description' => 'The {node}.nid that was read.', + 'entity_type' => array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'node', + 'length' => 255, + 'description' => 'The entity_type of the entity was read.', + ), + 'entity_id' => array( + 'description' => 'The entity_id that was read.', 'type' => 'int', 'not null' => TRUE, 'default' => 0, @@ -31,11 +38,19 @@ function history_schema() { 'default' => 0, ), ), - 'primary key' => array('uid', 'nid'), + 'primary key' => array( + 'uid', + 'entity_id', + array('entity_type', 32), + ), 'indexes' => array( - 'nid' => array('nid'), + 'history_entity' => array( + 'entity_id', + array('entity_type', 32), + ), ), ); return $schema; } + diff --git a/core/modules/history/history.module b/core/modules/history/history.module index 0dfad16..2e1acef 100644 --- a/core/modules/history/history.module +++ b/core/modules/history/history.module @@ -10,6 +10,7 @@ */ use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\entity\Entity\EntityDisplay; /** @@ -78,12 +79,12 @@ function history_read_multiple($nids) { return $return; } - $result = db_query('SELECT nid, timestamp FROM {history} WHERE uid = :uid AND nid IN(:nids)', array( + $result = db_query("SELECT entity_id, timestamp FROM {history} WHERE entity_type = 'node' AND uid = :uid AND entity_id IN(:nids)", array( ':uid' => \Drupal::currentUser()->id(), ':nids' => array_keys($nodes_to_read), )); foreach ($result as $row) { - $nodes_to_read[$row->nid] = (int) $row->timestamp; + $nodes_to_read[$row->entity_id] = (int) $row->timestamp; } $history += $nodes_to_read; @@ -93,13 +94,13 @@ function history_read_multiple($nids) { /** * Updates 'last viewed' timestamp of the specified entity for the current user. * - * @param $nid + * @param int $nid * The node ID that has been read. - * @param $account + * @param \Drupal\Core\Session\AccountInterface $account * (optional) The user account to update the history for. Defaults to the * current user. */ -function history_write($nid, $account = NULL) { +function history_write($nid, AccountInterface $account = NULL) { global $user; if (!isset($account)) { @@ -110,7 +111,8 @@ function history_write($nid, $account = NULL) { db_merge('history') ->key(array( 'uid' => $account->id(), - 'nid' => $nid, + 'entity_id' => $nid, + 'entity_type' => 'node', )) ->fields(array('timestamp' => REQUEST_TIME)) ->execute(); @@ -158,7 +160,8 @@ function history_node_view_alter(&$build, EntityInterface $node, EntityDisplay $ */ function history_node_delete(EntityInterface $node) { db_delete('history') - ->condition('nid', $node->id()) + ->condition('entity_id', $node->id()) + ->condition('entity_type', 'node') ->execute(); } diff --git a/core/modules/history/history.views.inc b/core/modules/history/history.views.inc index 50e12aa..571d7f3 100644 --- a/core/modules/history/history.views.inc +++ b/core/modules/history/history.views.inc @@ -22,8 +22,9 @@ function history_views_data() { 'node' => array( 'table' => 'history', 'left_field' => 'nid', - 'field' => 'nid', + 'field' => 'entity_id', 'extra' => array( + array('field' => 'entity_type', 'value' => 'node'), array('field' => 'uid', 'value' => '***CURRENT_USER***', 'numeric' => TRUE), ), ), diff --git a/core/modules/history/lib/Drupal/history/Controller/HistoryController.php b/core/modules/history/lib/Drupal/history/Controller/HistoryController.php index b605e6e..026b02e 100644 --- a/core/modules/history/lib/Drupal/history/Controller/HistoryController.php +++ b/core/modules/history/lib/Drupal/history/Controller/HistoryController.php @@ -25,7 +25,7 @@ class HistoryController extends ControllerBase { * @param \Symfony\Component\HttpFoundation\Request $request * The request of the page. * - * @return Symfony\Component\HttpFoundation\JsonResponse + * @return \Symfony\Component\HttpFoundation\JsonResponse * The JSON response. */ public function getNodeReadTimestamps(Request $request) { diff --git a/core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php b/core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php index 182174c..6eaca65 100644 --- a/core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php +++ b/core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php @@ -54,14 +54,16 @@ public function testHandlers() { db_insert('history') ->fields(array( 'uid' => $account->id(), - 'nid' => $nodes[0]->id(), + 'entity_id' => $nodes[0]->id(), + 'entity_type' => 'node', 'timestamp' => REQUEST_TIME - 100, ))->execute(); db_insert('history') ->fields(array( 'uid' => $account->id(), - 'nid' => $nodes[1]->id(), + 'entity_id' => $nodes[1]->id(), + 'entity_type' => 'node', 'timestamp' => REQUEST_TIME + 100, ))->execute(); diff --git a/core/modules/node/node.install b/core/modules/node/node.install index b240ecc..fceaa3b 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -720,9 +720,37 @@ function node_update_8011() { } /** - * Enable History module. + * Convert {history} table to new format and enable History module. */ function node_update_8012() { + // Drop all keys to properly rename constraints. + db_drop_primary_key('history'); + db_drop_index('history', 'nid'); + // Add new column. + db_add_field('history', 'entity_type', array( + 'type' => 'varchar', + 'not null' => TRUE, + 'default' => 'node', + 'length' => 255, + 'description' => 'The entity_type of the entity was read.', + )); + db_change_field('history', 'nid', 'entity_id', array( + 'description' => 'The entity_id that was read.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + )); + // Create new indexes. + db_add_primary_key('history', array( + 'uid', + 'entity_id', + array('entity_type', 32), + )); + db_add_index('history', 'history_entity', array( + 'entity_id', + array('entity_type', 32), + )); + // Enable the history module without re-installing the schema. \Drupal::moduleHandler()->install(array('history')); }