📄 fulltext_mysql.php
字号:
{ return false; } // if the total result count is not cached yet, retrieve it from the db if (!$result_count) { $sql = 'SELECT FOUND_ROWS() as result_count'; $result = $db->sql_query($sql); $result_count = (int) $db->sql_fetchfield('result_count'); $db->sql_freeresult($result); if (!$result_count) { 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, $result_count, $id_ary, $start, $sort_dir); $id_ary = array_slice($id_ary, 0, (int) $per_page); return $result_count; } /** * Performs a search on an author's posts without caring about message contents. Depends on display specific params * * @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 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 $result_count = 0; if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) { return $result_count; } $id_ary = array(); // Create some display specific sql strings $sql_author = $db->sql_in_set('p.poster_id', $author_ary); $sql_fora = (sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : ''; $sql_topic_id = ($topic_id) ? ' AND p.topic_id = ' . (int) $topic_id : ''; $sql_time = ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : ''; // 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 = ($type == 'posts') ? ' AND u.user_id = p.poster_id ' : ' AND u.user_id = t.topic_poster '; 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 ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) . ')'; } // If the cache was completely empty count the results $calc_results = ($result_count) ? '' : 'SQL_CALC_FOUND_ROWS '; // Build the query for really selecting the post_ids if ($type == 'posts') { $sql = "SELECT {$calc_results}p.post_id FROM " . $sql_sort_table . POSTS_TABLE . " p 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 {$calc_results}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 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); // retrieve the total result count if needed if (!$result_count) { $sql = 'SELECT FOUND_ROWS() as result_count'; $result = $db->sql_query($sql); $result_count = (int) $db->sql_fetchfield('result_count'); $db->sql_freeresult($result); if (!$result_count) { return false; } } if (sizeof($id_ary)) { $this->save_ids($search_key, '', $author_ary, $result_count, $id_ary, $start, $sort_dir); $id_ary = array_slice($id_ary, 0, $per_page); return $result_count; } return false; } /** * Destroys cached search results, that contained one of the new words in a post so the results won't be outdated. * * @param string $mode contains the post mode: edit, post, reply, quote ... */ function index($mode, $post_id, &$message, &$subject, $poster_id, $forum_id) { global $db; // Split old and new post/subject to obtain array of words $split_text = $this->split_message($message); $split_title = ($subject) ? $this->split_message($subject) : array(); $words = array(); if ($mode == 'edit') { $old_text = array(); $old_title = array(); $sql = 'SELECT post_text, post_subject FROM ' . POSTS_TABLE . " WHERE post_id = $post_id"; $result = $db->sql_query($sql); if ($row = $db->sql_fetchrow($result)) { $old_text = $this->split_message($row['post_text']); $old_title = $this->split_message($row['post_subject']); } $db->sql_freeresult($result); $words = array_unique(array_merge( array_diff($split_text, $old_text), array_diff($split_title, $old_title), array_diff($old_text, $split_text), array_diff($old_title, $split_title) )); unset($old_title); unset($old_text); } else { $words = array_unique(array_merge($split_text, $split_title)); } unset($split_text); unset($split_title); // destroy cached search results containing any of the words removed or added $this->destroy_cache($words, array($poster_id)); unset($words); } /** * Destroy cached results, that might be outdated after deleting a post */ function index_remove($post_ids, $author_ids, $forum_ids) { $this->destroy_cache(array(), $author_ids); } /** * Destroy old cache entries */ function tidy() { global $db, $config; // destroy too old cached search results $this->destroy_cache(array()); set_config('search_last_gc', time(), true); } /** * Create fulltext index */ function create_index($acp_module, $u_action) { global $db; // Make sure we can actually use MySQL with fulltext indexes if ($error = $this->init()) { return $error; } if (empty($this->stats)) { $this->get_stats(); } if (!isset($this->stats['post_subject'])) { $db->sql_query('ALTER TABLE ' . POSTS_TABLE . ' ADD FULLTEXT (post_subject)'); } if (!isset($this->stats['post_text'])) { $db->sql_query('ALTER TABLE ' . POSTS_TABLE . ' ADD FULLTEXT (post_text)'); } $db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE); return false; } /** * Drop fulltext index */ function delete_index($acp_module, $u_action) { global $db; // Make sure we can actually use MySQL with fulltext indexes if ($error = $this->init()) { return $error; } if (empty($this->stats)) { $this->get_stats(); } if (isset($this->stats['post_subject'])) { $db->sql_query('ALTER TABLE ' . POSTS_TABLE . ' DROP INDEX post_subject'); } if (isset($this->stats['post_text'])) { $db->sql_query('ALTER TABLE ' . POSTS_TABLE . ' DROP INDEX post_text'); } $db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE); return false; } /** * Returns true if both FULLTEXT indexes exist */ function index_created() { if (empty($this->stats)) { $this->get_stats(); } return (isset($this->stats['post_text']) && isset($this->stats['post_subject'])) ? true : false; } /** * Returns an associative array containing information about the indexes */ function index_stats() { global $user; if (empty($this->stats)) { $this->get_stats(); } return array( $user->lang['FULLTEXT_MYSQL_TOTAL_POSTS'] => ($this->index_created()) ? $this->stats['total_posts'] : 0, $user->lang['FULLTEXT_MYSQL_TEXT_CARDINALITY'] => isset($this->stats['post_text']['Cardinality']) ? $this->stats['post_text']['Cardinality'] : 0, $user->lang['FULLTEXT_MYSQL_SUBJECT_CARDINALITY'] => isset($this->stats['post_subject']['Cardinality']) ? $this->stats['post_subject']['Cardinality'] : 0, ); } function get_stats() { global $db; if (strpos($db->sql_layer, 'mysql') === false) { $this->stats = array(); return; } $sql = 'SHOW INDEX FROM ' . POSTS_TABLE; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { // deal with older MySQL versions which didn't use Index_type $index_type = (isset($row['Index_type'])) ? $row['Index_type'] : $row['Comment']; if ($index_type == 'FULLTEXT') { if ($row['Column_name'] == 'post_text') { $this->stats['post_text'] = $row; } else if ($row['Column_name'] == 'post_subject') { $this->stats['post_subject'] = $row; } } } $db->sql_freeresult($result); $sql = 'SELECT COUNT(post_id) as total_posts FROM ' . POSTS_TABLE; $result = $db->sql_query($sql); $this->stats['total_posts'] = (int) $db->sql_fetchfield('total_posts'); $db->sql_freeresult($result); }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -