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

Move DAO function to DAO class, call it from Merge class #12340

Merged
merged 1 commit into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,54 @@ public static function getReferencesToTable($tableName) {
return $refsFound;
}

/**
* Get all references to contact table.
*
* This includes core tables, custom group tables, tables added by the merge
* hook and the entity_tag table.
*
* Refer to CRM-17454 for information on the danger of querying the information
* schema to derive this.
*/
public static function getReferencesToContactTable() {
if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['contact_references'])) {
Copy link
Contributor Author

@eileenmcnaughton eileenmcnaughton Jun 21, 2018

Choose a reason for hiding this comment

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

@totten so I'm curious about converting this to a cache call

In theory the things that would change the list of tables here are

  1. a new custom table being created
  2. a change to core schema
  3. a change to hook-declared entity types.

3 breaks down into
a) new extension installed
b) new extension uninstalled
c) extension upgrade WITH upgrade extension script run
d) extension upgrade without any script run
e) main civicrm upgrade script runs and hooks are called in upgrade mode - causing the list of cached extension functions to be built without extension hooks being called and hence to be missing some

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(note I think this PR is mergeable regardless of my tangent)

return \Civi::$statics[__CLASS__]['contact_references'];
}
$contactReferences = [];
$coreReferences = CRM_Core_DAO::getReferencesToTable('civicrm_contact');
foreach ($coreReferences as $coreReference) {
if (!is_a($coreReference, 'CRM_Core_Reference_Dynamic')) {
$contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
}
}
self::appendCustomTablesExtendingContacts($contactReferences);

// FixME for time being adding below line statically as no Foreign key constraint defined for table 'civicrm_entity_tag'
$contactReferences['civicrm_entity_tag'][] = 'entity_id';
\Civi::$statics[__CLASS__]['contact_references'] = $contactReferences;
return \Civi::$statics[__CLASS__]['contact_references'];
}

/**
* Add custom tables that extend contacts to the list of contact references.
*
* CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
* function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
* - the down side is it is not cached.
*
* Further changes should be include tests in the CRM_Core_MergerTest class
* to ensure that disabled, subtype, multiple etc groups are still captured.
*
* @param array $cidRefs
*/
public static function appendCustomTablesExtendingContacts(&$cidRefs) {
$customValueTables = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact');
$customValueTables->find();
while ($customValueTables->fetch()) {
$cidRefs[$customValueTables->table_name] = array('entity_id');
}
}

/**
* Lookup the value of a MySQL global configuration variable.
*
Expand Down
51 changes: 10 additions & 41 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,35 +203,23 @@ public static function getActiveRelTables($cid) {
/**
* Get array tables and fields that reference civicrm_contact.id.
*
* This includes core tables, custom group tables, tables added by the merge
* hook and (somewhat randomly) the entity_tag table.
* This function calls the merge hook and only exists to wrap the DAO function to support that deprecated call.
* The entityTypes hook is the recommended way to add tables to this result.
*
* Refer to CRM-17454 for information on the danger of querying the information
* schema to derive this.
*
* This function calls the merge hook but the entityTypes hook is the recommended
* way to add tables to this result.
* I thought about adding another hook to alter tableReferences but decided it was unclear if there
* are use cases not covered by entityTables and instead we should wait & see.
*/
public static function cidRefs() {
if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['contact_references'])) {
return \Civi::$statics[__CLASS__]['contact_references'];
}
$contactReferences = array();
$coreReferences = CRM_Core_DAO::getReferencesToTable('civicrm_contact');
foreach ($coreReferences as $coreReference) {
if (!is_a($coreReference, 'CRM_Core_Reference_Dynamic')) {
$contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
}
}
self::addCustomTablesExtendingContactsToCidRefs($contactReferences);

// FixME for time being adding below line statically as no Foreign key constraint defined for table 'civicrm_entity_tag'
$contactReferences['civicrm_entity_tag'][] = 'entity_id';
$contactReferences = $coreReferences = CRM_Core_DAO::getReferencesToContactTable();

// Allow hook_civicrm_merge() to adjust $cidRefs.
// Note that if entities are registered using the entityTypes hook there
// is no need to use this hook.
CRM_Utils_Hook::merge('cidRefs', $contactReferences);
if ($contactReferences !== $coreReferences) {
Civi::log()->warning("Deprecated hook ::merge in context of 'cidRefs. Use entityTypes instead.", array('civi.tag' => 'deprecated'));
}
\Civi::$statics[__CLASS__]['contact_references'] = $contactReferences;
return \Civi::$statics[__CLASS__]['contact_references'];
}
Expand Down Expand Up @@ -486,7 +474,8 @@ public static function moveContactBelongings($mainId, $otherId, $tables = FALSE,
// getting all custom tables
$customTables = array();
if ($customTableToCopyFrom !== NULL) {
self::addCustomTablesExtendingContactsToCidRefs($customTables);
// @todo this duplicates cidRefs?
CRM_Core_DAO::appendCustomTablesExtendingContacts($customTables);
$customTables = array_keys($customTables);
}

Expand Down Expand Up @@ -1925,26 +1914,6 @@ public static function addMembershipToRealtedContacts($contactID) {
}
}

/**
* Add custom tables that extend contacts to the list of contact references.
*
* CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
* function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
* - the down side is it is not cached.
*
* Further changes should be include tests in the CRM_Core_MergerTest class
* to ensure that disabled, subtype, multiple etc groups are still captured.
*
* @param array $cidRefs
*/
public static function addCustomTablesExtendingContactsToCidRefs(&$cidRefs) {
$customValueTables = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact');
$customValueTables->find();
while ($customValueTables->fetch()) {
$cidRefs[$customValueTables->table_name] = array('entity_id');
}
}

/**
* Create activities tracking the merge on affected contacts.
*
Expand Down
2 changes: 1 addition & 1 deletion CRM/Logging/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ public static function disableLoggingForThisConnection() {
* but this is the only entity currently available...
*/
public function getLogTablesForContact() {
$tables = array_keys(CRM_Dedupe_Merger::cidRefs());
$tables = array_keys(CRM_Core_DAO::getReferencesToContactTable());
return array_intersect($tables, $this->tables);
}

Expand Down