3 # SPDX-FileCopyrightText: 2023 Andrew Engelbrecht
5 # SPDX-License-Identifier: GPL-3.0-or-later
8 from datetime
import date
12 static_group_id
= "1983"
15 condition_email
= "e.email = x.email"
16 condition_names
= "c.first_name = x.first_name and c.last_name = x.last_name"
19 sys
.stderr
.write("correct usage: find-mailing-duplicates.py [-n]")
24 elif len(sys
.argv
) == 2 and sys
.argv
[1] == '-n':
25 condition
= condition_names
26 elif len(sys
.argv
) == 2:
30 condition
= condition_email
33 sys
.stderr
.write('\nstarting new run... %s\n\n' % date
.today())
35 mydb
= mysql
.connector
.connect(
42 mycursor0
= mydb
.cursor(buffered
=True)
43 mycursor1
= mydb
.cursor(buffered
=True)
44 mycursor2
= mydb
.cursor(buffered
=True)
46 "search for pairs of contacts with matching criteria (name or email), of which each has at least one address."
51 from civicrm_contact c
52 inner join civicrm_email e on c.id = e.contact_id
53 inner join civicrm_address a on c.id = a.contact_id
55 select i.id, k.street_address, i.is_deleted, j.email, i.first_name, i.last_name
56 from civicrm_contact i
57 inner join civicrm_email j on i.id = j.contact_id
58 inner join civicrm_address k on i.id = k.contact_id
60 having count(i.id) > 0
62 where c.id != x.id and c.id > x.id and a.street_address is not NULL and x.street_address is not NULL and c.is_deleted != 1 and x.is_deleted != 1
64 having count(c.id) > 0;
72 mycursor1
.execute("select count(*) from civicrm_dedupe_exception where (contact_id1 = %s and contact_id2 = %s) or (contact_id1 = %s and contact_id2 = %s);", (id1
, id2
, id2
, id1
))
74 count1
= mycursor1
.fetchone()[0]
76 mycursor2
.execute(f
"select count(*) from civicrm_group_contact where (contact_id = {id1} or contact_id = {id2}) and group_id = {static_group_id};")
78 count2
= mycursor2
.fetchone()[0]
80 print(f
'https://crm.fsf.org/civicrm/contact/merge?reset=1&cid={id1}&oid={id2}')