Merge pull request #6680 from colemanw/cleanup
[civicrm-core.git] / CRM / Contact / BAO / GroupContactCache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33 class CRM_Contact_BAO_GroupContactCache extends CRM_Contact_DAO_GroupContactCache {
34
35 static $_alreadyLoaded = array();
36
37 /**
38 * Check to see if we have cache entries for this group.
39 *
40 * If not, regenerate, else return.
41 *
42 * @param $groupIDs
43 * Of group that we are checking against.
44 *
45 * @return bool
46 * TRUE if we did not regenerate, FALSE if we did
47 */
48 public static function check($groupIDs) {
49 if (empty($groupIDs)) {
50 return TRUE;
51 }
52
53 return self::loadAll($groupIDs);
54 }
55
56 /**
57 * Common function that formulates the query to see which groups needs to be refreshed
58 * based on their cache date and the smartGroupCacheTimeOut
59 *
60 * @param string $groupIDClause
61 * The clause which limits which groups we need to evaluate.
62 * @param bool $includeHiddenGroups
63 * Hidden groups are excluded by default.
64 *
65 * @return string
66 * the sql query which lists the groups that need to be refreshed
67 */
68 public static function groupRefreshedClause($groupIDClause = NULL, $includeHiddenGroups = FALSE) {
69 $smartGroupCacheTimeout = self::smartGroupCacheTimeout();
70 $now = CRM_Utils_Date::getUTCTime();
71
72 $query = "
73 SELECT g.id
74 FROM civicrm_group g
75 WHERE ( g.saved_search_id IS NOT NULL OR g.children IS NOT NULL )
76 AND g.is_active = 1
77 AND ( g.cache_date IS NULL OR
78 ( TIMESTAMPDIFF(MINUTE, g.cache_date, $now) >= $smartGroupCacheTimeout ) OR
79 ( $now >= g.refresh_date )
80 )
81 ";
82
83 if (!$includeHiddenGroups) {
84 $query .= "AND (g.is_hidden = 0 OR g.is_hidden IS NULL)";
85 }
86
87 if (!empty($groupIDClause)) {
88 $query .= " AND ( $groupIDClause ) ";
89 }
90
91 return $query;
92 }
93
94 /**
95 * Checks to see if a group has been refreshed recently. This is primarily used
96 * in a locking scenario when some other process might have refreshed things underneath
97 * this process
98 *
99 * @param int $groupID
100 * The group ID.
101 * @param bool $includeHiddenGroups
102 * Hidden groups are excluded by default.
103 *
104 * @return string
105 * the sql query which lists the groups that need to be refreshed
106 */
107 public static function shouldGroupBeRefreshed($groupID, $includeHiddenGroups = FALSE) {
108 $query = self::groupRefreshedClause("g.id = %1", $includeHiddenGroups);
109 $params = array(1 => array($groupID, 'Integer'));
110
111 // if the query returns the group ID, it means the group is a valid candidate for refreshing
112 return CRM_Core_DAO::singleValueQuery($query, $params);
113 }
114
115 /**
116 * Check to see if we have cache entries for this group
117 * if not, regenerate, else return
118 *
119 * @param int /array $groupIDs groupIDs of group that we are checking against
120 * if empty, all groups are checked
121 * @param int $limit
122 * Limits the number of groups we evaluate.
123 *
124 * @return bool
125 * TRUE if we did not regenerate, FALSE if we did
126 */
127 public static function loadAll($groupIDs = NULL, $limit = 0) {
128 // ensure that all the smart groups are loaded
129 // this function is expensive and should be sparingly used if groupIDs is empty
130 if (empty($groupIDs)) {
131 $groupIDClause = NULL;
132 $groupIDs = array();
133 }
134 else {
135 if (!is_array($groupIDs)) {
136 $groupIDs = array($groupIDs);
137 }
138
139 // note escapeString is a must here and we can't send the imploded value as second argument to
140 // the executeQuery(), since that would put single quote around the string and such a string
141 // of comma separated integers would not work.
142 $groupIDString = CRM_Core_DAO::escapeString(implode(', ', $groupIDs));
143
144 $groupIDClause = "g.id IN ({$groupIDString})";
145 }
146
147 $query = self::groupRefreshedClause($groupIDClause);
148
149 $limitClause = $orderClause = NULL;
150 if ($limit > 0) {
151 $limitClause = " LIMIT 0, $limit";
152 $orderClause = " ORDER BY g.cache_date, g.refresh_date";
153 }
154 // We ignore hidden groups and disabled groups
155 $query .= "
156 $orderClause
157 $limitClause
158 ";
159
160 $dao = CRM_Core_DAO::executeQuery($query);
161 $processGroupIDs = array();
162 $refreshGroupIDs = $groupIDs;
163 while ($dao->fetch()) {
164 $processGroupIDs[] = $dao->id;
165
166 // remove this id from refreshGroupIDs
167 foreach ($refreshGroupIDs as $idx => $gid) {
168 if ($gid == $dao->id) {
169 unset($refreshGroupIDs[$idx]);
170 break;
171 }
172 }
173 }
174
175 if (!empty($refreshGroupIDs)) {
176 $refreshGroupIDString = CRM_Core_DAO::escapeString(implode(', ', $refreshGroupIDs));
177 $time = CRM_Utils_Date::getUTCTime(self::smartGroupCacheTimeout() * 60);
178 $query = "
179 UPDATE civicrm_group g
180 SET g.refresh_date = $time
181 WHERE g.id IN ( {$refreshGroupIDString} )
182 AND g.refresh_date IS NULL
183 ";
184 CRM_Core_DAO::executeQuery($query);
185 }
186
187 if (empty($processGroupIDs)) {
188 return TRUE;
189 }
190 else {
191 self::add($processGroupIDs);
192 return FALSE;
193 }
194 }
195
196 /**
197 * FIXME: This function should not be needed, because the cache table should not be getting truncated
198 */
199 public static function fillIfEmpty() {
200 if (!CRM_Core_DAO::singleValueQuery("SELECT COUNT(id) FROM civicrm_group_contact_cache")) {
201 self::loadAll();
202 }
203 }
204
205 /**
206 * @param int $groupID
207 */
208 public static function add($groupID) {
209 // first delete the current cache
210 self::remove($groupID);
211 if (!is_array($groupID)) {
212 $groupID = array($groupID);
213 }
214
215 $returnProperties = array('contact_id');
216 foreach ($groupID as $gid) {
217 $params = array(array('group', 'IN', array($gid => 1), 0, 0));
218 // the below call updates the cache table as a byproduct of the query
219 CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, FALSE);
220 }
221 }
222
223 /**
224 * @param int $groupID
225 * @param $values
226 */
227 public static function store(&$groupID, &$values) {
228 $processed = FALSE;
229
230 // sort the values so we put group IDs in front and hence optimize
231 // mysql storage (or so we think) CRM-9493
232 sort($values);
233
234 // to avoid long strings, lets do BULK_INSERT_COUNT values at a time
235 while (!empty($values)) {
236 $processed = TRUE;
237 $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT);
238 $str = implode(',', $input);
239 $sql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) VALUES $str;";
240 CRM_Core_DAO::executeQuery($sql);
241 }
242 self::updateCacheTime($groupID, $processed);
243 }
244
245 /**
246 * Change the cache_date.
247 *
248 * @param array $groupID
249 * @param bool $processed
250 * Whether the cache data was recently modified.
251 */
252 public static function updateCacheTime($groupID, $processed) {
253 // only update cache entry if we had any values
254 if ($processed) {
255 // also update the group with cache date information
256 //make sure to give original timezone settings again.
257 $now = CRM_Utils_Date::getUTCTime();
258 $refresh = 'null';
259 }
260 else {
261 $now = 'null';
262 $refresh = 'null';
263 }
264
265 $groupIDs = implode(',', $groupID);
266 $sql = "
267 UPDATE civicrm_group
268 SET cache_date = $now, refresh_date = $refresh
269 WHERE id IN ( $groupIDs )
270 ";
271 CRM_Core_DAO::executeQuery($sql);
272 }
273
274 /**
275 * Removes all the cache entries pertaining to a specific group.
276 * If no groupID is passed in, removes cache entries for all groups
277 * Has an optimization to bypass repeated invocations of this function.
278 * Note that this function is an advisory, i.e. the removal respects the
279 * cache date, i.e. the removal is not done if the group was recently
280 * loaded into the cache.
281 *
282 * @param int $groupID
283 * the groupID to delete cache entries, NULL for all groups.
284 * @param bool $onceOnly
285 * run the function exactly once for all groups.
286 */
287 public static function remove($groupID = NULL, $onceOnly = TRUE) {
288 static $invoked = FALSE;
289
290 // typically this needs to happy only once per instance
291 // this is especially TRUE in import, where we dont need
292 // to do this all the time
293 // this optimization is done only when no groupID is passed
294 // i.e. cache is reset for all groups
295 if (
296 $onceOnly &&
297 $invoked &&
298 $groupID == NULL
299 ) {
300 return;
301 }
302
303 if ($groupID == NULL) {
304 $invoked = TRUE;
305 }
306 elseif (is_array($groupID)) {
307 foreach ($groupID as $gid) {
308 unset(self::$_alreadyLoaded[$gid]);
309 }
310 }
311 elseif ($groupID && array_key_exists($groupID, self::$_alreadyLoaded)) {
312 unset(self::$_alreadyLoaded[$groupID]);
313 }
314
315 $refresh = NULL;
316 $params = array();
317 $smartGroupCacheTimeout = self::smartGroupCacheTimeout();
318
319 $now = CRM_Utils_Date::getUTCTime();
320 $refreshTime = CRM_Utils_Date::getUTCTime($smartGroupCacheTimeout * 60);
321
322 if (!isset($groupID)) {
323 if ($smartGroupCacheTimeout == 0) {
324 $query = "
325 TRUNCATE civicrm_group_contact_cache
326 ";
327 $update = "
328 UPDATE civicrm_group g
329 SET cache_date = null,
330 refresh_date = null
331 ";
332 }
333 else {
334 $query = "
335 DELETE gc
336 FROM civicrm_group_contact_cache gc
337 INNER JOIN civicrm_group g ON g.id = gc.group_id
338 WHERE TIMESTAMPDIFF(MINUTE, g.cache_date, $now) >= $smartGroupCacheTimeout
339 ";
340 $update = "
341 UPDATE civicrm_group g
342 SET cache_date = null,
343 refresh_date = null
344 WHERE TIMESTAMPDIFF(MINUTE, cache_date, $now) >= $smartGroupCacheTimeout
345 ";
346 $refresh = "
347 UPDATE civicrm_group g
348 SET refresh_date = $refreshTime
349 WHERE TIMESTAMPDIFF(MINUTE, cache_date, $now) < $smartGroupCacheTimeout
350 AND refresh_date IS NULL
351 ";
352 }
353 }
354 elseif (is_array($groupID)) {
355 $groupIDs = implode(', ', $groupID);
356 $query = "
357 DELETE g
358 FROM civicrm_group_contact_cache g
359 WHERE g.group_id IN ( $groupIDs )
360 ";
361 $update = "
362 UPDATE civicrm_group g
363 SET cache_date = null,
364 refresh_date = null
365 WHERE id IN ( $groupIDs )
366 ";
367 }
368 else {
369 $query = "
370 DELETE g
371 FROM civicrm_group_contact_cache g
372 WHERE g.group_id = %1
373 ";
374 $update = "
375 UPDATE civicrm_group g
376 SET cache_date = null,
377 refresh_date = null
378 WHERE id = %1
379 ";
380 $params = array(1 => array($groupID, 'Integer'));
381 }
382
383 CRM_Core_DAO::executeQuery($query, $params);
384
385 if ($refresh) {
386 CRM_Core_DAO::executeQuery($refresh, $params);
387 }
388
389 // also update the cache_date for these groups
390 CRM_Core_DAO::executeQuery($update, $params);
391 }
392
393 /**
394 * Removes one or more contacts from the smart group cache.
395 * @param int|array $cid
396 * @param int $groupId
397 * @return bool
398 * TRUE if successful.
399 */
400 public static function removeContact($cid, $groupId = NULL) {
401 $cids = array();
402 // sanitize input
403 foreach ((array) $cid as $c) {
404 $cids[] = CRM_Utils_Type::escape($c, 'Integer');
405 }
406 if ($cids) {
407 $condition = count($cids) == 1 ? "= {$cids[0]}" : "IN (" . implode(',', $cids) . ")";
408 if ($groupId) {
409 $condition .= " AND group_id = " . CRM_Utils_Type::escape($groupId, 'Integer');
410 }
411 $sql = "DELETE FROM civicrm_group_contact_cache WHERE contact_id $condition";
412 CRM_Core_DAO::executeQuery($sql);
413 return TRUE;
414 }
415 return FALSE;
416 }
417
418 /**
419 * Load the smart group cache for a saved search.
420 *
421 * @param object $group
422 * The smart group that needs to be loaded.
423 * @param bool $force
424 * Should we force a search through.
425 */
426 public static function load(&$group, $force = FALSE) {
427 $groupID = $group->id;
428 $savedSearchID = $group->saved_search_id;
429 if (array_key_exists($groupID, self::$_alreadyLoaded) && !$force) {
430 return;
431 }
432
433 // grab a lock so other processes dont compete and do the same query
434 $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire("data.core.group.{$groupID}");
435 if (!$lock->isAcquired()) {
436 // this can cause inconsistent results since we dont know if the other process
437 // will fill up the cache before our calling routine needs it.
438 // however this routine does not return the status either, so basically
439 // its a "lets return and hope for the best"
440 return;
441 }
442
443 self::$_alreadyLoaded[$groupID] = 1;
444
445 // we now have the lock, but some other process could have actually done the work
446 // before we got here, so before we do any work, lets ensure that work needs to be
447 // done
448 // we allow hidden groups here since we dont know if the caller wants to evaluate an
449 // hidden group
450 if (!$force && !self::shouldGroupBeRefreshed($groupID, TRUE)) {
451 $lock->release();
452 return;
453 }
454
455 $sql = NULL;
456 $idName = 'id';
457 $customClass = NULL;
458 if ($savedSearchID) {
459 $ssParams = CRM_Contact_BAO_SavedSearch::getSearchParams($savedSearchID);
460
461 // rectify params to what proximity search expects if there is a value for prox_distance
462 // CRM-7021
463 if (!empty($ssParams)) {
464 CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams);
465 }
466
467 $returnProperties = array();
468 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) {
469 $fv = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
470 $returnProperties = CRM_Core_BAO_Mapping::returnProperties($fv);
471 }
472
473 if (isset($ssParams['customSearchID'])) {
474 // if custom search
475
476 // we split it up and store custom class
477 // so temp tables are not destroyed if they are used
478 // hence customClass is defined above at top of function
479 $customClass = CRM_Contact_BAO_SearchCustom::customClass($ssParams['customSearchID'], $savedSearchID);
480 $searchSQL = $customClass->contactIDs();
481 $searchSQL = str_replace('ORDER BY contact_a.id ASC', '', $searchSQL);
482 if (!strstr($searchSQL, 'WHERE')) {
483 $searchSQL .= " WHERE ( 1 ) ";
484 }
485 $idName = 'contact_id';
486 }
487 else {
488 $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
489
490 $query
491 = new CRM_Contact_BAO_Query(
492 $ssParams, $returnProperties, NULL,
493 FALSE, FALSE, 1,
494 TRUE, TRUE,
495 FALSE,
496 CRM_Utils_Array::value('display_relationship_type', $formValues),
497 CRM_Utils_Array::value('operator', $formValues, 'AND')
498 );
499 $query->_useDistinct = FALSE;
500 $query->_useGroupBy = FALSE;
501 $searchSQL
502 = $query->searchQuery(
503 0, 0, NULL,
504 FALSE, FALSE,
505 FALSE, TRUE,
506 TRUE,
507 NULL, NULL, NULL,
508 TRUE
509 );
510 }
511 $groupID = CRM_Utils_Type::escape($groupID, 'Integer');
512 $sql = $searchSQL . " AND contact_a.id NOT IN (
513 SELECT contact_id FROM civicrm_group_contact
514 WHERE civicrm_group_contact.status = 'Removed'
515 AND civicrm_group_contact.group_id = $groupID ) ";
516 }
517
518 if ($sql) {
519 $sql = preg_replace("/^\s*SELECT/", "SELECT $groupID as group_id, ", $sql);
520 }
521
522 // lets also store the records that are explicitly added to the group
523 // this allows us to skip the group contact LEFT JOIN
524 $sqlB = "
525 SELECT $groupID as group_id, contact_id as $idName
526 FROM civicrm_group_contact
527 WHERE civicrm_group_contact.status = 'Added'
528 AND civicrm_group_contact.group_id = $groupID ";
529
530 $groupIDs = array($groupID);
531 self::remove($groupIDs);
532 $processed = FALSE;
533 $tempTable = 'civicrm_temp_group_contact_cache' . rand(0, 2000);
534 foreach (array($sql, $sqlB) as $selectSql) {
535 if (!$selectSql) {
536 continue;
537 }
538 $insertSql = "CREATE TEMPORARY TABLE $tempTable ($selectSql);";
539 $processed = TRUE;
540 $result = CRM_Core_DAO::executeQuery($insertSql);
541 CRM_Core_DAO::executeQuery(
542 "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)
543 SELECT DISTINCT $idName, group_id FROM $tempTable
544 ");
545 CRM_Core_DAO::executeQuery(" DROP TEMPORARY TABLE $tempTable");
546 }
547
548 self::updateCacheTime($groupIDs, $processed);
549
550 if ($group->children) {
551
552 //Store a list of contacts who are removed from the parent group
553 $sql = "
554 SELECT contact_id
555 FROM civicrm_group_contact
556 WHERE civicrm_group_contact.status = 'Removed'
557 AND civicrm_group_contact.group_id = $groupID ";
558 $dao = CRM_Core_DAO::executeQuery($sql);
559 $removed_contacts = array();
560 while ($dao->fetch()) {
561 $removed_contacts[] = $dao->contact_id;
562 }
563
564 $childrenIDs = explode(',', $group->children);
565 foreach ($childrenIDs as $childID) {
566 $contactIDs = CRM_Contact_BAO_Group::getMember($childID, FALSE);
567 //Unset each contact that is removed from the parent group
568 foreach ($removed_contacts as $removed_contact) {
569 unset($contactIDs[$removed_contact]);
570 }
571 $values = array();
572 foreach ($contactIDs as $contactID => $dontCare) {
573 $values[] = "({$groupID},{$contactID})";
574 }
575
576 self::store($groupIDs, $values);
577 }
578 }
579
580 $lock->release();
581 }
582
583 /**
584 * @return int
585 */
586 public static function smartGroupCacheTimeout() {
587 $config = CRM_Core_Config::singleton();
588
589 if (
590 isset($config->smartGroupCacheTimeout) &&
591 is_numeric($config->smartGroupCacheTimeout) &&
592 $config->smartGroupCacheTimeout > 0
593 ) {
594 return $config->smartGroupCacheTimeout;
595 }
596
597 // lets have a min cache time of 5 mins if not set
598 return 5;
599 }
600
601 /**
602 * Get all the smart groups that this contact belongs to.
603 * Note that this could potentially be a super slow function since
604 * it ensure that all contact groups are loaded in the cache
605 *
606 * @param int $contactID
607 * @param bool $showHidden
608 * Hidden groups are shown only if this flag is set.
609 *
610 * @return array
611 * an array of groups that this contact belongs to
612 */
613 public static function contactGroup($contactID, $showHidden = FALSE) {
614 if (empty($contactID)) {
615 return NULL;
616 }
617
618 if (is_array($contactID)) {
619 $contactIDs = $contactID;
620 }
621 else {
622 $contactIDs = array($contactID);
623 }
624
625 self::loadAll();
626
627 $hiddenClause = '';
628 if (!$showHidden) {
629 $hiddenClause = ' AND (g.is_hidden = 0 OR g.is_hidden IS NULL) ';
630 }
631
632 $contactIDString = CRM_Core_DAO::escapeString(implode(', ', $contactIDs));
633 $sql = "
634 SELECT gc.group_id, gc.contact_id, g.title, g.children, g.description
635 FROM civicrm_group_contact_cache gc
636 INNER JOIN civicrm_group g ON g.id = gc.group_id
637 WHERE gc.contact_id IN ($contactIDString)
638 $hiddenClause
639 ORDER BY gc.contact_id, g.children
640 ";
641
642 $dao = CRM_Core_DAO::executeQuery($sql);
643 $contactGroup = array();
644 $prevContactID = NULL;
645 while ($dao->fetch()) {
646 if (
647 $prevContactID &&
648 $prevContactID != $dao->contact_id
649 ) {
650 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
651 }
652 $prevContactID = $dao->contact_id;
653 if (!array_key_exists($dao->contact_id, $contactGroup)) {
654 $contactGroup[$dao->contact_id]
655 = array('group' => array(), 'groupTitle' => array());
656 }
657
658 $contactGroup[$dao->contact_id]['group'][]
659 = array(
660 'id' => $dao->group_id,
661 'title' => $dao->title,
662 'description' => $dao->description,
663 'children' => $dao->children,
664 );
665 $contactGroup[$dao->contact_id]['groupTitle'][] = $dao->title;
666 }
667
668 if ($prevContactID) {
669 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
670 }
671
672 if ((!empty($contactGroup[$contactID]) && is_numeric($contactID))) {
673 return $contactGroup[$contactID];
674 }
675 else {
676 return $contactGroup;
677 }
678 }
679
680 }