Index: views_fastsearch.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views_fastsearch/views_fastsearch.module,v retrieving revision 1.2.2.46 diff -u -p -r1.2.2.46 views_fastsearch.module --- views_fastsearch.module 16 Jan 2008 12:36:33 -0000 1.2.2.46 +++ views_fastsearch.module 23 Sep 2008 21:47:10 -0000 @@ -114,25 +114,43 @@ function views_fastsearch_views_tables() return $tables; } +/** + * Implementation of hook_views_arguments + * builds all of the views fast search arguments + * + * @return array + */ function views_fastsearch_views_arguments() { + $arguments = array( + 'search_node_test' => array( + 'name' => t('Search: keyword'), + 'handler' => 'views_handler_arg_similarnode', + 'help' => t('Node to find similar nodes for (note: this node is filtered out of the results)'), + ), + ); if (_views_fastsearch_is_search_node_links()) { - return array( - 'search_node_linked_to' => array( - 'name' => t('Node: Links To'), - 'handler' => 'views_handler_arg_node_linked_to', - 'option' => 'integer', - 'help' => t('Filters by nodes that link to the argument node'), - ), - 'search_node_linked_from' => array( - 'name' => t('Node: Linked From'), - 'handler' => 'views_handler_arg_node_linked_from', - 'option' => 'integer', - 'help' => t('filters by nodes that are linked from the argument node'), - ), + $arguments['search_node_linked_to'] = array( + 'name' => t('Node: Links To'), + 'handler' => 'views_handler_arg_node_linked_to', + 'option' => 'integer', + 'help' => t('Filters by nodes that link to the argument node'), + ); + $arguments['search_node_linked_from'] = array( + 'name' => t('Node: Linked From'), + 'handler' => 'views_handler_arg_node_linked_from', + 'option' => 'integer', + 'help' => t('filters by nodes that are linked from the argument node'), ); } + $arguments['search_node_test'] = array( + 'name' => t('Search: content with keyword'), + 'handler' => 'views_handler_arg_search_keyword', + 'help' => t('Search node content for this keyword'), + ); + return $arguments; } + /* * Handle the node links argument. */ @@ -154,6 +172,23 @@ function views_handler_arg_node_linked_f } } + +/** + * handles the views fast search keyword argument + * @param string $op + * @param object $query + * @param unknown $argtype + * @param string $arg + */ +function views_handler_arg_search_keyword ($op, &$query, $argtype, $arg = ''){ + switch ($op) { + case 'filter': + views_fast_search_modify_query($query, $arg); + break; + } +} + + function _views_fastsearch_linked_arg($arg) { if (!empty($arg) && $arg != -1) { return db_escape_string($arg); @@ -161,130 +196,152 @@ function _views_fastsearch_linked_arg($a return (arg(0) == 'node' && is_numeric($nid = arg(1))) ? $nid : 0; } + /** * Custom filter for SEARCH operations + * @param string $op + * @param object $filter + * @param unknown $filterinfo + * @param object $query */ function views_fastsearch_views_handler_search_index($op, $filter, $filterinfo, &$query) { switch ($op) { case 'handler': - $word_count = 0; - if (!empty($filter['value'])) { - // use the newer fast search if the Dup indexes have been repaired - $search_index_unique = variable_get('search_index_unique', 0); - - // walk through each of the values - // building the AND, OR, and exclusion terms - foreach (explode(' ', search_simplify($filter['value'])) as $value) { - // OR applies to the next value only - $upper_value = strtoupper($value); - if ($upper_value == 'OR') { - // the initial AND assumption was wrong, - // convert the leading term to an OR term - if (isset($values['AND']) && count($values['AND']) == 1) { - $values['OR'] = $values['AND']; - unset($values['AND']); - } - $sqlop = ' OR '; - continue; - } - - // throw out too small words - if (drupal_strlen($value) < variable_get('minimum_word_size', 3)) { - continue; - } + views_fastsearch_arg_search($query, $filter['value'], $filter); + break; + } +} - // we've got something to search on! - $word_count ++; - if ($sqlop == ' OR ' || ($filter['operator'] == 'OR' && $word_count > 1)) { - $values['OR'][] = $value; +/** + * modifies the query for the filter and argument functions + * + * @param object $query + * views query object + * @param string $keywords + * keywords to search on + * @param object $filter + */ +function views_fast_search_modify_query(&$query, $keywords, $filter = null) { + if (!empty($keywords)) { + $word_count = 0; + // use the newer fast search if the Dup indexes have been repaired + $search_index_unique = variable_get('search_index_unique', 0); + + // walk through each of the values + // building the AND, OR, and exclusion terms + foreach (explode(' ', search_simplify($keywords)) as $value) { + // OR applies to the next value only + $upper_value = strtoupper($value); + if ($upper_value == 'OR') { + // the initial AND assumption was wrong, + // convert the leading term to an OR term + if (isset($values['AND']) && count($values['AND']) == 1) { + $values['OR'] = $values['AND']; + unset($values['AND']); + } + $sqlop = ' OR '; + continue; + } + + // throw out too small words + if (drupal_strlen($value) < variable_get('minimum_word_size', 3)) { + continue; + } + + // we've got something to search on! + $word_count ++; + + if ($sqlop == ' OR ' || ($filter['operator'] == 'OR' && $word_count > 1)) { + $values['OR'][] = $value; + } + else { // implied AND + // look for an end to a quoted phrase + if (isset($in_quote)) { + if (substr($value, -1) != $in_quote) { + $quote_value .= ' '. $value; } - else { // implied AND - // look for an end to a quoted phrase - if (isset($in_quote)) { - if (substr($value, -1) != $in_quote) { - $quote_value .= ' '. $value; - } - else { - if (!$search_index_unique) { - $tnc = $query->add_table('search_dataset', TRUE); - $tablename = $query->get_table_name('search_dataset', $tnc); - $and_clause[] = "$tablename.data like '%%%s%%'"; - } - $values['AND'][] = $quote_value .' '. drupal_substr($value, 0, -1); - unset($in_quote); - } - continue; - } - - $first_char = drupal_substr($value, 0, 1); - switch ($first_char) { - case '-': // values starting with a - are exlusion terms - $values['EXCLUDE'][] = drupal_substr($value, 1); - break; - case '"': // begin quoted phrase - case '\'': - if (substr($value, -1) == $first_char) { - $value = drupal_substr($value, 1, -2); - } - else { - $in_quote = $first_char; - $quote_value = drupal_substr($value, 1); - break; - } - // FALLTHROUGH - default: - if (!$search_index_unique) { - $tnc = $query->add_table('search_index', TRUE); - $tablename = $query->get_table_name('search_index', $tnc); - $extra['AND'][] = "$tablename.word='%s'"; - if ($filter['options']) { - $tablename = $query->get_table_name('search_index', $tnc); - $query->add_where("$tablename.type='%s'", $filter['options']); - } - } - $values['AND'][] = $value; - break; + else { + if (!$search_index_unique) { + $tnc = $query->add_table('search_dataset', TRUE); + $tablename = $query->get_table_name('search_dataset', $tnc); + $and_clause[] = "$tablename.data like '%%%s%%'"; } + $values['AND'][] = $quote_value .' '. drupal_substr($value, 0, -1); + unset($in_quote); } - $sqlop = ' AND '; + continue; } + + $first_char = drupal_substr($value, 0, 1); + switch ($first_char) { + case '-': // values starting with a - are exlusion terms + $values['EXCLUDE'][] = drupal_substr($value, 1); + break; + case '"': // begin quoted phrase + case '\'': + if (substr($value, -1) == $first_char) { + $value = drupal_substr($value, 1, -2); + } + else { + $in_quote = $first_char; + $quote_value = drupal_substr($value, 1); + break; + } + // FALLTHROUGH + default: + if (!$search_index_unique) { + $tnc = $query->add_table('search_index', TRUE); + $tablename = $query->get_table_name('search_index', $tnc); + $extra['AND'][] = "$tablename.word='%s'"; + if ($filter['options']) { + $tablename = $query->get_table_name('search_index', $tnc); + $query->add_where("$tablename.type='%s'", $filter['options']); + } + } + $values['AND'][] = $value; + break; + } + } + $sqlop = ' AND '; + } - // if there aren't any words - if ($word_count == 0) { - $query->add_where($filter['operator'] == 'AND+' ? '1' : '0'); - return; - } + // if there aren't any words + if ($word_count == 0) { + $query->add_where($filter['operator'] == 'AND+' ? '1' : '0'); + return; + } - // 'simplify' the search terms, which calls the preprocess - // - // also, the Drupal search engine doesn't index quote characters, - // so remove them - foreach ($values as $op => $value) { - foreach ($values[$op] as $index => $word) { - $values[$op][$index] = str_replace('"', '', search_simplify($word)); - } + // 'simplify' the search terms, which calls the preprocess + // + // also, the Drupal search engine doesn't index quote characters, + // so remove them + foreach ($values as $op => $value) { + foreach ($values[$op] as $index => $word) { + $values[$op][$index] = str_replace('"', '', search_simplify($word)); } + } - // NOTE: using global to pass values to theme_views_fastsearch_display - global $_vfs_search_keys; - $_vfs_search_keys = $values; - - // modify the query - if ($search_index_unique) { - _views_fastsearch_query_unique($query, $values, $extra); - } - else { - _views_fastsearch_query($query, $values, $extra); - } + // NOTE: using global to pass values to theme_views_fastsearch_display + global $_vfs_search_keys; + $_vfs_search_keys = $values; + + // modify the query + if ($search_index_unique) { + _views_fastsearch_query_unique($query, $values, $extra); + } + else { + _views_fastsearch_query($query, $values, $extra); + } - // Log the search keys: - $type = 'views_fastsearch'; - if ($filter['operator'] != '=') { - $type .= ' '. $filter['operator']; - } - watchdog('search', t('%keys (%type).', array('%keys' => $filter['value'], '%type' => $type)), WATCHDOG_NOTICE); + // Log the search keys: + $type = 'views_fastsearch'; + if ($filter['operator'] != '=') { + $type .= ' '. $filter['operator']; + } + + watchdog('search', t('%keys (%type).', array('%keys' => $keywords, '%type' => $type)), WATCHDOG_NOTICE); } }