Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isMultilingual to use static caching and respect current domain #17646

Merged
merged 1 commit into from
Jun 18, 2020

Conversation

colemanw
Copy link
Member

Overview

This improves the efficiency and accuracy of the CRM_Core_I18n::isMultilingual function, preventing a large number of duplicate queries on every request.

Before

The function CRM_Core_I18n::isMultilingual apparently had two problems:

  1. The results were not cached, and since this function is called frequently, that resulted in repetitive hits on the database.
  2. The function did not seem to respect multi-domain setups, as it always used domain 1 instead of the current domain.

After

Fixed.

Comments

This is going to especially help the efficiency of medatata-heavy operations like APIv4, as AllCoreTables calls this function repeatedly.

@civibot
Copy link

civibot bot commented Jun 18, 2020

(Standard links)

@civibot civibot bot added the master label Jun 18, 2020
@seamuslee001
Copy link
Contributor

@colemanw
Copy link
Member Author

I feel like I must be missing something because in the file you linked I see the exact same pattern which seems to completely ignore which domain we're on and just arbitrarily select the first one.

It does it here:

public static function dropAllViews() {
$domain = new CRM_Core_DAO_Domain();
$domain->find(TRUE);

And here:

public static function makeMultilingual($locale) {
$domain = new CRM_Core_DAO_Domain();
$domain->find(TRUE);

Am I missing something obvious? These lookups do not pass in domain ID before calling find() so wouldn't that always return domain 1? Is this a bug that no one noticed because it's so rare to use multi-domain and multi-lingual at the same time?
@mlutfy ?

@colemanw
Copy link
Member Author

Do we need to put any kind of statics flushing

@seamuslee001 I've switched this PR to use CRM_Core_DAO::getFieldValue() which has static caching and automatic flushing whenever there is a write operation to that object.

@mlutfy
Copy link
Member

mlutfy commented Jun 18, 2020

I never used multi-domain, but the fix seems OK (with my limited understanding of the underlying workings of statics).

@seamuslee001
Copy link
Contributor

Change makes sense to me tests have passed merging as per review from @mlutfy

@seamuslee001 seamuslee001 merged commit 29a0f2e into civicrm:master Jun 18, 2020
@colemanw
Copy link
Member Author

Thanks @seamuslee001 I followed up with civicrm/civicrm-drupal#604

$domain->find(TRUE);
return (bool) $domain->locales;
$domainId = CRM_Core_Config::domainID();
return (bool) CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $domainId, 'locales');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker/revertable thing -- but the function CRM_Core_BAO_Domain::getDomain() returns a cached instance of the active domain record, eg perhaps:

return (bool) CRM_Core_BAO_Domain::getDomain()->locales;

I'm pretty sure getDomain() is used in a bunch of other places, so using it would avoid the extra query.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@totten the results of CRM_Core_DAO::getFieldValue are cached in a static var.

I think the real problem with this PR is that it switches from reading domain 1 to reading the current domain, and it was an undocumented rule previously to only store locales data in domain 1. That's the part I want to revert but will have to think it through as the first record in the civicrm_domain table isn't guaranteed to have id 1, so it's not as simple as changing $domainId to 1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton @seamuslee001 @totten we have multiple discussion threads about this and multiple open PRs.
I've changed my mind about the solution and I think having locales populated consistently for all domains is a good thing.
https://lab.civicrm.org/dev/core/-/issues/1852

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaaah, that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants