diff -u b/core/modules/history/history.module b/core/modules/history/history.module --- b/core/modules/history/history.module +++ b/core/modules/history/history.module @@ -88,7 +88,7 @@ * \Drupal\history\HistoryRepositoryInterface::updateLastViewed() instead. * @see https://www.drupal.org/node/2197189 */ -function history_write($nid, $account = NULL) { +function history_write($nid, AccountInterface $account = NULL) { if (!isset($account)) { $account = \Drupal::currentUser(); reverted: --- b/core/modules/history/history.services.yml +++ /dev/null @@ -1,6 +0,0 @@ -services: - history.repository: - class: Drupal\history\HistoryRepository - arguments: ['@database', '@datetime.time', '@entity.memory_cache', '@current_user'] - tags: - - { name: backend_overridable } reverted: --- b/core/modules/history/src/HistoryRepository.php +++ /dev/null @@ -1,205 +0,0 @@ -connection = $connection; - $this->time = $time; - $this->memoryCache = $memory_cache; - $this->currentUser = $current_user; - } - - /** - * {@inheritdoc} - */ - public function getLastViewed($entity_type, array $entity_ids) { - $entities = []; - $entities_to_read = []; - $user_id = $this->currentUser->id(); - foreach ($entity_ids as $entity_id) { - // Load from the cache. - $cached = $this->memoryCache->get( - $this->buildCacheId($user_id, $entity_type, $entity_id - )); - if ($cached) { - $entities[$entity_id] = $cached->data; - } - else { - $entities_to_read[$entity_id] = 0; - } - } - - if (empty($entities_to_read)) { - return $entities; - } - - $result = $this->connection->select('history', 'h') - ->fields('h', ['entity_id', 'timestamp']) - ->condition('uid', $user_id) - ->condition('entity_type', $entity_type) - ->condition('entity_id', array_keys($entities_to_read), 'IN') - ->execute(); - - foreach ($result as $row) { - $timestamp = (int) $row->timestamp; - $this->memoryCache->set( - $this->buildCacheId($user_id, $entity_type, $row->entity_id), - $timestamp, - Cache::PERMANENT, - $this->getCacheTags($user_id, $row->entity_id) - ); - $entities_to_read[$row->entity_id] = $timestamp; - } - - return $entities + $entities_to_read; - } - - /** - * {@inheritdoc} - */ - public function updateLastViewed(EntityInterface $entity) { - if ($this->currentUser->isAuthenticated()) { - $user_id = $this->currentUser->id(); - $time = $this->time->getRequestTime(); - $entity_id = $entity->id(); - $entity_type_id = $entity->getEntityTypeId(); - $this->connection->merge('history') - ->keys([ - 'uid' => $user_id, - 'entity_id' => $entity_id, - 'entity_type' => $entity_type_id, - ]) - ->fields(['timestamp' => $time]) - ->execute(); - // Update cached value. - $this->memoryCache->set( - $this->buildCacheId($user_id, $entity_type_id, $entity_id), - $time, Cache::PERMANENT, - $this->getCacheTags($user_id, $entity_id) - ); - } - return $this; - } - - /** - * Builds the cache ID for the history timestamp. - * - * @param int $uid - * The User ID. - * @param string $entity_type - * The entity type. - * @param int $entity_id - * The entity ID. - * - * @return string - * Cache ID that can be passed to the cache backend. - */ - protected function buildCacheId($uid, $entity_type, $entity_id) { - return implode(':', ['history', $uid, $entity_type, $entity_id]); - } - - /** - * {@inheritdoc} - */ - public function getCacheTags($uid, $entity_id) { - return [ - 'history', - "history:user:{$uid}", - "history:entity:{$entity_id}", - ]; - } - - /** - * {@inheritdoc} - */ - public function purge() { - $this->connection->delete('history') - ->condition('timestamp', HISTORY_READ_LIMIT, '<') - ->execute(); - // Clean static cache. - Cache::invalidateTags(['history']); - } - - /** - * {@inheritdoc} - */ - public function deleteByUser(AccountInterface $account) { - $this->connection->delete('history') - ->condition('uid', $account->id()) - ->execute(); - // Clean static cache. - Cache::invalidateTags(["history:user:{$account->id()}"]); - } - - /** - * {@inheritdoc} - */ - public function deleteByEntity(EntityInterface $entity) { - $this->connection->delete('history') - ->condition('entity_id', $entity->id()) - ->condition('entity_type', $entity->getEntityTypeId()) - ->execute(); - // Clean static cache. - Cache::invalidateTags(["history:entity:{$entity->id()}"]); - } - -} reverted: --- b/core/modules/history/src/HistoryRepositoryInterface.php +++ /dev/null @@ -1,72 +0,0 @@ -databaseDumpFiles = [ - __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.8.0.bare.standard.php.gz', - ]; - } - - /** - * Tests changing nid to entity_id and adding an entity_type field to the history table. - * - * @see history_update_8801() - */ - public function testUpdateHookN() { - $database = \Drupal::database(); - $schema = $database->schema(); - - // Run updates. - $this->runUpdates(); - - // Ensure fields were added. - $this->assertTrue($schema->fieldExists('history', 'entity_type')); - $this->assertTrue($schema->fieldExists('history', 'entity_id')); - // Ensure field was removed. - $this->assertFalse($schema->fieldExists('history', 'nid')); - - $this->assertTrue($schema->indexExists('history', 'history_entity')); - $this->assertFalse($schema->indexExists('history', 'nid')); - - $entries = $database->select('history') - ->fields('history') - ->condition('entity_type', 'node', '!=') - ->countQuery() - ->execute() - ->fetchField(); - $this->assertEquals(0, $entries); - } - -}