Eileen has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/373176 )

Change subject: CRM-20658: Fatal error on Dedupe rule for > 1 match
......................................................................

CRM-20658: Fatal error on Dedupe rule for > 1 match

Bug: T160571
Change-Id: Ib735f997952c9edf5393464255da3e228e837ce7
---
M CRM/Dedupe/BAO/RuleGroup.php
1 file changed, 18 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm/civicrm 
refs/changes/76/373176/1

diff --git a/CRM/Dedupe/BAO/RuleGroup.php b/CRM/Dedupe/BAO/RuleGroup.php
index 5e4096a..87861b9 100644
--- a/CRM/Dedupe/BAO/RuleGroup.php
+++ b/CRM/Dedupe/BAO/RuleGroup.php
@@ -229,20 +229,35 @@
           $fieldWeight = array_keys($tableQueries);
           $fieldWeight = $fieldWeight[0];
           $query = array_shift($tableQueries);
+          $dupeWeightClause = 'ON DUPLICATE KEY UPDATE weight = weight + 
VALUES(weight)';
 
           if ($searchWithinDupes) {
             // get prepared to search within already found dupes if 
$searchWithinDupes flag is set
             $dao->query("DROP TEMPORARY TABLE IF EXISTS dedupe_copy");
             $dao->query("CREATE TEMPORARY TABLE dedupe_copy SELECT * FROM 
dedupe WHERE weight >= {$weightSum}");
-            $dao->free();
 
             preg_match($patternColumn, $query, $matches);
-            $query = str_replace(' WHERE ', str_replace('column', $matches[1], 
$dupeCopyJoin), $query);
+            $count = substr_count($query, ' WHERE ');
+            if ($count == 2 && strpos($query, 'UNION') !== FALSE) {
+              //Create second copy as single temp table cannot be referred 
twice in a single query.
+              $dao->query("CREATE TEMPORARY TABLE dedupe_copy_2 SELECT * FROM 
dedupe WHERE weight >= {$weightSum}");
+              $dupeCopyJoins = array($dupeCopyJoin, str_replace('dedupe_copy', 
'dedupe_copy_2', $dupeCopyJoin));
+              $queryArray = explode('UNION', $query);
+              foreach ($queryArray as $key => $value) {
+                $queryArray[$key] = str_replace(' WHERE ', 
str_replace('column', $matches[1], $dupeCopyJoins[$key]), $queryArray[$key]);
+              }
+              $query = implode(' UNION ', $queryArray);
+              $dupeWeightClause = str_replace('weight', 'dedupe.weight', 
$dupeWeightClause);
+            }
+            else {
+              $query = str_replace(' WHERE ', str_replace('column', 
$matches[1], $dupeCopyJoin), $query);
+            }
+            $dao->free();
           }
           $searchWithinDupes = 1;
 
           // construct and execute the intermediate query
-          $query = "{$insertClause} {$query} {$groupByClause} ON DUPLICATE KEY 
UPDATE weight = weight + VALUES(weight)";
+          $query = "{$insertClause} {$query} {$groupByClause} 
{$dupeWeightClause}";
           $dao->query($query);
 
           // FIXME: we need to be more acurate with affected rows, especially 
for insert vs duplicate insert.

-- 
To view, visit https://gerrit.wikimedia.org/r/373176
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib735f997952c9edf5393464255da3e228e837ce7
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm/civicrm
Gerrit-Branch: master
Gerrit-Owner: Eileen <emcnaugh...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to