Problem/Motivation

The history module uses procedural functions to read and write from storage. This makes it hard for other code to alter the functioning of the history.

Proposed resolution

Introduce a HistoryRepository service.

The API interface will allow for entity types other than node to have history, but support for entity types other than node will not be implemented.

Remaining tasks

User interface changes

None.

API changes

Addition of a HistoryRepositoryInterface

Data model changes

None.

Issue fork drupal-3267010

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jonathanshaw created an issue. See original summary.

jonathanshaw’s picture

We should consider compatability with #2006632: Make HISTORY_READ_LIMIT configurable.As long as we don't add @api to our new HistoryRepositoryInterface, we can add future methods to it related to #2006632: Make HISTORY_READ_LIMIT configurable, according to the core BC policy for interfaces.

jonathanshaw’s picture

OK, I've worked in various changes now. Key features:

1. I've added a Kernel test. Yaay!

2. I've settled on this for the API:

  public function getTime(EntityInterface $entity, ?AccountInterface $account, ?$default);
  public function getTimes(string $entity_type, array $entity_ids, ?AccountInterface $account, ?$default): array;
  public function setTime(EntityInterface $entity, ?AccountInterface $account, ?int $time): HistoryRepositoryInterface;
  public function setTimes(string $entity_type, array $entity_ids, ?AccountInterface $account, ?int $time): HistoryRepositoryInterface;

This doesn't support for reading or writing entities from multiple entity types in one database transaction, but that's not possible in this case for writing anyway with Drupal's upsert or merge queries, and I'm unsure anyone will ever need it for reading anyway. I considered supporting multiple entity types in one api call, but it adds a lot of complication to the code.

I'm strongly in faviour of including the account as a parameter here. I'm less concerned about including time but it seems harmless and even helps testing.

3. history_read_multiple() defaults to returning 0 as a time if an entity has no history. This is a dubious piece of API design as 0 is a valid time, and I'm not fond of continuing to force that choice on users of this API What I've done is allow callers to specify the default value they'd like. If they don't specify, or specify NULL, then entities for which we have no history are excluded from the array returned by getTimes(). I dislike the complexity of this and welcome discussion on it.

4. history_read_multiple() uses static caching. I've replicated this in order to avoid a hypothetical performance regression. However, it adds significant complexity and I'm unsure many sites are making repeated calls for history on the same entity in the same request. If we can get this committed without this caching, I'm in favour of removing it from here and deferring it to #3267020: Use caching for history.

jonathanshaw’s picture

Status: Active » Needs review

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave made their first commit to this issue’s fork.

smustgrave’s picture

Was trying to rebase the PR seemed to bork it. Taking a look. Sorry for the trouble

smustgrave’s picture

Fixed. Triggering a new build on https://git.drupalcode.org/project/drupal/-/merge_requests/1903/diffs?co... if no errors I think this is reviewed.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
jonathanshaw’s picture

Status: Reviewed & tested by the community » Needs work
Parent issue: » #2081585: [META] Modernize the History module

Sorry I dropped this one. After the work I did here, I reviewed more the histpry of #2081585: [META] Modernize the History module and realise that I did various things wrong here that did not fully reflect proper consideration of things already discussed in the parent issue.

Especially

(1) the static caching code I put in place to avoid using MemoryCache is simply a mistake. I thought it would be better to avoid MemoryCache to keep things simple in this issue, BUT history_read_multiple already has static caching so we can't do that. When I try to add in static caching in the repository service, the result is so complex it would be better to use MemoryCache

(2) I really didn't like the fact that we defaulted to zero as the last read time if there was no read history, which seems bad because zero is actually a valid unix time. However this adds complexity and has tricky BC issues.

Overall, I think this MR should actually be a lot closer to the last patch in the parent issue, just without the code to update the storage to work with non-node entity types.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

History can have per user LRU memory cache but it needs reasons

quietone’s picture

Status: Needs work » Postponed

The History Module was approved for removal in #3336276: [Policy] Deprecate History module.

This is Postponed. The status is set according to two policies. The Remove a core extension and move it to a contributed project and the Extensions approved for removal policies.

The deprecation work is in #3520462: [meta] Tasks to deprecate the History module and the removal work in #3520468: [meta] Tasks to remove History module.

History will be moved to a contributed project after the Drupal 12.x branch is open.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

longwave’s picture

Project: Drupal core » Node History
Version: main » 1.0.0
Component: history.module » Code
Status: Postponed » Active