* @return array|bool
*/
public static function batchMerge($rgid, $gid = NULL, $mode = 'safe', $autoFlip = TRUE, $batchLimit = 1, $isSelected = 2) {
- $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid);
- $join = CRM_Dedupe_Merger::getJoinOnDedupeTable();
-
- $where = "de.id IS NULL";
- if ($isSelected === 0 || $isSelected === 1) {
- $where .= " AND pn.is_selected = {$isSelected}";
- }// else consider all dupe pairs
- $where .= " LIMIT {$batchLimit}";
-
$redirectForPerformance = ($batchLimit > 1) ? TRUE : FALSE;
-
- $dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where);
- if (empty($dupePairs) && !$redirectForPerformance && $isSelected == 2) {
- // If we haven't found any dupes, probably cache is empty.
- // Try filling cache and give another try.
- CRM_Core_BAO_PrevNextCache::refillCache($rgid, $gid, $cacheKeyString);
- $dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where);
- }
+ $reloadCacheIfEmpty = (!$redirectForPerformance && $isSelected == 2);
+ $dupePairs = self::getDuplicatePairs($rgid, $gid, $reloadCacheIfEmpty, $batchLimit, $isSelected);
$cacheParams = array(
- 'cache_key_string' => $cacheKeyString,
- 'join' => $join,
- 'where' => $where,
+ 'cache_key_string' => self::getMergeCacheKeyString($rgid, $gid),
+ // @todo stop passing these parameters in & instead calculate them in the merge function based
+ // on the 'real' params like $isRespectExclusions $batchLimit and $isSelected.
+ 'join' => self::getJoinOnDedupeTable(),
+ 'where' => self::getWhereString($batchLimit, $isSelected),
);
return CRM_Dedupe_Merger::merge($dupePairs, $cacheParams, $mode, $autoFlip, $redirectForPerformance);
}
";
}
+ /**
+ * Get where string for dedupe join.
+ *
+ * @param int $batchLimit
+ * @param bool $isSelected
+ *
+ * @return string
+ */
+ protected static function getWhereString($batchLimit, $isSelected) {
+ $where = "de.id IS NULL";
+ if ($isSelected === 0 || $isSelected === 1) {
+ $where .= " AND pn.is_selected = {$isSelected}";
+ }
+ // else consider all dupe pairs
+ // @todo Adding limit to Where??!!
+ $where .= " LIMIT {$batchLimit}";
+ return $where;
+ }
+
public static function updateMergeStats($cacheKeyString, $result = array()) {
// gather latest stats
$merged = count($result['merged']);
if ($cacheKeyString && !$redirectForPerformance) {
// retrieve next pair of dupes
+ // @todo call getDuplicatePairs.
$dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString,
$cacheParams['join'],
$cacheParams['where']
}
}
+ /**
+ * Get Duplicate Pairs based on a rule for a group.
+ *
+ * @param int $rule_group_id
+ * @param int $group_id
+ * @param bool $reloadCacheIfEmpty
+ * @param int $batchLimit
+ * @param bool $isSelected
+ *
+ * @return array
+ * Array of matches meeting the criteria.
+ */
+ public static function getDuplicatePairs($rule_group_id, $group_id, $reloadCacheIfEmpty, $batchLimit, $isSelected) {
+ $where = self::getWhereString($batchLimit, $isSelected);
+ $cacheKeyString = self::getMergeCacheKeyString($rule_group_id, $group_id);
+ $join = self::getJoinOnDedupeTable();
+ $dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where);
+ if (empty($dupePairs) && $reloadCacheIfEmpty) {
+ // If we haven't found any dupes, probably cache is empty.
+ // Try filling cache and give another try.
+ CRM_Core_BAO_PrevNextCache::refillCache($rule_group_id, $group_id, $cacheKeyString);
+ $dupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where);
+ return $dupePairs;
+ }
+ return $dupePairs;
+ }
+
/**
* Get the cache key string for the merge action.
*
);
}
+ /**
+ * Test function that gets duplicate pairs.
+ *
+ * It turns out there are 2 code paths retrieving this data so my initial focus is on ensuring
+ * they match.
+ */
+ public function testGetMatches() {
+ $this->setupMatchData();
+ $pairs = CRM_Dedupe_Merger::getDuplicatePairs(
+ 1,
+ NULL,
+ TRUE,
+ 25,
+ FALSE
+ );
+
+ $this->assertEquals(array(
+ 0 => array(
+ 'srcID' => $this->contacts[0]['id'],
+ 'srcName' => 'Mr. Mickey Mouse II',
+ 'dstID' => $this->contacts[1]['id'],
+ 'dstName' => 'Mr. Mickey Mouse II',
+ 'weight' => 20,
+ 'canMerge' => TRUE,
+ ),
+ ), $pairs);
+ }
+
+ public function setupMatchData() {
+ $fixtures = array(
+ array(
+ 'first_name' => 'Mickey',
+ 'last_name' => 'Mouse',
+ 'email' => 'mickey@mouse.com',
+ ),
+ array(
+ 'first_name' => 'Mickey',
+ 'last_name' => 'Mouse',
+ 'email' => 'mickey@mouse.com',
+ ),
+ array(
+ 'first_name' => 'Minnie',
+ 'last_name' => 'Mouse',
+ 'email' => 'mickey@mouse.com',
+ ),
+ );
+ foreach ($fixtures as $fixture) {
+ $contactID = $this->individualCreate($fixture);
+ $this->contacts[] = array_merge($fixture, array('id' => $contactID));
+ }
+ }
+
+
/**
* Get the list of tables that refer to the CID.
*