Skip to content

Commit

Permalink
CRM-21109 clean up remove function to more or less no longer exist.
Browse files Browse the repository at this point in the history
Almost all the code in the function was to support a path no longer used in core, or seamingly anywhere
outside CiviHR.
  • Loading branch information
eileenmcnaughton committed Sep 20, 2017
1 parent 0626851 commit 7b6638f
Showing 1 changed file with 23 additions and 115 deletions.
138 changes: 23 additions & 115 deletions CRM/Contact/BAO/GroupContactCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,19 @@ public static function loadAll($groupIDs = NULL, $limit = 0) {
}

/**
* Build the smart group cache for a given group.
* Build the smart group cache for given groups.
*
* @param int $groupID
* @param array $groupIDs
*/
public static function add($groupID) {
// first delete the current cache
self::remove($groupID);
if (!is_array($groupID)) {
$groupID = array($groupID);
}
public static function add($groupIDs) {
$groupIDs = (array) $groupIDs;

$returnProperties = array('contact_id');
foreach ($groupID as $gid) {
$params = array(array('group', 'IN', array($gid), 0, 0));
foreach ($groupIDs as $groupID) {
// first delete the current cache
self::clearGroupContactCache($groupID);
$params = array(array('group', 'IN', array($groupID), 0, 0));
// the below call updates the cache table as a byproduct of the query
CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, FALSE);
CRM_Contact_BAO_Query::apiQuery($params, array('contact_id'), NULL, NULL, 0, 0, FALSE);
}
}

Expand Down Expand Up @@ -286,111 +283,20 @@ public static function updateCacheTime($groupID, $processed) {
}

/**
* Removes all the cache entries pertaining to a specific group.
* @deprecated function - the best function to call is
* CRM_Contact_BAO_Contact::updateContactCache at the moment, or api job.group_cache_flush
* to really force a flush.
*
* If no groupID is passed in, removes cache entries for all groups
* Has an optimization to bypass repeated invocations of this function.
* Note that this function is an advisory, i.e. the removal respects the
* cache date, i.e. the removal is not done if the group was recently
* loaded into the cache.
* Remove this function altogether by mid 2018.
*
* In fact it turned out there is little overlap between the code when group is passed in
* and group is not so it makes more sense as separate functions.
*
* @todo enforce groupID as an array & remove non group handling.
* Use flushCaches when no group id provided.
*
* @param int $groupIDs
* the groupID to delete cache entries, NULL for all groups.
* @param bool $onceOnly
* run the function exactly once for all groups.
* However, if updating code outside core to use this (or any BAO function) it is recommended that
* you add an api call to lock in into our contract. Currently there is not really a supported
* method for non core functions.
*/
public static function remove($groupIDs = NULL, $onceOnly = TRUE) {
if (!is_array($groupIDs)) {
Civi::log()
->warning('Deprecated code. This function should not be called without groupIDs. Extensions can use the api job.group_cache_flush', array('civi.tag' => 'deprecated'));
return;
}
if (!isset(Civi::$statics[__CLASS__]['remove_invoked'])) {
Civi::$statics[__CLASS__] = array('remove_invoked' => FALSE);
}

// typically this needs to happy only once per instance
// this is especially TRUE in import, where we don't need
// to do this all the time
// this optimization is done only when no groupID is passed
// i.e. cache is reset for all groups
if (
$onceOnly &&
Civi::$statics[__CLASS__]['remove_invoked'] &&
$groupIDs == NULL
) {
return;
}

if ($groupIDs == NULL) {
Civi::$statics[__CLASS__]['remove_invoked'] = TRUE;
}
elseif (is_array($groupIDs)) {
foreach ($groupIDs as $gid) {
unset(self::$_alreadyLoaded[$gid]);
}
}
elseif ($groupIDs && array_key_exists($groupIDs, self::$_alreadyLoaded)) {
unset(self::$_alreadyLoaded[$groupIDs]);
}

$refresh = NULL;
$smartGroupCacheTimeout = self::smartGroupCacheTimeout();
$params = array(
1 => array(self::getCacheInvalidDateTime(), 'String'),
2 => array(self::getRefreshDateTime(), 'String'),
);

if (!isset($groupIDs)) {
if ($smartGroupCacheTimeout == 0) {
$query = "
DELETE FROM civicrm_group_contact_cache
";
$update = "
UPDATE civicrm_group g
SET cache_date = null,
refresh_date = null
";
}
else {
$query = "
DELETE gc
FROM civicrm_group_contact_cache gc
INNER JOIN civicrm_group g ON g.id = gc.group_id
WHERE g.cache_date <= %1
";
$update = "
UPDATE civicrm_group g
SET cache_date = null,
refresh_date = null
WHERE g.cache_date <= %1
";
$refresh = "
UPDATE civicrm_group g
SET refresh_date = %2
WHERE g.cache_date < %1
AND refresh_date IS NULL
";
}

CRM_Core_DAO::executeQuery($query, $params);
if ($refresh) {
CRM_Core_DAO::executeQuery($refresh, $params);
}
// also update the cache_date for these groups
CRM_Core_DAO::executeQuery($update, $params);
}
else {
foreach ((array) $groupIDs as $groupID) {
self::clearGroupContactCache($groupID);
}
}
public static function remove() {
Civi::log()
->warning('Deprecated code. This function should not be called without groupIDs. Extensions can use the api job.group_cache_flush for a hard flush or add an api option for soft flush', array('civi.tag' => 'deprecated'));
CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
}

/**
Expand Down Expand Up @@ -419,6 +325,7 @@ public static function clearGroupContactCache($groupID) {
CRM_Core_DAO::executeQuery($query, $params);
// also update the cache_date for these groups
CRM_Core_DAO::executeQuery($update, $params);
unset(self::$_alreadyLoaded[$groupID]);

$transaction->commit();
}
Expand Down Expand Up @@ -678,7 +585,8 @@ public static function load(&$group, $force = FALSE) {
AND civicrm_group_contact.group_id = $groupID ";

$groupIDs = array($groupID);
self::remove($groupIDs);
self::clearGroupContactCache($groupID);

$processed = FALSE;
$tempTable = 'civicrm_temp_group_contact_cache' . rand(0, 2000);
foreach (array($sql, $sqlB) as $selectSql) {
Expand Down

0 comments on commit 7b6638f

Please sign in to comment.