Merge pull request #17980 from eileenmcnaughton/dupe
[civicrm-core.git] / CRM / Core / BAO / PrevNextCache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * BAO object for civicrm_prevnext_cache table.
20 */
21 class CRM_Core_BAO_PrevNextCache extends CRM_Core_DAO_PrevNextCache {
22
23 /**
24 * Get the previous and next keys.
25 *
26 * @param string $cacheKey
27 * @param int $id1
28 * @param int $id2
29 * @param int $mergeId
30 * @param string $join
31 * @param string $where
32 * @param bool $flip
33 *
34 * @return array
35 */
36 public static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) {
37 if ($flip) {
38 list($id1, $id2) = [$id2, $id1];
39 }
40
41 if ($mergeId == NULL) {
42 $query = "
43 SELECT id
44 FROM civicrm_prevnext_cache
45 WHERE cachekey = %3 AND
46 entity_id1 = %1 AND
47 entity_id2 = %2 AND
48 entity_table = 'civicrm_contact'
49 ";
50
51 $params = [
52 1 => [$id1, 'Integer'],
53 2 => [$id2, 'Integer'],
54 3 => [$cacheKey, 'String'],
55 ];
56
57 $mergeId = CRM_Core_DAO::singleValueQuery($query, $params);
58 }
59
60 $pos = ['foundEntry' => 0];
61 if ($mergeId) {
62 $pos['foundEntry'] = 1;
63
64 if ($where) {
65
66 $where = " AND {$where}";
67
68 }
69 $p = [
70 1 => [$mergeId, 'Integer'],
71 2 => [$cacheKey, 'String'],
72 ];
73 $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn {$join} ";
74 $wherePrev = " WHERE pn.id < %1 AND pn.cachekey = %2 {$where} ORDER BY ID DESC LIMIT 1";
75 $sqlPrev = $sql . $wherePrev;
76
77 $dao = CRM_Core_DAO::executeQuery($sqlPrev, $p);
78 if ($dao->fetch()) {
79 $pos['prev']['id1'] = $dao->entity_id1;
80 $pos['prev']['id2'] = $dao->entity_id2;
81 $pos['prev']['mergeId'] = $dao->id;
82 $pos['prev']['data'] = $dao->data;
83 }
84
85 $whereNext = " WHERE pn.id > %1 AND pn.cachekey = %2 {$where} ORDER BY ID ASC LIMIT 1";
86 $sqlNext = $sql . $whereNext;
87
88 $dao = CRM_Core_DAO::executeQuery($sqlNext, $p);
89 if ($dao->fetch()) {
90 $pos['next']['id1'] = $dao->entity_id1;
91 $pos['next']['id2'] = $dao->entity_id2;
92 $pos['next']['mergeId'] = $dao->id;
93 $pos['next']['data'] = $dao->data;
94 }
95 }
96 return $pos;
97 }
98
99 /**
100 * Delete an item from the prevnext cache table based on the entity.
101 *
102 * @param int $id
103 * @param string $cacheKey
104 * @param string $entityTable
105 */
106 public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') {
107
108 //clear cache
109 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
110 $params = [1 => [$entityTable, 'String']];
111
112 if (is_numeric($id)) {
113 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
114 $params[2] = [$id, 'Integer'];
115 }
116
117 if (isset($cacheKey)) {
118 $sql .= " AND cachekey LIKE %3";
119 $params[3] = ["{$cacheKey}%", 'String'];
120 }
121 CRM_Core_DAO::executeQuery($sql, $params);
122 }
123
124 /**
125 * Mark contacts as being in conflict.
126 *
127 * @param int $id1
128 * @param int $id2
129 * @param string $cacheKey
130 * @param array $conflicts
131 * @param string $mode
132 *
133 * @return bool
134 * @throws CRM_Core_Exception
135 */
136 public static function markConflict($id1, $id2, $cacheKey, $conflicts, $mode) {
137 if (empty($cacheKey) || empty($conflicts)) {
138 return FALSE;
139 }
140
141 $sql = "SELECT pn.*
142 FROM civicrm_prevnext_cache pn
143 WHERE
144 ((pn.entity_id1 = %1 AND pn.entity_id2 = %2) OR (pn.entity_id1 = %2 AND pn.entity_id2 = %1)) AND
145 (cachekey = %3 OR cachekey = %4)";
146 $params = [
147 1 => [$id1, 'Integer'],
148 2 => [$id2, 'Integer'],
149 3 => ["{$cacheKey}", 'String'],
150 4 => ["{$cacheKey}_conflicts", 'String'],
151 ];
152 $pncFind = CRM_Core_DAO::executeQuery($sql, $params);
153
154 $conflictTexts = [];
155
156 foreach ($conflicts as $entity => $entityConflicts) {
157 if ($entity === 'contact') {
158 foreach ($entityConflicts as $conflict) {
159 $conflictTexts[] = "{$conflict['title']}: '{$conflict[$id1]}' vs. '{$conflict[$id2]}'";
160 }
161 }
162 else {
163 foreach ($entityConflicts as $locationConflict) {
164 if (!is_array($locationConflict)) {
165 continue;
166 }
167 $displayField = CRM_Dedupe_Merger::getLocationBlockInfo()[$entity]['displayField'];
168 $conflictTexts[] = "{$locationConflict['title']}: '{$locationConflict[$displayField][$id1]}' vs. '{$locationConflict[$displayField][$id2]}'";
169 }
170 }
171 }
172 $conflictString = implode(', ', $conflictTexts);
173
174 while ($pncFind->fetch()) {
175 $data = $pncFind->data;
176 if (!empty($data)) {
177 $data = CRM_Core_DAO::unSerializeField($data, CRM_Core_DAO::SERIALIZE_PHP);
178 $data['conflicts'] = $conflictString;
179 $data[$mode]['conflicts'] = $conflicts;
180
181 $pncUp = new CRM_Core_DAO_PrevNextCache();
182 $pncUp->id = $pncFind->id;
183 if ($pncUp->find(TRUE)) {
184 $pncUp->data = serialize($data);
185 $pncUp->cachekey = "{$cacheKey}_conflicts";
186 $pncUp->save();
187 }
188 }
189 }
190 return TRUE;
191 }
192
193 /**
194 * Retrieve from prev-next cache.
195 *
196 * This function is used from a variety of merge related functions, although
197 * it would probably be good to converge on calling CRM_Dedupe_Merger::getDuplicatePairs.
198 *
199 * We seem to currently be storing stats in this table too & they might make more sense in
200 * the main cache table.
201 *
202 * @param string $cacheKey
203 * @param string $join
204 * @param string $whereClause
205 * @param int $offset
206 * @param int $rowCount
207 * @param array $select
208 * @param string $orderByClause
209 * @param bool $includeConflicts
210 * Should we return rows that have already been idenfified as having a conflict.
211 * When this is TRUE you should be careful you do not set up a loop.
212 * @param array $params
213 *
214 * @return array
215 */
216 public static function retrieve($cacheKey, $join = NULL, $whereClause = NULL, $offset = 0, $rowCount = 0, $select = [], $orderByClause = '', $includeConflicts = TRUE, $params = []) {
217 $selectString = 'pn.*';
218
219 if (!empty($select)) {
220 $aliasArray = [];
221 foreach ($select as $column => $alias) {
222 $aliasArray[] = $column . ' as ' . $alias;
223 }
224 $selectString .= " , " . implode(' , ', $aliasArray);
225 }
226
227 $params = [
228 1 => [$cacheKey, 'String'],
229 ] + $params;
230
231 if (!empty($whereClause)) {
232 $whereClause = " AND " . $whereClause;
233 }
234 if ($includeConflicts) {
235 $where = ' WHERE (pn.cachekey = %1 OR pn.cachekey = %2)' . $whereClause;
236 $params[2] = ["{$cacheKey}_conflicts", 'String'];
237 }
238 else {
239 $where = ' WHERE (pn.cachekey = %1)' . $whereClause;
240 }
241
242 $query = "
243 SELECT SQL_CALC_FOUND_ROWS {$selectString}
244 FROM civicrm_prevnext_cache pn
245 {$join}
246 $where
247 $orderByClause
248 ";
249
250 if ($rowCount) {
251 $offset = CRM_Utils_Type::escape($offset, 'Int');
252 $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
253
254 $query .= " LIMIT {$offset}, {$rowCount}";
255 }
256
257 $dao = CRM_Core_DAO::executeQuery($query, $params);
258
259 $main = [];
260 $count = 0;
261 while ($dao->fetch()) {
262 if (self::is_serialized($dao->data)) {
263 $main[$count] = CRM_Utils_String::unserialize($dao->data);
264 }
265 else {
266 $main[$count] = $dao->data;
267 }
268
269 if (!empty($select)) {
270 $extraData = [];
271 foreach ($select as $sfield) {
272 $extraData[$sfield] = $dao->$sfield;
273 }
274 $main[$count] = [
275 'prevnext_id' => $dao->id,
276 'is_selected' => $dao->is_selected,
277 'entity_id1' => $dao->entity_id1,
278 'entity_id2' => $dao->entity_id2,
279 'data' => $main[$count],
280 ];
281 $main[$count] = array_merge($main[$count], $extraData);
282 }
283 $count++;
284 }
285
286 return $main;
287 }
288
289 /**
290 * @param $string
291 *
292 * @return bool
293 */
294 public static function is_serialized($string) {
295 return (@CRM_Utils_String::unserialize($string) !== FALSE);
296 }
297
298 /**
299 * @param string $sqlValues string of SQLValues to insert
300 * @return array
301 */
302 public static function convertSetItemValues($sqlValues) {
303 CRM_Core_Error::deprecatedFunctionWarning('Deprecated function');
304 $closingBrace = strpos($sqlValues, ')') - strlen($sqlValues);
305 $valueArray = array_map('trim', explode(', ', substr($sqlValues, strpos($sqlValues, '(') + 1, $closingBrace - 1)));
306 foreach ($valueArray as $key => &$value) {
307 // remove any quotes from values.
308 if (substr($value, 0, 1) == "'") {
309 $valueArray[$key] = substr($value, 1, -1);
310 }
311 }
312 return $valueArray;
313 }
314
315 /**
316 * @deprecated
317 *
318 * @param array|string $entity_table
319 * @param int $entity_id1
320 * @param int $entity_id2
321 * @param string $cacheKey
322 * @param string $data
323 */
324 public static function setItem($entity_table = NULL, $entity_id1 = NULL, $entity_id2 = NULL, $cacheKey = NULL, $data = NULL) {
325 CRM_Core_Error::deprecatedFunctionWarning('Deprecated function');
326 // If entity table is an array we are passing in an older format where this function only had 1 param $values. We put a deprecation warning.
327 if (!empty($entity_table) && is_array($entity_table)) {
328 Civi::log()->warning('Deprecated code path. Values should not be set this is going away in the future in favour of specific function params for each column.', array('civi.tag' => 'deprecated'));
329 foreach ($values as $value) {
330 $valueArray = self::convertSetItemValues($value);
331 self::setItem($valueArray[0], $valueArray[1], $valueArray[2], $valueArray[3], $valueArray[4]);
332 }
333 }
334 else {
335 CRM_Core_DAO::executeQuery("INSERT INTO civicrm_prevnext_cache (entity_table, entity_id1, entity_id2, cacheKey, data) VALUES
336 (%1, %2, %3, %4, '{$data}')", [
337 1 => [$entity_table, 'String'],
338 2 => [$entity_id1, 'Integer'],
339 3 => [$entity_id2, 'Integer'],
340 4 => [$cacheKey, 'String'],
341 ]);
342 }
343 }
344
345 /**
346 * Get count of matching rows.
347 *
348 * @param string $cacheKey
349 * @param string $join
350 * @param string $where
351 * @param string $op
352 * @param array $params
353 * Extra query params to parse into the query.
354 *
355 * @return int
356 */
357 public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=", $params = []) {
358 $query = "
359 SELECT COUNT(*) FROM civicrm_prevnext_cache pn
360 {$join}
361 WHERE (pn.cachekey $op %1 OR pn.cachekey $op %2)
362 ";
363 if ($where) {
364 $query .= " AND {$where}";
365 }
366
367 $params = [
368 1 => [$cacheKey, 'String'],
369 2 => ["{$cacheKey}_conflicts", 'String'],
370 ] + $params;
371 return (int) CRM_Core_DAO::singleValueQuery($query, $params, TRUE, FALSE);
372 }
373
374 /**
375 * Repopulate the cache of merge prospects.
376 *
377 * @param int $rgid
378 * @param int $gid
379 * @param array $criteria
380 * Additional criteria to filter by.
381 *
382 * @param bool $checkPermissions
383 * Respect logged in user's permissions.
384 *
385 * @param int $searchLimit
386 * Limit for the number of contacts to be used for comparison.
387 * The search methodology finds all matches for the searchedContacts so this limits
388 * the number of searched contacts, not the matches found.
389 *
390 * @throws \CRM_Core_Exception
391 * @throws \CiviCRM_API3_Exception
392 */
393 public static function refillCache($rgid, $gid, $criteria, $checkPermissions, $searchLimit = 0) {
394 $cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions, $searchLimit);
395
396 // 1. Clear cache if any
397 $sql = "DELETE FROM civicrm_prevnext_cache WHERE cachekey LIKE %1";
398 CRM_Core_DAO::executeQuery($sql, [1 => ["{$cacheKeyString}%", 'String']]);
399
400 // FIXME: we need to start using temp tables / queries here instead of arrays.
401 // And cleanup code in CRM/Contact/Page/DedupeFind.php
402
403 // 2. FILL cache
404 $foundDupes = [];
405 if ($rgid && $gid) {
406 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($rgid, $gid, $searchLimit);
407 }
408 elseif ($rgid) {
409 $contactIDs = [];
410 // The thing we really need to filter out is any chaining that would 'DO SOMETHING' to the DB.
411 // criteria could be passed in via url so we want to ensure nothing could be in that url that
412 // would chain to a delete. Limiting to getfields for 'get' limits us to declared fields,
413 // although we might wish to revisit later to allow joins.
414 $validFieldsForRetrieval = civicrm_api3('Contact', 'getfields', ['action' => 'get'])['values'];
415 $filteredCriteria = isset($criteria['contact']) ? array_intersect_key($criteria['contact'], $validFieldsForRetrieval) : [];
416
417 if (!empty($criteria) || !empty($searchLimit)) {
418 $contacts = civicrm_api3('Contact', 'get', array_merge([
419 'options' => ['limit' => $searchLimit],
420 'return' => 'id',
421 'check_permissions' => TRUE,
422 'contact_type' => civicrm_api3('RuleGroup', 'getvalue', ['id' => $rgid, 'return' => 'contact_type']),
423 ], $filteredCriteria));
424 $contactIDs = array_keys($contacts['values']);
425
426 if (empty($contactIDs)) {
427 // If there is criteria but no contacts were found then we should return now
428 // since we have no contacts to match.
429 return [];
430 }
431 }
432 $foundDupes = CRM_Dedupe_Finder::dupes($rgid, $contactIDs, $checkPermissions);
433 }
434
435 if (!empty($foundDupes)) {
436 CRM_Dedupe_Finder::parseAndStoreDupePairs($foundDupes, $cacheKeyString);
437 }
438 }
439
440 /**
441 * Old function to clean up he cache.
442 *
443 * @deprecated.
444 */
445 public static function cleanupCache() {
446 CRM_Core_Error::deprecatedFunctionWarning('Deprecated function');
447 Civi::service('prevnext')->cleanup();
448 }
449
450 /**
451 * Get the selections.
452 *
453 * NOTE: This stub has been preserved because one extension in `universe`
454 * was referencing the function.
455 *
456 * @deprecated
457 * @see CRM_Core_PrevNextCache_Sql::getSelection()
458 */
459 public static function getSelection($cacheKey, $action = 'get') {
460 CRM_Core_Error::deprecatedFunctionWarning('Deprecated function');
461 return Civi::service('prevnext')->getSelection($cacheKey, $action);
462 }
463
464 /**
465 * Flip 2 contacts in the prevNext cache.
466 *
467 * @param array $prevNextId
468 * @param bool $onlySelected
469 * Only flip those which have been marked as selected.
470 */
471 public static function flipPair(array $prevNextId, $onlySelected) {
472 $dao = new CRM_Core_DAO_PrevNextCache();
473 if ($onlySelected) {
474 $dao->is_selected = 1;
475 }
476 foreach ($prevNextId as $id) {
477 $dao->id = $id;
478 if ($dao->find(TRUE)) {
479 $originalData = CRM_Utils_String::unserialize($dao->data);
480 $srcFields = ['ID', 'Name'];
481 $swapFields = ['srcID', 'srcName', 'dstID', 'dstName'];
482 $data = array_diff_assoc($originalData, array_fill_keys($swapFields, 1));
483 foreach ($srcFields as $key) {
484 $data['src' . $key] = $originalData['dst' . $key];
485 $data['dst' . $key] = $originalData['src' . $key];
486 }
487 $dao->data = serialize($data);
488 $dao->entity_id1 = $data['dstID'];
489 $dao->entity_id2 = $data['srcID'];
490 $dao->save();
491 }
492 }
493 }
494
495 /**
496 * Get a list of available backend services.
497 *
498 * @return array
499 * Array(string $id => string $label).
500 */
501 public static function getPrevNextBackends() {
502 return [
503 'default' => ts('Default (Auto-detect)'),
504 'sql' => ts('SQL'),
505 'redis' => ts('Redis'),
506 ];
507 }
508
509 /**
510 * Generate and assign an arbitrary value to a field of a test object.
511 *
512 * This specifically supports testing the dedupe use case.
513 *
514 * @param string $fieldName
515 * @param array $fieldDef
516 * @param int $counter
517 * The globally-unique ID of the test object.
518 *
519 * @throws \CRM_Core_Exception
520 */
521 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
522 if ($fieldName === 'cachekey') {
523 $this->cachekey = 'merge_' . rand();
524 return;
525 }
526 if ($fieldName === 'data') {
527 $this->data = serialize([]);
528 return;
529 }
530 parent::assignTestValue($fieldName, $fieldDef, $counter);
531 }
532
533 }