class fulltext_ashe { var $word_length = array(); var $common_words = array(); var $must_contain_ids = array(); var $must_not_contain_ids = array(); var $must_exclude_one_ids = array(); function fulltext_ashe(&$error) { global $config; /** * replace this with fulltext_ashe specific values */ $this->word_length = array('min' => $config['fulltext_native_min_chars'], 'max' => $config['fulltext_native_max_chars']); $error = false; } /** * This function returns the id or ids for the given word contained in the associative $words array * @param string|array $word contains the original word or an array of original words, can contain * which is a wildcard * @param array $words is an associative array which contains word_texts as keys and word_ids as values * @return int|array containing the id(s) for the specified word(s), if no id was found the id will be set to -1 */ function word_to_id($word, &$words) { if (is_array($word)) { foreach ($word as $i => $w) { $id = $this->word_to_id($w, $words); if ($id != -1) { $word[$i] = $id; } else { unset($word[$i]); } } return array_values($word); } if (isset($words[$word])) { return $words[$word]; } else if (strpos($word, '*') !== false) { /** * @todo find words matching $word in the $words array */ return -1; } else { return -1; } } /** * Splits keywords entered by a user into an array of words stored in $this->split_words * * @param string $keywords Contains the keyword as entered by the user * @param string $terms is either 'all' or 'any' * @return false if no valid keywords were found and otherwise true */ function split_keywords(&$keywords, $terms) { global $db, $config; // input format: each word seperated by a space, words in a bracket are not seperated $keywords = '+abc +(def|ghi) -jkl'; // generate the split_words array shown to the user $this->split_words = explode(' ', $keywords); $words = explode(' ', str_replace(array('+', '-', '(', ')', '|'), array('', '', '', '', ' '), $keywords)); // start with building a query to retrieve all word ids $where_sql = array(); foreach ($words as $i => $word) { // we need to use LIKE for wildcard words if (strpos($word, '*') !== false) { $where_sql[] = 'word_text LIKE \'' . $db->sql_escape(str_replace('*', '%', $word)) . '\''; unset($words[$i]); } else { $words[$i] = '\'' . $db->sql_escape($word) . '\''; } } // we can match exact words with one IN if (sizeof($words)) { $where_sql[] = 'word_text IN (' . implode(', ', $words) . ')'; } $sql = 'SELECT word_text, word_id FROM ' . SEARCH_WORDLIST_TABLE . ' WHERE ' . implode(' OR ', $where_sql); $result = $db->sql_query($sql); // retrieve the list of word texts and ids $words = array(); while ($row = $db->sql_fetchrow($result)) { $words[$row['word_text']] = $row['word_id']; } $db->sql_freeresult($result); // now analyse the search query, first split it using the spaces $query = explode(' ', $keywords); $this->must_contain_ids = array(); $this->must_not_contain_ids = array(); $this->must_exclude_one_ids = array(); foreach ($query as $word) { // words which should not be included if ($word[0] == '-') { $word = substr($word, 1); // a group of which at least one may not be in the resulting posts if ($word[0] == '(') { $word = explode('|', substr($word, 1, -1)); $ids = $this->word_to_id($word, $words); if (sizeof($ids)) { $this->must_exclude_one_ids[] = $ids; } } // one word which should not be in the resulting posts else { $id = $this->word_to_id($word, $words); if ($id != -1) { $this->must_not_contain_ids[] = $id; } } } // words which have to be included else { // no prefix is the same as a +prefix if ($word[0] == '+') { $word = substr($word, 1); } // a group of words of which at least one word should be in every resulting post if ($word[0] == '(') { $word = explode('|', substr($word, 1, -1)); $ids = $this->word_to_id($word, $words); if (sizeof($ids)) { $this->must_contain_ids[] = $id; } } // one word which has to be in the resulting posts else { $id = $this->word_to_id($word, $words); if ($id != -1) { $this->must_contain_ids[] = $id; } } } } // we can't search for negatives only if (!sizeof($this->must_contain_ids)) { return false; } if (sizeof($this->split_words)) { $this->split_words = array_values($this->split_words); return true; } return false; } /** * Performs a search on keywords depending on display specific params. * * @param string $type contains either posts or topics depending on what should be searched for * @param string $fields contains either titleonly (topic titles should be searched), msgonly (only message bodies should be searched), firstpost (only subject and body of the first post should be searched) or all (all post bodies and subjects should be searched) * @param string $terms is either 'all' (use query as entered, words without prefix should default to "have to be in field") or 'any' (ignore search query parts and just return all posts that contain any of the specified words) * @param array $sort_by_sql contains SQL code for the ORDER BY part of a query * @param string $sort_key is the key of $sort_by_sql for the selected sorting * @param string $sort_dir is either a or d representing ASC and DESC * @param string $sort_days specifies the maximum amount of days a post may be old * @param array $ex_fid_ary specifies an array of forum ids which should not be searched * @param array $m_approve_fid_ary specifies an array of forum ids in which the searcher is allowed to view unapproved posts * @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched * @param array $author_ary an array of author ids if the author should be ignored during the search the array is empty * @param array $id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered * @param int $start indicates the first index of the page * @param int $per_page number of ids each page is supposed to contain * @return boolean|int total number of results */ function keyword_search($type, &$fields, &$terms, &$sort_by_sql, &$sort_key, &$sort_dir, &$sort_days, &$ex_fid_ary, &$m_approve_fid_ary, &$topic_id, &$author_ary, &$id_ary, $start, $per_page) { global $config, $db; // No keywords? No posts. if (!sizeof($this->split_words)) { return false; } /** * @todo Add a query optimizer (handle stuff like "+(4|3) +4") */ $sql_array = array( 'SELECT' => 'm0.post_id', 'FROM' => array(SEARCH_WORDMATCH_TABLE => array()), 'LEFT_JOIN' => array() ); $from = ''; $where_sql = ''; $group_by = false; $m_num = 0; foreach ($this->must_contain_ids as $m_num => $subquery) { if (is_array($subquery)) { $group_by = true; $where_sql[] = "m$m_num.word_id IN (" . implode(',', $subquery) . ")"; } else { $where_sql[] = "m$m_num.word_id = $subquery"; } $sql_array['FROM'][SEARCH_WORDMATCH_TABLE][] = 'm' . $m_num; if ($m_num != 0) { $where_sql[] = "m$m_num.post_id = m0.post_id"; } } if (sizeof($this->must_not_contain_ids)) { $m_num++; $sql_array['LEFT_JOIN'][] = array( 'FROM' => array(SEARCH_WORDMATCH_TABLE => 'm' . $m_num), 'ON' => "m$m_num.word_id " . ((sizeof($this->must_not_contain_ids) == 1) ? "= {$this->must_not_contain_ids[0]}" : "IN (" . implode(',', $this->must_not_contain_ids) . ')') . " AND m$m_num.post_id = m0.post_id" ); $where_sql[] = "m$m_num.word_id IS NULL"; } foreach ($this->must_exclude_one_ids as $ids) { $where_not_list = array(); foreach ($ids as $id) { $m_num++; $sql_array['LEFT_JOIN'][] = array( 'FROM' => array(SEARCH_WORDMATCH_TABLE => 'm' . $m_num), 'ON' => "m$m_num.word_id = $id AND m$m_num.post_id = m0.post_id" ); $where_not_list[] = "m$m_num.word_id IS NULL"; } $where_sql[] = '(' . implode(' OR ', $where_not_list) . ')'; } $sql_array['WHERE'] = implode(' AND ', $where_sql); $sql_array['GROUP_BY'] = ($group_by) ? 'm0.post_id' : ''; $sql = $db->sql_build_query('SELECT', $sql_array); var_dump($sql); $id_ary = array(1); return 1; $result = $db->sql_query($sql); $db->sql_freeresult($result); unset($sql_array); unset($where_sql); unset($where_not_list); //$id_ary = $per_page ids return $total_results; } }