Commit | Line | Data |
---|---|---|
9287a0b7 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 5 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2020 | | |
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 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2020 | |
32 | * | |
33 | * This class exists primarily for the purposes of supporting code clean up in the Merger class. | |
34 | * | |
35 | * It is expected to be fast-moving and calling it outside the refactoring work is not advised. | |
36 | */ | |
37 | class CRM_Dedupe_MergeHandler { | |
38 | ||
39 | /** | |
40 | * ID of contact to be kept. | |
41 | * | |
42 | * @var int | |
43 | */ | |
44 | protected $toKeepID; | |
45 | ||
46 | /** | |
47 | * ID of contact to be merged and deleted. | |
48 | * | |
49 | * @var int | |
50 | */ | |
51 | protected $toRemoveID; | |
52 | ||
53 | /** | |
54 | * @return mixed | |
55 | */ | |
56 | public function getToKeepID() { | |
57 | return $this->toKeepID; | |
58 | } | |
59 | ||
60 | /** | |
61 | * @param mixed $toKeepID | |
62 | */ | |
63 | public function setToKeepID($toKeepID) { | |
64 | $this->toKeepID = $toKeepID; | |
65 | } | |
66 | ||
67 | /** | |
68 | * @return mixed | |
69 | */ | |
70 | public function getToRemoveID() { | |
71 | return $this->toRemoveID; | |
72 | } | |
73 | ||
74 | /** | |
75 | * @param mixed $toRemoveID | |
76 | */ | |
77 | public function setToRemoveID($toRemoveID) { | |
78 | $this->toRemoveID = $toRemoveID; | |
79 | } | |
80 | ||
81 | /** | |
82 | * CRM_Dedupe_MergeHandler constructor. | |
83 | * | |
84 | * @param int $toKeepID | |
85 | * ID of contact to be kept. | |
86 | * @param int $toRemoveID | |
87 | * ID of contact to be removed. | |
88 | */ | |
89 | public function __construct(int $toKeepID, int $toRemoveID) { | |
90 | $this->setToKeepID($toKeepID); | |
91 | $this->setToRemoveID($toRemoveID); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Get an array of tables that relate to the contact entity and will need consideration in a merge. | |
96 | * | |
97 | * The list of potential tables is filtered by tables which have data for the relevant contacts. | |
98 | */ | |
99 | public function getTablesRelatedToTheMergePair() { | |
100 | $relTables = CRM_Dedupe_Merger::relTables(); | |
101 | $activeRelTables = CRM_Dedupe_Merger::getActiveRelTables($this->toRemoveID); | |
102 | $activeMainRelTables = CRM_Dedupe_Merger::getActiveRelTables($this->toKeepID); | |
103 | foreach ($relTables as $name => $null) { | |
104 | if (!in_array($name, $activeRelTables, TRUE) && | |
105 | !(($name === 'rel_table_users') && in_array($name, $activeMainRelTables, TRUE)) | |
106 | ) { | |
107 | unset($relTables[$name]); | |
108 | } | |
109 | } | |
110 | return $relTables; | |
111 | } | |
112 | ||
eca28463 | 113 | /** |
114 | * Get an array of tables that have a dynamic reference to the contact table. | |
115 | * | |
116 | * This would be the case when the table uses entity_table + entity_id rather than an FK. | |
117 | * | |
118 | * There are a number of tables that 'could' but don't have contact related data so we | |
119 | * do a cached check for this, ensuring the query is only done once per batch run. | |
120 | * | |
121 | * @return array | |
122 | */ | |
123 | public function getTablesDynamicallyRelatedToContactTable() { | |
124 | if (!isset(\Civi::$statics[__CLASS__]['dynamic'])) { | |
125 | \Civi::$statics[__CLASS__]['dynamic'] = []; | |
9811efd4 | 126 | foreach (CRM_Core_DAO::getDynamicReferencesToTable('civicrm_contact') as $tableName => $fields) { |
d035857f | 127 | if ($tableName === 'civicrm_financial_item') { |
128 | // It turns out that civicrm_financial_item does not have an index on entity_table (only as the latter | |
129 | // part of a entity_id/entity_table index which probably is not adding any value over & above entity_id | |
130 | // only. This means this is a slow query. The correct fix is probably to add a whitelist to | |
131 | // values for entity_table in the schema. | |
132 | continue; | |
133 | } | |
9811efd4 JG |
134 | foreach ($fields as $field) { |
135 | $sql[] = "(SELECT '$tableName' as civicrm_table, '{$field[0]}' as field_name FROM $tableName WHERE {$field[1]} = 'civicrm_contact' LIMIT 1)"; | |
136 | } | |
eca28463 | 137 | } |
138 | $sqlString = implode(' UNION ', $sql); | |
139 | if ($sqlString) { | |
140 | $result = CRM_Core_DAO::executeQuery($sqlString); | |
141 | while ($result->fetch()) { | |
142 | \Civi::$statics[__CLASS__]['dynamic'][$result->civicrm_table] = $result->field_name; | |
143 | } | |
144 | } | |
145 | } | |
146 | return \Civi::$statics[__CLASS__]['dynamic']; | |
147 | } | |
148 | ||
9287a0b7 | 149 | } |