/** * fulltext_ashe * phpBB's own db driven fulltext search, version 2 * @package search */ 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(); /** * 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 boolean false if no valid keywords were found and otherwise true */ function split_keywords(&$keywords, $terms) { global $db, $config, $user; // $keywords input format: each word seperated by a space, words in a bracket are not seperated // the user wants to search for any word, convert the search query if ($terms == 'any') { $words = array(); preg_match_all('#([^\\s+\\-|()]+)(?:$|[\\s+\\-|()])#', $keywords, $words); if (sizeof($words)) { $keywords = '(' . implode('|', $words) . ')'; } } // generate the split_words array shown to the user $this->split_words = explode(' ', $keywords); $exact_words = array(); preg_match_all('#([^\\s+\\-|*()]+)(?:$|[\\s+\\-|()])#', $keywords, $exact_words); $exact_words = $exact_words[1]; if (sizeof($exact_words)) { // we can match exact words with one IN foreach ($exact_words as $i => $word) { $exact_words[$i] = '\'' . $db->sql_escape($word) . '\''; } $sql = 'SELECT word_id, word_text, word_common FROM ' . SEARCH_WORDLIST_TABLE . ' WHERE word_text ' . ((sizeof($exact_words) > 1) ? 'IN (' . implode(', ', $exact_words) . ')' : '= ' . $exact_words[0]); $result = $db->sql_query($sql); // store an array of words and ids, remove common words while ($row = $db->sql_fetchrow($result)) { if ($row['word_common']) { $this->ignore_words[] = $row['wort_text']; continue; } $words[$row['word_text']] = (int) $row['word_id']; } $db->sql_freeresult($result); } unset($exact_words); // 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(); $mode = ''; $ignore_no_id = true; 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)); $mode = 'must_exclude_one'; } // one word which should not be in the resulting posts else { $mode = 'must_not_contain'; } $ignore_no_id = true; } // 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)); } $ignore_no_id = false; $mode = 'must_contain'; } // if this is an array of words then retrieve an id for each if (is_array($word)) { $id_words = array(); foreach ($word as $i => $word_part) { if (strpos($word_part, '*') !== false) { $id_words[] = '\'' . $db->sql_escape(str_replace('*', '%', $word_part)) . '\''; } if (isset($words[$word_part])) { $id_words[] = $words[$word_part]; } } if (sizeof($id_words)) { $id_words = array_values($id_words); if (sizeof($id_words) > 1) { $this->{$mode . '_ids'}[] = $id_words; } else { $mode = ($mode == 'must_exclude_one') ? 'must_not_contain' : $mode; $this->{$mode . '_ids'}[] = $id_words[0]; } } // throw an error if we shall not ignore unexistant words else if (!$ignore_no_id) { trigger_error(sprintf($user->lang['WORDS_IN_NO_POST'], implode(', ', $word))); } } // else we only need one id else if (($wildcard = strpos($word, '*') !== false) || isset($words[$word])) { if ($wildcard) { $this->{$mode . '_ids'}[] = '\'' . $db->sql_escape(str_replace('*', '%', $word)) . '\''; } else { $this->{$mode . '_ids'}[] = $words[$word]; } } // throw an error if we shall not ignore unexistant words else if (!$ignore_no_id) { trigger_error(sprintf($user->lang['WORD_IN_NO_POST'], $word)); } } // 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(), SEARCH_WORDLIST_TABLE => array()), 'LEFT_JOIN' => array() ); $where_sql = ''; $group_by = false; $m_num = 0; $w_num = 0; foreach ($this->must_contain_ids as $subquery) { if (is_array($subquery)) { $group_by = true; $word_id_sql = array(); $word_ids = array(); foreach ($subquery as $id) { if (is_string($id)) { $sql_array['LEFT_JOIN'][] = array( 'FROM' => array(SEARCH_WORDLIST_TABLE => 'w' . $w_num), 'ON' => "w$w_num.word_text LIKE $id" ); $word_ids[] = "w$w_num.word_id"; $w_num++; } else { $word_ids[] = $id; } } $where_sql[] = (sizeof($word_ids) > 1) ? "m$m_num.word_id IN (" . implode(', ', $word_ids) . ')' : "m$m_num.word_id = {$word_ids[0]}"; unset($word_id_sql); unset($word_ids); } else if (is_string($subquery)) { $sql_array['FROM'][SEARCH_WORDLIST_TABLE][] = 'w' . $w_num; $where_sql[] = "w$w_num.word_text LIKE $subquery"; $where_sql[] = "m$m_num.word_id = w$w_num.word_id"; $group_by = true; $w_num++; } 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"; } $m_num++; } foreach ($this->must_not_contain_ids as $key => $subquery) { if (is_string($subquery)) { $sql_array['LEFT_JOIN'][] = array( 'FROM' => array(SEARCH_WORDLIST_TABLE => 'w' . $w_num), 'ON' => "w$w_num.word_text LIKE $subquery" ); $this->must_not_contain_ids[$key] = "w$w_num.word_id"; $group_by = true; $w_num++; } } if (sizeof($this->must_not_contain_ids)) { $sql_array['LEFT_JOIN'][] = array( 'FROM' => array(SEARCH_WORDMATCH_TABLE => 'm' . $m_num), 'ON' => ((sizeof($this->must_not_contain_ids) > 1) ? "m$m_num.word_id IN (" . implode(', ', $this->must_not_contain_ids) . ')' : "m$m_num.word_id = " . $this->must_not_contain_ids[0]) . " AND m$m_num.post_id = m0.post_id" ); $where_sql[] = "m$m_num.word_id IS NULL"; $m_num++; } foreach ($this->must_exclude_one_ids as $ids) { $is_null_joins = array(); foreach ($ids as $id) { if (is_string($id)) { $sql_array['LEFT_JOIN'][] = array( 'FROM' => array(SEARCH_WORDLIST_TABLE => 'w' . $w_num), 'ON' => "w$w_num.word_text LIKE $id" ); $id = "w$w_num.word_id"; $group_by = true; $w_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" ); $is_null_joins[] = "m$m_num.word_id IS NULL"; $m_num++; } $where_sql[] = '(' . implode(' OR ', $is_null_joins) . ')'; } $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; } }