/**
  * Return an array of possible dupes, based on the provided array of
  * params, using the default rule group for the given contact type and
  * level.
  *
  * check_permission is a boolean flag to indicate if permission should be considered.
  * default is to always check permissioning but public pages for example might not want
  * permission to be checked for anonymous users. Refer CRM-6211. We might be beaking
  * Multi-Site dedupe for public pages.
  *
  * @param array  $params  array of params of the form $params[$table][$field] == $value
  * @param string $ctype   contact type to match against
  * @param string $level   dedupe rule group level ('Fuzzy' or 'Strict')
  * @param array  $except  array of contacts that shouldn't be considered dupes
  * @param int    $ruleGroupID the id of the dedupe rule we should be using
  *
  * @return array  matching contact ids
  */
 static function dupesByParams($params, $ctype, $level = 'Strict', $except = array(), $ruleGroupID = NULL)
 {
     // If $params is empty there is zero reason to proceed.
     if (!$params) {
         return array();
     }
     $foundByID = FALSE;
     if ($ruleGroupID) {
         $rgBao = new CRM_Dedupe_BAO_RuleGroup();
         $rgBao->id = $ruleGroupID;
         $rgBao->contact_type = $ctype;
         if ($rgBao->find(TRUE)) {
             $foundByID = TRUE;
         }
     }
     if (!$foundByID) {
         $rgBao = new CRM_Dedupe_BAO_RuleGroup();
         $rgBao->contact_type = $ctype;
         $rgBao->level = $level;
         $rgBao->is_default = 1;
         if (!$rgBao->find(TRUE)) {
             CRM_Core_Error::fatal("{$level} rule for {$ctype} does not exist");
         }
     }
     $params['check_permission'] = CRM_Utils_Array::value('check_permission', $params, TRUE);
     $rgBao->params = $params;
     $rgBao->fillTable();
     $dao = new CRM_Core_DAO();
     $dao->query($rgBao->thresholdQuery($params['check_permission']));
     $dupes = array();
     while ($dao->fetch()) {
         if (isset($dao->id) && $dao->id) {
             $dupes[] = $dao->id;
         }
     }
     $dao->query($rgBao->tableDropQuery());
     return array_diff($dupes, $except);
 }