From 2ae260015fedb9ae1c863ca09160c8acb9a95c12 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 27 Apr 2016 14:45:08 +1200 Subject: [PATCH] Remove duplication of definition of cacheKeyString This is preliminary tidy up towards CRM-18443. There is no functional change, just the extraction of the definition of the cacheKeyString & the joinClause. Extracting the join clause does not shorten it but it makes it clear the clause is the same, ensures updates are done in all places & hopefully leads to the join being added somewhere more sensible later - ie. instead of passing the join clause in call the same function, or a wrapper function with $isExcludeDedupeExclusions as a paramter. --- CRM/Contact/Form/Merge.php | 18 ++++----------- CRM/Contact/Page/AJAX.php | 5 +--- CRM/Contact/Page/DedupeFind.php | 21 +++-------------- CRM/Contact/Page/DedupeMerge.php | 5 +--- CRM/Core/BAO/PrevNextCache.php | 5 +--- CRM/Dedupe/BAO/RuleGroup.php | 23 +++++++++++++++++++ CRM/Dedupe/Merger.php | 39 +++++++++++++++++++++++++++----- 7 files changed, 67 insertions(+), 49 deletions(-) diff --git a/CRM/Contact/Form/Merge.php b/CRM/Contact/Form/Merge.php index ba62395199..fb7ef8a10e 100644 --- a/CRM/Contact/Form/Merge.php +++ b/CRM/Contact/Form/Merge.php @@ -73,14 +73,9 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form { CRM_Core_Error::statusBounce(ts('The selected pair of contacts are marked as non duplicates. If these records should be merged, you can remove this exception on the Dedupe Exceptions page.', array(1 => CRM_Utils_System::url('civicrm/dedupe/exception', 'reset=1')))); } - //load cache mechanism - $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'contact_type'); - $cacheKey = "merge $contactType"; - $cacheKey .= $rgid ? "_{$rgid}" : '_0'; - $cacheKey .= $gid ? "_{$gid}" : '_0'; - - $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND - pn.entity_id2 = de.contact_id2 )"; + $cacheKey = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); + + $join = CRM_Dedupe_Merger::getJoinOnDedupeTable(); $where = "de.id IS NULL"; $pos = CRM_Core_BAO_PrevNextCache::getPositions($cacheKey, $cid, $oid, $this->_mergeId, $join, $where, $flip); @@ -337,12 +332,9 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form { } if ($this->next && $this->_mergeId) { - $cacheKey = "merge {$this->_contactType}"; - $cacheKey .= $this->_rgid ? "_{$this->_rgid}" : '_0'; - $cacheKey .= $this->_gid ? "_{$this->_gid}" : '_0'; + $cacheKey = CRM_Dedupe_Merger::getMergeCacheKeyString($this->_rgid, $this->_gid); - $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND - pn.entity_id2 = de.contact_id2 )"; + $join = CRM_Dedupe_Merger::getJoinOnDedupeTable(); $where = "de.id IS NULL"; $pos = CRM_Core_BAO_PrevNextCache::getPositions($cacheKey, NULL, NULL, $this->_mergeId, $join, $where); diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php index cd97123088..35f94469f2 100644 --- a/CRM/Contact/Page/AJAX.php +++ b/CRM/Contact/Page/AJAX.php @@ -978,10 +978,7 @@ LIMIT {$offset}, {$rowCount} $pnid = $_REQUEST['pnid']; $isSelected = CRM_Utils_Type::escape($_REQUEST['is_selected'], 'Boolean'); - $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type'); - $cacheKeyString = "merge $contactType"; - $cacheKeyString .= $rgid ? "_{$rgid}" : '_0'; - $cacheKeyString .= $gid ? "_{$gid}" : '_0'; + $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); $params = array( 1 => array($isSelected, 'Boolean'), diff --git a/CRM/Contact/Page/DedupeFind.php b/CRM/Contact/Page/DedupeFind.php index 3305b9d71a..498981eefa 100644 --- a/CRM/Contact/Page/DedupeFind.php +++ b/CRM/Contact/Page/DedupeFind.php @@ -72,11 +72,7 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0); if ($rgid) { - $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type'); - $cacheKeyString = "merge $contactType"; - $cacheKeyString .= $rgid ? "_{$rgid}" : '_0'; - $cacheKeyString .= $gid ? "_{$gid}" : '_0'; - CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKeyString); + CRM_Core_BAO_PrevNextCache::deleteItem(NULL, CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid)); } $urlQry = "reset=1&action=update&rgid={$rgid}"; if ($gid) { @@ -135,14 +131,6 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { $rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE, 0); $this->action = CRM_Core_Action::UPDATE; - //calculate the $contactType - if ($rgid) { - $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', - $rgid, - 'contact_type' - ); - } - $sourceParams = 'snippet=4'; if ($gid) { $sourceParams .= "&gid={$gid}"; @@ -157,9 +145,7 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/dedupefind', $sourceParams, FALSE, NULL, FALSE)); //reload from cache table - $cacheKeyString = "merge $contactType"; - $cacheKeyString .= $rgid ? "_{$rgid}" : '_0'; - $cacheKeyString .= $gid ? "_{$gid}" : '_0'; + $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); $stats = CRM_Dedupe_Merger::getMergeStatsMsg($cacheKeyString); if ($stats) { @@ -167,8 +153,7 @@ class CRM_Contact_Page_DedupeFind extends CRM_Core_Page_Basic { // reset so we not displaying same message again CRM_Dedupe_Merger::resetMergeStats($cacheKeyString); } - $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND - pn.entity_id2 = de.contact_id2 )"; + $join = CRM_Dedupe_Merger::getJoinOnDedupeTable(); $where = "de.id IS NULL"; if ($context == 'conflicts') { $where .= " AND pn.is_selected = 1"; diff --git a/CRM/Contact/Page/DedupeMerge.php b/CRM/Contact/Page/DedupeMerge.php index 6dc571756e..93b0a1ddd9 100644 --- a/CRM/Contact/Page/DedupeMerge.php +++ b/CRM/Contact/Page/DedupeMerge.php @@ -60,10 +60,7 @@ class CRM_Contact_Page_DedupeMerge extends CRM_Core_Page { $action = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject); $mode = CRM_Utils_Request::retrieve('mode', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'safe'); - $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type'); - $cacheKeyString = "merge {$contactType}"; - $cacheKeyString .= $rgid ? "_{$rgid}" : '_0'; - $cacheKeyString .= $gid ? "_{$gid}" : '_0'; + $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); $urlQry = "reset=1&action=update&rgid={$rgid}"; $urlQry = $gid ? ($urlQry . "&gid={$gid}") : $urlQry; diff --git a/CRM/Core/BAO/PrevNextCache.php b/CRM/Core/BAO/PrevNextCache.php index 898b9da9fd..8231dea31f 100644 --- a/CRM/Core/BAO/PrevNextCache.php +++ b/CRM/Core/BAO/PrevNextCache.php @@ -325,10 +325,7 @@ WHERE (pn.cacheKey $op %1 OR pn.cacheKey $op %2) */ public static function refillCache($rgid = NULL, $gid = NULL, $cacheKeyString = NULL) { if (!$cacheKeyString && $rgid) { - $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type'); - $cacheKeyString = "merge {$contactType}"; - $cacheKeyString .= $rgid ? "_{$rgid}" : '_0'; - $cacheKeyString .= $gid ? "_{$gid}" : '_0'; + $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); } if (!$cacheKeyString) { diff --git a/CRM/Dedupe/BAO/RuleGroup.php b/CRM/Dedupe/BAO/RuleGroup.php index de7463b297..56081b4036 100644 --- a/CRM/Dedupe/BAO/RuleGroup.php +++ b/CRM/Dedupe/BAO/RuleGroup.php @@ -468,4 +468,27 @@ class CRM_Dedupe_BAO_RuleGroup extends CRM_Dedupe_DAO_RuleGroup { return $result; } + + /** + * Get the cached contact type for a particular rule group. + * + * @param int $rule_group_id + * + * @return string + */ + public static function getContactTypeForRuleGroup($rule_group_id) { + if (!isset(\Civi::$statics[__CLASS__]) || !isset(\Civi::$statics[__CLASS__]['rule_groups'])) { + \Civi::$statics[__CLASS__]['rule_groups'] = array(); + } + if (empty(\Civi::$statics[__CLASS__]['rule_groups'][$rule_group_id])) { + \Civi::$statics[__CLASS__]['rule_groups'][$rule_group_id]['contact_type'] = CRM_Core_DAO::getFieldValue( + 'CRM_Dedupe_DAO_RuleGroup', + $rule_group_id, + 'contact_type' + ); + } + + return \Civi::$statics[__CLASS__]['rule_groups'][$rule_group_id]['contact_type']; + } + } diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index c5079be6cd..39fa568af9 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -593,12 +593,8 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m * @return array|bool */ public static function batchMerge($rgid, $gid = NULL, $mode = 'safe', $autoFlip = TRUE, $batchLimit = 1, $isSelected = 2) { - $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type'); - $cacheKeyString = "merge {$contactType}"; - $cacheKeyString .= $rgid ? "_{$rgid}" : '_0'; - $cacheKeyString .= $gid ? "_{$gid}" : '_0'; - $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND - pn.entity_id2 = de.contact_id2 )"; + $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid); + $join = CRM_Dedupe_Merger::getJoinOnDedupeTable(); $where = "de.id IS NULL"; if ($isSelected === 0 || $isSelected === 1) { @@ -624,6 +620,21 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m return CRM_Dedupe_Merger::merge($dupePairs, $cacheParams, $mode, $autoFlip, $redirectForPerformance); } + /** + * Get the string to join the prevnext cache to the dedupe table. + * + * @return string + * The join string to join prevnext cache on the dedupe table. + */ + public static function getJoinOnDedupeTable() { + return " + LEFT JOIN civicrm_dedupe_exception de + ON ( + pn.entity_id1 = de.contact_id1 + AND pn.entity_id2 = de.contact_id2 ) + "; + } + public static function updateMergeStats($cacheKeyString, $result = array()) { // gather latest stats $merged = count($result['merged']); @@ -1947,4 +1958,20 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m } } + /** + * Get the cache key string for the merge action. + * + * @param int $rule_group_id + * @param int $group_id + * + * @return string + */ + public static function getMergeCacheKeyString($rule_group_id, $group_id) { + $contactType = CRM_Dedupe_BAO_RuleGroup::getContactTypeForRuleGroup($rule_group_id); + $cacheKeyString = "merge {$contactType}"; + $cacheKeyString .= $rule_group_id ? "_{$rule_group_id}" : '_0'; + $cacheKeyString .= $group_id ? "_{$group_id}" : '_0'; + return $cacheKeyString; + } + } -- 2.25.1