Skip to content

Commit

Permalink
Take static copy of the query finder for the legacy code function
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Feb 12, 2025
1 parent e9f6a4b commit b9fcc39
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 58 deletions.
14 changes: 9 additions & 5 deletions CRM/Dedupe/BAO/DedupeRuleGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,20 @@ public static function hook_civicrm_findExistingDuplicates(GenericHookEvent $eve
if (empty($tableQueries)) {
return;
}
$threshold = $ruleGroup->threshold;

$tempTable = $ruleGroup->runTablesQuery([], $tableQueries, $threshold);
$ruleGroup = DedupeRuleGroup::get(FALSE)
->addWhere('id', '=', $ruleGroup->id)
->execute()
->first();
$self = new self();
$tempTable = $self->runTablesQuery([], $tableQueries, $ruleGroup['threshold']);
if (!$tempTable) {
return;
}
$aclFrom = $aclWhere = '';
$dedupeTable = $tempTable;
$contactType = $ruleGroup->contact_type;
$threshold = $ruleGroup->threshold;
$contactType = $ruleGroup['contact_type'];
$threshold = $ruleGroup['threshold'];

if ($event->checkPermissions) {
[$aclFrom, $aclWhere] = CRM_Contact_BAO_Contact_Permission::cacheClause(['c1', 'c2']);
Expand All @@ -192,7 +196,7 @@ public static function hook_civicrm_findExistingDuplicates(GenericHookEvent $eve
$duplicates[] = ['entity_id_1' => $dao->id1, 'entity_id_2' => $dao->id2, 'weight' => $dao->weight];
}
$event->duplicates = $duplicates;
\CRM_Core_DAO::executeQuery($ruleGroup->tableDropQuery());
\CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS ' . $dedupeTable);
}

/**
Expand Down
48 changes: 0 additions & 48 deletions CRM/Dedupe/FinderQueryOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,6 @@ public function __construct(int $dedupeRuleGroupID, array $contactIDs, array $pa
}
}

/**
* Is a file based reserved query configured.
*
* File based reserved queries were an early idea about how to optimise the dedupe queries.
*
* In theory extensions could implement them although there is no evidence any of them have.
* However, if these are implemented by core or by extensions we should not attempt to optimise
* the query by (e.g.) combining queries.
*
* In practice the queries implemented only return one query anyway
*
* @internal for core use only.
*
* @return bool
* @throws \CRM_Core_Exception
*
* @see \CRM_Dedupe_BAO_QueryBuilder_IndividualGeneral
* @see \CRM_Dedupe_BAO_QueryBuilder_IndividualSupervised
*/
public function isUseReservedQuery(): bool {
return $this->lookup('RuleGroup', 'is_reserved') &&
CRM_Utils_File::isIncludable('CRM/Dedupe/BAO/QueryBuilder/' . $this->lookup('RuleGroup', 'name') . '.php');
}

/**
* Return the SQL query for the given rule - either for finding matching
* pairs of contacts, or for matching against the $params variable (if set).
Expand Down Expand Up @@ -228,30 +204,6 @@ private function getContactIDFieldName(string $tableName): string {
throw new CRM_Core_Exception('invalid field');
}

/**
* Get the reserved query based on a static class.
*
* This was an early idea about optimisation & extendability. It is likely
* there are no implementations of rules this way outside the 3 core files.
*
* It is also likely the core files can go once we are optimising the queries based on the
* rule.
*
* @internal Do not call from outside of core.
*
* @return array
* @throws \CRM_Core_Exception
*/
public function getReservedQuery(): array {
$bao = new CRM_Dedupe_BAO_DedupeRuleGroup();
$bao->id = $this->lookup('RuleGroup', 'id');
$bao->find(TRUE);
$bao->params = $this->lookupParameters;
$bao->contactIds = $this->contactIDs;
$command = empty($this->lookupParameters) ? 'internal' : 'record';
return call_user_func(["CRM_Dedupe_BAO_QueryBuilder_" . $this->lookup('RuleGroup', 'name'), $command], $bao);
}

/**
* Get the queries to fill the table for the various rules.
*
Expand Down
11 changes: 6 additions & 5 deletions ext/legacydedupefinder/Civi/LegacyFinder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Civi\LegacyFinder;

use Civ\LegacyFinder\FinderQueryOptimizer;
use Civi\Core\Event\GenericHookEvent;
use Civi\Core\Service\AutoSubscriber;

Expand Down Expand Up @@ -29,7 +30,7 @@ public static function findExistingDuplicates(GenericHookEvent $event): void {
$contactIDs = explode(',', \CRM_Core_DAO::singleValueQuery('SELECT GROUP_CONCAT(id) FROM ' . $event->tableName));
}
$ruleGroup->contactIds = $contactIDs;
// make sure we've got a fetched dbrecord, not sure if this is enforced
// make sure we've got a fetched db record, not sure if this is enforced
$ruleGroup->find(TRUE);
$tempTable = self::fillTable($ruleGroup, $ruleGroup->id, $contactIDs, []);
if (!$tempTable) {
Expand Down Expand Up @@ -116,21 +117,21 @@ public static function findDuplicates(GenericHookEvent $event): void {
$event->dedupeResults['ids'] = array_diff($dupes, $event->dedupeParams['excluded_contact_ids']);
}


/**
* Fill the dedupe finder table.
*
* @internal do not access from outside core.
* @internal do not access from outside core
*
* @param \CRM_Dedupe_BAO_DedupeRuleGroup $ruleGroup
* @param int $id
* @param array $contactIDs
* @param array $params
*
* @return false|string
* @throws \Civi\Core\Exception\DBQueryException
* @throws \CRM_Core_Exception
*/
private static function fillTable($ruleGroup, int $id, array $contactIDs, array $params) {
$optimizer = new \CRM_Dedupe_FinderQueryOptimizer($id, $contactIDs, $params);
$optimizer = new FinderQueryOptimizer($id, $contactIDs, $params);
// Reserved Rule Groups can optionally get special treatment by
// implementing an optimization class and returning a query array.
if ($optimizer->isUseReservedQuery()) {
Expand Down
Loading

0 comments on commit b9fcc39

Please sign in to comment.