Eileen has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/253171

Change subject: Fix slow email search
......................................................................

Fix slow email search

CRM-17574 note the upstream commit is 
https://github.com/civicrm/civicrm-core/pull/7212 & includes a test
I was going to try to merge it through with the test as a test on our process 
but decided there was
some urgency around this fix so leaving for now.

Bug: T117492 (but also at least 1-2 others I think will turn out to be flavours 
of this)
Change-Id: Iaef571537cafe6446e7e7c5d3fd37140e999357f
---
M CRM/Contact/BAO/Query.php
1 file changed, 35 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm/civicrm 
refs/changes/71/253171/1

diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php
index e96a9a5..a8d6eb4 100644
--- a/CRM/Contact/BAO/Query.php
+++ b/CRM/Contact/BAO/Query.php
@@ -3323,18 +3323,7 @@
     $value = $strtolower(CRM_Core_DAO::escapeString(trim($value)));
     if (strlen($value)) {
       $fieldsub = array();
-      if ($wildcard && $op == 'LIKE') {
-        if ($config->includeWildCardInName) {
-          $value = "'%$value%'";
-        }
-        else {
-          $value = "'$value%'";
-        }
-        $op = 'LIKE';
-      }
-      else {
-        $value = "'$value'";
-      }
+      $value = "'" . $this->getWildCardedValue($wildcard, $op, $value) . "'";
       if ($fieldName == 'sort_name') {
         $wc = self::caseImportant($op) ? "LOWER(contact_a.sort_name)" : 
"contact_a.sort_name";
       }
@@ -3384,29 +3373,20 @@
    *
    * @return void
    */
-  public function email(&$values) {
+  protected function email(&$values) {
     list($name, $op, $value, $grouping, $wildcard) = $values;
 
-    $n = trim($value);
+    $n = strtolower(trim($value));
     if ($n) {
-      $config = CRM_Core_Config::singleton();
-
       if (substr($n, 0, 1) == '"' &&
         substr($n, -1, 1) == '"'
       ) {
         $n = substr($n, 1, -1);
-        $value = strtolower(CRM_Core_DAO::escapeString($n));
-        $value = "'$value'";
+        $value = CRM_Core_DAO::escapeString($n);
         $op = '=';
       }
       else {
-        $value = strtolower($n);
-        if ($wildcard) {
-          if (strpos($value, '%') === FALSE) {
-            $value = "%{$value}%";
-          }
-          $op = 'LIKE';
-        }
+        $value = $this->getWildCardedValue($wildcard, $op, $n);
       }
       $this->_qill[$grouping][] = ts('Email') . " $op '$n'";
       $this->_where[$grouping][] = self::buildClause('civicrm_email.email', 
$op, $value, 'String');
@@ -5897,4 +5877,34 @@
     return array(CRM_Utils_Array::value($op, $qillOperators, $op), 
$fieldValue);
   }
 
+  /**
+   * Alter value to reflect wildcard settings.
+   *
+   * The form will have tried to guess whether this is a good field to 
wildcard but there is
+   * also a site-wide setting that specifies whether it is OK to append the 
wild card to the beginning
+   * or only the end of the string
+   *
+   * @param bool $wildcard
+   *   This is a bool made on an assessment 'elsewhere' on whether this is a 
good field to wildcard.
+   * @param string $op
+   *   Generally '=' or 'LIKE'.
+   * @param string $value
+   *   The search string.
+   *
+   * @return string
+   */
+  public function getWildCardedValue($wildcard, $op, $value) {
+    if ($wildcard && $op == 'LIKE') {
+      if (CRM_Core_Config::singleton()->includeWildCardInName && 
(substr($value, 0, 1) != '%')) {
+        return "%$value%";
+      }
+      else {
+        return "$value%";
+      }
+    }
+    else {
+      return "$value";
+    }
+  }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/253171
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iaef571537cafe6446e7e7c5d3fd37140e999357f
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm/civicrm
Gerrit-Branch: master
Gerrit-Owner: Eileen <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to