From e3e87c738e8276dbfd4d3a0f9d74302896074558 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 20 Jun 2018 11:45:48 +1200 Subject: [PATCH] Move DAO function to DAO class, call it from Merge class --- CRM/Core/DAO.php | 48 +++++++++++++++++++++++++++++++++++++++ CRM/Dedupe/Merger.php | 51 +++++++++--------------------------------- CRM/Logging/Schema.php | 2 +- 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 4d7a6f83fe03..b4d07e75adcc 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -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'])) { + 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. * diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 009f842c0aad..921f30b4c2ff 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -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']; } @@ -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); } @@ -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. * diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index 996653ce6cd4..a24906ef91b1 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -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); }