indvdum@0: word_length = array('min' => $config['fulltext_mysql_min_word_len'], 'max' => $config['fulltext_mysql_max_word_len']); indvdum@0: indvdum@0: if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.0', '>='))) indvdum@0: { indvdum@0: // While this is the proper range of PHP versions, PHP may not be linked with the bundled PCRE lib and instead with an older version indvdum@0: if (@preg_match('/\p{L}/u', 'a') !== false) indvdum@0: { indvdum@0: $this->pcre_properties = true; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (function_exists('mb_ereg')) indvdum@0: { indvdum@0: $this->mbstring_regex = true; indvdum@0: mb_regex_encoding('UTF-8'); indvdum@0: } indvdum@0: indvdum@0: $error = false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Checks for correct MySQL version and stores min/max word length in the config indvdum@0: */ indvdum@0: function init() indvdum@0: { indvdum@0: global $db, $user; indvdum@0: indvdum@0: if ($db->sql_layer != 'mysql4' && $db->sql_layer != 'mysqli') indvdum@0: { indvdum@0: return $user->lang['FULLTEXT_MYSQL_INCOMPATIBLE_VERSION']; indvdum@0: } indvdum@0: indvdum@0: $result = $db->sql_query('SHOW TABLE STATUS LIKE \'' . POSTS_TABLE . '\''); indvdum@0: $info = $db->sql_fetchrow($result); indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: $engine = ''; indvdum@0: if (isset($info['Engine'])) indvdum@0: { indvdum@0: $engine = $info['Engine']; indvdum@0: } indvdum@0: else if (isset($info['Type'])) indvdum@0: { indvdum@0: $engine = $info['Type']; indvdum@0: } indvdum@0: indvdum@0: if ($engine != 'MyISAM') indvdum@0: { indvdum@0: return $user->lang['FULLTEXT_MYSQL_NOT_MYISAM']; indvdum@0: } indvdum@0: indvdum@0: $sql = 'SHOW VARIABLES indvdum@0: LIKE \'ft\_%\''; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: $mysql_info = array(); indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $mysql_info[$row['Variable_name']] = $row['Value']; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: set_config('fulltext_mysql_max_word_len', $mysql_info['ft_max_word_len']); indvdum@0: set_config('fulltext_mysql_min_word_len', $mysql_info['ft_min_word_len']); indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Splits keywords entered by a user into an array of words stored in $this->split_words indvdum@0: * Stores the tidied search query in $this->search_query indvdum@0: * indvdum@0: * @param string &$keywords Contains the keyword as entered by the user indvdum@0: * @param string $terms is either 'all' or 'any' indvdum@0: * @return bool false if no valid keywords were found and otherwise true indvdum@0: */ indvdum@0: function split_keywords(&$keywords, $terms) indvdum@0: { indvdum@0: global $config, $user; indvdum@0: indvdum@0: if ($terms == 'all') indvdum@0: { indvdum@0: $match = array('#\sand\s#iu', '#\sor\s#iu', '#\snot\s#iu', '#(^|\s)\+#', '#(^|\s)-#', '#(^|\s)\|#'); indvdum@0: $replace = array(' +', ' |', ' -', ' +', ' -', ' |'); indvdum@0: indvdum@0: $keywords = preg_replace($match, $replace, $keywords); indvdum@0: } indvdum@0: indvdum@0: // Filter out as above indvdum@0: $split_keywords = preg_replace("#[\n\r\t]+#", ' ', trim(htmlspecialchars_decode($keywords))); indvdum@0: indvdum@0: // Split words indvdum@0: if ($this->pcre_properties) indvdum@0: { indvdum@0: $split_keywords = preg_replace('#([^\p{L}\p{N}\'*"()])#u', '$1$1', str_replace('\'\'', '\' \'', trim($split_keywords))); indvdum@0: } indvdum@0: else if ($this->mbstring_regex) indvdum@0: { indvdum@0: $split_keywords = mb_ereg_replace('([^\w\'*"()])', '\\1\\1', str_replace('\'\'', '\' \'', trim($split_keywords))); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $split_keywords = preg_replace('#([^\w\'*"()])#u', '$1$1', str_replace('\'\'', '\' \'', trim($split_keywords))); indvdum@0: } indvdum@0: indvdum@0: if ($this->pcre_properties) indvdum@0: { indvdum@0: $matches = array(); indvdum@0: preg_match_all('#(?:[^\p{L}\p{N}*"()]|^)([+\-|]?(?:[\p{L}\p{N}*"()]+\'?)*[\p{L}\p{N}*"()])(?:[^\p{L}\p{N}*"()]|$)#u', $split_keywords, $matches); indvdum@0: $this->split_words = $matches[1]; indvdum@0: } indvdum@0: else if ($this->mbstring_regex) indvdum@0: { indvdum@0: mb_ereg_search_init($split_keywords, '(?:[^\w*"()]|^)([+\-|]?(?:[\w*"()]+\'?)*[\w*"()])(?:[^\w*"()]|$)'); indvdum@0: indvdum@0: while (($word = mb_ereg_search_regs())) indvdum@0: { indvdum@0: $this->split_words[] = $word[1]; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $matches = array(); indvdum@0: preg_match_all('#(?:[^\w*"()]|^)([+\-|]?(?:[\w*"()]+\'?)*[\w*"()])(?:[^\w*"()]|$)#u', $split_keywords, $matches); indvdum@0: $this->split_words = $matches[1]; indvdum@0: } indvdum@0: indvdum@0: // We limit the number of allowed keywords to minimize load on the database indvdum@0: if ($config['max_num_search_keywords'] && sizeof($this->split_words) > $config['max_num_search_keywords']) indvdum@0: { indvdum@0: trigger_error($user->lang('MAX_NUM_SEARCH_KEYWORDS_REFINE', $config['max_num_search_keywords'], sizeof($this->split_words))); indvdum@0: } indvdum@0: indvdum@0: // to allow phrase search, we need to concatenate quoted words indvdum@0: $tmp_split_words = array(); indvdum@0: $phrase = ''; indvdum@0: foreach ($this->split_words as $word) indvdum@0: { indvdum@0: if ($phrase) indvdum@0: { indvdum@0: $phrase .= ' ' . $word; indvdum@0: if (strpos($word, '"') !== false && substr_count($word, '"') % 2 == 1) indvdum@0: { indvdum@0: $tmp_split_words[] = $phrase; indvdum@0: $phrase = ''; indvdum@0: } indvdum@0: } indvdum@0: else if (strpos($word, '"') !== false && substr_count($word, '"') % 2 == 1) indvdum@0: { indvdum@0: $phrase = $word; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $tmp_split_words[] = $word . ' '; indvdum@0: } indvdum@0: } indvdum@0: if ($phrase) indvdum@0: { indvdum@0: $tmp_split_words[] = $phrase; indvdum@0: } indvdum@0: indvdum@0: $this->split_words = $tmp_split_words; indvdum@0: indvdum@0: unset($tmp_split_words); indvdum@0: unset($phrase); indvdum@0: indvdum@0: foreach ($this->split_words as $i => $word) indvdum@0: { indvdum@0: $clean_word = preg_replace('#^[+\-|"]#', '', $word); indvdum@0: indvdum@0: // check word length indvdum@0: $clean_len = utf8_strlen(str_replace('*', '', $clean_word)); indvdum@0: if (($clean_len < $config['fulltext_mysql_min_word_len']) || ($clean_len > $config['fulltext_mysql_max_word_len'])) indvdum@0: { indvdum@0: $this->common_words[] = $word; indvdum@0: unset($this->split_words[$i]); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ($terms == 'any') indvdum@0: { indvdum@0: $this->search_query = ''; indvdum@0: foreach ($this->split_words as $word) indvdum@0: { indvdum@0: if ((strpos($word, '+') === 0) || (strpos($word, '-') === 0) || (strpos($word, '|') === 0)) indvdum@0: { indvdum@0: $word = substr($word, 1); indvdum@0: } indvdum@0: $this->search_query .= $word . ' '; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $this->search_query = ''; indvdum@0: foreach ($this->split_words as $word) indvdum@0: { indvdum@0: if ((strpos($word, '+') === 0) || (strpos($word, '-') === 0)) indvdum@0: { indvdum@0: $this->search_query .= $word . ' '; indvdum@0: } indvdum@0: else if (strpos($word, '|') === 0) indvdum@0: { indvdum@0: $this->search_query .= substr($word, 1) . ' '; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $this->search_query .= '+' . $word . ' '; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $this->search_query = utf8_htmlspecialchars($this->search_query); indvdum@0: indvdum@0: if ($this->search_query) indvdum@0: { indvdum@0: $this->split_words = array_values($this->split_words); indvdum@0: sort($this->split_words); indvdum@0: return true; indvdum@0: } indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Turns text into an array of words indvdum@0: */ indvdum@0: function split_message($text) indvdum@0: { indvdum@0: global $config; indvdum@0: indvdum@0: // Split words indvdum@0: if ($this->pcre_properties) indvdum@0: { indvdum@0: $text = preg_replace('#([^\p{L}\p{N}\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text))); indvdum@0: } indvdum@0: else if ($this->mbstring_regex) indvdum@0: { indvdum@0: $text = mb_ereg_replace('([^\w\'*])', '\\1\\1', str_replace('\'\'', '\' \'', trim($text))); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $text = preg_replace('#([^\w\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text))); indvdum@0: } indvdum@0: indvdum@0: if ($this->pcre_properties) indvdum@0: { indvdum@0: $matches = array(); indvdum@0: preg_match_all('#(?:[^\p{L}\p{N}*]|^)([+\-|]?(?:[\p{L}\p{N}*]+\'?)*[\p{L}\p{N}*])(?:[^\p{L}\p{N}*]|$)#u', $text, $matches); indvdum@0: $text = $matches[1]; indvdum@0: } indvdum@0: else if ($this->mbstring_regex) indvdum@0: { indvdum@0: mb_ereg_search_init($text, '(?:[^\w*]|^)([+\-|]?(?:[\w*]+\'?)*[\w*])(?:[^\w*]|$)'); indvdum@0: indvdum@0: $text = array(); indvdum@0: while (($word = mb_ereg_search_regs())) indvdum@0: { indvdum@0: $text[] = $word[1]; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $matches = array(); indvdum@0: preg_match_all('#(?:[^\w*]|^)([+\-|]?(?:[\w*]+\'?)*[\w*])(?:[^\w*]|$)#u', $text, $matches); indvdum@0: $text = $matches[1]; indvdum@0: } indvdum@0: indvdum@0: // remove too short or too long words indvdum@0: $text = array_values($text); indvdum@0: for ($i = 0, $n = sizeof($text); $i < $n; $i++) indvdum@0: { indvdum@0: $text[$i] = trim($text[$i]); indvdum@0: if (utf8_strlen($text[$i]) < $config['fulltext_mysql_min_word_len'] || utf8_strlen($text[$i]) > $config['fulltext_mysql_max_word_len']) indvdum@0: { indvdum@0: unset($text[$i]); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return array_values($text); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Performs a search on keywords depending on display specific params. You have to run split_keywords() first. indvdum@0: * indvdum@0: * @param string $type contains either posts or topics depending on what should be searched for indvdum@0: * @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) indvdum@0: * @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) indvdum@0: * @param array $sort_by_sql contains SQL code for the ORDER BY part of a query indvdum@0: * @param string $sort_key is the key of $sort_by_sql for the selected sorting indvdum@0: * @param string $sort_dir is either a or d representing ASC and DESC indvdum@0: * @param string $sort_days specifies the maximum amount of days a post may be old indvdum@0: * @param array $ex_fid_ary specifies an array of forum ids which should not be searched indvdum@0: * @param array $m_approve_fid_ary specifies an array of forum ids in which the searcher is allowed to view unapproved posts indvdum@0: * @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 indvdum@0: * @param array $author_ary an array of author ids if the author should be ignored during the search the array is empty indvdum@0: * @param string $author_name specifies the author match, when ANONYMOUS is also a search-match indvdum@0: * @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered indvdum@0: * @param int $start indicates the first index of the page indvdum@0: * @param int $per_page number of ids each page is supposed to contain indvdum@0: * @return boolean|int total number of results indvdum@0: * indvdum@0: * @access public indvdum@0: */ indvdum@0: 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, $author_name, &$id_ary, $start, $per_page) indvdum@0: { indvdum@0: global $config, $db; indvdum@0: indvdum@0: // No keywords? No posts. indvdum@0: if (!$this->search_query) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // generate a search_key from all the options to identify the results indvdum@0: $search_key = md5(implode('#', array( indvdum@0: implode(', ', $this->split_words), indvdum@0: $type, indvdum@0: $fields, indvdum@0: $terms, indvdum@0: $sort_days, indvdum@0: $sort_key, indvdum@0: $topic_id, indvdum@0: implode(',', $ex_fid_ary), indvdum@0: implode(',', $m_approve_fid_ary), indvdum@0: implode(',', $author_ary) indvdum@0: ))); indvdum@0: indvdum@0: // try reading the results from cache indvdum@0: $result_count = 0; indvdum@0: if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) indvdum@0: { indvdum@0: return $result_count; indvdum@0: } indvdum@0: indvdum@0: $id_ary = array(); indvdum@0: indvdum@0: $join_topic = ($type == 'posts') ? false : true; indvdum@0: indvdum@0: // Build sql strings for sorting indvdum@0: $sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC'); indvdum@0: $sql_sort_table = $sql_sort_join = ''; indvdum@0: indvdum@0: switch ($sql_sort[0]) indvdum@0: { indvdum@0: case 'u': indvdum@0: $sql_sort_table = USERS_TABLE . ' u, '; indvdum@0: $sql_sort_join = ($type == 'posts') ? ' AND u.user_id = p.poster_id ' : ' AND u.user_id = t.topic_poster '; indvdum@0: break; indvdum@0: indvdum@0: case 't': indvdum@0: $join_topic = true; indvdum@0: break; indvdum@0: indvdum@0: case 'f': indvdum@0: $sql_sort_table = FORUMS_TABLE . ' f, '; indvdum@0: $sql_sort_join = ' AND f.forum_id = p.forum_id '; indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: // Build some display specific sql strings indvdum@0: switch ($fields) indvdum@0: { indvdum@0: case 'titleonly': indvdum@0: $sql_match = 'p.post_subject'; indvdum@0: $sql_match_where = ' AND p.post_id = t.topic_first_post_id'; indvdum@0: $join_topic = true; indvdum@0: break; indvdum@0: indvdum@0: case 'msgonly': indvdum@0: $sql_match = 'p.post_text'; indvdum@0: $sql_match_where = ''; indvdum@0: break; indvdum@0: indvdum@0: case 'firstpost': indvdum@0: $sql_match = 'p.post_subject, p.post_text'; indvdum@0: $sql_match_where = ' AND p.post_id = t.topic_first_post_id'; indvdum@0: $join_topic = true; indvdum@0: break; indvdum@0: indvdum@0: default: indvdum@0: $sql_match = 'p.post_subject, p.post_text'; indvdum@0: $sql_match_where = ''; indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: if (!sizeof($m_approve_fid_ary)) indvdum@0: { indvdum@0: $m_approve_fid_sql = ' AND p.post_approved = 1'; indvdum@0: } indvdum@0: else if ($m_approve_fid_ary === array(-1)) indvdum@0: { indvdum@0: $m_approve_fid_sql = ''; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $m_approve_fid_sql = ' AND (p.post_approved = 1 OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) . ')'; indvdum@0: } indvdum@0: indvdum@0: $sql_select = (!$result_count) ? 'SQL_CALC_FOUND_ROWS ' : ''; indvdum@0: $sql_select = ($type == 'posts') ? $sql_select . 'p.post_id' : 'DISTINCT ' . $sql_select . 't.topic_id'; indvdum@0: $sql_from = ($join_topic) ? TOPICS_TABLE . ' t, ' : ''; indvdum@0: $field = ($type == 'posts') ? 'post_id' : 'topic_id'; indvdum@0: if (sizeof($author_ary) && $author_name) indvdum@0: { indvdum@0: // first one matches post of registered users, second one guests and deleted users indvdum@0: $sql_author = ' AND (' . $db->sql_in_set('p.poster_id', array_diff($author_ary, array(ANONYMOUS)), false, true) . ' OR p.post_username ' . $author_name . ')'; indvdum@0: } indvdum@0: else if (sizeof($author_ary)) indvdum@0: { indvdum@0: $sql_author = ' AND ' . $db->sql_in_set('p.poster_id', $author_ary); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $sql_author = ''; indvdum@0: } indvdum@0: indvdum@0: $sql_where_options = $sql_sort_join; indvdum@0: $sql_where_options .= ($topic_id) ? ' AND p.topic_id = ' . $topic_id : ''; indvdum@0: $sql_where_options .= ($join_topic) ? ' AND t.topic_id = p.topic_id' : ''; indvdum@0: $sql_where_options .= (sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : ''; indvdum@0: $sql_where_options .= $m_approve_fid_sql; indvdum@0: $sql_where_options .= $sql_author; indvdum@0: $sql_where_options .= ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : ''; indvdum@0: $sql_where_options .= $sql_match_where; indvdum@0: indvdum@0: $sql = "SELECT $sql_select indvdum@0: FROM $sql_from$sql_sort_table" . POSTS_TABLE . " p indvdum@0: WHERE MATCH ($sql_match) AGAINST ('" . $db->sql_escape(htmlspecialchars_decode($this->search_query)) . "' IN BOOLEAN MODE) indvdum@0: $sql_where_options indvdum@0: ORDER BY $sql_sort"; indvdum@0: $result = $db->sql_query_limit($sql, $config['search_block_size'], $start); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $id_ary[] = (int) $row[$field]; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: $id_ary = array_unique($id_ary); indvdum@0: indvdum@0: if (!sizeof($id_ary)) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: // if the total result count is not cached yet, retrieve it from the db indvdum@0: if (!$result_count) indvdum@0: { indvdum@0: $sql = 'SELECT FOUND_ROWS() as result_count'; indvdum@0: $result = $db->sql_query($sql); indvdum@0: $result_count = (int) $db->sql_fetchfield('result_count'); indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: if (!$result_count) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page indvdum@0: $this->save_ids($search_key, implode(' ', $this->split_words), $author_ary, $result_count, $id_ary, $start, $sort_dir); indvdum@0: $id_ary = array_slice($id_ary, 0, (int) $per_page); indvdum@0: indvdum@0: return $result_count; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Performs a search on an author's posts without caring about message contents. Depends on display specific params indvdum@0: * indvdum@0: * @param string $type contains either posts or topics depending on what should be searched for indvdum@0: * @param boolean $firstpost_only if true, only topic starting posts will be considered indvdum@0: * @param array $sort_by_sql contains SQL code for the ORDER BY part of a query indvdum@0: * @param string $sort_key is the key of $sort_by_sql for the selected sorting indvdum@0: * @param string $sort_dir is either a or d representing ASC and DESC indvdum@0: * @param string $sort_days specifies the maximum amount of days a post may be old indvdum@0: * @param array $ex_fid_ary specifies an array of forum ids which should not be searched indvdum@0: * @param array $m_approve_fid_ary specifies an array of forum ids in which the searcher is allowed to view unapproved posts indvdum@0: * @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 indvdum@0: * @param array $author_ary an array of author ids indvdum@0: * @param string $author_name specifies the author match, when ANONYMOUS is also a search-match indvdum@0: * @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered indvdum@0: * @param int $start indicates the first index of the page indvdum@0: * @param int $per_page number of ids each page is supposed to contain indvdum@0: * @return boolean|int total number of results indvdum@0: * indvdum@0: * @access public indvdum@0: */ indvdum@0: function author_search($type, $firstpost_only, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_ary, $author_name, &$id_ary, $start, $per_page) indvdum@0: { indvdum@0: global $config, $db; indvdum@0: indvdum@0: // No author? No posts. indvdum@0: if (!sizeof($author_ary)) indvdum@0: { indvdum@0: return 0; indvdum@0: } indvdum@0: indvdum@0: // generate a search_key from all the options to identify the results indvdum@0: $search_key = md5(implode('#', array( indvdum@0: '', indvdum@0: $type, indvdum@0: ($firstpost_only) ? 'firstpost' : '', indvdum@0: '', indvdum@0: '', indvdum@0: $sort_days, indvdum@0: $sort_key, indvdum@0: $topic_id, indvdum@0: implode(',', $ex_fid_ary), indvdum@0: implode(',', $m_approve_fid_ary), indvdum@0: implode(',', $author_ary), indvdum@0: $author_name, indvdum@0: ))); indvdum@0: indvdum@0: // try reading the results from cache indvdum@0: $result_count = 0; indvdum@0: if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) indvdum@0: { indvdum@0: return $result_count; indvdum@0: } indvdum@0: indvdum@0: $id_ary = array(); indvdum@0: indvdum@0: // Create some display specific sql strings indvdum@0: if ($author_name) indvdum@0: { indvdum@0: // first one matches post of registered users, second one guests and deleted users indvdum@0: $sql_author = '(' . $db->sql_in_set('p.poster_id', array_diff($author_ary, array(ANONYMOUS)), false, true) . ' OR p.post_username ' . $author_name . ')'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $sql_author = $db->sql_in_set('p.poster_id', $author_ary); indvdum@0: } indvdum@0: $sql_fora = (sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : ''; indvdum@0: $sql_topic_id = ($topic_id) ? ' AND p.topic_id = ' . (int) $topic_id : ''; indvdum@0: $sql_time = ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : ''; indvdum@0: $sql_firstpost = ($firstpost_only) ? ' AND p.post_id = t.topic_first_post_id' : ''; indvdum@0: indvdum@0: // Build sql strings for sorting indvdum@0: $sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC'); indvdum@0: $sql_sort_table = $sql_sort_join = ''; indvdum@0: switch ($sql_sort[0]) indvdum@0: { indvdum@0: case 'u': indvdum@0: $sql_sort_table = USERS_TABLE . ' u, '; indvdum@0: $sql_sort_join = ($type == 'posts') ? ' AND u.user_id = p.poster_id ' : ' AND u.user_id = t.topic_poster '; indvdum@0: break; indvdum@0: indvdum@0: case 't': indvdum@0: $sql_sort_table = ($type == 'posts' && !$firstpost_only) ? TOPICS_TABLE . ' t, ' : ''; indvdum@0: $sql_sort_join = ($type == 'posts' && !$firstpost_only) ? ' AND t.topic_id = p.topic_id ' : ''; indvdum@0: break; indvdum@0: indvdum@0: case 'f': indvdum@0: $sql_sort_table = FORUMS_TABLE . ' f, '; indvdum@0: $sql_sort_join = ' AND f.forum_id = p.forum_id '; indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: if (!sizeof($m_approve_fid_ary)) indvdum@0: { indvdum@0: $m_approve_fid_sql = ' AND p.post_approved = 1'; indvdum@0: } indvdum@0: else if ($m_approve_fid_ary == array(-1)) indvdum@0: { indvdum@0: $m_approve_fid_sql = ''; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $m_approve_fid_sql = ' AND (p.post_approved = 1 OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) . ')'; indvdum@0: } indvdum@0: indvdum@0: // If the cache was completely empty count the results indvdum@0: $calc_results = ($result_count) ? '' : 'SQL_CALC_FOUND_ROWS '; indvdum@0: indvdum@0: // Build the query for really selecting the post_ids indvdum@0: if ($type == 'posts') indvdum@0: { indvdum@0: $sql = "SELECT {$calc_results}p.post_id indvdum@0: FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t ' : ' ') . " indvdum@0: WHERE $sql_author indvdum@0: $sql_topic_id indvdum@0: $sql_firstpost indvdum@0: $m_approve_fid_sql indvdum@0: $sql_fora indvdum@0: $sql_sort_join indvdum@0: $sql_time indvdum@0: ORDER BY $sql_sort"; indvdum@0: $field = 'post_id'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $sql = "SELECT {$calc_results}t.topic_id indvdum@0: FROM " . $sql_sort_table . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p indvdum@0: WHERE $sql_author indvdum@0: $sql_topic_id indvdum@0: $sql_firstpost indvdum@0: $m_approve_fid_sql indvdum@0: $sql_fora indvdum@0: AND t.topic_id = p.topic_id indvdum@0: $sql_sort_join indvdum@0: $sql_time indvdum@0: GROUP BY t.topic_id indvdum@0: ORDER BY $sql_sort"; indvdum@0: $field = 'topic_id'; indvdum@0: } indvdum@0: indvdum@0: // Only read one block of posts from the db and then cache it indvdum@0: $result = $db->sql_query_limit($sql, $config['search_block_size'], $start); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $id_ary[] = (int) $row[$field]; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: // retrieve the total result count if needed indvdum@0: if (!$result_count) indvdum@0: { indvdum@0: $sql = 'SELECT FOUND_ROWS() as result_count'; indvdum@0: $result = $db->sql_query($sql); indvdum@0: $result_count = (int) $db->sql_fetchfield('result_count'); indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: if (!$result_count) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if (sizeof($id_ary)) indvdum@0: { indvdum@0: $this->save_ids($search_key, '', $author_ary, $result_count, $id_ary, $start, $sort_dir); indvdum@0: $id_ary = array_slice($id_ary, 0, $per_page); indvdum@0: indvdum@0: return $result_count; indvdum@0: } indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Destroys cached search results, that contained one of the new words in a post so the results won't be outdated. indvdum@0: * indvdum@0: * @param string $mode contains the post mode: edit, post, reply, quote ... indvdum@0: */ indvdum@0: function index($mode, $post_id, &$message, &$subject, $poster_id, $forum_id) indvdum@0: { indvdum@0: global $db; indvdum@0: indvdum@0: // Split old and new post/subject to obtain array of words indvdum@0: $split_text = $this->split_message($message); indvdum@0: $split_title = ($subject) ? $this->split_message($subject) : array(); indvdum@0: indvdum@0: $words = array_unique(array_merge($split_text, $split_title)); indvdum@0: indvdum@0: unset($split_text); indvdum@0: unset($split_title); indvdum@0: indvdum@0: // destroy cached search results containing any of the words removed or added indvdum@0: $this->destroy_cache($words, array($poster_id)); indvdum@0: indvdum@0: unset($words); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Destroy cached results, that might be outdated after deleting a post indvdum@0: */ indvdum@0: function index_remove($post_ids, $author_ids, $forum_ids) indvdum@0: { indvdum@0: $this->destroy_cache(array(), $author_ids); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Destroy old cache entries indvdum@0: */ indvdum@0: function tidy() indvdum@0: { indvdum@0: global $db, $config; indvdum@0: indvdum@0: // destroy too old cached search results indvdum@0: $this->destroy_cache(array()); indvdum@0: indvdum@0: set_config('search_last_gc', time(), true); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Create fulltext index indvdum@0: */ indvdum@0: function create_index($acp_module, $u_action) indvdum@0: { indvdum@0: global $db; indvdum@0: indvdum@0: // Make sure we can actually use MySQL with fulltext indexes indvdum@0: if ($error = $this->init()) indvdum@0: { indvdum@0: return $error; indvdum@0: } indvdum@0: indvdum@0: if (empty($this->stats)) indvdum@0: { indvdum@0: $this->get_stats(); indvdum@0: } indvdum@0: indvdum@0: $alter = array(); indvdum@0: indvdum@0: if (!isset($this->stats['post_subject'])) indvdum@0: { indvdum@0: if ($db->sql_layer == 'mysqli' || version_compare($db->sql_server_info(true), '4.1.3', '>=')) indvdum@0: { indvdum@0: //$alter[] = 'MODIFY post_subject varchar(100) COLLATE utf8_unicode_ci DEFAULT \'\' NOT NULL'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $alter[] = 'MODIFY post_subject text NOT NULL'; indvdum@0: } indvdum@0: $alter[] = 'ADD FULLTEXT (post_subject)'; indvdum@0: } indvdum@0: indvdum@0: if (!isset($this->stats['post_text'])) indvdum@0: { indvdum@0: if ($db->sql_layer == 'mysqli' || version_compare($db->sql_server_info(true), '4.1.3', '>=')) indvdum@0: { indvdum@0: $alter[] = 'MODIFY post_text mediumtext COLLATE utf8_unicode_ci NOT NULL'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $alter[] = 'MODIFY post_text mediumtext NOT NULL'; indvdum@0: } indvdum@0: $alter[] = 'ADD FULLTEXT (post_text)'; indvdum@0: } indvdum@0: indvdum@0: if (!isset($this->stats['post_content'])) indvdum@0: { indvdum@0: $alter[] = 'ADD FULLTEXT post_content (post_subject, post_text)'; indvdum@0: } indvdum@0: indvdum@0: if (sizeof($alter)) indvdum@0: { indvdum@0: $db->sql_query('ALTER TABLE ' . POSTS_TABLE . ' ' . implode(', ', $alter)); indvdum@0: } indvdum@0: indvdum@0: $db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE); indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Drop fulltext index indvdum@0: */ indvdum@0: function delete_index($acp_module, $u_action) indvdum@0: { indvdum@0: global $db; indvdum@0: indvdum@0: // Make sure we can actually use MySQL with fulltext indexes indvdum@0: if ($error = $this->init()) indvdum@0: { indvdum@0: return $error; indvdum@0: } indvdum@0: indvdum@0: if (empty($this->stats)) indvdum@0: { indvdum@0: $this->get_stats(); indvdum@0: } indvdum@0: indvdum@0: $alter = array(); indvdum@0: indvdum@0: if (isset($this->stats['post_subject'])) indvdum@0: { indvdum@0: $alter[] = 'DROP INDEX post_subject'; indvdum@0: } indvdum@0: indvdum@0: if (isset($this->stats['post_text'])) indvdum@0: { indvdum@0: $alter[] = 'DROP INDEX post_text'; indvdum@0: } indvdum@0: indvdum@0: if (isset($this->stats['post_content'])) indvdum@0: { indvdum@0: $alter[] = 'DROP INDEX post_content'; indvdum@0: } indvdum@0: indvdum@0: if (sizeof($alter)) indvdum@0: { indvdum@0: $db->sql_query('ALTER TABLE ' . POSTS_TABLE . ' ' . implode(', ', $alter)); indvdum@0: } indvdum@0: indvdum@0: $db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE); indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Returns true if both FULLTEXT indexes exist indvdum@0: */ indvdum@0: function index_created() indvdum@0: { indvdum@0: if (empty($this->stats)) indvdum@0: { indvdum@0: $this->get_stats(); indvdum@0: } indvdum@0: indvdum@0: return (isset($this->stats['post_text']) && isset($this->stats['post_subject']) && isset($this->stats['post_content'])) ? true : false; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Returns an associative array containing information about the indexes indvdum@0: */ indvdum@0: function index_stats() indvdum@0: { indvdum@0: global $user; indvdum@0: indvdum@0: if (empty($this->stats)) indvdum@0: { indvdum@0: $this->get_stats(); indvdum@0: } indvdum@0: indvdum@0: return array( indvdum@0: $user->lang['FULLTEXT_MYSQL_TOTAL_POSTS'] => ($this->index_created()) ? $this->stats['total_posts'] : 0, indvdum@0: ); indvdum@0: } indvdum@0: indvdum@0: function get_stats() indvdum@0: { indvdum@0: global $db; indvdum@0: indvdum@0: if (strpos($db->sql_layer, 'mysql') === false) indvdum@0: { indvdum@0: $this->stats = array(); indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: $sql = 'SHOW INDEX indvdum@0: FROM ' . POSTS_TABLE; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: // deal with older MySQL versions which didn't use Index_type indvdum@0: $index_type = (isset($row['Index_type'])) ? $row['Index_type'] : $row['Comment']; indvdum@0: indvdum@0: if ($index_type == 'FULLTEXT') indvdum@0: { indvdum@0: if ($row['Key_name'] == 'post_text') indvdum@0: { indvdum@0: $this->stats['post_text'] = $row; indvdum@0: } indvdum@0: else if ($row['Key_name'] == 'post_subject') indvdum@0: { indvdum@0: $this->stats['post_subject'] = $row; indvdum@0: } indvdum@0: else if ($row['Key_name'] == 'post_content') indvdum@0: { indvdum@0: $this->stats['post_content'] = $row; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: $sql = 'SELECT COUNT(post_id) as total_posts indvdum@0: FROM ' . POSTS_TABLE; indvdum@0: $result = $db->sql_query($sql); indvdum@0: $this->stats['total_posts'] = (int) $db->sql_fetchfield('total_posts'); indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Display a note, that UTF-8 support is not available with certain versions of PHP indvdum@0: */ indvdum@0: function acp() indvdum@0: { indvdum@0: global $user, $config; indvdum@0: indvdum@0: $tpl = ' indvdum@0: