2c2
< // $Id: search.module,v 1.250.2.5 2009/02/25 16:17:40 goba Exp $
---
> // $Id: search.module,v 1.250.2.6 2009/03/10 17:20:01 goba Exp $
642a643
>       break;
714,715c715
<   if ($text !='*') {
<     preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' '. $text, $matches, PREG_SET_ORDER);
---
>   preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' '. $text, $matches, PREG_SET_ORDER);
717,719c717,719
<     if (count($matches) < 1) {
<       return NULL;
<     }
---
>   if (count($matches) < 1) {
>     return NULL;
>   }
721,751c721,747
<     // Classify tokens
<     $or = FALSE;
<     $warning = '';
<     $simple = TRUE;
<     foreach ($matches as $match) {
<       $phrase = FALSE;
<       // Strip off phrase quotes
<       if ($match[2]{0} == '"') {
<         $match[2] = substr($match[2], 1, -1);
<         $phrase = TRUE;
<         $simple = FALSE;
<       }
<       // Simplify keyword according to indexing rules and external preprocessors
<       $words = search_simplify($match[2]);
<       // Re-explode in case simplification added more words, except when matching a phrase
<       $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
<       // Negative matches
<       if ($match[1] == '-') {
<         $keys['negative'] = array_merge($keys['negative'], $words);
<       }
<       // OR operator: instead of a single keyword, we store an array of all
<       // OR'd keywords.
<       elseif ($match[2] == 'OR' && count($keys['positive'])) {
<         $last = array_pop($keys['positive']);
<         // Starting a new OR?
<         if (!is_array($last)) {
<           $last = array($last);
<         }
<         $keys['positive'][] = $last;
<         $or = TRUE;
<         continue;
---
>   // Classify tokens
>   $or = FALSE;
>   $warning = '';
>   $simple = TRUE;
>   foreach ($matches as $match) {
>     $phrase = FALSE;
>     // Strip off phrase quotes
>     if ($match[2]{0} == '"') {
>       $match[2] = substr($match[2], 1, -1);
>       $phrase = TRUE;
>       $simple = FALSE;
>     }
>     // Simplify keyword according to indexing rules and external preprocessors
>     $words = search_simplify($match[2]);
>     // Re-explode in case simplification added more words, except when matching a phrase
>     $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
>     // Negative matches
>     if ($match[1] == '-') {
>       $keys['negative'] = array_merge($keys['negative'], $words);
>     }
>     // OR operator: instead of a single keyword, we store an array of all
>     // OR'd keywords.
>     elseif ($match[2] == 'OR' && count($keys['positive'])) {
>       $last = array_pop($keys['positive']);
>       // Starting a new OR?
>       if (!is_array($last)) {
>         $last = array($last);
753,754c749,761
<       // AND operator: implied, so just ignore it
<       elseif ($match[2] == 'AND' || $match[2] == 'and') {
---
>       $keys['positive'][] = $last;
>       $or = TRUE;
>       continue;
>     }
>     // AND operator: implied, so just ignore it
>     elseif ($match[2] == 'AND' || $match[2] == 'and') {
>       $warning = $match[2];
>       continue;
>     }
> 
>     // Plain keyword
>     else {
>       if ($match[2] == 'or') {
756d762
<         continue;
758,759c764,767
< 
<       // Plain keyword
---
>       if ($or) {
>         // Add to last element (which is an array)
>         $keys['positive'][count($keys['positive']) - 1] = array_merge($keys['positive'][count($keys['positive']) - 1], $words);
>       }
761,770c769
<         if ($match[2] == 'or') {
<           $warning = $match[2];
<         }
<         if ($or) {
<           // Add to last element (which is an array)
<           $keys['positive'][count($keys['positive']) - 1] = array_merge($keys['positive'][count($keys['positive']) - 1], $words);
<         }
<         else {
<           $keys['positive'] = array_merge($keys['positive'], $words);
<         }
---
>         $keys['positive'] = array_merge($keys['positive'], $words);
772d770
<       $or = FALSE;
773a772,773
>     $or = FALSE;
>   }
775,807c775,792
<     // Convert keywords into SQL statements.
<     $query = array();
<     $query2 = array();
<     $arguments = array();
<     $arguments2 = array();
<     $matches = 0;
<     $simple_and = FALSE;
<     $simple_or = FALSE;
<     // Positive matches
<     foreach ($keys['positive'] as $key) {
<       // Group of ORed terms
<       if (is_array($key) && count($key)) {
<         $simple_or = TRUE;
<         $queryor = array();
<         $any = FALSE;
<         foreach ($key as $or) {
<           list($q, $num_new_scores) = _search_parse_query($or, $arguments2);
<           $any |= $num_new_scores;
<           if ($q) {
<             $queryor[] = $q;
<             $arguments[] = $or;
<           }
<         }
<         if (count($queryor)) {
<           $query[] = '('. implode(' OR ', $queryor) .')';
<           // A group of OR keywords only needs to match once
<           $matches += ($any > 0);
<         }
<       }
<       // Single ANDed term
<       else {
<         $simple_and = TRUE;
<         list($q, $num_new_scores, $num_valid_words) = _search_parse_query($key, $arguments2);
---
>   // Convert keywords into SQL statements.
>   $query = array();
>   $query2 = array();
>   $arguments = array();
>   $arguments2 = array();
>   $matches = 0;
>   $simple_and = FALSE;
>   $simple_or = FALSE;
>   // Positive matches
>   foreach ($keys['positive'] as $key) {
>     // Group of ORed terms
>     if (is_array($key) && count($key)) {
>       $simple_or = TRUE;
>       $queryor = array();
>       $any = FALSE;
>       foreach ($key as $or) {
>         list($q, $num_new_scores) = _search_parse_query($or, $arguments2);
>         $any |= $num_new_scores;
809,815c794,795
<           $query[] = $q;
<           $arguments[] = $key;
<           if (!$num_valid_words) {
<             $simple = FALSE;
<           }
<           // Each AND keyword needs to match at least once
<           $matches += $num_new_scores;
---
>           $queryor[] = $q;
>           $arguments[] = $or;
817a798,802
>       if (count($queryor)) {
>         $query[] = '('. implode(' OR ', $queryor) .')';
>         // A group of OR keywords only needs to match once
>         $matches += ($any > 0);
>       }
819,824c804,807
<     if ($simple_and && $simple_or) {
<       $simple = FALSE;
<     }
<     // Negative matches
<     foreach ($keys['negative'] as $key) {
<       list($q) = _search_parse_query($key, $arguments2, TRUE);
---
>     // Single ANDed term
>     else {
>       $simple_and = TRUE;
>       list($q, $num_new_scores, $num_valid_words) = _search_parse_query($key, $arguments2);
828c811,815
<         $simple = FALSE;
---
>         if (!$num_valid_words) {
>           $simple = FALSE;
>         }
>         // Each AND keyword needs to match at least once
>         $matches += $num_new_scores;
831,841d817
<     $query = implode(' AND ', $query);
< 
<     // Build word-index conditions for the first pass
<     $query2 = substr(str_repeat("i.word = '%s' OR ", count($arguments2)), 0, -4);
<  }
<   else {
<     $query = "d.data LIKE '%% %s %%''";
<     $arguments[0] = '*';
<     $matches['*'] = '*';
<     $simple = 1;
<     $warning = 1;
842a819,834
>   if ($simple_and && $simple_or) {
>     $simple = FALSE;
>   }
>   // Negative matches
>   foreach ($keys['negative'] as $key) {
>     list($q) = _search_parse_query($key, $arguments2, TRUE);
>     if ($q) {
>       $query[] = $q;
>       $arguments[] = $key;
>       $simple = FALSE;
>     }
>   }
>   $query = implode(' AND ', $query);
> 
>   // Build word-index conditions for the first pass
>   $query2 = substr(str_repeat("i.word = '%s' OR ", count($arguments2)), 0, -4);
937,938c929
< 
<   if ($query === NULL || $query[0] == '') {  
---
>   if ($query === NULL || $query[0] == '' || $query[2] == '') {
943,952c934,935
<   if ($query[2])
<     $conditions = "$where1 AND ($query[2]) AND i.type = '%s'";
<   else
<     $conditions = "$where1 AND i.type = '%s'";
< 
<   if ($query[3])
<     $arguments1 = array_merge($arguments1, $query[3], array($type));
<   else
<     $arguments1 = array_merge($arguments1, array($type));
< 
---
>   $conditions = "$where1 AND ($query[2]) AND i.type = '%s'";
>   $arguments1 = array_merge($arguments1, $query[3], array($type));
963d945
< 
