Merge remote-tracking branch 'upstream/4.4' into 4.4-4.5-2014-10-14-11-16-10
[civicrm-core.git] / CRM / Dedupe / BAO / QueryBuilder / IndividualUnsupervised.php
CommitLineData
6a488035 1<?php
03e04002 2/*
3 +--------------------------------------------------------------------+
06b69b18 4| CiviCRM version 4.5 |
03e04002 5+--------------------------------------------------------------------+
06b69b18 6| Copyright CiviCRM LLC (c) 2004-2014 |
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+--------------------------------------------------------------------+
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 */
6a488035
TO
38 static function record($rg) {
39 $civicrm_email = CRM_Utils_Array::value('civicrm_email', $rg->params, array());
40
41 $params = array(
42 1 => array(CRM_Utils_Array::value('email', $civicrm_email, ''), 'String'),
43 );
44
45 return array(
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),
52 );
53 }
54
e0ef6999
EM
55 /**
56 * @param $rg
57 *
58 * @return array
59 */
6a488035
TO
60 static function internal($rg) {
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);
71 return array("civicrm_contact.{$rg->name}.{$rg->threshold}" => $query);
72 }
73
74 /**
75 * An alternative version which might perform a lot better
76 * than the above. Will need to do some testing
77 */
78 static function internalOptimized($rg) {
79 $sql = "
80CREATE TEMPORARY TABLE emails (
81 email varchar(255),
82 contact_id1 int,
83 contact_id2 int,
84 INDEX(contact_id1),
85 INDEX(contact_id2)
86 ) ENGINE=MyISAM
87";
88 CRM_Core_DAO::executeQuery($sql);
89
90 $sql = "
91INSERT INTO emails
92 SELECT email1.email as email, email1.contact_id as contact_id1, email2.contact_id as contact_id2
93 FROM civicrm_email as email1
94 JOIN civicrm_email as email2 USING (email)
95 WHERE email1.contact_id < email2.contact_id
96 AND " . self::internalFilters($rg, "email1.contact_id", "email2.contact_id" );
97 CRM_Core_DAO::executeQuery($sql);
98
99 $query = "
100SELECT contact_id1 as id1, contact_id2 as id2, {$rg->threshold} as weight
101FROM emails
102JOIN civicrm_contact as contact1 on contact1.id=contact_id1
103JOIN civicrm_contact as contact2 on contact2.id=contact_id2
03e04002 104WHERE contact1.contact_type='Individual'
6a488035
TO
105AND contact2.contact_type='Individual'
106AND " . self::internalFilters($rg);
107
108 return array("civicrm_contact.{$rg->name}.{$rg->threshold}" => $query);
109 }
110};
111
112
113