From 744b4e344aa23fbb4655797b057244109dfb2c61 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 2 Jul 2018 14:11:21 -0700 Subject: [PATCH] (dev/core#217) PrevNext - Allow swapping `deleteItem()` for purposes of contact-search The `deleteItem()` function is used by both contact-search and dedupe-merge use-cases. We can classify several of these just based on the files: * Contact-search use-cases. (These should be updated to use the interface.) * `CRM/Campaign/Selector/Search.php: CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact');` * `CRM/Contact/Form/Search.php: CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);` * `CRM/Contact/Selector.php: CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact');` * Dedupe-merge use-cases. (These should contiue using the BAO.) * `CRM/Contact/Form/DedupeRules.php: CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey);` * `CRM/Contact/Page/DedupeFind.php: CRM_Core_BAO_PrevNextCache::deleteItem(NULL, CRM_Dedupe_Merger::getMergeCacheKeyString($rgid, $gid, $criteria))` * `CRM/Dedupe/Merger.php: CRM_Core_BAO_PrevNextCache::deleteItem(NULL, "{$cacheKeyString}_stats");` Additionally, there are two oddballs which are harder to categorize. * `CRM_Contact_BAO_Contact_Utils::clearContactCaches($isEmptyPrevNextTable = FALSE)` deletes *all* prev-next cache-records (`CRM_Core_BAO_PrevNextCache::deleteItem();`). It only does so in one scenario (as part of `CRM/Contact/Import/Form/Preview.php`), which has this explanatory comment: "Clear all caches, forcing any searches to recheck the ACLs or group membership as the import may have changed it." * `CRM_Contact_BAO_Contact::deleteContact(...)` deletes any prev-next cache-records which reference a specific contact (`CRM_Core_BAO_PrevNextCache::deleteItem($id)`). I suppose this provides a low-grade form of referential integrity. Part of me thinks those should be re-considered (e.g. to use a hook/event -- and reduce the coupling between `Contact` and `PrevNext` subsystems). However, for purposes of dev/core#217, it seems OK to send `deleteItem(...)` to both BAO (SQL-only) and service (SQL-or-memory) variants. --- CRM/Campaign/Selector/Search.php | 2 +- CRM/Contact/BAO/Contact.php | 5 ++++- CRM/Contact/BAO/Contact/Utils.php | 3 +++ CRM/Contact/Form/Search.php | 2 +- CRM/Contact/Selector.php | 2 +- CRM/Core/PrevNextCache/Interface.php | 9 +++++++++ CRM/Core/PrevNextCache/Sql.php | 11 +++++++++++ 7 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CRM/Campaign/Selector/Search.php b/CRM/Campaign/Selector/Search.php index 08b233be67..28c2b15b6d 100644 --- a/CRM/Campaign/Selector/Search.php +++ b/CRM/Campaign/Selector/Search.php @@ -271,7 +271,7 @@ class CRM_Campaign_Selector_Search extends CRM_Core_Selector_Base implements CRM if (!$crmPID) { $cacheKey = "civicrm search {$this->_key}"; - CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact'); + Civi::service('prevnext')->deleteItem(NULL, $cacheKey, 'civicrm_contact'); $sql = $this->_query->searchQuery(0, 0, $sort, FALSE, FALSE, diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index f222e19594..9e4488208c 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -1013,7 +1013,10 @@ WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); CRM_Utils_Recent::delContact($id); self::updateContactCache($id, empty($restore)); - // delete any dupe cache entry + // delete any prevnext/dupe cache entry + // These two calls are redundant in default deployments, but they're + // meaningful if "prevnext" is memory-backed. + Civi::service('prevnext')->deleteItem($id); CRM_Core_BAO_PrevNextCache::deleteItem($id); $transaction->commit(); diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index f3b391362a..2cee6e99dc 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -917,6 +917,9 @@ INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_i return; } if ($isEmptyPrevNextTable) { + // These two calls are redundant in default deployments, but they're + // meaningful if "prevnext" is memory-backed. + Civi::service('prevnext')->deleteItem(); CRM_Core_BAO_PrevNextCache::deleteItem(); } // clear acl cache if any. diff --git a/CRM/Contact/Form/Search.php b/CRM/Contact/Form/Search.php index eadf418b4d..a9ea4d57d9 100644 --- a/CRM/Contact/Form/Search.php +++ b/CRM/Contact/Form/Search.php @@ -782,7 +782,7 @@ class CRM_Contact_Form_Search extends CRM_Core_Form_Search { ) { //reset the cache table for new search $cacheKey = "civicrm search {$this->controller->_key}"; - CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey); + Civi::service('prevnext')->deleteItem(NULL, $cacheKey); } //get the button name diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index a3c270231c..945c2b3162 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -881,7 +881,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se // check for current != previous to ensure cache is not reset if paging is done without changing // sort criteria if (!$pageNum || (!empty($currentSortID) && $currentSortID != $previousSortID)) { - CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact'); + Civi::service('prevnext')->deleteItem(NULL, $cacheKey, 'civicrm_contact'); // this means it's fresh search, so set pageNum=1 if (!$pageNum) { $pageNum = 1; diff --git a/CRM/Core/PrevNextCache/Interface.php b/CRM/Core/PrevNextCache/Interface.php index 0b1bada79d..57742338f0 100644 --- a/CRM/Core/PrevNextCache/Interface.php +++ b/CRM/Core/PrevNextCache/Interface.php @@ -95,4 +95,13 @@ interface CRM_Core_PrevNextCache_Interface { */ public function getPositions($cacheKey, $id1, $id2); + /** + * Delete an item from the prevnext cache table based on the entity. + * + * @param int $id + * @param string $cacheKey + * @param string $entityTable + */ + public function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact'); + } diff --git a/CRM/Core/PrevNextCache/Sql.php b/CRM/Core/PrevNextCache/Sql.php index 653bb079d3..c9a26a187d 100644 --- a/CRM/Core/PrevNextCache/Sql.php +++ b/CRM/Core/PrevNextCache/Sql.php @@ -191,4 +191,15 @@ ORDER BY id return CRM_Core_BAO_PrevNextCache::getPositions($cacheKey, $id1, $id2); } + /** + * Delete an item from the prevnext cache table based on the entity. + * + * @param int $id + * @param string $cacheKey + * @param string $entityTable + */ + public function deleteItem($id = NULL, $cacheKey = NULL, $entityTable = 'civicrm_contact') { + CRM_Core_BAO_PrevNextCache::deleteItem($id, $cacheKey, $entityTable); + } + } -- 2.25.1