3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * BAO object for civicrm_prevnext_cache table.
37 class CRM_Core_BAO_PrevNextCache
extends CRM_Core_DAO_PrevNextCache
{
40 * Get the previous and next keys.
42 * @param string $cacheKey
47 * @param string $where
52 public static function getPositions($cacheKey, $id1, $id2, &$mergeId = NULL, $join = NULL, $where = NULL, $flip = FALSE) {
54 list($id1, $id2) = [$id2, $id1];
57 if ($mergeId == NULL) {
60 FROM civicrm_prevnext_cache
61 WHERE cachekey = %3 AND
64 entity_table = 'civicrm_contact'
68 1 => [$id1, 'Integer'],
69 2 => [$id2, 'Integer'],
70 3 => [$cacheKey, 'String'],
73 $mergeId = CRM_Core_DAO
::singleValueQuery($query, $params);
76 $pos = ['foundEntry' => 0];
78 $pos['foundEntry'] = 1;
82 $where = " AND {$where}";
86 1 => [$mergeId, 'Integer'],
87 2 => [$cacheKey, 'String'],
89 $sql = "SELECT pn.id, pn.entity_id1, pn.entity_id2, pn.data FROM civicrm_prevnext_cache pn {$join} ";
90 $wherePrev = " WHERE pn.id < %1 AND pn.cachekey = %2 {$where} ORDER BY ID DESC LIMIT 1";
91 $sqlPrev = $sql . $wherePrev;
93 $dao = CRM_Core_DAO
::executeQuery($sqlPrev, $p);
95 $pos['prev']['id1'] = $dao->entity_id1
;
96 $pos['prev']['id2'] = $dao->entity_id2
;
97 $pos['prev']['mergeId'] = $dao->id
;
98 $pos['prev']['data'] = $dao->data
;
101 $whereNext = " WHERE pn.id > %1 AND pn.cachekey = %2 {$where} ORDER BY ID ASC LIMIT 1";
102 $sqlNext = $sql . $whereNext;
104 $dao = CRM_Core_DAO
::executeQuery($sqlNext, $p);
106 $pos['next']['id1'] = $dao->entity_id1
;
107 $pos['next']['id2'] = $dao->entity_id2
;
108 $pos['next']['mergeId'] = $dao->id
;
109 $pos['next']['data'] = $dao->data
;
116 * Delete an item from the prevnext cache table based on the entity.
119 * @param string $cacheKey
120 * @param string $entityTable
122 public static function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') {
125 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = %1";
126 $params = [1 => [$entityTable, 'String']];
128 if (is_numeric($id)) {
129 $sql .= " AND ( entity_id1 = %2 OR entity_id2 = %2 )";
130 $params[2] = [$id, 'Integer'];
133 if (isset($cacheKey)) {
134 $sql .= " AND cachekey LIKE %3";
135 $params[3] = ["{$cacheKey}%", 'String'];
137 CRM_Core_DAO
::executeQuery($sql, $params);
141 * Delete pair from the previous next cache table to remove it from further merge consideration.
143 * The pair may have been flipped, so make sure we delete using both orders
147 * @param string $cacheKey
149 public static function deletePair($id1, $id2, $cacheKey = NULL) {
150 $sql = "DELETE FROM civicrm_prevnext_cache WHERE entity_table = 'civicrm_contact'";
152 $pair = "(entity_id1 = %2 AND entity_id2 = %3) OR (entity_id1 = %3 AND entity_id2 = %2)";
153 $sql .= " AND ( {$pair} )";
154 $params[2] = [$id1, 'Integer'];
155 $params[3] = [$id2, 'Integer'];
157 if (isset($cacheKey)) {
158 $sql .= " AND cachekey LIKE %4";
159 // used % to address any row with conflict-cacheKey e.g "merge Individual_8_0_conflicts"
160 $params[4] = ["{$cacheKey}%", 'String'];
163 CRM_Core_DAO
::executeQuery($sql, $params);
167 * Mark contacts as being in conflict.
171 * @param string $cacheKey
172 * @param array $conflicts
173 * @param string $mode
176 * @throws CRM_Core_Exception
178 public static function markConflict($id1, $id2, $cacheKey, $conflicts, $mode) {
179 if (empty($cacheKey) ||
empty($conflicts)) {
184 FROM civicrm_prevnext_cache pn
186 ((pn.entity_id1 = %1 AND pn.entity_id2 = %2) OR (pn.entity_id1 = %2 AND pn.entity_id2 = %1)) AND
187 (cachekey = %3 OR cachekey = %4)";
189 1 => [$id1, 'Integer'],
190 2 => [$id2, 'Integer'],
191 3 => ["{$cacheKey}", 'String'],
192 4 => ["{$cacheKey}_conflicts", 'String'],
194 $pncFind = CRM_Core_DAO
::executeQuery($sql, $params);
198 foreach ($conflicts as $entity => $entityConflicts) {
199 if ($entity === 'contact') {
200 foreach ($entityConflicts as $conflict) {
201 $conflictTexts[] = "{$conflict['title']}: '{$conflict[$id1]}' vs. '{$conflict[$id2]}'";
205 foreach ($entityConflicts as $locationConflict) {
206 if (!is_array($locationConflict)) {
209 $displayField = CRM_Dedupe_Merger
::getLocationBlockInfo()[$entity]['displayField'];
210 $conflictTexts[] = "{$locationConflict['title']}: '{$locationConflict[$displayField][$id1]}' vs. '{$locationConflict[$displayField][$id2]}'";
214 $conflictString = implode(', ', $conflictTexts);
216 while ($pncFind->fetch()) {
217 $data = $pncFind->data
;
219 $data = CRM_Core_DAO
::unSerializeField($data, CRM_Core_DAO
::SERIALIZE_PHP
);
220 $data['conflicts'] = $conflictString;
221 $data[$mode]['conflicts'] = $conflicts;
223 $pncUp = new CRM_Core_DAO_PrevNextCache();
224 $pncUp->id
= $pncFind->id
;
225 if ($pncUp->find(TRUE)) {
226 $pncUp->data
= serialize($data);
227 $pncUp->cachekey
= "{$cacheKey}_conflicts";
236 * Retrieve from prev-next cache.
238 * This function is used from a variety of merge related functions, although
239 * it would probably be good to converge on calling CRM_Dedupe_Merger::getDuplicatePairs.
241 * We seem to currently be storing stats in this table too & they might make more sense in
242 * the main cache table.
244 * @param string $cacheKey
245 * @param string $join
246 * @param string $whereClause
248 * @param int $rowCount
249 * @param array $select
250 * @param string $orderByClause
251 * @param bool $includeConflicts
252 * Should we return rows that have already been idenfified as having a conflict.
253 * When this is TRUE you should be careful you do not set up a loop.
254 * @param array $params
258 public static function retrieve($cacheKey, $join = NULL, $whereClause = NULL, $offset = 0, $rowCount = 0, $select = [], $orderByClause = '', $includeConflicts = TRUE, $params = []) {
259 $selectString = 'pn.*';
261 if (!empty($select)) {
263 foreach ($select as $column => $alias) {
264 $aliasArray[] = $column . ' as ' . $alias;
266 $selectString .= " , " . implode(' , ', $aliasArray);
270 1 => [$cacheKey, 'String'],
273 if (!empty($whereClause)) {
274 $whereClause = " AND " . $whereClause;
276 if ($includeConflicts) {
277 $where = ' WHERE (pn.cachekey = %1 OR pn.cachekey = %2)' . $whereClause;
278 $params[2] = ["{$cacheKey}_conflicts", 'String'];
281 $where = ' WHERE (pn.cachekey = %1)' . $whereClause;
285 SELECT SQL_CALC_FOUND_ROWS {$selectString}
286 FROM civicrm_prevnext_cache pn
293 $offset = CRM_Utils_Type
::escape($offset, 'Int');
294 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
296 $query .= " LIMIT {$offset}, {$rowCount}";
299 $dao = CRM_Core_DAO
::executeQuery($query, $params);
303 while ($dao->fetch()) {
304 if (self
::is_serialized($dao->data
)) {
305 $main[$count] = unserialize($dao->data
);
308 $main[$count] = $dao->data
;
311 if (!empty($select)) {
313 foreach ($select as $sfield) {
314 $extraData[$sfield] = $dao->$sfield;
317 'prevnext_id' => $dao->id
,
318 'is_selected' => $dao->is_selected
,
319 'entity_id1' => $dao->entity_id1
,
320 'entity_id2' => $dao->entity_id2
,
321 'data' => $main[$count],
323 $main[$count] = array_merge($main[$count], $extraData);
336 public static function is_serialized($string) {
337 return (@unserialize
($string) !== FALSE);
343 public static function setItem($values) {
344 $insert = "INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cachekey, data ) VALUES \n";
345 $query = $insert . implode(",\n ", $values);
347 //dump the dedupe matches in the prevnext_cache table
348 CRM_Core_DAO
::executeQuery($query);
352 * Get count of matching rows.
354 * @param string $cacheKey
355 * @param string $join
356 * @param string $where
358 * @param array $params
359 * Extra query params to parse into the query.
363 public static function getCount($cacheKey, $join = NULL, $where = NULL, $op = "=", $params = []) {
365 SELECT COUNT(*) FROM civicrm_prevnext_cache pn
367 WHERE (pn.cachekey $op %1 OR pn.cachekey $op %2)
370 $query .= " AND {$where}";
374 1 => [$cacheKey, 'String'],
375 2 => ["{$cacheKey}_conflicts", 'String'],
377 return (int) CRM_Core_DAO
::singleValueQuery($query, $params, TRUE, FALSE);
381 * Repopulate the cache of merge prospects.
385 * @param array $criteria
386 * Additional criteria to filter by.
388 * @param bool $checkPermissions
389 * Respect logged in user's permissions.
391 * @param int $searchLimit
392 * Limit for the number of contacts to be used for comparison.
393 * The search methodology finds all matches for the searchedContacts so this limits
394 * the number of searched contacts, not the matches found.
396 * @throws \CRM_Core_Exception
397 * @throws \CiviCRM_API3_Exception
399 public static function refillCache($rgid, $gid, $criteria, $checkPermissions, $searchLimit = 0) {
400 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid, $criteria, $checkPermissions);
402 // 1. Clear cache if any
403 $sql = "DELETE FROM civicrm_prevnext_cache WHERE cachekey LIKE %1";
404 CRM_Core_DAO
::executeQuery($sql, [1 => ["{$cacheKeyString}%", 'String']]);
406 // FIXME: we need to start using temp tables / queries here instead of arrays.
407 // And cleanup code in CRM/Contact/Page/DedupeFind.php
412 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($rgid, $gid, $searchLimit);
416 // The thing we really need to filter out is any chaining that would 'DO SOMETHING' to the DB.
417 // criteria could be passed in via url so we want to ensure nothing could be in that url that
418 // would chain to a delete. Limiting to getfields for 'get' limits us to declared fields,
419 // although we might wish to revisit later to allow joins.
420 $validFieldsForRetrieval = civicrm_api3('Contact', 'getfields', ['action' => 'get'])['values'];
421 if (!empty($criteria)) {
422 $contacts = civicrm_api3('Contact', 'get', array_merge([
423 'options' => ['limit' => 0],
425 'check_permissions' => TRUE,
426 ], array_intersect_key($criteria['contact'], $validFieldsForRetrieval)));
427 $contactIDs = array_keys($contacts['values']);
429 $foundDupes = CRM_Dedupe_Finder
::dupes($rgid, $contactIDs, $checkPermissions, $searchLimit);
432 if (!empty($foundDupes)) {
433 CRM_Dedupe_Finder
::parseAndStoreDupePairs($foundDupes, $cacheKeyString);
437 public static function cleanupCache() {
438 // clean up all prev next caches older than $cacheTimeIntervalDays days
439 $cacheTimeIntervalDays = 2;
441 // first find all the cacheKeys that match this
445 INNER JOIN civicrm_prevnext_cache pn ON c.path = pn.cachekey
446 WHERE c.group_name = %1
447 AND c.created_date < date_sub( NOW( ), INTERVAL %2 day )
450 1 => [CRM_Utils_Cache
::cleanKey('CiviCRM Search PrevNextCache'), 'String'],
451 2 => [$cacheTimeIntervalDays, 'Integer'],
453 CRM_Core_DAO
::executeQuery($sql, $params);
457 * Get the selections.
459 * NOTE: This stub has been preserved because one extension in `universe`
460 * was referencing the function.
463 * @see CRM_Core_PrevNextCache_Sql::getSelection()
465 public static function getSelection($cacheKey, $action = 'get') {
466 return Civi
::service('prevnext')->getSelection($cacheKey, $action);
470 * Flip 2 contacts in the prevNext cache.
472 * @param array $prevNextId
473 * @param bool $onlySelected
474 * Only flip those which have been marked as selected.
476 public static function flipPair(array $prevNextId, $onlySelected) {
477 $dao = new CRM_Core_DAO_PrevNextCache();
479 $dao->is_selected
= 1;
481 foreach ($prevNextId as $id) {
483 if ($dao->find(TRUE)) {
484 $originalData = unserialize($dao->data
);
485 $srcFields = ['ID', 'Name'];
486 $swapFields = ['srcID', 'srcName', 'dstID', 'dstName'];
487 $data = array_diff_assoc($originalData, array_fill_keys($swapFields, 1));
488 foreach ($srcFields as $key) {
489 $data['src' . $key] = $originalData['dst' . $key];
490 $data['dst' . $key] = $originalData['src' . $key];
492 $dao->data
= serialize($data);
493 $dao->entity_id1
= $data['dstID'];
494 $dao->entity_id2
= $data['srcID'];
501 * Get a list of available backend services.
504 * Array(string $id => string $label).
506 public static function getPrevNextBackends() {
508 'default' => ts('Default (Auto-detect)'),
510 'redis' => ts('Redis'),
515 * Generate and assign an arbitrary value to a field of a test object.
517 * This specifically supports testing the dedupe use case.
519 * @param string $fieldName
520 * @param array $fieldDef
521 * @param int $counter
522 * The globally-unique ID of the test object.
524 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
525 if ($fieldName === 'cachekey') {
526 $this->cachekey
= 'merge_' . rand();
529 if ($fieldName === 'data') {
530 $this->data
= serialize([]);
533 parent
::assignTestValue($fieldName, $fieldDef, $counter);