From b068bfde8ebf928286dac60594ef9c5fbeed4a93 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 2 Sep 2019 10:46:36 +1200 Subject: [PATCH] [Ref] Rationalise dedupe code loop. We have a function 'dedupePair' which is intended to act on a specific pair and a loop function which iterates them. The 'pair actions' and the 'looping actions' are currently jumbled together. This moves the 'pair actions' to the dedupePair function and keeps the 'looping actions' in the parent looping function. Athough I left it out of scope for this PR the api that calls this function should only call the 'dedupePair' function not the multiple pair wrapper --- CRM/Dedupe/Merger.php | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 8de09096c9ac..efd6a5d0e062 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -866,6 +866,10 @@ public static function getMergeStatsMsg($stats) { * Respect logged in user permissions. * * @return array|bool + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public static function merge($dupePairs = [], $cacheParams = [], $mode = 'safe', $redirectForPerformance = FALSE, $checkPermissions = TRUE @@ -883,16 +887,17 @@ public static function merge($dupePairs = [], $cacheParams = [], $mode = 'safe', unset($dupePairs[$index]); continue; } - CRM_Utils_Hook::merge('flip', $dupes, $dupes['dstID'], $dupes['srcID']); - $mainId = $dupes['dstID']; - $otherId = $dupes['srcID']; - - if (!$mainId || !$otherId) { - // return error - return FALSE; + if (($result = self::dedupePair($dupes, $mode, $checkPermissions, $cacheKeyString)) === FALSE) { + unset($dupePairs[$index]); + continue; + } + if (!empty($result['merged'])) { + $deletedContacts[] = $result['merged'][0]['other_id']; + $resultStats['merged'][] = ($result['merged'][0]); + } + else { + $resultStats['skipped'][] = ($result['skipped'][0]); } - - self::dedupePair($resultStats, $deletedContacts, $mode, $checkPermissions, $mainId, $otherId, $cacheKeyString); } if ($cacheKeyString && !$redirectForPerformance) { @@ -2111,20 +2116,26 @@ public static function mergeLocations($mainId, $otherId, $migrationInfo) { /** * Dedupe a pair of contacts. * - * @param array $resultStats - * @param array $deletedContacts + * @param array $dupes * @param string $mode * @param bool $checkPermissions - * @param int $mainId - * @param int $otherId * @param string $cacheKeyString * + * @return bool|array * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception * @throws \API_Exception */ - protected static function dedupePair(&$resultStats, &$deletedContacts, $mode, $checkPermissions, $mainId, $otherId, $cacheKeyString) { - + protected static function dedupePair($dupes, $mode = 'safe', $checkPermissions = TRUE, $cacheKeyString = NULL) { + CRM_Utils_Hook::merge('flip', $dupes, $dupes['dstID'], $dupes['srcID']); + $mainId = $dupes['dstID']; + $otherId = $dupes['srcID']; + $resultStats = []; + + if (!$mainId || !$otherId) { + // return error + return FALSE; + } $migrationInfo = []; $conflicts = []; if (!CRM_Dedupe_Merger::skipMerge($mainId, $otherId, $migrationInfo, $mode, $conflicts)) { @@ -2133,7 +2144,6 @@ protected static function dedupePair(&$resultStats, &$deletedContacts, $mode, $c 'main_id' => $mainId, 'other_id' => $otherId, ]; - $deletedContacts[] = $otherId; } else { $resultStats['skipped'][] = [ @@ -2149,6 +2159,7 @@ protected static function dedupePair(&$resultStats, &$deletedContacts, $mode, $c else { CRM_Core_BAO_PrevNextCache::deletePair($mainId, $otherId, $cacheKeyString); } + return $resultStats; } /**