From cac28ed026c631d0caf980a3ea7adad3f91b2dc2 Mon Sep 17 00:00:00 2001
From: Alexandr Kirienko <alex@kirienko.net>
Date: Sun, 10 May 2015 23:12:41 +0600
Subject: [PATCH] #2463541: synonyms_get_term_by_synonym() function is too slow

---
 synonyms.module | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/synonyms.module b/synonyms.module
index e3f07db..a80ab57 100644
--- a/synonyms.module
+++ b/synonyms.module
@@ -351,21 +351,25 @@ function synonyms_autocomplete_validate($element, &$form_state) {
  *   found term, otherwise returns 0
  */
 function synonyms_get_term_by_synonym($name, $vocabulary, $parent = 0) {
-  $name = mb_strtoupper(trim($name), 'UTF-8');
+  foreach (synonyms_synonyms_fields($vocabulary) as $field) {
+    $query = new EntityFieldQuery();
+    $query->entityCondition('entity_type', 'taxonomy_term');
+    $query->propertyCondition('vid', $vocabulary->vid);
+    $query->fieldCondition($field, 'value', $name, 'like');
+
+    //@TODO: Do we really need to check parent here?
+
+    $result = $query->execute();
+
+    if (isset($result['taxonomy_term'])) {
+      $term = reset($result['taxonomy_term']);
 
-  $tree = taxonomy_get_tree($vocabulary->vid, $parent, NULL, TRUE);
-  foreach ($tree as $term) {
-    if (mb_strtoupper($term->name, 'UTF-8') == $name) {
       return $term->tid;
     }
+  }
 
-    // We additionally scan through the synonyms.
-    $synonyms = synonyms_get_sanitized($term);
-    foreach ($synonyms as $item) {
-      if (mb_strtoupper($item, 'UTF-8') == $name) {
-        return $term->tid;
-      }
-    }
+  if ($term = taxonomy_get_term_by_name($name, $vocabulary->machine_name)) {
+    return $term->tid;
   }
 
   // If we have reached down here, this means we haven't got any match
-- 
1.9.1

