word_length = array('min' => $config['fulltext_native_min_chars'], 'max' => $config['fulltext_native_max_chars']); $error = false; } /** * 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) { if (empty($word)) { continue; } // 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)) { sort($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; } sort($this->must_contain_ids); sort($this->must_not_contain_ids); sort($this->must_exclude_one_ids); 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; } // generate a search_key from all the options to identify the results $search_key = md5(implode('#', array( serialize($this->must_contain_ids), serialize($this->must_not_contain_ids), serialize($this->must_exclude_one_ids), $type, $fields, $terms, $sort_days, $sort_key, $topic_id, implode(',', $ex_fid_ary), implode(',', $m_approve_fid_ary), implode(',', $author_ary) ))); // try reading the results from cache $total_results = 0; if ($this->obtain_ids($search_key, $total_results, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) { return $total_results; } $id_ary = array(); $sql_where = array(); $group_by = false; $m_num = 0; $w_num = 0; $sql_array = array( 'SELECT' => ($type == 'posts') ? 'p.post_id' : 'p.topic_id', 'FROM' => array( SEARCH_WORDMATCH_TABLE => array(), SEARCH_WORDLIST_TABLE => array(), POSTS_TABLE => 'p' ), 'LEFT_JOIN' => array() ); $sql_where[] = 'm0.post_id = p.post_id'; if ($type == 'topics') { $sql_array['FROM'][TOPICS_TABLE] = 't'; $group_by = true; } $title_match = ''; // Build some display specific sql strings switch ($fields) { case 'titleonly': $title_match = 'title_match = 1'; // no break case 'firstpost': $sql_array['FROM'][TOPICS_TABLE] = 't'; $sql_where[] = 'p.post_id = t.topic_first_post_id'; break; case 'msgonly': $title_match = 'title_match = 0'; break; } /** * @todo Add a query optimizer (handle stuff like "+(4|3) +4") */ 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; } } $sql_where[] = (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; $sql_where[] = "w$w_num.word_text LIKE $subquery"; $sql_where[] = "m$m_num.word_id = w$w_num.word_id"; $group_by = true; $w_num++; } else { $sql_where[] = "m$m_num.word_id = $subquery"; } $sql_array['FROM'][SEARCH_WORDMATCH_TABLE][] = 'm' . $m_num; if ($title_match) { $sql_where[] = "m$m_num.$title_match"; } if ($m_num != 0) { $sql_where[] = "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]) . (($title_match) ? "m$m_num.$title_match" : '') . " AND m$m_num.post_id = m0.post_id" ); $sql_where[] = "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" . (($title_match) ? "m$m_num.$title_match" : '') ); $is_null_joins[] = "m$m_num.word_id IS NULL"; $m_num++; } $sql_where[] = '(' . implode(' OR ', $is_null_joins) . ')'; } if (!sizeof($m_approve_fid_ary)) { $sql_where[] = 'p.post_approved = 1'; } else if ($m_approve_fid_ary !== array(-1)) { $sql_where[] = '(p.post_approved = 1 OR p.forum_id ' . ((sizeof($m_approve_fid_ary) == 1) ? '= ' . $m_approve_fid_ary[0] : 'NOT IN (' . implode(', ', $m_approve_fid_ary) . ')' ) . ')'; } if ($topic_id) { $sql_where[] = 'p.topic_id = ' . $topic_id; } if (sizeof($author_ary)) { $sql_where[] = 'p.poster_id ' . ((sizeof($author_ary) == 1) ? ' = ' . $author_ary[0] : 'IN (' . implode(',', $author_ary) . ')'); } if (sizeof($ex_fid_ary)) { $sql_where[] = 'p.forum_id ' . ((sizeof($ex_fid_ary) == 1) ? '!= ' . $ex_fid_ary[0] : 'NOT IN (' . implode(',', $ex_fid_ary) . ')'); } if ($sort_days) { $sql_where[] = 'p.post_time >= ' . (time() - ($sort_days * 86400)); } $sql_array['WHERE'] = implode(' AND ', $sql_where); // if the total result count is not cached yet, retrieve it from the db if (!$total_results) { $sql_array_count = $sql_array; $sql_array_count['SELECT'] = ($type == 'posts') ? 'COUNT(DISTINCT p.post_id) AS total_results' : 'COUNT(DISTINCT p.topic_id) AS total_results'; $sql = $db->sql_build_query('SELECT', $sql_array_count); $result = $db->sql_query($sql); $total_results = (int) $db->sql_fetchfield('total_results'); $db->sql_freeresult($result); if (!$total_results) { return false; } unset($sql_array_count); } // Build sql strings for sorting $sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC'); switch ($sql_sort[0]) { case 'u': $sql_array['FROM'][USERS_TABLE] = 'u'; $sql_where[] = 'u.user_id = p.poster_id '; break; case 't': if (!isset($sql_array['FROM'][TOPICS_TABLE])) { $sql_array['FROM'][TOPICS_TABLE] = 't'; $sql_where[] = 'p.topic_id = t.topic_id'; } break; case 'f': $sql_array['FROM'][FORUMS_TABLE] = 'f'; $sql_where[] = 'f.forum_id = p.forum_id'; break; } $sql_array['GROUP_BY'] = ($group_by) ? (($type == 'posts') ? 'p.post_id' : 'p.topic_id') . ', ' . $sort_by_sql[$sort_key] : ''; $sql_array['ORDER_BY'] = $sql_sort; $sql = $db->sql_build_query('SELECT', $sql_array); $result = $db->sql_query_limit($sql, $config['search_block_size'], $start); while ($row = $db->sql_fetchrow($result)) { $id_ary[] = $row[(($type == 'posts') ? 'post_id' : 'topic_id')]; } $db->sql_freeresult($result); if (!sizeof($id_ary)) { return false; } // store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page $this->save_ids($search_key, implode(' ', $this->split_words), $author_ary, $total_results, $id_ary, $start, $sort_dir); $id_ary = array_slice($id_ary, 0, (int) $per_page); return $total_results; } /** * Performs a search on an author's posts without caring about message contents. Depends on display specific params * * @param string $type contains either posts or topics depending on what should be searched for * @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 * @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 author_search($type, &$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 author? No posts. if (!sizeof($author_ary)) { return 0; } // generate a search_key from all the options to identify the results $search_key = md5(implode('#', array( '', $type, '', '', $sort_days, $sort_key, $topic_id, implode(',', $ex_fid_ary), implode(',', $m_approve_fid_ary), implode(',', $author_ary) ))); // try reading the results from cache $total_results = 0; if ($this->obtain_ids($search_key, $total_results, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) { return $total_results; } $id_ary = array(); // Create some display specific sql strings $sql_author = 'p.poster_id ' . ((sizeof($author_ary) > 1) ? 'IN (' . implode(',', $author_ary) . ')' : '= ' . $author_ary[0]); $sql_fora = (sizeof($ex_fid_ary)) ? ' AND p.forum_id ' . ((sizeof($ex_fid_ary) == 1) ? '!= ' . $ex_fid_ary[0] : 'NOT IN (' . implode(',', $ex_fid_ary) . ')') : ''; $sql_time = ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : ''; $sql_topic_id = ($topic_id) ? ' AND p.topic_id = ' . (int) $topic_id : ''; // Build sql strings for sorting $sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC'); $sql_sort_table = $sql_sort_join = ''; switch ($sql_sort[0]) { case 'u': $sql_sort_table = USERS_TABLE . ' u, '; $sql_sort_join = ' AND u.user_id = p.poster_id '; break; case 't': $sql_sort_table = ($type == 'posts') ? TOPICS_TABLE . ' t, ' : ''; $sql_sort_join = ($type == 'posts') ? ' AND t.topic_id = p.topic_id ' : ''; break; case 'f': $sql_sort_table = FORUMS_TABLE . ' f, '; $sql_sort_join = ' AND f.forum_id = p.forum_id '; break; } if (!sizeof($m_approve_fid_ary)) { $m_approve_fid_sql = ' AND p.post_approved = 1'; } else if ($m_approve_fid_ary == array(-1)) { $m_approve_fid_sql = ''; } else { $m_approve_fid_sql = ' AND (p.post_approved = 1 OR p.forum_id ' . ((sizeof($m_approve_fid_ary) == 1) ? '= ' . $m_approve_fid_ary[0] : 'IN (' . implode($m_approve_fid_ary) . ')' ) . ')'; } // If the cache was completely empty count the results if (!$total_results) { if ($type == 'posts') { $sql = 'SELECT COUNT(p.post_id) as result_count FROM ' . POSTS_TABLE . " p WHERE $sql_author $sql_topic_id $m_approve_fid_sql $sql_fora $sql_time"; } else { $sql = 'SELECT COUNT(DISTINCT t.topic_id) as result_count FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p WHERE $sql_author $sql_topic_id $m_approve_fid_sql $sql_fora AND t.topic_id = p.topic_id $sql_time"; } $result = $db->sql_query($sql); if ($row = $db->sql_fetchrow()) { $total_results = $row['result_count']; } $db->sql_freeresult($result); } // Build the query for really selecting the post_ids if ($type == 'posts') { $sql = 'SELECT p.post_id FROM ' . $sql_sort_table . POSTS_TABLE . ' p' . (($topic_id) ? ', ' . TOPICS_TABLE . ' t' : '') . " WHERE $sql_author $sql_topic_id $m_approve_fid_sql $sql_fora $sql_sort_join $sql_time ORDER BY $sql_sort"; $field = 'post_id'; } else { $sql = 'SELECT t.topic_id FROM ' . $sql_sort_table . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p WHERE $sql_author $sql_topic_id $m_approve_fid_sql $sql_fora AND t.topic_id = p.topic_id $sql_sort_join $sql_time GROUP BY t.topic_id, " . $sort_by_sql[$sort_key] . ' ORDER BY ' . $sql_sort; $field = 'topic_id'; } // Only read one block of posts from the db and then cache it $result = $db->sql_query_limit($sql, $config['search_block_size'], $start); while ($row = $db->sql_fetchrow($result)) { $id_ary[] = $row[$field]; } $db->sql_freeresult($result); if (sizeof($id_ary)) { $this->save_ids($search_key, '', $author_ary, $total_results, $id_ary, $start, $sort_dir); $id_ary = array_slice($id_ary, 0, $per_page); return $total_results; } return false; } } ?>