3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * BAO object for civicrm_prevnext_cache table.
21 class CRM_Core_BAO_PrevNextCache
extends CRM_Core_DAO_PrevNextCache
{
24 * Get the previous and next keys.
26 * @param string $cacheKey
31 * @param string $where
36 public static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) {
38 list($id1, $id2) = [$id2, $id1];
41 if ($mergeId == NULL) {
44 FROM civicrm_prevnext_cache
45 WHERE cachekey = %3 AND
48 entity_table = 'civicrm_contact'
52 1 => [$id1, 'Integer'],
53 2 => [$id2, 'Integer'],
54 3 => [$cacheKey, 'String'],
57 $mergeId = CRM_Core_DAO
::singleValueQuery($query, $params);
60 $pos = ['foundEntry' => 0];
62 $pos['foundEntry'] = 1;
66 $where = " AND {$where}";
70 1 => [$mergeId, 'Integer'],
71 2 => [$cacheKey, 'String'],
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;
77 $dao = CRM_Core_DAO
::executeQuery($sqlPrev, $p);
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
;
85 $whereNext = " WHERE pn.id > %1 AND pn.cachekey = %2 {$where} ORDER BY ID ASC LIMIT 1";
86 $sqlNext = $sql . $whereNext;
88 $dao = CRM_Core_DAO
::executeQuery($sqlNext, $p);
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
;
100 * Delete an item from the prevnext cache table based on the entity.
103 * @param string $cacheKey
104 * @param string $entityTable
106 public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') {
109 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
110 $params = [1 => [$entityTable, 'String']];
112 if (is_numeric($id)) {
113 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
114 $params[2] = [$id, 'Integer'];
117 if (isset($cacheKey)) {
118 $sql .= " AND cachekey LIKE %3";
119 $params[3] = ["{$cacheKey}%", 'String'];
121 CRM_Core_DAO
::executeQuery($sql, $params);
125 * Mark contacts as being in conflict.
129 * @param string $cacheKey
130 * @param array $conflicts
131 * @param string $mode
134 * @throws CRM_Core_Exception
136 public static function markConflict($id1, $id2, $cacheKey, $conflicts, $mode) {
137 if (empty($cacheKey) ||
empty($conflicts)) {
142 FROM civicrm_prevnext_cache pn
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)";
147 1 => [$id1, 'Integer'],
148 2 => [$id2, 'Integer'],
149 3 => ["{$cacheKey}", 'String'],
150 4 => ["{$cacheKey}_conflicts", 'String'],
152 $pncFind = CRM_Core_DAO
::executeQuery($sql, $params);
156 foreach ($conflicts as $entity => $entityConflicts) {
157 if ($entity === 'contact') {
158 foreach ($entityConflicts as $conflict) {
159 $conflictTexts[] = "{$conflict['title']}: '{$conflict[$id1]}' vs. '{$conflict[$id2]}'";
163 foreach ($entityConflicts as $locationConflict) {
164 if (!is_array($locationConflict)) {
167 $displayField = CRM_Dedupe_Merger
::getLocationBlockInfo()[$entity]['displayField'];
168 $conflictTexts[] = "{$locationConflict['title']}: '{$locationConflict[$displayField][$id1]}' vs. '{$locationConflict[$displayField][$id2]}'";
172 $conflictString = implode(', ', $conflictTexts);
174 while ($pncFind->fetch()) {
175 $data = $pncFind->data
;
177 $data = CRM_Core_DAO
::unSerializeField($data, CRM_Core_DAO
::SERIALIZE_PHP
);
178 $data['conflicts'] = $conflictString;
179 $data[$mode]['conflicts'] = $conflicts;
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";
194 * Retrieve from prev-next cache.
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.
199 * We seem to currently be storing stats in this table too & they might make more sense in
200 * the main cache table.
202 * @param string $cacheKey
203 * @param string $join
204 * @param string $whereClause
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
216 public static function retrieve($cacheKey, $join = NULL, $whereClause = NULL, $offset = 0, $rowCount = 0, $select = [], $orderByClause = '', $includeConflicts = TRUE, $params = []) {
217 $selectString = 'pn.*';
219 if (!empty($select)) {
221 foreach ($select as $column => $alias) {
222 $aliasArray[] = $column . ' as ' . $alias;
224 $selectString .= " , " . implode(' , ', $aliasArray);
228 1 => [$cacheKey, 'String'],
231 if (!empty($whereClause)) {
232 $whereClause = " AND " . $whereClause;
234 if ($includeConflicts) {
235 $where = ' WHERE (pn.cachekey = %1 OR pn.cachekey = %2)' . $whereClause;
236 $params[2] = ["{$cacheKey}_conflicts", 'String'];
239 $where = ' WHERE (pn.cachekey = %1)' . $whereClause;
243 SELECT SQL_CALC_FOUND_ROWS {$selectString}
244 FROM civicrm_prevnext_cache pn
251 $offset = CRM_Utils_Type
::escape($offset, 'Int');
252 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
254 $query .= " LIMIT {$offset}, {$rowCount}";
257 $dao = CRM_Core_DAO
::executeQuery($query, $params);
261 while ($dao->fetch()) {
262 if (self
::is_serialized($dao->data
)) {
263 $main[$count] = CRM_Utils_String
::unserialize($dao->data
);
266 $main[$count] = $dao->data
;
269 if (!empty($select)) {
271 foreach ($select as $sfield) {
272 $extraData[$sfield] = $dao->$sfield;
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],
281 $main[$count] = array_merge($main[$count], $extraData);
294 public static function is_serialized($string) {
295 return (@CRM_Utils_String
::unserialize($string) !== FALSE);
299 * @param string $sqlValues string of SQLValues to insert
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);
318 * @param array|string $entity_table
319 * @param int $entity_id1
320 * @param int $entity_id2
321 * @param string $cacheKey
322 * @param string $data
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 CRM_Core_Error
::deprecatedWarning('Deprecated code path. Values should not be set this is going away in the future in favour of specific function params for each column.');
329 foreach ($values as $value) {
330 $valueArray = self
::convertSetItemValues($value);
331 self
::setItem($valueArray[0], $valueArray[1], $valueArray[2], $valueArray[3], $valueArray[4]);
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'],
346 * Get count of matching rows.
348 * @param string $cacheKey
349 * @param string $join
350 * @param string $where
352 * @param array $params
353 * Extra query params to parse into the query.
357 public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=", $params = []) {
359 SELECT COUNT(*) FROM civicrm_prevnext_cache pn
361 WHERE (pn.cachekey $op %1 OR pn.cachekey $op %2)
364 $query .= " AND {$where}";
368 1 => [$cacheKey, 'String'],
369 2 => ["{$cacheKey}_conflicts", 'String'],
371 return (int) CRM_Core_DAO
::singleValueQuery($query, $params, TRUE, FALSE);
375 * Repopulate the cache of merge prospects.
379 * @param array $criteria
380 * Additional criteria to filter by.
382 * @param bool $checkPermissions
383 * Respect logged in user's permissions.
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.
390 * @throws \CRM_Core_Exception
391 * @throws \CiviCRM_API3_Exception
393 public static function refillCache($rgid, $gid, $criteria, $checkPermissions, $searchLimit = 0) {
394 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions, $searchLimit);
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']]);
400 // FIXME: we need to start using temp tables / queries here instead of arrays.
401 // And cleanup code in CRM/Contact/Page/DedupeFind.php
406 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid, $searchLimit);
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) : [];
417 if (!empty($criteria) ||
!empty($searchLimit)) {
418 $contacts = civicrm_api3('Contact', 'get', array_merge([
419 'options' => ['limit' => $searchLimit],
421 'check_permissions' => TRUE,
422 'contact_type' => civicrm_api3('RuleGroup', 'getvalue', ['id' => $rgid, 'return' => 'contact_type']),
423 ], $filteredCriteria));
424 $contactIDs = array_keys($contacts['values']);
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.
432 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid, $contactIDs, $checkPermissions);
435 if (!empty($foundDupes)) {
436 CRM_Dedupe_Finder
::parseAndStoreDupePairs($foundDupes, $cacheKeyString);
441 * Old function to clean up he cache.
445 public static function cleanupCache() {
446 CRM_Core_Error
::deprecatedFunctionWarning('Deprecated function');
447 Civi
::service('prevnext')->cleanup();
451 * Get the selections.
453 * NOTE: This stub has been preserved because one extension in `universe`
454 * was referencing the function.
457 * @see CRM_Core_PrevNextCache_Sql::getSelection()
459 public static function getSelection($cacheKey, $action = 'get') {
460 CRM_Core_Error
::deprecatedFunctionWarning('Deprecated function');
461 return Civi
::service('prevnext')->getSelection($cacheKey, $action);
465 * Flip 2 contacts in the prevNext cache.
467 * @param array $prevNextId
468 * @param bool $onlySelected
469 * Only flip those which have been marked as selected.
471 public static function flipPair(array $prevNextId, $onlySelected) {
472 $dao = new CRM_Core_DAO_PrevNextCache();
474 $dao->is_selected
= 1;
476 foreach ($prevNextId as $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];
487 $dao->data
= serialize($data);
488 $dao->entity_id1
= $data['dstID'];
489 $dao->entity_id2
= $data['srcID'];
496 * Get a list of available backend services.
499 * Array(string $id => string $label).
501 public static function getPrevNextBackends() {
503 'default' => ts('Default (Auto-detect)'),
505 'redis' => ts('Redis'),
510 * Generate and assign an arbitrary value to a field of a test object.
512 * This specifically supports testing the dedupe use case.
514 * @param string $fieldName
515 * @param array $fieldDef
516 * @param int $counter
517 * The globally-unique ID of the test object.
519 * @throws \CRM_Core_Exception
521 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
522 if ($fieldName === 'cachekey') {
523 $this->cachekey
= 'merge_' . rand();
526 if ($fieldName === 'data') {
527 $this->data
= serialize([]);
530 parent
::assignTestValue($fieldName, $fieldDef, $counter);