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 +--------------------------------------------------------------------+
12 use Civi\Api4\Query\SqlExpression
;
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Contact_BAO_GroupContactCache
extends CRM_Contact_DAO_GroupContactCache
{
21 public static $_alreadyLoaded = [];
24 * Get a list of caching modes.
28 public static function getModes() {
30 // Flush expired caches in response to user actions.
31 'opportunistic' => ts('Opportunistic Flush'),
33 // Flush expired caches via background cron jobs.
34 'deterministic' => ts('Cron Flush'),
39 * Check to see if we have cache entries for this group.
41 * If not, regenerate, else return.
43 * @param array $groupIDs
44 * Of group that we are checking against.
47 * TRUE if we did not regenerate, FALSE if we did
49 public static function check($groupIDs) {
50 if (empty($groupIDs)) {
54 return self
::loadAll($groupIDs);
58 * Formulate the query to see which groups needs to be refreshed.
60 * The calculation is based on their cache date and the smartGroupCacheTimeOut
62 * @param string $groupIDClause
63 * The clause which limits which groups we need to evaluate.
64 * @param bool $includeHiddenGroups
65 * Hidden groups are excluded by default.
68 * the sql query which lists the groups that need to be refreshed
70 public static function groupRefreshedClause($groupIDClause = NULL, $includeHiddenGroups = FALSE): string {
71 $smartGroupCacheTimeoutDateTime = self
::getCacheInvalidDateTime();
76 WHERE ( g.saved_search_id IS NOT NULL OR g.children IS NOT NULL )
80 OR cache_date <= $smartGroupCacheTimeoutDateTime
83 if (!$includeHiddenGroups) {
84 $query .= "AND (g.is_hidden = 0 OR g.is_hidden IS NULL)";
87 if (!empty($groupIDClause)) {
88 $query .= " AND ( $groupIDClause ) ";
95 * Check to see if a group has been refreshed recently.
97 * This is primarily used in a locking scenario when some other process might have refreshed things underneath
100 * @param int $groupID
102 * @param bool $includeHiddenGroups
103 * Hidden groups are excluded by default.
106 * the sql query which lists the groups that need to be refreshed
108 public static function shouldGroupBeRefreshed($groupID, $includeHiddenGroups = FALSE) {
109 $query = self
::groupRefreshedClause("g.id = %1", $includeHiddenGroups);
110 $params = [1 => [$groupID, 'Integer']];
112 // if the query returns the group ID, it means the group is a valid candidate for refreshing
113 return CRM_Core_DAO
::singleValueQuery($query, $params);
117 * Check to see if we have cache entries for this group.
119 * if not, regenerate, else return
121 * @param int|array $groupIDs groupIDs of group that we are checking against
122 * if empty, all groups are checked
124 * Limits the number of groups we evaluate.
127 * TRUE if we did not regenerate, FALSE if we did
129 public static function loadAll($groupIDs = NULL, $limit = 0) {
130 // ensure that all the smart groups are loaded
131 // this function is expensive and should be sparingly used if groupIDs is empty
132 if (empty($groupIDs)) {
133 $groupIDClause = NULL;
137 if (!is_array($groupIDs)) {
138 $groupIDs = [$groupIDs];
141 // note escapeString is a must here and we can't send the imploded value as second argument to
142 // the executeQuery(), since that would put single quote around the string and such a string
143 // of comma separated integers would not work.
144 $groupIDString = CRM_Core_DAO
::escapeString(implode(', ', $groupIDs));
146 $groupIDClause = "g.id IN ({$groupIDString})";
149 $query = self
::groupRefreshedClause($groupIDClause);
151 $limitClause = $orderClause = NULL;
153 $limitClause = " LIMIT 0, $limit";
154 $orderClause = " ORDER BY g.cache_date";
156 // We ignore hidden groups and disabled groups
162 $dao = CRM_Core_DAO
::executeQuery($query);
163 $processGroupIDs = [];
164 $refreshGroupIDs = $groupIDs;
165 while ($dao->fetch()) {
166 $processGroupIDs[] = $dao->id
;
168 // remove this id from refreshGroupIDs
169 foreach ($refreshGroupIDs as $idx => $gid) {
170 if ($gid == $dao->id
) {
171 unset($refreshGroupIDs[$idx]);
177 if (!empty($refreshGroupIDs)) {
178 $refreshGroupIDString = CRM_Core_DAO
::escapeString(implode(', ', $refreshGroupIDs));
180 UPDATE civicrm_group g
181 SET g.cache_date = NOW()
182 WHERE g.id IN ( {$refreshGroupIDString} )
184 CRM_Core_DAO
::executeQuery($query);
187 if (empty($processGroupIDs)) {
191 self
::add($processGroupIDs);
197 * Build the smart group cache for given groups.
199 * @param array $groupIDs
201 public static function add($groupIDs) {
202 $groupIDs = (array) $groupIDs;
204 foreach ($groupIDs as $groupID) {
205 // first delete the current cache
206 self
::clearGroupContactCache($groupID);
207 $params = [['group', 'IN', [$groupID], 0, 0]];
208 // the below call updates the cache table as a byproduct of the query
209 CRM_Contact_BAO_Query
::apiQuery($params, ['contact_id'], NULL, NULL, 0, 0, FALSE);
214 * Store values into the group contact cache.
216 * @todo review use of INSERT IGNORE. This function appears to be slower that inserting
217 * with a left join. Also, 200 at once seems too little.
219 * @param array $groupID
220 * @param array $values
222 public static function store($groupID, &$values) {
225 // sort the values so we put group IDs in front and hence optimize
226 // mysql storage (or so we think) CRM-9493
229 // to avoid long strings, lets do BULK_INSERT_COUNT values at a time
230 while (!empty($values)) {
232 $input = array_splice($values, 0, CRM_Core_DAO
::BULK_INSERT_COUNT
);
233 $str = implode(',', $input);
234 $sql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) VALUES $str;";
235 CRM_Core_DAO
::executeQuery($sql);
237 self
::updateCacheTime($groupID, $processed);
241 * Change the cache_date.
243 * @param array $groupID
244 * @param bool $processed
245 * Whether the cache data was recently modified.
247 public static function updateCacheTime($groupID, $processed) {
248 // only update cache entry if we had any values
250 // also update the group with cache date information
251 $now = date('YmdHis');
257 $groupIDs = implode(',', $groupID);
260 SET cache_date = $now
261 WHERE id IN ( $groupIDs )
263 CRM_Core_DAO
::executeQuery($sql);
267 * Function to clear group contact cache and reset the corresponding
268 * group's cache and refresh date
270 * @param int $groupID
273 public static function clearGroupContactCache($groupID) {
274 $transaction = new CRM_Core_Transaction();
277 FROM civicrm_group_contact_cache g
278 WHERE g.group_id = %1 ";
281 UPDATE civicrm_group g
282 SET cache_date = null
286 1 => [$groupID, 'Integer'],
289 CRM_Core_DAO
::executeQuery($query, $params);
290 // also update the cache_date for these groups
291 CRM_Core_DAO
::executeQuery($update, $params);
292 unset(self
::$_alreadyLoaded[$groupID]);
294 $transaction->commit();
298 * Refresh the smart group cache tables.
300 * This involves clearing out any aged entries (based on the site timeout setting) and resetting the time outs.
302 * This function should be called via the opportunistic or deterministic cache refresh function to make the intent
305 protected static function flushCaches() {
307 $lock = self
::getLockForRefresh();
309 catch (CRM_Core_Exception
$e) {
310 // Someone else is kindly doing the refresh for us right now.
313 $params = [1 => [self
::getCacheInvalidDateTime(), 'String']];
314 $groupsDAO = CRM_Core_DAO
::executeQuery("SELECT id FROM civicrm_group WHERE cache_date <= %1", $params);
316 while ($groupsDAO->fetch()) {
317 $expiredGroups[] = $groupsDAO->id
;
319 if (!empty($expiredGroups)) {
320 $expiredGroups = implode(',', $expiredGroups);
321 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_group_contact_cache WHERE group_id IN ({$expiredGroups})");
323 // Clear these out without resetting them because we are not building caches here, only clearing them,
324 // so the state is 'as if they had never been built'.
325 CRM_Core_DAO
::executeQuery("UPDATE civicrm_group SET cache_date = NULL WHERE id IN ({$expiredGroups})");
331 * Check if the refresh is already initiated.
333 * We have 2 imperfect methods for this:
334 * 1) a static variable in the function. This works fine within a request
335 * 2) a mysql lock. This works fine as long as CiviMail is not running, or if mysql is version 5.7+
337 * Where these 2 locks fail we get 2 processes running at the same time, but we have at least minimised that.
339 * @return \Civi\Core\Lock\LockInterface
340 * @throws \CRM_Core_Exception
342 protected static function getLockForRefresh() {
343 if (!isset(Civi
::$statics[__CLASS__
]['is_refresh_init'])) {
344 Civi
::$statics[__CLASS__
] = ['is_refresh_init' => FALSE];
347 if (Civi
::$statics[__CLASS__
]['is_refresh_init']) {
348 throw new CRM_Core_Exception('A refresh has already run in this process');
350 $lock = Civi
::lockManager()->acquire('data.core.group.refresh');
351 if ($lock->isAcquired()) {
352 Civi
::$statics[__CLASS__
]['is_refresh_init'] = TRUE;
355 throw new CRM_Core_Exception('Mysql lock unavailable');
359 * Do an opportunistic cache refresh if the site is configured for these.
361 * Sites that do not run the smart group clearing cron job should refresh the
362 * caches on demand. The user session will be forced to wait so it is less
365 public static function opportunisticCacheFlush() {
366 if (Civi
::settings()->get('smart_group_cache_refresh_mode') == 'opportunistic') {
372 * Do a forced cache refresh.
374 * This function is appropriate to be called by system jobs & non-user sessions.
376 public static function deterministicCacheFlush() {
377 if (self
::smartGroupCacheTimeout() == 0) {
378 CRM_Core_DAO
::executeQuery("TRUNCATE civicrm_group_contact_cache");
379 CRM_Core_DAO
::executeQuery("UPDATE civicrm_group SET cache_date = NULL");
387 * Remove one or more contacts from the smart group cache.
389 * @param int|array $cid
390 * @param int $groupId
393 * TRUE if successful.
395 public static function removeContact($cid, $groupId = NULL) {
398 foreach ((array) $cid as $c) {
399 $cids[] = CRM_Utils_Type
::escape($c, 'Integer');
402 $condition = count($cids) == 1 ?
"= {$cids[0]}" : "IN (" . implode(',', $cids) . ")";
404 $condition .= " AND group_id = " . CRM_Utils_Type
::escape($groupId, 'Integer');
406 $sql = "DELETE FROM civicrm_group_contact_cache WHERE contact_id $condition";
407 CRM_Core_DAO
::executeQuery($sql);
414 * Load the smart group cache for a saved search.
416 * @param object $group
417 * The smart group that needs to be loaded.
419 * Should we force a search through.
421 * @throws \CRM_Core_Exception
423 public static function load(&$group, $force = FALSE) {
424 $groupID = $group->id
;
425 $savedSearchID = $group->saved_search_id
;
426 if (array_key_exists($groupID, self
::$_alreadyLoaded) && !$force) {
430 self
::$_alreadyLoaded[$groupID] = 1;
432 // FIXME: some other process could have actually done the work before we got here,
433 // Ensure that work needs to be done before continuing
434 if (!$force && !self
::shouldGroupBeRefreshed($groupID, TRUE)) {
439 if ($savedSearchID) {
440 $ssParams = CRM_Contact_BAO_SavedSearch
::getSearchParams($savedSearchID);
441 $groupID = CRM_Utils_Type
::escape($groupID, 'Integer');
443 $excludeClause = "NOT IN (
444 SELECT contact_id FROM civicrm_group_contact
445 WHERE civicrm_group_contact.status = 'Removed'
446 AND civicrm_group_contact.group_id = $groupID )";
447 $addSelect = "$groupID AS group_id";
449 if (!empty($ssParams['api_entity'])) {
450 $sql = self
::getApiSQL($ssParams, $addSelect, $excludeClause);
453 // CRM-7021 rectify params to what proximity search expects if there is a value for prox_distance
454 if (!empty($ssParams)) {
455 CRM_Contact_BAO_ProximityQuery
::fixInputParams($ssParams);
457 if (isset($ssParams['customSearchID'])) {
458 $sql = self
::getCustomSearchSQL($savedSearchID, $ssParams, $addSelect, $excludeClause);
461 $sql = self
::getQueryObjectSQL($savedSearchID, $ssParams, $addSelect, $excludeClause);
466 $groupContactsTempTable = CRM_Utils_SQL_TempTable
::build()->setCategory('gccache')->setMemory();
467 $tempTable = $groupContactsTempTable->getName();
468 $groupContactsTempTable->createWithColumns('contact_id int, group_id int, UNIQUE UI_contact_group (contact_id,group_id)');
471 $contactQueries[] = $sql;
473 // lets also store the records that are explicitly added to the group
474 // this allows us to skip the group contact LEFT JOIN
476 "SELECT $groupID as group_id, contact_id as contact_id
477 FROM civicrm_group_contact
478 WHERE civicrm_group_contact.status = 'Added' AND civicrm_group_contact.group_id = $groupID ";
480 self
::clearGroupContactCache($groupID);
482 foreach ($contactQueries as $contactQuery) {
483 CRM_Core_DAO
::executeQuery("INSERT IGNORE INTO $tempTable (group_id, contact_id) {$contactQuery}");
486 if ($group->children
) {
488 // Store a list of contacts who are removed from the parent group
489 $sqlContactsRemovedFromGroup = "
491 FROM civicrm_group_contact
492 WHERE civicrm_group_contact.status = 'Removed'
493 AND civicrm_group_contact.group_id = $groupID ";
494 $dao = CRM_Core_DAO
::executeQuery($sqlContactsRemovedFromGroup);
495 $removed_contacts = [];
496 while ($dao->fetch()) {
497 $removed_contacts[] = $dao->contact_id
;
500 $childrenIDs = explode(',', $group->children
);
501 foreach ($childrenIDs as $childID) {
502 $contactIDs = CRM_Contact_BAO_Group
::getMember($childID, FALSE);
503 // Unset each contact that is removed from the parent group
504 foreach ($removed_contacts as $removed_contact) {
505 unset($contactIDs[$removed_contact]);
507 if (empty($contactIDs)) {
508 // This child group has no contact IDs so we don't need to add them to
512 foreach ($contactIDs as $contactID => $dontCare) {
513 $values[] = "({$groupID},{$contactID})";
515 $str = implode(',', $values);
516 CRM_Core_DAO
::executeQuery("INSERT IGNORE INTO $tempTable (group_id, contact_id) VALUES $str");
520 // grab a lock so other processes don't compete and do the same query
521 $lock = Civi
::lockManager()->acquire("data.core.group.{$groupID}");
522 if (!$lock->isAcquired()) {
523 // this can cause inconsistent results since we don't know if the other process
524 // will fill up the cache before our calling routine needs it.
525 // however this routine does not return the status either, so basically
526 // its a "lets return and hope for the best"
530 // Don't call clearGroupContactCache as we don't want to clear the cache dates
531 // The will get updated by updateCacheTime() below and not clearing the dates reduces
532 // the chance that loadAll() will try and rebuild at the same time.
535 FROM civicrm_group_contact_cache g
536 WHERE g.group_id = %1 ";
538 1 => [$groupID, 'Integer'],
540 CRM_Core_DAO
::executeQuery($clearCacheQuery, $params);
542 CRM_Core_DAO
::executeQuery(
543 "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)
544 SELECT DISTINCT contact_id, group_id FROM $tempTable
546 $groupContactsTempTable->drop();
547 self
::updateCacheTime([$groupID], TRUE);
553 * Retrieve the smart group cache timeout in minutes.
555 * This checks if a timeout has been configured. If one has then smart groups should not
556 * be refreshed more frequently than the time out. If a group was recently refreshed it should not
557 * refresh again within that period.
561 public static function smartGroupCacheTimeout() {
562 $config = CRM_Core_Config
::singleton();
565 isset($config->smartGroupCacheTimeout
) &&
566 is_numeric($config->smartGroupCacheTimeout
)
568 return $config->smartGroupCacheTimeout
;
571 // Default to 5 minutes.
576 * Get all the smart groups that this contact belongs to.
578 * Note that this could potentially be a super slow function since
579 * it ensure that all contact groups are loaded in the cache
581 * @param int $contactID
584 * an array of groups that this contact belongs to
586 public static function contactGroup(int $contactID): array {
591 SELECT gc.group_id, gc.contact_id, g.title, g.children, g.description
592 FROM civicrm_group_contact_cache gc
593 INNER JOIN civicrm_group g ON g.id = gc.group_id
594 WHERE gc.contact_id = $contactID
595 AND (g.is_hidden = 0 OR g.is_hidden IS NULL)
596 ORDER BY gc.contact_id, g.children
599 $dao = CRM_Core_DAO
::executeQuery($sql);
601 $prevContactID = NULL;
602 while ($dao->fetch()) {
605 $prevContactID != $dao->contact_id
607 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
609 $prevContactID = $dao->contact_id
;
610 if (!array_key_exists($dao->contact_id
, $contactGroup)) {
611 $contactGroup[$dao->contact_id
]
612 = ['group' => [], 'groupTitle' => []];
615 $contactGroup[$dao->contact_id
]['group'][]
617 'id' => $dao->group_id
,
618 'title' => $dao->title
,
619 'description' => $dao->description
,
620 'children' => $dao->children
,
622 $contactGroup[$dao->contact_id
]['groupTitle'][] = $dao->title
;
625 if ($prevContactID) {
626 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
629 if ((!empty($contactGroup[$contactID]))) {
630 return $contactGroup[$contactID];
632 return $contactGroup;
636 * Get the datetime from which the cache should be considered invalid.
638 * Ie if the smartgroup cache timeout is 5 minutes ago then the cache is invalid if it was
639 * refreshed 6 minutes ago, but not if it was refreshed 4 minutes ago.
643 public static function getCacheInvalidDateTime() {
644 return date('YmdHis', strtotime("-" . self
::smartGroupCacheTimeout() . " Minutes"));
648 * Invalidates the smart group cache for a particular group
649 * @param int $groupID - Group to invalidate
651 public static function invalidateGroupContactCache($groupID) {
652 CRM_Core_DAO
::executeQuery("UPDATE civicrm_group
653 SET cache_date = NULL
655 1 => [$groupID, 'Positive'],
660 * @param array $savedSearch
661 * @param string $addSelect
662 * @param string $excludeClause
664 * @throws API_Exception
665 * @throws \Civi\API\Exception\NotImplementedException
666 * @throws CRM_Core_Exception
668 protected static function getApiSQL(array $savedSearch, string $addSelect, string $excludeClause) {
669 $apiParams = $savedSearch['api_params'] +
['select' => ['id'], 'checkPermissions' => FALSE];
670 $idField = SqlExpression
::convert($apiParams['select'][0], TRUE)->getAlias();
671 // Unless there's a HAVING clause, we don't care about other columns
672 if (empty($apiParams['having'])) {
673 $apiParams['select'] = array_slice($apiParams['select'], 0, 1);
675 $api = \Civi\API\Request
::create($savedSearch['api_entity'], 'get', $apiParams);
676 $query = new \Civi\Api4\Query\
Api4SelectQuery($api);
677 $query->forceSelectId
= FALSE;
678 $query->getQuery()->having("$idField $excludeClause");
679 $sql = $query->getSql();
680 // Place sql in a nested sub-query, otherwise HAVING is impossible on any field other than contact_id
681 return "SELECT $addSelect, `$idField` AS contact_id FROM ($sql) api_query";
685 * Get sql from a custom search.
687 * We split it up and store custom class
688 * so temp tables are not destroyed if they are used
690 * @param int $savedSearchID
691 * @param array $ssParams
692 * @param string $addSelect
693 * @param string $excludeClause
698 protected static function getCustomSearchSQL($savedSearchID, array $ssParams, string $addSelect, string $excludeClause) {
699 $searchSQL = CRM_Contact_BAO_SearchCustom
::customClass($ssParams['customSearchID'], $savedSearchID)->contactIDs();
700 $searchSQL = str_replace('ORDER BY contact_a.id ASC', '', $searchSQL);
701 if (strpos($searchSQL, 'WHERE') === FALSE) {
702 $searchSQL .= " WHERE contact_a.id $excludeClause";
705 $searchSQL .= " AND contact_a.id $excludeClause";
707 return preg_replace("/^\s*SELECT /", "SELECT $addSelect, ", $searchSQL);
711 * Get array of sql from a saved query object group.
713 * @param int $savedSearchID
714 * @param array $ssParams
715 * @param string $addSelect
716 * @param string $excludeClause
719 * @throws \CRM_Core_Exception
720 * @throws \CiviCRM_API3_Exception
722 protected static function getQueryObjectSQL($savedSearchID, array $ssParams, string $addSelect, string $excludeClause) {
723 $returnProperties = NULL;
724 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) {
725 $fv = CRM_Contact_BAO_SavedSearch
::getFormValues($savedSearchID);
726 $returnProperties = CRM_Core_BAO_Mapping
::returnProperties($fv);
728 $formValues = CRM_Contact_BAO_SavedSearch
::getFormValues($savedSearchID);
729 // CRM-17075 using the formValues in this way imposes extra logic and complexity.
730 // we have the where_clause and where tables stored in the saved_search table
731 // and should use these rather than re-processing the form criteria (which over-works
732 // the link between the form layer & the query layer too).
733 // It's hard to think of when you would want to use anything other than return
734 // properties = array('contact_id' => 1) here as the point would appear to be to
735 // generate the list of contact ids in the group.
736 // @todo review this to use values in saved_search table (preferably for 4.8).
738 = new CRM_Contact_BAO_Query(
739 $ssParams, $returnProperties, NULL,
743 $formValues['display_relationship_type'] ??
NULL,
744 $formValues['operator'] ??
'AND'
746 $query->_useDistinct
= FALSE;
747 $query->_useGroupBy
= FALSE;
748 $sqlParts = $query->getSearchSQLParts(
752 "contact_a.id $excludeClause"
754 $select = preg_replace("/^\s*SELECT /", "SELECT $addSelect, ", $sqlParts['select']);
756 return "$select {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['group_by']} {$sqlParts['having']}";