Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * The CiviCRM duplicate discovery engine is based on an | |
38 | * algorithm designed by David Strauss <david@fourkitchens.com>. | |
39 | */ | |
40 | class CRM_Dedupe_BAO_RuleGroup extends CRM_Dedupe_DAO_RuleGroup { | |
41 | ||
42 | /** | |
100fef9d | 43 | * Ids of the contacts to limit the SQL queries (whole-database queries otherwise) |
6a488035 TO |
44 | */ |
45 | var $contactIds = array(); | |
46 | ||
47 | /** | |
100fef9d | 48 | * Params to dedupe against (queries against the whole contact set otherwise) |
6a488035 TO |
49 | */ |
50 | var $params = array(); | |
51 | ||
52 | /** | |
100fef9d | 53 | * If there are no rules in rule group |
6a488035 TO |
54 | */ |
55 | var $noRules = FALSE; | |
56 | ||
57 | /** | |
58 | * Return a structure holding the supported tables, fields and their titles | |
59 | * | |
98997235 TO |
60 | * @param string $requestedType |
61 | * The requested contact type. | |
6a488035 TO |
62 | * |
63 | * @return array a table-keyed array of field-keyed arrays holding supported fields' titles | |
64 | */ | |
00be9182 | 65 | public static function &supportedFields($requestedType) { |
6a488035 TO |
66 | static $fields = NULL; |
67 | if (!$fields) { | |
68 | // this is needed, as we're piggy-backing importableFields() below | |
69 | $replacements = array( | |
70 | 'civicrm_country.name' => 'civicrm_address.country_id', | |
71 | 'civicrm_county.name' => 'civicrm_address.county_id', | |
72 | 'civicrm_state_province.name' => 'civicrm_address.state_province_id', | |
73 | 'gender.label' => 'civicrm_contact.gender_id', | |
74 | 'individual_prefix.label' => 'civicrm_contact.prefix_id', | |
75 | 'individual_suffix.label' => 'civicrm_contact.suffix_id', | |
76 | 'addressee.label' => 'civicrm_contact.addressee_id', | |
77 | 'email_greeting.label' => 'civicrm_contact.email_greeting_id', | |
78 | 'postal_greeting.label' => 'civicrm_contact.postal_greeting_id', | |
79 | ); | |
80 | // the table names we support in dedupe rules - a filter for importableFields() | |
81 | $supportedTables = array( | |
82 | 'civicrm_address', 'civicrm_contact', 'civicrm_email', | |
83 | 'civicrm_im', 'civicrm_note', 'civicrm_openid', 'civicrm_phone', | |
84 | ); | |
85 | ||
309a09df | 86 | foreach (array('Individual', 'Organization', 'Household') as $ctype) { |
6a488035 TO |
87 | // take the table.field pairs and their titles from importableFields() if the table is supported |
88 | foreach (CRM_Contact_BAO_Contact::importableFields($ctype) as $iField) { | |
89 | if (isset($iField['where'])) { | |
90 | $where = $iField['where']; | |
91 | if (isset($replacements[$where])) { | |
92 | $where = $replacements[$where]; | |
93 | } | |
94 | list($table, $field) = explode('.', $where); | |
95 | if (!in_array($table, $supportedTables)) { | |
96 | continue; | |
97 | } | |
98 | $fields[$ctype][$table][$field] = $iField['title']; | |
99 | } | |
100 | } | |
101 | // add custom data fields | |
102 | foreach (CRM_Core_BAO_CustomGroup::getTree($ctype, CRM_Core_DAO::$_nullObject, NULL, -1) as $key => $cg) { | |
103 | if (!is_int($key)) { | |
104 | continue; | |
105 | } | |
106 | foreach ($cg['fields'] as $cf) { | |
107 | $fields[$ctype][$cg['table_name']][$cf['column_name']] = $cf['label']; | |
108 | } | |
109 | } | |
110 | } | |
111 | } | |
1cf05c3e | 112 | CRM_Utils_Hook::dupeQuery(CRM_Core_DAO::$_nullObject, 'supportedFields', $fields); |
6a488035 TO |
113 | return $fields[$requestedType]; |
114 | } | |
115 | ||
116 | /** | |
117 | * Return the SQL query for dropping the temporary table. | |
118 | */ | |
00be9182 | 119 | public function tableDropQuery() { |
6a488035 TO |
120 | return 'DROP TEMPORARY TABLE IF EXISTS dedupe'; |
121 | } | |
122 | ||
123 | /** | |
124 | * Return a set of SQL queries whose cummulative weights will mark matched | |
125 | * records for the RuleGroup::threasholdQuery() to retrieve. | |
126 | */ | |
00be9182 | 127 | public function tableQuery() { |
6a488035 TO |
128 | // make sure we've got a fetched dbrecord, not sure if this is enforced |
129 | if (!$this->name == NULL || $this->is_reserved == NULL) { | |
130 | $this->find(TRUE); | |
131 | } | |
132 | ||
133 | // Reserved Rule Groups can optionally get special treatment by | |
134 | // implementing an optimization class and returning a query array. | |
135 | if ($this->is_reserved && | |
136 | CRM_Utils_File::isIncludable("CRM/Dedupe/BAO/QueryBuilder/{$this->name}.php") | |
137 | ) { | |
138 | include_once "CRM/Dedupe/BAO/QueryBuilder/{$this->name}.php"; | |
139 | $class = "CRM_Dedupe_BAO_QueryBuilder_{$this->name}"; | |
140 | $command = empty($this->params) ? 'internal' : 'record'; | |
141 | $queries = call_user_func(array($class, $command), $this); | |
142 | } | |
143 | else { | |
144 | // All other rule groups have queries generated by the member dedupe | |
145 | // rules defined in the administrative interface. | |
146 | ||
147 | // Find all rules contained by this script sorted by weight so that | |
148 | // their execution can be short circuited on RuleGroup::fillTable() | |
149 | $bao = new CRM_Dedupe_BAO_Rule(); | |
150 | $bao->dedupe_rule_group_id = $this->id; | |
151 | $bao->orderBy('rule_weight DESC'); | |
152 | $bao->find(); | |
153 | ||
154 | // Generate a SQL query for each rule in the rule group that is | |
155 | // tailored to respect the param and contactId options provided. | |
156 | $queries = array(); | |
157 | while ($bao->fetch()) { | |
158 | $bao->contactIds = $this->contactIds; | |
159 | $bao->params = $this->params; | |
160 | ||
161 | // Skipping empty rules? Empty rules shouldn't exist; why check? | |
162 | if ($query = $bao->sql()) { | |
163 | $queries["{$bao->rule_table}.{$bao->rule_field}.{$bao->rule_weight}"] = $query; | |
164 | } | |
165 | } | |
166 | } | |
167 | ||
168 | // if there are no rules in this rule group | |
169 | // add an empty query fulfilling the pattern | |
170 | if (!$queries) { | |
171 | $queries = array('SELECT 0 id1, 0 id2, 0 weight LIMIT 0'); | |
172 | $this->noRules = TRUE; | |
173 | } | |
174 | ||
175 | return $queries; | |
176 | } | |
177 | ||
00be9182 | 178 | public function fillTable() { |
6a488035 TO |
179 | // get the list of queries handy |
180 | $tableQueries = $this->tableQuery(); | |
181 | ||
182 | if ($this->params && !$this->noRules) { | |
183 | $tempTableQuery = "CREATE TEMPORARY TABLE dedupe (id1 int, weight int, UNIQUE UI_id1 (id1)) ENGINE=MyISAM"; | |
184 | $insertClause = "INSERT INTO dedupe (id1, weight)"; | |
185 | $groupByClause = "GROUP BY id1"; | |
186 | $dupeCopyJoin = " JOIN dedupe_copy ON dedupe_copy.id1 = t1.column WHERE "; | |
187 | } | |
188 | else { | |
189 | $tempTableQuery = "CREATE TEMPORARY TABLE dedupe (id1 int, id2 int, weight int, UNIQUE UI_id1_id2 (id1, id2)) ENGINE=MyISAM"; | |
190 | $insertClause = "INSERT INTO dedupe (id1, id2, weight)"; | |
191 | $groupByClause = "GROUP BY id1, id2"; | |
192 | $dupeCopyJoin = " JOIN dedupe_copy ON dedupe_copy.id1 = t1.column AND dedupe_copy.id2 = t2.column WHERE "; | |
193 | } | |
194 | $patternColumn = '/t1.(\w+)/'; | |
195 | $exclWeightSum = array(); | |
196 | ||
197 | // create temp table | |
198 | $dao = new CRM_Core_DAO(); | |
199 | $dao->query($tempTableQuery); | |
200 | ||
6a488035 TO |
201 | CRM_Utils_Hook::dupeQuery($this, 'table', $tableQueries); |
202 | ||
203 | while (!empty($tableQueries)) { | |
204 | list($isInclusive, $isDie) = self::isQuerySetInclusive($tableQueries, $this->threshold, $exclWeightSum); | |
205 | ||
206 | if ($isInclusive) { | |
207 | // order queries by table count | |
208 | self::orderByTableCount($tableQueries); | |
209 | ||
210 | $weightSum = array_sum($exclWeightSum); | |
211 | $searchWithinDupes = !empty($exclWeightSum) ? 1 : 0; | |
212 | ||
213 | while (!empty($tableQueries)) { | |
214 | // extract the next query ( and weight ) to be executed | |
215 | $fieldWeight = array_keys($tableQueries); | |
216 | $fieldWeight = $fieldWeight[0]; | |
217 | $query = array_shift($tableQueries); | |
218 | ||
219 | if ($searchWithinDupes) { | |
220 | // get prepared to search within already found dupes if $searchWithinDupes flag is set | |
221 | $dao->query("DROP TEMPORARY TABLE IF EXISTS dedupe_copy"); | |
222 | $dao->query("CREATE TEMPORARY TABLE dedupe_copy SELECT * FROM dedupe WHERE weight >= {$weightSum}"); | |
223 | $dao->free(); | |
224 | ||
225 | preg_match($patternColumn, $query, $matches); | |
226 | $query = str_replace(' WHERE ', str_replace('column', $matches[1], $dupeCopyJoin), $query); | |
227 | } | |
228 | $searchWithinDupes = 1; | |
229 | ||
230 | // construct and execute the intermediate query | |
231 | $query = "{$insertClause} {$query} {$groupByClause} ON DUPLICATE KEY UPDATE weight = weight + VALUES(weight)"; | |
232 | $dao->query($query); | |
233 | ||
234 | // FIXME: we need to be more acurate with affected rows, especially for insert vs duplicate insert. | |
235 | // And that will help optimize further. | |
236 | $affectedRows = $dao->affectedRows(); | |
237 | $dao->free(); | |
238 | ||
239 | // In an inclusive situation, failure of any query means no further processing - | |
240 | if ($affectedRows == 0) { | |
241 | // reset to make sure no further execution is done. | |
242 | $tableQueries = array(); | |
243 | break; | |
244 | } | |
245 | $weightSum = substr($fieldWeight, strrpos($fieldWeight, '.') + 1) + $weightSum; | |
246 | } | |
247 | // An exclusive situation - | |
248 | } | |
249 | elseif (!$isDie) { | |
250 | // since queries are already sorted by weights, we can continue as is | |
251 | $fieldWeight = array_keys($tableQueries); | |
252 | $fieldWeight = $fieldWeight[0]; | |
253 | $query = array_shift($tableQueries); | |
254 | $query = "{$insertClause} {$query} {$groupByClause} ON DUPLICATE KEY UPDATE weight = weight + VALUES(weight)"; | |
255 | $dao->query($query); | |
256 | if ($dao->affectedRows() >= 1) { | |
257 | $exclWeightSum[] = substr($fieldWeight, strrpos($fieldWeight, '.') + 1); | |
258 | } | |
259 | $dao->free(); | |
260 | } | |
261 | else { | |
262 | // its a die situation | |
263 | break; | |
264 | } | |
265 | } | |
266 | } | |
267 | ||
268 | // Function to determine if a given query set contains inclusive or exclusive set of weights. | |
269 | // The function assumes that the query set is already ordered by weight in desc order. | |
e0ef6999 EM |
270 | /** |
271 | * @param $tableQueries | |
272 | * @param $threshold | |
273 | * @param array $exclWeightSum | |
274 | * | |
275 | * @return array | |
276 | */ | |
00be9182 | 277 | public static function isQuerySetInclusive($tableQueries, $threshold, $exclWeightSum = array()) { |
6a488035 TO |
278 | $input = array(); |
279 | foreach ($tableQueries as $key => $query) { | |
280 | $input[] = substr($key, strrpos($key, '.') + 1); | |
281 | } | |
282 | ||
283 | if (!empty($exclWeightSum)) { | |
284 | $input = array_merge($input, $exclWeightSum); | |
285 | rsort($input); | |
286 | } | |
287 | ||
288 | if (count($input) == 1) { | |
289 | return array(FALSE, $input[0] < $threshold); | |
290 | } | |
291 | ||
292 | $totalCombinations = 0; | |
293 | for ($i = 0; $i < count($input); $i++) { | |
294 | $combination = array($input[$i]); | |
295 | if (array_sum($combination) >= $threshold) { | |
296 | $totalCombinations++; | |
297 | continue; | |
298 | } | |
299 | for ($j = $i + 1; $j < count($input); $j++) { | |
300 | $combination[] = $input[$j]; | |
301 | if (array_sum($combination) >= $threshold) { | |
302 | $totalCombinations++; | |
303 | } | |
304 | } | |
305 | } | |
306 | return array($totalCombinations == 1, $totalCombinations <= 0); | |
307 | } | |
308 | ||
309 | // sort queries by number of records for the table associated with them | |
e0ef6999 EM |
310 | /** |
311 | * @param $tableQueries | |
312 | */ | |
00be9182 | 313 | public static function orderByTableCount(&$tableQueries) { |
6a488035 TO |
314 | static $tableCount = array(); |
315 | ||
316 | $tempArray = array(); | |
317 | foreach ($tableQueries as $key => $query) { | |
318 | $table = explode(".", $key); | |
319 | $table = $table[0]; | |
320 | if (!array_key_exists($table, $tableCount)) { | |
321 | $query = "SELECT COUNT(*) FROM {$table}"; | |
322 | $tableCount[$table] = CRM_Core_DAO::singleValueQuery($query); | |
323 | } | |
324 | $tempArray[$key] = $tableCount[$table]; | |
325 | } | |
326 | ||
327 | asort($tempArray); | |
328 | foreach ($tempArray as $key => $count) { | |
329 | $tempArray[$key] = $tableQueries[$key]; | |
330 | } | |
331 | $tableQueries = $tempArray; | |
332 | } | |
333 | ||
334 | /** | |
335 | * Return the SQL query for getting only the interesting results out of the dedupe table. | |
336 | * | |
337 | * @$checkPermission boolean $params a flag to indicate if permission should be considered. | |
338 | * default is to always check permissioning but public pages for example might not want | |
339 | * permission to be checked for anonymous users. Refer CRM-6211. We might be beaking | |
340 | * Multi-Site dedupe for public pages. | |
341 | * | |
342 | */ | |
00be9182 | 343 | public function thresholdQuery($checkPermission = TRUE) { |
6a488035 TO |
344 | $this->_aclFrom = ''; |
345 | // CRM-6603: anonymous dupechecks side-step ACLs | |
346 | $this->_aclWhere = ' AND is_deleted = 0 '; | |
347 | ||
348 | if ($this->params && !$this->noRules) { | |
349 | if ($checkPermission) { | |
350 | list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('civicrm_contact'); | |
351 | $this->_aclWhere = $this->_aclWhere ? "AND {$this->_aclWhere}" : ''; | |
352 | } | |
353 | $query = "SELECT dedupe.id1 as id | |
354 | FROM dedupe JOIN civicrm_contact ON dedupe.id1 = civicrm_contact.id {$this->_aclFrom} | |
355 | WHERE contact_type = '{$this->contact_type}' {$this->_aclWhere} | |
356 | AND weight >= {$this->threshold}"; | |
357 | } | |
358 | else { | |
359 | $this->_aclWhere = ' AND c1.is_deleted = 0 AND c2.is_deleted = 0'; | |
360 | if ($checkPermission) { | |
361 | list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause(array('c1', 'c2')); | |
362 | $this->_aclWhere = $this->_aclWhere ? "AND {$this->_aclWhere}" : ''; | |
363 | } | |
364 | $query = "SELECT dedupe.id1, dedupe.id2, dedupe.weight | |
d4c8a770 | 365 | FROM dedupe JOIN civicrm_contact c1 ON dedupe.id1 = c1.id |
6a488035 TO |
366 | JOIN civicrm_contact c2 ON dedupe.id2 = c2.id {$this->_aclFrom} |
367 | LEFT JOIN civicrm_dedupe_exception exc ON dedupe.id1 = exc.contact_id1 AND dedupe.id2 = exc.contact_id2 | |
d4c8a770 | 368 | WHERE c1.contact_type = '{$this->contact_type}' AND |
6a488035 TO |
369 | c2.contact_type = '{$this->contact_type}' {$this->_aclWhere} |
370 | AND weight >= {$this->threshold} AND exc.contact_id1 IS NULL"; | |
371 | } | |
372 | ||
373 | CRM_Utils_Hook::dupeQuery($this, 'threshold', $query); | |
374 | return $query; | |
375 | } | |
376 | ||
377 | /** | |
378 | * To find fields related to a rule group. | |
379 | * | |
c490a46a | 380 | * @param array $params |
77b97be7 EM |
381 | * |
382 | * @return array (rule field => weight) array and threshold associated to rule group@access public | |
6a488035 | 383 | */ |
00be9182 | 384 | public static function dedupeRuleFieldsWeight($params) { |
6a488035 | 385 | $rgBao = new CRM_Dedupe_BAO_RuleGroup(); |
6a488035 | 386 | $rgBao->contact_type = $params['contact_type']; |
03390e26 | 387 | if (CRM_Utils_Array::value('id', $params)) { |
388 | // accept an ID if provided | |
389 | $rgBao->id = $params['id']; | |
390 | } | |
391 | else { | |
392 | $rgBao->used = $params['used']; | |
393 | } | |
6a488035 TO |
394 | $rgBao->find(TRUE); |
395 | ||
396 | $ruleBao = new CRM_Dedupe_BAO_Rule(); | |
397 | $ruleBao->dedupe_rule_group_id = $rgBao->id; | |
398 | $ruleBao->find(); | |
399 | $ruleFields = array(); | |
400 | while ($ruleBao->fetch()) { | |
401 | $ruleFields[$ruleBao->rule_field] = $ruleBao->rule_weight; | |
402 | } | |
403 | ||
404 | return array($ruleFields, $rgBao->threshold); | |
405 | } | |
406 | ||
03390e26 | 407 | /** |
408 | * Get all of the combinations of fields that would work with a rule | |
409 | */ | |
410 | ||
00be9182 | 411 | public static function combos($rgFields, $threshold, &$combos, $running = array()) { |
03390e26 | 412 | foreach ($rgFields as $rgField => $weight) { |
413 | unset($rgFields[$rgField]); | |
414 | $diff = $threshold - $weight; | |
415 | $runningnow = $running; | |
416 | $runningnow[] = $rgField; | |
417 | if ($diff > 0) { | |
418 | self::combos($rgFields, $diff, $combos, $runningnow); | |
419 | } | |
420 | else { | |
421 | $combos[] = $runningnow; | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
6a488035 TO |
426 | /** |
427 | * Get an array of rule group id to rule group name | |
428 | * for all th groups for that contactType. If contactType | |
429 | * not specified, do it for all | |
430 | * | |
98997235 TO |
431 | * @param string $contactType |
432 | * Individual, Household or Organization. | |
6a488035 TO |
433 | * |
434 | * @static | |
435 | * | |
436 | * @return array id => "nice name" of rule group | |
437 | */ | |
00be9182 | 438 | public static function getByType($contactType = NULL) { |
6a488035 TO |
439 | $dao = new CRM_Dedupe_DAO_RuleGroup(); |
440 | ||
441 | if ($contactType) { | |
442 | $dao->contact_type = $contactType; | |
443 | } | |
444 | ||
445 | $dao->find(); | |
446 | $result = array(); | |
447 | while ($dao->fetch()) { | |
01a93ecd | 448 | $title = |
d58a19a1 | 449 | !empty($dao->title) ? $dao->title : (!empty($dao->name) ? $dao->name : $dao->contact_type); |
01a93ecd DL |
450 | |
451 | $name = "$title - {$dao->used}"; | |
6a488035 TO |
452 | $result[$dao->id] = $name; |
453 | } | |
454 | return $result; | |
455 | } | |
456 | } |