Merge pull request #14004 from mfb/set-utf8
[civicrm-core.git] / CRM / Dedupe / BAO / QueryBuilder / IndividualUnsupervised.php
CommitLineData
6a488035 1<?php
03e04002 2/*
3 +--------------------------------------------------------------------+
fee14197 4| CiviCRM version 5 |
03e04002 5+--------------------------------------------------------------------+
6b83d5bd 6| Copyright CiviCRM LLC (c) 2004-2019 |
03e04002 7+--------------------------------------------------------------------+
8| This file is a part of CiviCRM. |
9| |
10| CiviCRM is free software; you can copy, modify, and distribute it |
11| under the terms of the GNU Affero General Public License |
12| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13| |
14| CiviCRM is distributed in the hope that it will be useful, but |
15| WITHOUT ANY WARRANTY; without even the implied warranty of |
16| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17| See the GNU Affero General Public License for more details. |
18| |
19| You should have received a copy of the GNU Affero General Public |
20| License and the CiviCRM Licensing Exception along |
21| with this program; if not, contact CiviCRM LLC |
22| at info[AT]civicrm[DOT]org. If you have questions about the |
23| GNU Affero General Public License or the licensing of CiviCRM, |
24| see the CiviCRM license FAQ at http://civicrm.org/licensing |
25+--------------------------------------------------------------------+
e70a7fc0 26 */
4c6ce474
EM
27
28/**
29 * Class CRM_Dedupe_BAO_QueryBuilder_IndividualUnsupervised
30 */
6a488035
TO
31class CRM_Dedupe_BAO_QueryBuilder_IndividualUnsupervised extends CRM_Dedupe_BAO_QueryBuilder {
32
e0ef6999
EM
33 /**
34 * @param $rg
35 *
36 * @return array
37 */
00be9182 38 public static function record($rg) {
be2fb01f 39 $civicrm_email = CRM_Utils_Array::value('civicrm_email', $rg->params, []);
6a488035 40
be2fb01f
CW
41 $params = [
42 1 => [CRM_Utils_Array::value('email', $civicrm_email, ''), 'String'],
43 ];
6a488035 44
be2fb01f 45 return [
6a488035
TO
46 "civicrm_contact.{$rg->name}.{$rg->threshold}" => CRM_Core_DAO::composeQuery("
47 SELECT contact.id as id1, {$rg->threshold} as weight
48 FROM civicrm_contact as contact
49 JOIN civicrm_email as email ON email.contact_id=contact.id
50 WHERE contact_type = 'Individual'
51 AND email = %1", $params, TRUE),
be2fb01f 52 ];
6a488035
TO
53 }
54
e0ef6999
EM
55 /**
56 * @param $rg
57 *
58 * @return array
59 */
00be9182 60 public static function internal($rg) {
6a488035
TO
61 $query = "
62 SELECT contact1.id as id1, contact2.id as id2, {$rg->threshold} as weight
63 FROM civicrm_contact as contact1
64 JOIN civicrm_email as email1 ON email1.contact_id=contact1.id
65 JOIN civicrm_contact as contact2
66 JOIN civicrm_email as email2 ON
67 email2.contact_id=contact2.id AND
68 email1.email=email2.email
69 WHERE contact1.contact_type = 'Individual'
70 AND " . self::internalFilters($rg);
be2fb01f 71 return ["civicrm_contact.{$rg->name}.{$rg->threshold}" => $query];
6a488035
TO
72 }
73
74 /**
75 * An alternative version which might perform a lot better
76 * than the above. Will need to do some testing
ad37ac8e 77 *
78 * @param string $rg
79 *
80 * @return array
6a488035 81 */
00be9182 82 public static function internalOptimized($rg) {
6a488035
TO
83 $sql = "
84CREATE TEMPORARY TABLE emails (
85 email varchar(255),
86 contact_id1 int,
87 contact_id2 int,
88 INDEX(contact_id1),
89 INDEX(contact_id2)
75f1cd78 90 ) ENGINE=InnoDB
6a488035
TO
91";
92 CRM_Core_DAO::executeQuery($sql);
93
94 $sql = "
95INSERT INTO emails
96 SELECT email1.email as email, email1.contact_id as contact_id1, email2.contact_id as contact_id2
97 FROM civicrm_email as email1
98 JOIN civicrm_email as email2 USING (email)
99 WHERE email1.contact_id < email2.contact_id
481a74f4 100 AND " . self::internalFilters($rg, "email1.contact_id", "email2.contact_id");
6a488035
TO
101 CRM_Core_DAO::executeQuery($sql);
102
103 $query = "
104SELECT contact_id1 as id1, contact_id2 as id2, {$rg->threshold} as weight
105FROM emails
106JOIN civicrm_contact as contact1 on contact1.id=contact_id1
107JOIN civicrm_contact as contact2 on contact2.id=contact_id2
03e04002 108WHERE contact1.contact_type='Individual'
6a488035
TO
109AND contact2.contact_type='Individual'
110AND " . self::internalFilters($rg);
111
be2fb01f 112 return ["civicrm_contact.{$rg->name}.{$rg->threshold}" => $query];
6a488035 113 }
96025800 114
353ffa53 115}