indvdum@0: ignore_words)) indvdum@0: { indvdum@0: global $user, $phpEx; indvdum@0: indvdum@0: $words = array(); indvdum@0: indvdum@0: if (file_exists("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx")) indvdum@0: { indvdum@0: // include the file containing ignore words indvdum@0: include("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx"); indvdum@0: } indvdum@0: indvdum@0: $this->ignore_words = $words; indvdum@0: unset($words); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Stores a list of synonyms that should be replaced in $this->match_synonym and $this->replace_synonym and caches them indvdum@0: */ indvdum@0: function get_synonyms() indvdum@0: { indvdum@0: if (!sizeof($this->match_synonym)) indvdum@0: { indvdum@0: global $user, $phpEx; indvdum@0: indvdum@0: $synonyms = array(); indvdum@0: indvdum@0: if (file_exists("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx")) indvdum@0: { indvdum@0: // include the file containing synonyms indvdum@0: include("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx"); indvdum@0: } indvdum@0: indvdum@0: $this->match_synonym = array_keys($synonyms); indvdum@0: $this->replace_synonym = array_values($synonyms); indvdum@0: indvdum@0: unset($synonyms); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Retrieves cached search results indvdum@0: * indvdum@0: * @param int &$result_count will contain the number of all results for the search (not only for the current page) indvdum@0: * @param array &$id_ary is filled with the ids belonging to the requested page that are stored in the cache indvdum@0: * indvdum@0: * @return int SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE indvdum@0: */ indvdum@0: function obtain_ids($search_key, &$result_count, &$id_ary, $start, $per_page, $sort_dir) indvdum@0: { indvdum@0: global $cache; indvdum@0: indvdum@0: if (!($stored_ids = $cache->get('_search_results_' . $search_key))) indvdum@0: { indvdum@0: // no search results cached for this search_key indvdum@0: return SEARCH_RESULT_NOT_IN_CACHE; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $result_count = $stored_ids[-1]; indvdum@0: $reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false; indvdum@0: $complete = true; indvdum@0: indvdum@0: // change the start to the actual end of the current request if the sort direction differs indvdum@0: // from the dirction in the cache and reverse the ids later indvdum@0: if ($reverse_ids) indvdum@0: { indvdum@0: $start = $result_count - $start - $per_page; indvdum@0: indvdum@0: // the user requested a page past the last index indvdum@0: if ($start < 0) indvdum@0: { indvdum@0: return SEARCH_RESULT_NOT_IN_CACHE; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++) indvdum@0: { indvdum@0: if (!isset($stored_ids[$i])) indvdum@0: { indvdum@0: $complete = false; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $id_ary[] = $stored_ids[$i]; indvdum@0: } indvdum@0: } indvdum@0: unset($stored_ids); indvdum@0: indvdum@0: if ($reverse_ids) indvdum@0: { indvdum@0: $id_ary = array_reverse($id_ary); indvdum@0: } indvdum@0: indvdum@0: if (!$complete) indvdum@0: { indvdum@0: return SEARCH_RESULT_INCOMPLETE; indvdum@0: } indvdum@0: return SEARCH_RESULT_IN_CACHE; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Caches post/topic ids indvdum@0: * indvdum@0: * @param array &$id_ary contains a list of post or topic ids that shall be cached, the first element indvdum@0: * must have the absolute index $start in the result set. indvdum@0: */ indvdum@0: function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir) indvdum@0: { indvdum@0: global $cache, $config, $db, $user; indvdum@0: indvdum@0: $length = min(sizeof($id_ary), $config['search_block_size']); indvdum@0: indvdum@0: // nothing to cache so exit indvdum@0: if (!$length) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: $store_ids = array_slice($id_ary, 0, $length); indvdum@0: indvdum@0: // create a new resultset if there is none for this search_key yet indvdum@0: // or add the ids to the existing resultset indvdum@0: if (!($store = $cache->get('_search_results_' . $search_key))) indvdum@0: { indvdum@0: // add the current keywords to the recent searches in the cache which are listed on the search page indvdum@0: if (!empty($keywords) || sizeof($author_ary)) indvdum@0: { indvdum@0: $sql = 'SELECT search_time indvdum@0: FROM ' . SEARCH_RESULTS_TABLE . ' indvdum@0: WHERE search_key = \'' . $db->sql_escape($search_key) . '\''; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: if (!$db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $sql_ary = array( indvdum@0: 'search_key' => $search_key, indvdum@0: 'search_time' => time(), indvdum@0: 'search_keywords' => $keywords, indvdum@0: 'search_authors' => ' ' . implode(' ', $author_ary) . ' ' indvdum@0: ); indvdum@0: indvdum@0: $sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary); indvdum@0: $db->sql_query($sql); indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: $sql = 'UPDATE ' . USERS_TABLE . ' indvdum@0: SET user_last_search = ' . time() . ' indvdum@0: WHERE user_id = ' . $user->data['user_id']; indvdum@0: $db->sql_query($sql); indvdum@0: indvdum@0: $store = array(-1 => $result_count, -2 => $sort_dir); indvdum@0: $id_range = range($start, $start + $length - 1); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // we use one set of results for both sort directions so we have to calculate the indizes indvdum@0: // for the reversed array and we also have to reverse the ids themselves indvdum@0: if ($store[-2] != $sort_dir) indvdum@0: { indvdum@0: $store_ids = array_reverse($store_ids); indvdum@0: $id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $id_range = range($start, $start + $length - 1); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $store_ids = array_combine($id_range, $store_ids); indvdum@0: indvdum@0: // append the ids indvdum@0: if (is_array($store_ids)) indvdum@0: { indvdum@0: $store += $store_ids; indvdum@0: indvdum@0: // if the cache is too big indvdum@0: if (sizeof($store) - 2 > 20 * $config['search_block_size']) indvdum@0: { indvdum@0: // remove everything in front of two blocks in front of the current start index indvdum@0: for ($i = 0, $n = $id_range[0] - 2 * $config['search_block_size']; $i < $n; $i++) indvdum@0: { indvdum@0: if (isset($store[$i])) indvdum@0: { indvdum@0: unset($store[$i]); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // remove everything after two blocks after the current stop index indvdum@0: end($id_range); indvdum@0: for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $config['search_block_size']; $i > $n; $i--) indvdum@0: { indvdum@0: if (isset($store[$i])) indvdum@0: { indvdum@0: unset($store[$i]); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: $cache->put('_search_results_' . $search_key, $store, $config['search_store_results']); indvdum@0: indvdum@0: $sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . ' indvdum@0: SET search_time = ' . time() . ' indvdum@0: WHERE search_key = \'' . $db->sql_escape($search_key) . '\''; indvdum@0: $db->sql_query($sql); indvdum@0: } indvdum@0: indvdum@0: unset($store); indvdum@0: unset($store_ids); indvdum@0: unset($id_range); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Removes old entries from the search results table and removes searches with keywords that contain a word in $words. indvdum@0: */ indvdum@0: function destroy_cache($words, $authors = false) indvdum@0: { indvdum@0: global $db, $cache, $config; indvdum@0: indvdum@0: // clear all searches that searched for the specified words indvdum@0: if (sizeof($words)) indvdum@0: { indvdum@0: $sql_where = ''; indvdum@0: foreach ($words as $word) indvdum@0: { indvdum@0: $sql_where .= " OR search_keywords " . $db->sql_like_expression($db->any_char . $word . $db->any_char); indvdum@0: } indvdum@0: indvdum@0: $sql = 'SELECT search_key indvdum@0: FROM ' . SEARCH_RESULTS_TABLE . " indvdum@0: WHERE search_keywords LIKE '%*%' $sql_where"; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $cache->destroy('_search_results_' . $row['search_key']); indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: // clear all searches that searched for the specified authors indvdum@0: if (is_array($authors) && sizeof($authors)) indvdum@0: { indvdum@0: $sql_where = ''; indvdum@0: foreach ($authors as $author) indvdum@0: { indvdum@0: $sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors LIKE \'% ' . (int) $author . ' %\''; indvdum@0: } indvdum@0: indvdum@0: $sql = 'SELECT search_key indvdum@0: FROM ' . SEARCH_RESULTS_TABLE . " indvdum@0: WHERE $sql_where"; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $cache->destroy('_search_results_' . $row['search_key']); indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: $sql = 'DELETE indvdum@0: FROM ' . SEARCH_RESULTS_TABLE . ' indvdum@0: WHERE search_time < ' . (time() - $config['search_store_results']); indvdum@0: $db->sql_query($sql); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: ?>