Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | class CRM_Contact_BAO_GroupContactCache extends CRM_Contact_DAO_GroupContactCache { | |
34 | ||
35 | static $_alreadyLoaded = array(); | |
36 | ||
a8eb1fe6 TO |
37 | /** |
38 | * Get a list of caching modes. | |
39 | * | |
40 | * @return array | |
41 | */ | |
42 | public static function getModes() { | |
43 | return array( | |
44 | // Flush expired caches in response to user actions. | |
45 | 'opportunistic' => ts('Opportunistic Flush'), | |
46 | ||
47 | // Flush expired caches via background cron jobs. | |
48 | 'deterministic' => ts('Cron Flush'), | |
49 | ); | |
50 | } | |
51 | ||
6a488035 | 52 | /** |
67d19299 | 53 | * Check to see if we have cache entries for this group. |
54 | * | |
55 | * If not, regenerate, else return. | |
6a488035 | 56 | * |
adf28ffd | 57 | * @param array $groupIDs |
77c5b619 | 58 | * Of group that we are checking against. |
6a488035 | 59 | * |
5396af74 | 60 | * @return bool |
a6c01b45 | 61 | * TRUE if we did not regenerate, FALSE if we did |
6a488035 | 62 | */ |
00be9182 | 63 | public static function check($groupIDs) { |
6a488035 TO |
64 | if (empty($groupIDs)) { |
65 | return TRUE; | |
66 | } | |
67 | ||
68 | return self::loadAll($groupIDs); | |
69 | } | |
70 | ||
abdb2607 | 71 | /** |
adf28ffd | 72 | * Formulate the query to see which groups needs to be refreshed. |
73 | * | |
74 | * The calculation is based on their cache date and the smartGroupCacheTimeOut | |
abdb2607 | 75 | * |
77c5b619 TO |
76 | * @param string $groupIDClause |
77 | * The clause which limits which groups we need to evaluate. | |
78 | * @param bool $includeHiddenGroups | |
79 | * Hidden groups are excluded by default. | |
abdb2607 | 80 | * |
a6c01b45 CW |
81 | * @return string |
82 | * the sql query which lists the groups that need to be refreshed | |
abdb2607 | 83 | */ |
e60f24eb | 84 | public static function groupRefreshedClause($groupIDClause = NULL, $includeHiddenGroups = FALSE) { |
4052239b | 85 | $smartGroupCacheTimeoutDateTime = self::getCacheInvalidDateTime(); |
abdb2607 DL |
86 | |
87 | $query = " | |
88 | SELECT g.id | |
89 | FROM civicrm_group g | |
90 | WHERE ( g.saved_search_id IS NOT NULL OR g.children IS NOT NULL ) | |
91 | AND g.is_active = 1 | |
4052239b | 92 | AND ( |
93 | g.cache_date IS NULL | |
94 | OR cache_date <= $smartGroupCacheTimeoutDateTime | |
95 | OR NOW() >= g.refresh_date | |
96 | )"; | |
abdb2607 DL |
97 | |
98 | if (!$includeHiddenGroups) { | |
99 | $query .= "AND (g.is_hidden = 0 OR g.is_hidden IS NULL)"; | |
100 | } | |
101 | ||
102 | if (!empty($groupIDClause)) { | |
103 | $query .= " AND ( $groupIDClause ) "; | |
104 | } | |
105 | ||
106 | return $query; | |
107 | } | |
108 | ||
109 | /** | |
adf28ffd | 110 | * Check to see if a group has been refreshed recently. |
111 | * | |
112 | * This is primarily used in a locking scenario when some other process might have refreshed things underneath | |
abdb2607 DL |
113 | * this process |
114 | * | |
77c5b619 TO |
115 | * @param int $groupID |
116 | * The group ID. | |
117 | * @param bool $includeHiddenGroups | |
118 | * Hidden groups are excluded by default. | |
abdb2607 | 119 | * |
a6c01b45 CW |
120 | * @return string |
121 | * the sql query which lists the groups that need to be refreshed | |
abdb2607 | 122 | */ |
00be9182 | 123 | public static function shouldGroupBeRefreshed($groupID, $includeHiddenGroups = FALSE) { |
abdb2607 DL |
124 | $query = self::groupRefreshedClause("g.id = %1", $includeHiddenGroups); |
125 | $params = array(1 => array($groupID, 'Integer')); | |
126 | ||
4f53db5a | 127 | // if the query returns the group ID, it means the group is a valid candidate for refreshing |
abdb2607 DL |
128 | return CRM_Core_DAO::singleValueQuery($query, $params); |
129 | } | |
130 | ||
6a488035 | 131 | /** |
adf28ffd | 132 | * Check to see if we have cache entries for this group. |
133 | * | |
6a488035 TO |
134 | * if not, regenerate, else return |
135 | * | |
adf28ffd | 136 | * @param int|array $groupIDs groupIDs of group that we are checking against |
6a488035 | 137 | * if empty, all groups are checked |
77c5b619 TO |
138 | * @param int $limit |
139 | * Limits the number of groups we evaluate. | |
6a488035 | 140 | * |
5396af74 | 141 | * @return bool |
a6c01b45 | 142 | * TRUE if we did not regenerate, FALSE if we did |
6a488035 | 143 | */ |
e60f24eb | 144 | public static function loadAll($groupIDs = NULL, $limit = 0) { |
6a488035 TO |
145 | // ensure that all the smart groups are loaded |
146 | // this function is expensive and should be sparingly used if groupIDs is empty | |
6a488035 | 147 | if (empty($groupIDs)) { |
e60f24eb | 148 | $groupIDClause = NULL; |
481a74f4 | 149 | $groupIDs = array(); |
6a488035 TO |
150 | } |
151 | else { | |
152 | if (!is_array($groupIDs)) { | |
153 | $groupIDs = array($groupIDs); | |
154 | } | |
155 | ||
b44e3f84 | 156 | // note escapeString is a must here and we can't send the imploded value as second argument to |
6a488035 TO |
157 | // the executeQuery(), since that would put single quote around the string and such a string |
158 | // of comma separated integers would not work. | |
0e4e5a49 | 159 | $groupIDString = CRM_Core_DAO::escapeString(implode(', ', $groupIDs)); |
6a488035 | 160 | |
abdb2607 | 161 | $groupIDClause = "g.id IN ({$groupIDString})"; |
6a488035 TO |
162 | } |
163 | ||
abdb2607 | 164 | $query = self::groupRefreshedClause($groupIDClause); |
6a488035 TO |
165 | |
166 | $limitClause = $orderClause = NULL; | |
167 | if ($limit > 0) { | |
168 | $limitClause = " LIMIT 0, $limit"; | |
169 | $orderClause = " ORDER BY g.cache_date, g.refresh_date"; | |
170 | } | |
e2422b8f | 171 | // We ignore hidden groups and disabled groups |
abdb2607 | 172 | $query .= " |
6a488035 | 173 | $orderClause |
f9e16e9a | 174 | $limitClause |
6a488035 TO |
175 | "; |
176 | ||
177 | $dao = CRM_Core_DAO::executeQuery($query); | |
178 | $processGroupIDs = array(); | |
179 | $refreshGroupIDs = $groupIDs; | |
180 | while ($dao->fetch()) { | |
181 | $processGroupIDs[] = $dao->id; | |
182 | ||
183 | // remove this id from refreshGroupIDs | |
184 | foreach ($refreshGroupIDs as $idx => $gid) { | |
185 | if ($gid == $dao->id) { | |
186 | unset($refreshGroupIDs[$idx]); | |
187 | break; | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
192 | if (!empty($refreshGroupIDs)) { | |
82a837e9 | 193 | $refreshGroupIDString = CRM_Core_DAO::escapeString(implode(', ', $refreshGroupIDs)); |
a986056d | 194 | $time = self::getRefreshDateTime(); |
6a488035 TO |
195 | $query = " |
196 | UPDATE civicrm_group g | |
197 | SET g.refresh_date = $time | |
198 | WHERE g.id IN ( {$refreshGroupIDString} ) | |
199 | AND g.refresh_date IS NULL | |
200 | "; | |
82a837e9 | 201 | CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
202 | } |
203 | ||
204 | if (empty($processGroupIDs)) { | |
205 | return TRUE; | |
206 | } | |
207 | else { | |
208 | self::add($processGroupIDs); | |
209 | return FALSE; | |
210 | } | |
211 | } | |
212 | ||
86538308 | 213 | /** |
adf28ffd | 214 | * Build the smart group cache for a given group. |
215 | * | |
100fef9d | 216 | * @param int $groupID |
86538308 | 217 | */ |
00be9182 | 218 | public static function add($groupID) { |
6a488035 TO |
219 | // first delete the current cache |
220 | self::remove($groupID); | |
221 | if (!is_array($groupID)) { | |
222 | $groupID = array($groupID); | |
223 | } | |
224 | ||
225 | $returnProperties = array('contact_id'); | |
226 | foreach ($groupID as $gid) { | |
1dbdc161 | 227 | $params = array(array('group', 'IN', array($gid), 0, 0)); |
abdb2607 | 228 | // the below call updates the cache table as a byproduct of the query |
6a488035 TO |
229 | CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, FALSE); |
230 | } | |
231 | } | |
232 | ||
86538308 | 233 | /** |
adf28ffd | 234 | * Store values into the group contact cache. |
235 | * | |
236 | * @todo review use of INSERT IGNORE. This function appears to be slower that inserting | |
237 | * with a left join. Also, 200 at once seems too little. | |
238 | * | |
100fef9d | 239 | * @param int $groupID |
adf28ffd | 240 | * @param array $values |
86538308 | 241 | */ |
00be9182 | 242 | public static function store(&$groupID, &$values) { |
6a488035 TO |
243 | $processed = FALSE; |
244 | ||
245 | // sort the values so we put group IDs in front and hence optimize | |
246 | // mysql storage (or so we think) CRM-9493 | |
247 | sort($values); | |
eb917190 | 248 | |
6a488035 TO |
249 | // to avoid long strings, lets do BULK_INSERT_COUNT values at a time |
250 | while (!empty($values)) { | |
251 | $processed = TRUE; | |
353ffa53 TO |
252 | $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT); |
253 | $str = implode(',', $input); | |
254 | $sql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) VALUES $str;"; | |
6a488035 TO |
255 | CRM_Core_DAO::executeQuery($sql); |
256 | } | |
257 | self::updateCacheTime($groupID, $processed); | |
258 | } | |
259 | ||
260 | /** | |
fe482240 | 261 | * Change the cache_date. |
6a488035 | 262 | * |
5a4f6742 CW |
263 | * @param array $groupID |
264 | * @param bool $processed | |
265 | * Whether the cache data was recently modified. | |
6a488035 | 266 | */ |
00be9182 | 267 | public static function updateCacheTime($groupID, $processed) { |
6a488035 TO |
268 | // only update cache entry if we had any values |
269 | if ($processed) { | |
270 | // also update the group with cache date information | |
a986056d | 271 | $now = date('YmdHis'); |
6a488035 TO |
272 | $refresh = 'null'; |
273 | } | |
274 | else { | |
353ffa53 | 275 | $now = 'null'; |
6a488035 TO |
276 | $refresh = 'null'; |
277 | } | |
278 | ||
279 | $groupIDs = implode(',', $groupID); | |
280 | $sql = " | |
281 | UPDATE civicrm_group | |
282 | SET cache_date = $now, refresh_date = $refresh | |
283 | WHERE id IN ( $groupIDs ) | |
284 | "; | |
285 | CRM_Core_DAO::executeQuery($sql); | |
286 | } | |
287 | ||
cee4f6a1 | 288 | /** |
fe482240 | 289 | * Removes all the cache entries pertaining to a specific group. |
adf28ffd | 290 | * |
cee4f6a1 DL |
291 | * If no groupID is passed in, removes cache entries for all groups |
292 | * Has an optimization to bypass repeated invocations of this function. | |
293 | * Note that this function is an advisory, i.e. the removal respects the | |
294 | * cache date, i.e. the removal is not done if the group was recently | |
295 | * loaded into the cache. | |
296 | * | |
cc7b1cd9 | 297 | * In fact it turned out there is little overlap between the code when group is passed in |
298 | * and group is not so it makes more sense as separate functions. | |
299 | * | |
300 | * @todo remove last call to this function from outside the class then make function protected, | |
301 | * enforce groupID as an array & remove non group handling. | |
302 | * | |
4ebc8d27 | 303 | * @param int $groupIDs |
5a4f6742 CW |
304 | * the groupID to delete cache entries, NULL for all groups. |
305 | * @param bool $onceOnly | |
306 | * run the function exactly once for all groups. | |
cee4f6a1 | 307 | */ |
4ebc8d27 | 308 | public static function remove($groupIDs = NULL, $onceOnly = TRUE) { |
6a488035 TO |
309 | static $invoked = FALSE; |
310 | ||
311 | // typically this needs to happy only once per instance | |
4052239b | 312 | // this is especially TRUE in import, where we don't need |
6a488035 TO |
313 | // to do this all the time |
314 | // this optimization is done only when no groupID is passed | |
315 | // i.e. cache is reset for all groups | |
abdb2607 DL |
316 | if ( |
317 | $onceOnly && | |
6a488035 | 318 | $invoked && |
4ebc8d27 | 319 | $groupIDs == NULL |
6a488035 TO |
320 | ) { |
321 | return; | |
322 | } | |
323 | ||
4ebc8d27 | 324 | if ($groupIDs == NULL) { |
6a488035 | 325 | $invoked = TRUE; |
0db6c3e1 | 326 | } |
4ebc8d27 | 327 | elseif (is_array($groupIDs)) { |
328 | foreach ($groupIDs as $gid) { | |
6a488035 | 329 | unset(self::$_alreadyLoaded[$gid]); |
abdb2607 | 330 | } |
0db6c3e1 | 331 | } |
4ebc8d27 | 332 | elseif ($groupIDs && array_key_exists($groupIDs, self::$_alreadyLoaded)) { |
333 | unset(self::$_alreadyLoaded[$groupIDs]); | |
6a488035 TO |
334 | } |
335 | ||
e60f24eb | 336 | $refresh = NULL; |
6a488035 | 337 | $smartGroupCacheTimeout = self::smartGroupCacheTimeout(); |
4052239b | 338 | $params = array( |
339 | 1 => array(self::getCacheInvalidDateTime(), 'String'), | |
340 | 2 => array(self::getRefreshDateTime(), 'String'), | |
341 | ); | |
6a488035 | 342 | |
4ebc8d27 | 343 | if (!isset($groupIDs)) { |
6a488035 TO |
344 | if ($smartGroupCacheTimeout == 0) { |
345 | $query = " | |
3a5b8ace | 346 | DELETE FROM civicrm_group_contact_cache |
6a488035 TO |
347 | "; |
348 | $update = " | |
349 | UPDATE civicrm_group g | |
350 | SET cache_date = null, | |
351 | refresh_date = null | |
352 | "; | |
353 | } | |
354 | else { | |
355 | $query = " | |
356 | DELETE gc | |
357 | FROM civicrm_group_contact_cache gc | |
358 | INNER JOIN civicrm_group g ON g.id = gc.group_id | |
9fd7ef4f | 359 | WHERE g.cache_date <= %1 |
6a488035 TO |
360 | "; |
361 | $update = " | |
362 | UPDATE civicrm_group g | |
363 | SET cache_date = null, | |
364 | refresh_date = null | |
9fd7ef4f | 365 | WHERE g.cache_date <= %1 |
6a488035 TO |
366 | "; |
367 | $refresh = " | |
368 | UPDATE civicrm_group g | |
4052239b | 369 | SET refresh_date = %2 |
370 | WHERE g.cache_date < %1 | |
6a488035 TO |
371 | AND refresh_date IS NULL |
372 | "; | |
373 | } | |
4ebc8d27 | 374 | |
375 | CRM_Core_DAO::executeQuery($query, $params); | |
376 | if ($refresh) { | |
377 | CRM_Core_DAO::executeQuery($refresh, $params); | |
378 | } | |
379 | // also update the cache_date for these groups | |
380 | CRM_Core_DAO::executeQuery($update, $params); | |
6a488035 TO |
381 | } |
382 | else { | |
4ebc8d27 | 383 | foreach ((array) $groupIDs as $groupID) { |
384 | self::clearGroupContactCache($groupID); | |
385 | } | |
6a488035 | 386 | } |
4ebc8d27 | 387 | } |
6a488035 | 388 | |
4ebc8d27 | 389 | /** |
390 | * Function to clear group contact cache and reset the corresponding | |
391 | * group's cache and refresh date | |
392 | * | |
ffb47ce2 | 393 | * @param int $groupID |
4ebc8d27 | 394 | * |
395 | */ | |
396 | public static function clearGroupContactCache($groupID) { | |
397 | $transaction = new CRM_Core_Transaction(); | |
398 | $query = " | |
399 | DELETE g | |
400 | FROM civicrm_group_contact_cache g | |
401 | WHERE g.group_id = %1 "; | |
6a488035 | 402 | |
4ebc8d27 | 403 | $update = " |
404 | UPDATE civicrm_group g | |
405 | SET cache_date = null, refresh_date = null | |
406 | WHERE id = %1 "; | |
407 | ||
408 | $params = array( | |
409 | 1 => array($groupID, 'Integer'), | |
410 | ); | |
6a488035 | 411 | |
4ebc8d27 | 412 | CRM_Core_DAO::executeQuery($query, $params); |
6a488035 TO |
413 | // also update the cache_date for these groups |
414 | CRM_Core_DAO::executeQuery($update, $params); | |
4ebc8d27 | 415 | |
416 | $transaction->commit(); | |
6a488035 TO |
417 | } |
418 | ||
801bafd7 | 419 | /** |
420 | * Refresh the smart group cache tables. | |
421 | * | |
422 | * This involves clearing out any aged entries (based on the site timeout setting) and resetting the time outs. | |
423 | * | |
424 | * This function should be called via the opportunistic or deterministic cache refresh function to make the intent | |
425 | * clear. | |
426 | */ | |
2b68a50c | 427 | protected static function flushCaches() { |
cc7b1cd9 | 428 | try { |
429 | $lock = self::getLockForRefresh(); | |
430 | } | |
431 | catch (CRM_Core_Exception $e) { | |
432 | // Someone else is kindly doing the refresh for us right now. | |
801bafd7 | 433 | return; |
434 | } | |
801bafd7 | 435 | $params = array(1 => array(self::getCacheInvalidDateTime(), 'String')); |
cc7b1cd9 | 436 | // @todo this is consistent with previous behaviour but as the first query could take several seconds the second |
437 | // could become inaccurate. It seems to make more sense to fetch them first & delete from an array (which would | |
438 | // also reduce joins). If we do this we should also consider how best to iterate the groups. If we do them one at | |
439 | // a time we could call a hook, allowing people to manage the frequency on their groups, or possibly custom searches | |
440 | // might do that too. However, for 2000 groups that's 2000 iterations. If we do all once we potentially create a | |
441 | // slow query. It's worth noting the speed issue generally relates to the size of the group but if one slow group | |
442 | // is in a query with 500 fast ones all 500 get locked. One approach might be to calculate group size or the | |
443 | // number of groups & then process all at once or many query runs depending on what is found. Of course those | |
444 | // preliminary queries would need speed testing. | |
801bafd7 | 445 | CRM_Core_DAO::executeQuery( |
446 | " | |
447 | DELETE gc | |
448 | FROM civicrm_group_contact_cache gc | |
449 | INNER JOIN civicrm_group g ON g.id = gc.group_id | |
450 | WHERE g.cache_date <= %1 | |
451 | ", | |
452 | $params | |
453 | ); | |
454 | ||
cc7b1cd9 | 455 | // Clear these out without resetting them because we are not building caches here, only clearing them, |
456 | // so the state is 'as if they had never been built'. | |
801bafd7 | 457 | CRM_Core_DAO::executeQuery( |
458 | " | |
459 | UPDATE civicrm_group g | |
cc7b1cd9 | 460 | SET cache_date = NULL, |
461 | refresh_date = NULL | |
801bafd7 | 462 | WHERE g.cache_date <= %1 |
463 | ", | |
464 | $params | |
465 | ); | |
cc7b1cd9 | 466 | $lock->release(); |
801bafd7 | 467 | } |
468 | ||
469 | /** | |
470 | * Check if the refresh is already initiated. | |
471 | * | |
472 | * We have 2 imperfect methods for this: | |
473 | * 1) a static variable in the function. This works fine within a request | |
474 | * 2) a mysql lock. This works fine as long as CiviMail is not running, or if mysql is version 5.7+ | |
475 | * | |
476 | * Where these 2 locks fail we get 2 processes running at the same time, but we have at least minimised that. | |
cc7b1cd9 | 477 | * |
478 | * @return \Civi\Core\Lock\LockInterface | |
479 | * @throws \CRM_Core_Exception | |
801bafd7 | 480 | */ |
cc7b1cd9 | 481 | protected static function getLockForRefresh() { |
482 | if (!isset(Civi::$statics[__CLASS__])) { | |
483 | Civi::$statics[__CLASS__] = array('is_refresh_init' => FALSE); | |
484 | } | |
485 | ||
486 | if (Civi::$statics[__CLASS__]['is_refresh_init']) { | |
487 | throw new CRM_Core_Exception('A refresh has already run in this process'); | |
801bafd7 | 488 | } |
489 | $lock = Civi::lockManager()->acquire('data.core.group.refresh'); | |
cc7b1cd9 | 490 | if ($lock->isAcquired()) { |
491 | Civi::$statics[__CLASS__]['is_refresh_init'] = TRUE; | |
492 | return $lock; | |
801bafd7 | 493 | } |
cc7b1cd9 | 494 | throw new CRM_Core_Exception('Mysql lock unavailable'); |
801bafd7 | 495 | } |
496 | ||
497 | /** | |
498 | * Do an opportunistic cache refresh if the site is configured for these. | |
499 | * | |
e047612e CB |
500 | * Sites that do not run the smart group clearing cron job should refresh the |
501 | * caches on demand. The user session will be forced to wait so it is less | |
502 | * ideal. | |
801bafd7 | 503 | */ |
2b68a50c | 504 | public static function opportunisticCacheFlush() { |
cc7b1cd9 | 505 | if (Civi::settings()->get('smart_group_cache_refresh_mode') == 'opportunistic') { |
2b68a50c | 506 | self::flushCaches(); |
801bafd7 | 507 | } |
508 | } | |
509 | ||
510 | /** | |
511 | * Do a forced cache refresh. | |
512 | * | |
513 | * This function is appropriate to be called by system jobs & non-user sessions. | |
801bafd7 | 514 | */ |
2b68a50c | 515 | public static function deterministicCacheFlush() { |
801bafd7 | 516 | if (self::smartGroupCacheTimeout() == 0) { |
517 | CRM_Core_DAO::executeQuery("TRUNCATE civicrm_group_contact_cache"); | |
518 | CRM_Core_DAO::executeQuery(" | |
519 | UPDATE civicrm_group g | |
520 | SET cache_date = null, refresh_date = null"); | |
521 | } | |
522 | else { | |
2b68a50c | 523 | self::flushCaches(); |
801bafd7 | 524 | } |
525 | } | |
526 | ||
2c6bbd06 | 527 | /** |
adf28ffd | 528 | * Remove one or more contacts from the smart group cache. |
529 | * | |
2c6bbd06 CW |
530 | * @param int|array $cid |
531 | * @param int $groupId | |
adf28ffd | 532 | * |
4eeb9a5b TO |
533 | * @return bool |
534 | * TRUE if successful. | |
2c6bbd06 | 535 | */ |
00be9182 | 536 | public static function removeContact($cid, $groupId = NULL) { |
2c6bbd06 CW |
537 | $cids = array(); |
538 | // sanitize input | |
539 | foreach ((array) $cid as $c) { | |
540 | $cids[] = CRM_Utils_Type::escape($c, 'Integer'); | |
541 | } | |
542 | if ($cids) { | |
543 | $condition = count($cids) == 1 ? "= {$cids[0]}" : "IN (" . implode(',', $cids) . ")"; | |
544 | if ($groupId) { | |
545 | $condition .= " AND group_id = " . CRM_Utils_Type::escape($groupId, 'Integer'); | |
546 | } | |
547 | $sql = "DELETE FROM civicrm_group_contact_cache WHERE contact_id $condition"; | |
548 | CRM_Core_DAO::executeQuery($sql); | |
549 | return TRUE; | |
550 | } | |
551 | return FALSE; | |
552 | } | |
553 | ||
6a488035 | 554 | /** |
fe482240 | 555 | * Load the smart group cache for a saved search. |
abdb2607 | 556 | * |
77c5b619 TO |
557 | * @param object $group |
558 | * The smart group that needs to be loaded. | |
559 | * @param bool $force | |
560 | * Should we force a search through. | |
6a488035 | 561 | */ |
00be9182 | 562 | public static function load(&$group, $force = FALSE) { |
6a488035 TO |
563 | $groupID = $group->id; |
564 | $savedSearchID = $group->saved_search_id; | |
abdb2607 | 565 | if (array_key_exists($groupID, self::$_alreadyLoaded) && !$force) { |
6a488035 TO |
566 | return; |
567 | } | |
cc13551d | 568 | |
4052239b | 569 | // grab a lock so other processes don't compete and do the same query |
83617886 | 570 | $lock = Civi::lockManager()->acquire("data.core.group.{$groupID}"); |
cc13551d | 571 | if (!$lock->isAcquired()) { |
4052239b | 572 | // this can cause inconsistent results since we don't know if the other process |
cc13551d DL |
573 | // will fill up the cache before our calling routine needs it. |
574 | // however this routine does not return the status either, so basically | |
575 | // its a "lets return and hope for the best" | |
576 | return; | |
577 | } | |
578 | ||
6a488035 | 579 | self::$_alreadyLoaded[$groupID] = 1; |
abdb2607 | 580 | |
b44e3f84 | 581 | // we now have the lock, but some other process could have actually done the work |
abdb2607 DL |
582 | // before we got here, so before we do any work, lets ensure that work needs to be |
583 | // done | |
584 | // we allow hidden groups here since we dont know if the caller wants to evaluate an | |
585 | // hidden group | |
3cd86345 | 586 | if (!$force && !self::shouldGroupBeRefreshed($groupID, TRUE)) { |
abdb2607 DL |
587 | $lock->release(); |
588 | return; | |
589 | } | |
590 | ||
353ffa53 TO |
591 | $sql = NULL; |
592 | $idName = 'id'; | |
6a488035 TO |
593 | $customClass = NULL; |
594 | if ($savedSearchID) { | |
595 | $ssParams = CRM_Contact_BAO_SavedSearch::getSearchParams($savedSearchID); | |
596 | ||
597 | // rectify params to what proximity search expects if there is a value for prox_distance | |
598 | // CRM-7021 | |
599 | if (!empty($ssParams)) { | |
600 | CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams); | |
601 | } | |
602 | ||
6a488035 TO |
603 | $returnProperties = array(); |
604 | if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) { | |
605 | $fv = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID); | |
606 | $returnProperties = CRM_Core_BAO_Mapping::returnProperties($fv); | |
607 | } | |
608 | ||
609 | if (isset($ssParams['customSearchID'])) { | |
610 | // if custom search | |
611 | ||
612 | // we split it up and store custom class | |
613 | // so temp tables are not destroyed if they are used | |
614 | // hence customClass is defined above at top of function | |
5396af74 | 615 | $customClass = CRM_Contact_BAO_SearchCustom::customClass($ssParams['customSearchID'], $savedSearchID); |
6a488035 | 616 | $searchSQL = $customClass->contactIDs(); |
26a9b6ab | 617 | $searchSQL = str_replace('ORDER BY contact_a.id ASC', '', $searchSQL); |
d02c030f | 618 | if (!strstr($searchSQL, 'WHERE')) { |
619 | $searchSQL .= " WHERE ( 1 ) "; | |
620 | } | |
6a488035 TO |
621 | $idName = 'contact_id'; |
622 | } | |
623 | else { | |
624 | $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID); | |
beccb90f | 625 | // CRM-17075 using the formValues in this way imposes extra logic and complexity. |
626 | // we have the where_clause and where tables stored in the saved_search table | |
627 | // and should use these rather than re-processing the form criteria (which over-works | |
628 | // the link between the form layer & the query layer too). | |
629 | // It's hard to think of when you would want to use anything other than return | |
630 | // properties = array('contact_id' => 1) here as the point would appear to be to | |
631 | // generate the list of contact ids in the group. | |
632 | // @todo review this to use values in saved_search table (preferably for 4.8). | |
5396af74 | 633 | $query |
634 | = new CRM_Contact_BAO_Query( | |
6a488035 | 635 | $ssParams, $returnProperties, NULL, |
cc13551d DL |
636 | FALSE, FALSE, 1, |
637 | TRUE, TRUE, | |
638 | FALSE, | |
6a488035 TO |
639 | CRM_Utils_Array::value('display_relationship_type', $formValues), |
640 | CRM_Utils_Array::value('operator', $formValues, 'AND') | |
353ffa53 | 641 | ); |
6a488035 | 642 | $query->_useDistinct = FALSE; |
353ffa53 | 643 | $query->_useGroupBy = FALSE; |
5396af74 | 644 | $searchSQL |
645 | = $query->searchQuery( | |
6a488035 | 646 | 0, 0, NULL, |
353ffa53 TO |
647 | FALSE, FALSE, |
648 | FALSE, TRUE, | |
649 | TRUE, | |
650 | NULL, NULL, NULL, | |
651 | TRUE | |
652 | ); | |
6a488035 TO |
653 | } |
654 | $groupID = CRM_Utils_Type::escape($groupID, 'Integer'); | |
655 | $sql = $searchSQL . " AND contact_a.id NOT IN ( | |
656 | SELECT contact_id FROM civicrm_group_contact | |
657 | WHERE civicrm_group_contact.status = 'Removed' | |
658 | AND civicrm_group_contact.group_id = $groupID ) "; | |
659 | } | |
660 | ||
661 | if ($sql) { | |
662 | $sql = preg_replace("/^\s*SELECT/", "SELECT $groupID as group_id, ", $sql); | |
663 | } | |
664 | ||
665 | // lets also store the records that are explicitly added to the group | |
666 | // this allows us to skip the group contact LEFT JOIN | |
667 | $sqlB = " | |
668 | SELECT $groupID as group_id, contact_id as $idName | |
669 | FROM civicrm_group_contact | |
670 | WHERE civicrm_group_contact.status = 'Added' | |
671 | AND civicrm_group_contact.group_id = $groupID "; | |
672 | ||
673 | $groupIDs = array($groupID); | |
674 | self::remove($groupIDs); | |
abdb2607 | 675 | $processed = FALSE; |
353ffa53 | 676 | $tempTable = 'civicrm_temp_group_contact_cache' . rand(0, 2000); |
6a488035 TO |
677 | foreach (array($sql, $sqlB) as $selectSql) { |
678 | if (!$selectSql) { | |
679 | continue; | |
680 | } | |
b8eae4bd | 681 | $insertSql = "CREATE TEMPORARY TABLE $tempTable ($selectSql);"; |
eb917190 | 682 | $processed = TRUE; |
801bafd7 | 683 | CRM_Core_DAO::executeQuery($insertSql); |
b8eae4bd | 684 | CRM_Core_DAO::executeQuery( |
685 | "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id) | |
eab13399 | 686 | SELECT DISTINCT $idName, group_id FROM $tempTable |
b8eae4bd | 687 | "); |
5467c9fb | 688 | CRM_Core_DAO::executeQuery(" DROP TEMPORARY TABLE $tempTable"); |
6a488035 | 689 | } |
b8eae4bd | 690 | |
6a488035 TO |
691 | self::updateCacheTime($groupIDs, $processed); |
692 | ||
693 | if ($group->children) { | |
694 | ||
695 | //Store a list of contacts who are removed from the parent group | |
696 | $sql = " | |
697 | SELECT contact_id | |
698 | FROM civicrm_group_contact | |
699 | WHERE civicrm_group_contact.status = 'Removed' | |
700 | AND civicrm_group_contact.group_id = $groupID "; | |
701 | $dao = CRM_Core_DAO::executeQuery($sql); | |
702 | $removed_contacts = array(); | |
703 | while ($dao->fetch()) { | |
704 | $removed_contacts[] = $dao->contact_id; | |
705 | } | |
706 | ||
707 | $childrenIDs = explode(',', $group->children); | |
708 | foreach ($childrenIDs as $childID) { | |
709 | $contactIDs = CRM_Contact_BAO_Group::getMember($childID, FALSE); | |
710 | //Unset each contact that is removed from the parent group | |
711 | foreach ($removed_contacts as $removed_contact) { | |
712 | unset($contactIDs[$removed_contact]); | |
713 | } | |
714 | $values = array(); | |
715 | foreach ($contactIDs as $contactID => $dontCare) { | |
716 | $values[] = "({$groupID},{$contactID})"; | |
717 | } | |
718 | ||
719 | self::store($groupIDs, $values); | |
720 | } | |
721 | } | |
cc13551d DL |
722 | |
723 | $lock->release(); | |
6a488035 TO |
724 | } |
725 | ||
86538308 | 726 | /** |
adf28ffd | 727 | * Retrieve the smart group cache timeout in minutes. |
728 | * | |
729 | * This checks if a timeout has been configured. If one has then smart groups should not | |
730 | * be refreshed more frequently than the time out. If a group was recently refreshed it should not | |
731 | * refresh again within that period. | |
732 | * | |
86538308 EM |
733 | * @return int |
734 | */ | |
00be9182 | 735 | public static function smartGroupCacheTimeout() { |
6a488035 TO |
736 | $config = CRM_Core_Config::singleton(); |
737 | ||
738 | if ( | |
739 | isset($config->smartGroupCacheTimeout) && | |
9bdcddd7 | 740 | is_numeric($config->smartGroupCacheTimeout) |
353ffa53 | 741 | ) { |
6a488035 TO |
742 | return $config->smartGroupCacheTimeout; |
743 | } | |
744 | ||
adf28ffd | 745 | // Default to 5 minutes. |
6a488035 TO |
746 | return 5; |
747 | } | |
748 | ||
9be31c7a | 749 | /** |
fe482240 | 750 | * Get all the smart groups that this contact belongs to. |
adf28ffd | 751 | * |
9be31c7a DL |
752 | * Note that this could potentially be a super slow function since |
753 | * it ensure that all contact groups are loaded in the cache | |
754 | * | |
77c5b619 TO |
755 | * @param int $contactID |
756 | * @param bool $showHidden | |
757 | * Hidden groups are shown only if this flag is set. | |
9be31c7a | 758 | * |
a6c01b45 CW |
759 | * @return array |
760 | * an array of groups that this contact belongs to | |
9be31c7a | 761 | */ |
00be9182 | 762 | public static function contactGroup($contactID, $showHidden = FALSE) { |
6a488035 | 763 | if (empty($contactID)) { |
5396af74 | 764 | return NULL; |
6a488035 TO |
765 | } |
766 | ||
767 | if (is_array($contactID)) { | |
768 | $contactIDs = $contactID; | |
769 | } | |
770 | else { | |
771 | $contactIDs = array($contactID); | |
772 | } | |
773 | ||
774 | self::loadAll(); | |
775 | ||
9be31c7a DL |
776 | $hiddenClause = ''; |
777 | if (!$showHidden) { | |
778 | $hiddenClause = ' AND (g.is_hidden = 0 OR g.is_hidden IS NULL) '; | |
779 | } | |
780 | ||
6a488035 TO |
781 | $contactIDString = CRM_Core_DAO::escapeString(implode(', ', $contactIDs)); |
782 | $sql = " | |
783 | SELECT gc.group_id, gc.contact_id, g.title, g.children, g.description | |
784 | FROM civicrm_group_contact_cache gc | |
785 | INNER JOIN civicrm_group g ON g.id = gc.group_id | |
786 | WHERE gc.contact_id IN ($contactIDString) | |
9be31c7a | 787 | $hiddenClause |
6a488035 TO |
788 | ORDER BY gc.contact_id, g.children |
789 | "; | |
790 | ||
791 | $dao = CRM_Core_DAO::executeQuery($sql); | |
792 | $contactGroup = array(); | |
e60f24eb | 793 | $prevContactID = NULL; |
6a488035 TO |
794 | while ($dao->fetch()) { |
795 | if ( | |
796 | $prevContactID && | |
797 | $prevContactID != $dao->contact_id | |
798 | ) { | |
799 | $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']); | |
800 | } | |
801 | $prevContactID = $dao->contact_id; | |
802 | if (!array_key_exists($dao->contact_id, $contactGroup)) { | |
5396af74 | 803 | $contactGroup[$dao->contact_id] |
804 | = array('group' => array(), 'groupTitle' => array()); | |
6a488035 TO |
805 | } |
806 | ||
5396af74 | 807 | $contactGroup[$dao->contact_id]['group'][] |
808 | = array( | |
6a488035 TO |
809 | 'id' => $dao->group_id, |
810 | 'title' => $dao->title, | |
811 | 'description' => $dao->description, | |
21dfd5f5 | 812 | 'children' => $dao->children, |
6a488035 TO |
813 | ); |
814 | $contactGroup[$dao->contact_id]['groupTitle'][] = $dao->title; | |
815 | } | |
816 | ||
817 | if ($prevContactID) { | |
818 | $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']); | |
819 | } | |
820 | ||
13982f7b | 821 | if ((!empty($contactGroup[$contactID]) && is_numeric($contactID))) { |
6a488035 TO |
822 | return $contactGroup[$contactID]; |
823 | } | |
824 | else { | |
825 | return $contactGroup; | |
826 | } | |
827 | } | |
828 | ||
4052239b | 829 | /** |
830 | * Get the datetime from which the cache should be considered invalid. | |
831 | * | |
832 | * Ie if the smartgroup cache timeout is 5 minutes ago then the cache is invalid if it was | |
833 | * refreshed 6 minutes ago, but not if it was refreshed 4 minutes ago. | |
834 | * | |
835 | * @return string | |
836 | */ | |
837 | public static function getCacheInvalidDateTime() { | |
8b6074a7 | 838 | return date('YmdHis', strtotime("-" . self::smartGroupCacheTimeout() . " Minutes")); |
4052239b | 839 | } |
840 | ||
a986056d | 841 | /** |
842 | * Get the date when the cache should be refreshed from. | |
843 | * | |
844 | * Ie. now + the offset & we will delete anything prior to then. | |
845 | * | |
846 | * @return string | |
847 | */ | |
4052239b | 848 | public static function getRefreshDateTime() { |
8b6074a7 | 849 | return date('YmdHis', strtotime("+ " . self::smartGroupCacheTimeout() . " Minutes")); |
a986056d | 850 | } |
851 | ||
6a488035 | 852 | } |