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
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 */
27
28 /**
29 * Class CRM_Dedupe_BAO_QueryBuilder_IndividualUnsupervised
30 */
31 class CRM_Dedupe_BAO_QueryBuilder_IndividualUnsupervised extends CRM_Dedupe_BAO_QueryBuilder {
32
33 /**
34 * @param $rg
35 *
36 * @return array
37 */
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
55 /**
56 * @param $rg
57 *
58 * @return array
59 */
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 = "
80 CREATE 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 = "
91 INSERT 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 = "
100 SELECT contact_id1 as id1, contact_id2 as id2, {$rg->threshold} as weight
101 FROM emails
102 JOIN civicrm_contact as contact1 on contact1.id=contact_id1
103 JOIN civicrm_contact as contact2 on contact2.id=contact_id2
104 WHERE contact1.contact_type='Individual'
105 AND contact2.contact_type='Individual'
106 AND " . self::internalFilters($rg);
107
108 return array("civicrm_contact.{$rg->name}.{$rg->threshold}" => $query);
109 }
110 };
111
112
113