Index: modules/search/search.module =================================================================== RCS file: /cvs/drupal/drupal/modules/search/search.module,v retrieving revision 1.301 diff -u -r1.301 search.module --- modules/search/search.module 5 Jul 2009 18:00:10 -0000 1.301 +++ modules/search/search.module 8 Jul 2009 16:25:10 -0000 @@ -312,7 +312,7 @@ /** * Simplifies a string according to indexing rules. */ -function search_simplify($text) { +function search_simplify($text, $lang = NULL) { // Decode entities to UTF-8 $text = decode_entities($text); @@ -320,7 +320,9 @@ $text = drupal_strtolower($text); // Call an external processor for word handling. - search_invoke_preprocess($text); + foreach (module_implements('search_preprocess') as $module) { + $text = module_invoke($module, 'search_preprocess', $text, $lang); + } // Simple CJK handling if (variable_get('overlap_cjk', TRUE)) { @@ -378,7 +380,7 @@ /** * Splits a string into tokens for indexing. */ -function search_index_split($text) { +function search_index_split($text, $lang = NULL) { static $last = NULL; static $lastsplit = NULL; @@ -386,7 +388,7 @@ return $lastsplit; } // Process words - $text = search_simplify($text); + $text = search_simplify($text, $lang); $words = explode(' ', $text); array_walk($words, '_search_index_truncate'); @@ -405,15 +407,6 @@ } /** - * Invokes hook_search_preprocess() in modules. - */ -function search_invoke_preprocess(&$text) { - foreach (module_implements('search_preprocess') as $module) { - $text = module_invoke($module, 'search_preprocess', $text); - } -} - -/** * Update the full-text search index for a particular item. * * @param $sid @@ -425,9 +418,12 @@ * @param $text * The content of this item. Must be a piece of HTML text. * + * @param $lang + * The language the text is in. NULL indicates unknown language. + * * @ingroup search */ -function search_index($sid, $type, $text) { +function search_index($sid, $type, $text, $lang = NULL) { $minimum_word_size = variable_get('minimum_word_size', 3); // Link matching @@ -532,7 +528,7 @@ $value = $linktitle; } } - $words = search_index_split($value); + $words = search_index_split($value, $lang); foreach ($words as $word) { // Add word to accumulator $accum .= $word . ' '; @@ -732,6 +728,10 @@ * * @param $text * The search keys. + * @param $lang + * The language the search keys are presumed to be in (needed for + * preprocessing the search keys with stemming modules). NULL indicates + * unknown language. * @return * A list of six elements. * * A series of statements AND'd together which will be used to provide all @@ -744,7 +744,7 @@ * * A boolean indicating the presence of a lowercase or. Maybe the user * wanted to use OR. */ -function search_parse_query($text) { +function search_parse_query($text, $lang = NULL) { $keys = array('positive' => array(), 'negative' => array()); // Tokenize query string @@ -767,7 +767,7 @@ $simple = FALSE; } // Simplify keyword according to indexing rules and external preprocessors - $words = search_simplify($match[2]); + $words = search_simplify($match[2], $lang); // 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 @@ -919,6 +919,13 @@ * @param $type * A string identifying the calling module. * + * @param $lang_override + * (optional) The language the search keys are presumed to be in (needed for + * preprocessing with stemming modules). If set to FALSE, the global + * $language is passed to search_parse_query() as the language. + * If set to NULL (to indicate an unknown language) or a string, that + * value is passed to search_parse_query(). + * * @param $join1 * (optional) Inserted into the JOIN part of the first SQL query. * For example "INNER JOIN {node} n ON n.nid = i.sid". @@ -951,8 +958,15 @@ * * @ingroup search */ -function do_search($keywords, $type, $join1 = '', $where1 = '1 = 1', $arguments1 = array(), $columns2 = 'SUM(i.relevance) AS calculated_score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY calculated_score DESC') { - $query = search_parse_query($keywords); +function do_search($keywords, $type, $lang_override = FALSE, $join1 = '', $where1 = '1 = 1', $arguments1 = array(), $columns2 = 'SUM(i.relevance) AS calculated_score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY calculated_score DESC') { + global $language; + + $lang = $language->language; + if( is_null( $lang_override) || is_string( $lang_override )) { + $lang = $lang_override; + } + + $query = search_parse_query($keywords, $lang); if ($query[2] == '') { form_set_error('keys', format_plural(variable_get('minimum_word_size', 3), 'You must include at least one positive keyword with 1 character or more.', 'You must include at least one positive keyword with @count characters or more.')); Index: modules/search/search.api.php =================================================================== RCS file: /cvs/drupal/drupal/modules/search/search.api.php,v retrieving revision 1.11 diff -u -r1.11 search.api.php --- modules/search/search.api.php 22 Jun 2009 09:10:06 -0000 1.11 +++ modules/search/search.api.php 8 Jul 2009 16:25:10 -0000 @@ -135,14 +135,21 @@ $keys = search_query_insert($keys, 'category'); } + $lang = $language->language; if ($languages = search_query_extract($keys, 'language')) { - $categories = array(); + $terms = array(); foreach (explode(',', $languages) as $l) { - $categories[] = "n.language = '%s'"; + $terms[] = "n.language = '%s'"; $arguments1[] = $l; + $lang = $l; } - $conditions1 .= ' AND (' . implode(' OR ', $categories) . ')'; + $conditions1 .= ' AND (' . implode(' OR ', $terms) . ')'; $keys = search_query_insert($keys, 'language'); + if (count($terms ) > 1) { + // they asked for multiple languages, so we don't know + // what language the search keys are in + $lang = NULL; + } } // Get the ranking expressions. @@ -164,7 +171,7 @@ } // Do search. - $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid ' . $join1, $conditions1 . (empty($where1) ? '' : ' AND ' . $where1), $arguments1, $select2, $join2, $arguments2); + $find = do_search($keys, 'node', $lang, 'INNER JOIN {node} n ON n.nid = i.sid ' . $join1, $conditions1 . (empty($where1) ? '' : ' AND ' . $where1), $arguments1, $select2, $join2, $arguments2); // Load results. $results = array(); @@ -211,11 +218,20 @@ * @param $text * The text to split. This is a single piece of plain-text that was * extracted from between two HTML tags. Will not contain any HTML entities. + * @param $lang + * The language this text is in; NULL if unknown. * @return * The text after processing. */ -function hook_search_preprocess($text) { +function hook_search_preprocess($text, $lang = NULL) { + // Check the language + if (!is_null( $lang ) && $lang != 'en') { + // Not a language we want to preprocess + return $text; + } + // Do processing on $text + return $text; } @@ -268,7 +284,7 @@ } // Update index - search_index($node->nid, 'node', $text); + search_index($node->nid, 'node', $text, $node->language); } } /** Index: modules/node/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node/node.module,v retrieving revision 1.1080 diff -u -r1.1080 node.module --- modules/node/node.module 7 Jul 2009 13:51:58 -0000 1.1080 +++ modules/node/node.module 8 Jul 2009 16:25:10 -0000 @@ -1342,6 +1342,8 @@ * Implement hook_search(). */ function node_search($op = 'search', $keys = NULL) { + global $language; + switch ($op) { case 'name': return t('Content'); @@ -1409,14 +1411,21 @@ $keys = search_query_insert($keys, 'term'); } + $lang = $language->language; if ($languages = search_query_extract($keys, 'language')) { $terms = array(); foreach (explode(',', $languages) as $l) { $terms[] = "n.language = '%s'"; $arguments1[] = $l; + $lang = $l; } $conditions1 .= ' AND (' . implode(' OR ', $terms) . ')'; $keys = search_query_insert($keys, 'language'); + if (count($terms ) > 1) { + // they asked for multiple languages, so we don't know + // what language the search keys are in + $lang = NULL; + } } // Get the ranking expressions. @@ -1438,7 +1447,7 @@ } // Do search. - $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid ' . $join1, $conditions1 . (empty($where1) ? '' : ' AND ' . $where1), $arguments1, $select2, $join2, $arguments2); + $find = do_search($keys, 'node', $lang, 'INNER JOIN {node} n ON n.nid = i.sid ' . $join1, $conditions1 . (empty($where1) ? '' : ' AND ' . $where1), $arguments1, $select2, $join2, $arguments2); // Load results. $results = array(); @@ -2058,7 +2067,7 @@ } // Update index - search_index($node->nid, 'node', $text); + search_index($node->nid, 'node', $text, $node->language); } /**