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 67bf42a
Showing 1 changed file with 27 additions and 119 deletions.
146 changes: 27 additions & 119 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 All @@ -236,10 +233,10 @@ public static function add($groupID) {
* @todo review use of INSERT IGNORE. This function appears to be slower that inserting
* with a left join. Also, 200 at once seems too little.
*
* @param int $groupID
* @param array $groupID
* @param array $values
*/
public static function store(&$groupID, &$values) {
public static function store($groupID, &$values) {
$processed = FALSE;

// sort the values so we put group IDs in front and hence optimize
Expand Down Expand Up @@ -286,111 +283,20 @@ public static function updateCacheTime($groupID, $processed) {
}

/**
* Removes all the cache entries pertaining to a specific group.
*
* 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.
* @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.
*
* 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.
* Remove this function altogether by mid 2018.
*
* @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(array($groupID));

$processed = FALSE;
$tempTable = 'civicrm_temp_group_contact_cache' . rand(0, 2000);
foreach (array($sql, $sqlB) as $selectSql) {
Expand All @@ -695,7 +603,7 @@ public static function load(&$group, $force = FALSE) {
CRM_Core_DAO::executeQuery(" DROP TEMPORARY TABLE $tempTable");
}

self::updateCacheTime($groupIDs, $processed);
self::updateCacheTime(array($groupID), $processed);

if ($group->children) {

Expand Down Expand Up @@ -723,7 +631,7 @@ public static function load(&$group, $force = FALSE) {
$values[] = "({$groupID},{$contactID})";
}

self::store($groupIDs, $values);
self::store(array($groupID), $values);
}
}

Expand Down

0 comments on commit 67bf42a

Please sign in to comment.