diff options
author | Nils Adermann <naderman@naderman.de> | 2006-01-11 18:56:07 +0000 |
---|---|---|
committer | Nils Adermann <naderman@naderman.de> | 2006-01-11 18:56:07 +0000 |
commit | 0e0b1120fba8ed4f2ebc5d62eb29b1a34c1b1007 (patch) | |
tree | 5cd57f820281c20c6936433d92483cc4712a7ab7 /phpBB | |
parent | 9ea5fa1768feebfb04f2303788eb4c685161e3dd (diff) | |
download | forums-0e0b1120fba8ed4f2ebc5d62eb29b1a34c1b1007.tar forums-0e0b1120fba8ed4f2ebc5d62eb29b1a34c1b1007.tar.gz forums-0e0b1120fba8ed4f2ebc5d62eb29b1a34c1b1007.tar.bz2 forums-0e0b1120fba8ed4f2ebc5d62eb29b1a34c1b1007.tar.xz forums-0e0b1120fba8ed4f2ebc5d62eb29b1a34c1b1007.zip |
- overhauled search system
- updated structure for search backend plugins
- better result caching using ACM
- search results no longer session restricted => link to them by copying the URL :)
- in-topic search
- indexing posts now uses search backend plugins
- develop/search_fill.php working again
- fulltext_mysql not working yet
- tiny bugfixes to ACM
git-svn-id: file:///svn/phpbb/trunk@5441 89ea8834-ac86-4346-8a33-228a782c2dd0
Diffstat (limited to 'phpBB')
22 files changed, 1484 insertions, 998 deletions
diff --git a/phpBB/develop/search_fill.php b/phpBB/develop/search_fill.php index d8a2dfc11d..a4337245fa 100644 --- a/phpBB/develop/search_fill.php +++ b/phpBB/develop/search_fill.php @@ -13,21 +13,34 @@ // set_time_limit(0); -$phpbb_root_path = "../"; -include($phpbb_root_path . 'extension.inc'); +define('IN_PHPBB', true); +$phpbb_root_path = '../'; +$phpEx = substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.'.$phpEx); -include($phpbb_root_path . 'includes/search.'.$phpEx); -$common_percent = 0.4; // Percentage of posts in which a word has to appear to be marked as common +// Start session management +$user->session_begin(); +$auth->acl($user->data); +$user->setup(); -print "<html>\n<body>\n"; +$search_type = $config['search_type']; -// -// Try and load stopword and synonym files -// -// This needs fixing! Shouldn't be hardcoded to English files! -$stopword_array = file($phpbb_root_path . "language/lang_english/search_stopwords.txt"); -$synonym_array = file($phpbb_root_path . "language/lang_english/search_synonyms.txt"); +if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx)) +{ + trigger_error('NO_SUCH_SEARCH_MODULE'); +} + +require($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx); + +$error = false; +$search = new $search_type($error); + +if ($error) +{ + trigger_error($error); +} + +print "<html>\n<body>\n"; // // Fetch a batch of posts_text entries @@ -74,9 +87,9 @@ for(;$postcounter <= $max_post_id; $postcounter += $batchsize) if( $post_rows ) { - // $sql = "LOCK TABLES ".POST_TEXT_TABLE." WRITE"; - // $result = $db->sql_query($sql); - print "\n<p>\n<a href='$PHP_SELF?batchstart=$batchstart'>Restart from posting $batchstart</a><br>\n"; + // $sql = "LOCK TABLES ".POST_TEXT_TABLE." WRITE"; + // $result = $db->sql_query($sql); + print "\n<p>\n<a href='{$_SERVER['PHP_SELF']}?batchstart=$batchstart'>Restart from posting $batchstart</a><br>\n"; // For every post in the batch: for($post_nr = 0; $post_nr < $post_rows; $post_nr++ ) @@ -86,105 +99,19 @@ for(;$postcounter <= $max_post_id; $postcounter += $batchsize) $post_id = $rowset[$post_nr]['post_id']; - $matches = array(); - $matches['text'] = split_words(clean_words("post", $rowset[$post_nr]['post_text'], $stopword_array, $synonym_array)); - $matches['title'] = split_words(clean_words("post", $rowset[$post_nr]['post_subject'], $stopword_array, $synonym_array)); - - while( list($match_type, $match_ary) = @each($matches) ) - { - $title_match = ( $match_type == 'title' ) ? 1 : 0; - - $num_matches = count($match_ary); - - if ( $num_matches < 1 ) - { - // Skip this post if no words where found - continue; - } - - // For all words in the posting - $sql_in = ""; - - $sql_insert = ''; - $sql_select = ''; - - $word = array(); - $word_count = array(); - - for($j = 0; $j < $num_matches; $j++) - { - $this_word = strtolower(trim($match_ary[$j])); - if ( $this_word != '' ) - { - $word_count[$this_word] = ( isset($word_count[$this_word]) ) ? $word_count[$this_word] + 1 : 0; - $comma = ($sql_insert != '')? ', ': ''; - - $sql_insert .= "$comma('" . $this_word . "')"; - $sql_select .= "$comma'" . $this_word . "'"; - } - } - - if ( $sql_insert == '' ) - { - die("no words found"); - } - - $sql = 'INSERT IGNORE INTO ' . SEARCH_WORD_TABLE . " - (word_text) - VALUES $sql_insert"; - if ( !$result = $db->sql_query($sql) ) - { - $error = $db->sql_error(); - die("Couldn't INSERT words :: " . $sql . " :: " . $error['message']); - } - - // Get the word_id's out of the DB (to see if they are already there) - $sql = "SELECT word_id, word_text - FROM " . SEARCH_WORD_TABLE . " - WHERE word_text IN ($sql_select) - GROUP BY word_text"; - $result = $db->sql_query($sql); - if ( !$result ) - { - $error = $db->sql_error(); - die("Couldn't select words :: " . $sql . " :: " . $error['message']); - } - - $sql_insert = array(); - while( $row = $db->sql_fetchrow($result) ) - { - $sql_insert[] = "($post_id, " . $row['word_id'] . ", $title_match)"; - } - - $db->sql_freeresult($result); - - $sql = "INSERT INTO " . SEARCH_MATCH_TABLE . " - (post_id, word_id, title_match) - VALUES " . implode(", ", $sql_insert); - $result = $db->sql_query($sql); - if ( !$result ) - { - $error = $db->sql_error(); - die("Couldn't insert new word match :: " . $sql . " :: " . $error['message']); - } - - } // All posts + $search->index('post', $rowset[$post_nr]['post_id'], $rowset[$post_nr]['post_text'], $rowset[$post_nr]['post_subject']); } - - // $sql = "UNLOCK TABLES"; - // $result = $db->sql_query($sql); + // $sql = "UNLOCK TABLES"; + // $result = $db->sql_query($sql); } - - // Remove common words after the first 2 batches and after every 4th batch after that. - if( $batchcount % 4 == 3 ) - { - print "<br>Removing common words (words that appear in more than $common_percent of the posts)<br>\n"; - flush(); - print "Removed ". remove_common("global", $common_percent) ." words that where too common.<br>"; - } } +print "<br>Removing common words (words that appear in more than 50% of the posts)<br>\n"; +flush(); +$search->tidy(); +print "Removed words that where too common.<br>"; + echo "<br>Done"; ?> diff --git a/phpBB/includes/acm/acm_db.php b/phpBB/includes/acm/acm_db.php index 0204666aa4..83f52362cd 100644 --- a/phpBB/includes/acm/acm_db.php +++ b/phpBB/includes/acm/acm_db.php @@ -57,7 +57,7 @@ class acm if (!$var_names) { - $var_requested[] = $row['var_name']; + $this->var_requested[] = $row['var_name']; } } } @@ -137,7 +137,7 @@ class acm if ($var_name{0} == '_') { - if (!in_array($this->var_requested, $var_name)) + if (!in_array($var_name, $this->var_requested)) { $this->var_requested[] = $var_name; diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index 0df3c05a98..fa430b9247 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -154,6 +154,11 @@ class acm { global $phpEx; + if (!$this->_exists($var_name)) + { + return; + } + if ($var_name == 'sql' && !empty($table)) { $regex = '(' . ((is_array($table)) ? implode('|', $table) : $table) . ')'; diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index 559b8bef65..fbcd0b0a03 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -564,7 +564,7 @@ class acp_board $dp = opendir($phpbb_root_path . 'includes/search'); while ($file = readdir($dp)) { - if (preg_match('#\.' . $phpEx . '$#', $file)) + if ((preg_match('#\.' . $phpEx . '$#', $file)) && ($file != "search.$phpEx")) { $search_plugins[] = preg_replace('#^(.*?)\.' . $phpEx . '$#', '\1', $file); } diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 6b0b5d3642..80639c29b2 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2057,6 +2057,11 @@ function page_footer() // Tidy some table rows every week $cron_type = 'tidy_database'; } + else if (time() - $config['search_last_gc'] > $config['search_gc']) + { + // Tidy the cache + $cron_type = 'tidy_search'; + } /** * @todo add session garbage collection diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index 3929b43448..08f90fab83 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -509,7 +509,7 @@ function delete_topics($where_type, $where_ids, $auto_sync = true) */ function delete_posts($where_type, $where_ids, $auto_sync = true) { - global $db; + global $db, $config, $phpbb_root_path, $phpEx; if (is_array($where_ids)) { @@ -542,7 +542,7 @@ function delete_posts($where_type, $where_ids, $auto_sync = true) $db->sql_transaction('begin'); - $table_ary = array(POSTS_TABLE, REPORTS_TABLE, SEARCH_MATCH_TABLE); + $table_ary = array(POSTS_TABLE, REPORTS_TABLE); foreach ($table_ary as $table) { @@ -552,6 +552,26 @@ function delete_posts($where_type, $where_ids, $auto_sync = true) } unset($table_ary); + // Remove the message from the search index + $search_type = $config['search_type']; + + if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx)) + { + trigger_error('NO_SUCH_SEARCH_MODULE'); + } + + require("{$phpbb_root_path}includes/search/$search_type.$phpEx"); + + $error = false; + $search = new $search_type($error); + + if ($error) + { + trigger_error($error); + } + + $search->index_remove($where_ids); + delete_attachments('post', $post_ids, false); $db->sql_transaction('commit'); diff --git a/phpBB/includes/message_parser.php b/phpBB/includes/message_parser.php index 1aad064c07..cb7b898d12 100644 --- a/phpBB/includes/message_parser.php +++ b/phpBB/includes/message_parser.php @@ -1212,325 +1212,4 @@ class parse_message extends bbcode_firstpass } } -/** -* @package phpBB3 -* Parses a given message and updates/maintains the fulltext tables -* @todo replace fulltext_search in message_parser with search modules -*/ -class fulltext_search -{ - function split_words($mode, $text) - { - global $user, $config; - - static $drop_char_match, $drop_char_replace, $stopwords, $replace_synonym, $match_synonym; - - // Is the fulltext indexer disabled? If yes then we need not - // carry on ... it's okay ... I know when I'm not wanted boo hoo - if (!$config['load_search_upd']) - { - return; - } - - if (!is_array($drop_char_match)) - { - $drop_char_match = array('-', '^', '$', ';', '#', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '\'', '!', '*'); - $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', ' ', ' ', ' ', ' ', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' , ' ', ' ', ' ', ' ', ' '); - - if ($fp = @fopen($user->lang_path . '/search_stopwords.txt', 'rb')) - { - $stopwords = explode("\n", str_replace("\r\n", "\n", fread($fp, filesize($user->lang_path . '/search_stopwords.txt')))); - } - fclose($fp); - - if ($fp = @fopen($user->lang_path . '/search_synonyms.txt', 'rb')) - { - preg_match_all('#^(.*?) (.*?)$#ms', fread($fp, filesize($user->lang_path . '/search_synonyms.txt')), $match); - $replace_synonym = &$match[1]; - $match_synonym = &$match[2]; - } - fclose($fp); - } - - $match = array(); - // Comments for hardcoded bbcode elements (urls, smilies, html) - $match[] = '#<!\-\- .* \-\->(.*?)<!\-\- .* \-\->#is'; - // New lines, carriage returns - $match[] = "#[\n\r]+#"; - // NCRs like etc. - $match[] = '#(&|&)[\#a-z0-9]+?;#i'; - // Do not index code - $match[] = '#\[code=?.*?(\:?[0-9a-z]{5,})\].*?\[\/code(\:?[0-9a-z]{5,})\]#is'; - // BBcode - $match[] = '#\[\/?[a-z\*\+\-]+=?.*?(\:?[0-9a-z]{5,})\]#'; - // Sequences > min_search_chars & < max_search_chars -// $match[] = '#\s([\b]{1,' . $config['min_search_chars'] . '}|[\b]{' . $config['max_search_chars'] . ',})\s#is'; -// $match[] = '#\s((&\#[0-9]+;){1,' . $config['min_search_chars'] . '}|(&\#[0-9]+;){' . $config['max_search_chars'] . ',})\s#is'; - // Filter out ; and # but not &#[0-9]+; -// $match[] = '#(&\#[0-9]+;)|;|\#|&#'; - - $text = preg_replace($match, ' ', ' ' . strtolower(trim($text)) . ' '); - $text = str_replace(array(' + ', ' - '), array(' and ', ' not '), $text); - - // Filter out non-alphabetical chars - $text = str_replace($drop_char_match, $drop_char_replace, $text); - - // Split words - $text = explode(' ', preg_replace('#\s+#', ' ', trim($text))); - - if (sizeof($stopwords)) - { - $stopped_words = array_intersect($text, $stopwords); - $text = array_diff($text, $stopwords); - } - - if (sizeof($replace_synonym)) - { - $text = str_replace($replace_synonym, $match_synonym, $text); - } - - foreach ($text as $index => $word) - { - if (strlen($word) < $config['min_search_chars'] || strlen($word) > $config['max_search_chars']) - { - unset($text[$index]); - } - } - - return $text; - } - - function add($mode, $post_id, &$message, &$subject) - { - global $config, $db; - - // Is the fulltext indexer disabled? If yes then we need not - // carry on ... it's okay ... I know when I'm not wanted boo hoo - if (!$config['load_search_upd']) - { - return; - } - - // Split old and new post/subject to obtain array of 'words' - $split_text = $this->split_words('post', $message); - $split_title = ($subject) ? $this->split_words('post', $subject) : array(); - - $words = array(); - if ($mode == 'edit') - { - $words['add']['post'] = array(); - $words['add']['title'] = array(); - $words['del']['post'] = array(); - $words['del']['title'] = array(); - - $sql = 'SELECT w.word_id, w.word_text, m.title_match - FROM ' . SEARCH_WORD_TABLE . ' w, ' . SEARCH_MATCH_TABLE . " m - WHERE m.post_id = $post_id - AND w.word_id = m.word_id"; - $result = $db->sql_query($sql); - - $cur_words = array(); - while ($row = $db->sql_fetchrow($result)) - { - $which = ($row['title_match']) ? 'title' : 'post'; - $cur_words[$which][$row['word_text']] = $row['word_id']; - } - $db->sql_freeresult($result); - - $words['add']['post'] = array_diff($split_text, array_keys($cur_words['post'])); - $words['add']['title'] = array_diff($split_title, array_keys($cur_words['title'])); - $words['del']['post'] = array_diff(array_keys($cur_words['post']), $split_text); - $words['del']['title'] = array_diff(array_keys($cur_words['title']), $split_title); - } - else - { - $words['add']['post'] = $split_text; - $words['add']['title'] = $split_title; - $words['del']['post'] = array(); - $words['del']['title'] = array(); - } - unset($split_text); - unset($split_title); - - // Get unique words from the above arrays - $unique_add_words = array_unique(array_merge($words['add']['post'], $words['add']['title'])); - - // We now have unique arrays of all words to be added and removed and - // individual arrays of added and removed words for text and title. What - // we need to do now is add the new words (if they don't already exist) - // and then add (or remove) matches between the words and this post - if (sizeof($unique_add_words)) - { - $sql = 'SELECT word_id, word_text - FROM ' . SEARCH_WORD_TABLE . ' - WHERE word_text IN (' . implode(', ', preg_replace('#^(.*)$#', '\'$1\'', $unique_add_words)) . ")"; - $result = $db->sql_query($sql); - - $word_ids = array(); - while ($row = $db->sql_fetchrow($result)) - { - $word_ids[$row['word_text']] = $row['word_id']; - } - $db->sql_freeresult($result); - - $new_words = array_diff($unique_add_words, array_keys($word_ids)); - unset($unique_add_words); - - if (sizeof($new_words)) - { - switch (SQL_LAYER) - { - case 'mysql': - $sql = 'INSERT INTO ' . SEARCH_WORD_TABLE . ' (word_text) - VALUES ' . implode(', ', preg_replace('#^(.*)$#', '(\'$1\')', $new_words)); - $db->sql_query($sql); - break; - - case 'mysql4': - case 'mysqli': - case 'mssql': - case 'mssql_odbc': - case 'sqlite': - $sql = 'INSERT INTO ' . SEARCH_WORD_TABLE . ' (word_text) ' . implode(' UNION ALL ', preg_replace('#^(.*)$#', "SELECT '\$1'", $new_words)); - $db->sql_query($sql); - break; - - default: - foreach ($new_words as $word) - { - $sql = 'INSERT INTO ' . SEARCH_WORD_TABLE . " (word_text) - VALUES ('$word')"; - $db->sql_query($sql); - } - break; - } - } - unset($new_words); - } - - foreach ($words['del'] as $word_in => $word_ary) - { - $title_match = ($word_in == 'title') ? 1 : 0; - - if (sizeof($word_ary)) - { - $sql_in = array(); - foreach ($word_ary as $word) - { - $sql_in[] = $cur_words[$word_in][$word]; - } - - $sql = 'DELETE FROM ' . SEARCH_MATCH_TABLE . ' - WHERE word_id IN (' . implode(', ', $sql_in) . ') - AND post_id = ' . intval($post_id) . " - AND title_match = $title_match"; - $db->sql_query($sql); - unset($sql_in); - } - } - - foreach ($words['add'] as $word_in => $word_ary) - { - $title_match = ($word_in == 'title') ? 1 : 0; - - if (sizeof($word_ary)) - { - $sql = 'INSERT INTO ' . SEARCH_MATCH_TABLE . " (post_id, word_id, title_match) - SELECT $post_id, word_id, $title_match - FROM " . SEARCH_WORD_TABLE . ' - WHERE word_text IN (' . implode(', ', preg_replace('#^(.*)$#', '\'$1\'', $word_ary)) . ')'; - $db->sql_query($sql); - } - } - - unset($words); - - // Run the cleanup infrequently, once per session cleanup - if ($config['search_last_gc'] < time() - $config['search_gc']) - { - $this->search_tidy(); - } - } - - // Tidy up indexes, tag 'common words', remove - // words no longer referenced in the match table, etc. - function search_tidy() - { - global $db, $config; - - // Is the fulltext indexer disabled? If yes then we need not - // carry on ... it's okay ... I know when I'm not wanted boo hoo - if (!$config['load_search_upd']) - { - return; - } - - // Remove common (> 60% of posts ) words - $sql = 'SELECT SUM(forum_posts) AS total_posts - FROM ' . FORUMS_TABLE; - $result = $db->sql_query($sql); - - $row = $db->sql_fetchrow($result); - $db->sql_freeresult($result); - - if ($row['total_posts'] >= 100) - { - $sql = 'SELECT word_id - FROM ' . SEARCH_MATCH_TABLE . ' - GROUP BY word_id - HAVING COUNT(word_id) > ' . floor($row['total_posts'] * 0.6); - $result = $db->sql_query($sql); - - if ($row = $db->sql_fetchrow($result)) - { - $sql_in = array(); - do - { - $sql_in[] = $row['word_id']; - } - while ($row = $db->sql_fetchrow($result)); - - $sql_in = implode(', ', $sql_in); - - $sql = 'UPDATE ' . SEARCH_WORD_TABLE . " - SET word_common = 1 - WHERE word_id IN ($sql_in)"; - $db->sql_query($sql); - - $sql = 'DELETE FROM ' . SEARCH_MATCH_TABLE . " - WHERE word_id IN ($sql_in)"; - $db->sql_query($sql); - unset($sql_in); - } - $db->sql_freeresult($result); - } - - // Remove words with no matches ... this is a potentially nasty query - $sql = 'SELECT w.word_id - FROM ' . SEARCH_WORD_TABLE . ' w - LEFT JOIN ' . SEARCH_MATCH_TABLE . ' m ON (w.word_id = m.word_id) - WHERE w.word_common = 0 AND m.word_id IS NULL - GROUP BY m.word_id'; - $result = $db->sql_query($sql); - - if ($row = $db->sql_fetchrow($result)) - { - $sql_in = array(); - do - { - $sql_in[] = $row['word_id']; - } - while ($row = $db->sql_fetchrow($result)); - - $sql = 'DELETE FROM ' . SEARCH_WORD_TABLE . ' - WHERE word_id IN (' . implode(', ', $sql_in) . ')'; - $db->sql_query($sql); - unset($sql_in); - } - $db->sql_freeresult($result); - - set_config('search_last_gc', time()); - } -} - ?>
\ No newline at end of file diff --git a/phpBB/includes/search/fulltext_phpbb.php b/phpBB/includes/search/fulltext_phpbb.php index b4b0efb7b1..517a586579 100644 --- a/phpBB/includes/search/fulltext_phpbb.php +++ b/phpBB/includes/search/fulltext_phpbb.php @@ -9,313 +9,900 @@ */ /** +* @ignore +*/ +include($phpbb_root_path . 'includes/search/search.' . $phpEx); + +/** * @package search * fulltext_phpbb * phpBB's own db driven fulltext search */ -class fulltext_phpbb +class fulltext_phpbb extends search_backend { - var $split_words = array(); - var $common_words = array(); - var $old_split_words = array(); - function fulltext_phpbb(&$error) { $error = false; } - function search($type, &$fields, &$terms, &$fid_ary, &$keywords, &$author, &$pid_ary, $sort_days) + /** + * 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 false if no valid keywords were found and otherwise true + */ + function split_keywords(&$keywords, $terms) { - global $phpbb_root_path, $phpEx, $config, $db, $user, $SID; + global $db, $config; - // Are we looking for words - if ($keywords) - { - $author = ($author) ? ' AND ' . $author : ''; - - $this->split_words = $this->common_words = array(); - $drop_char_match = array('-', '^', '$', ';', '#', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '\'', '!', '*'); - $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', ' ', ' ', ' ', ' ', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' , ' ', ' ', ' ', ' ', ' '); - - if ($fp = @fopen($user->lang_path . '/search_stopwords.txt', 'rb')) - { - $stopwords = explode("\n", str_replace("\r\n", "\n", fread($fp, filesize($user->lang_path . '/search_stopwords.txt')))); - } - fclose($fp); + $drop_char_match = array('^', '$', ';', '#', '&', '(', ')', '<', '>', '`', '\'', '"', ',', '@', '_', '?', '%', '~', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '\'', '!'); + $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', ' ', ' ', ' ', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' , ' ', ' ', ' ', ' '); - if ($fp = @fopen($user->lang_path . '/search_synonyms.txt', 'rb')) - { - preg_match_all('#^(.*?) (.*?)$#ms', fread($fp, filesize($user->lang_path . '/search_synonyms.txt')), $match); - $replace_synonym = &$match[1]; - $match_synonym = &$match[2]; - } - fclose($fp); + $this->get_ignore_words(); + $this->get_synonyms(); + if ($terms == 'all') + { $match = array('#\sand\s#i', '#\sor\s#i', '#\snot\s#i', '#\+#', '#-#', '#\|#'); $replace = array(' + ', ' | ', ' - ', ' + ', ' - ', ' | '); $keywords = preg_replace($match, $replace, $keywords); + } + + $match = array(); + // New lines, carriage returns + $match[] = "#[\n\r]+#"; + // NCRs like etc. + $match[] = '#(&|&)[\#a-z0-9]+?;#i'; - $match = array(); - // Comments for hardcoded bbcode elements (urls, smilies, html) - $match[] = '#<!\-\- .* \-\->(.*?)<!\-\- .* \-\->#is'; - // New lines, carriage returns - $match[] = "#[\n\r]+#"; - // NCRs like etc. - $match[] = '#(&|&)[\#a-z0-9]+?;#i'; - // BBcode - $match[] = '#\[\/?[a-z\*\+\-]+(=.*)?(\:?[0-9a-z]{5,})\]#'; + // Filter out as above + $keywords = preg_replace($match, ' ', strtolower(trim($keywords))); + $keywords = str_replace($drop_char_match, $drop_char_replace, $keywords); - // Filter out as above - $keywords = preg_replace($match, ' ', strtolower(trim($keywords))); - $keywords = str_replace($drop_char_match, $drop_char_replace, $keywords); + // Split words + $this->split_words = explode(' ', preg_replace('#\s+#', ' ', $keywords)); + + if (sizeof($this->ignore_words)) + { + $this->common_words = array_intersect($this->split_words, $this->ignore_words); + $this->split_words = array_diff($this->split_words, $this->ignore_words); + } - // Split words - $this->split_words = explode(' ', preg_replace('#\s+#', ' ', $keywords)); + if (sizeof($this->replace_synonym)) + { + $this->split_words = str_replace($this->replace_synonym, $this->match_synonym, $this->split_words); + } - if (sizeof($stopwords)) + $prefixes = array('+', '-', '|'); + $prefixed = false; + $in_words = ''; + foreach ($this->split_words as $i => $word) + { + if (in_array($word, $prefixes)) { - $this->common_words = array_intersect($this->split_words, $stopwords); - $this->split_words = array_diff($this->split_words, $stopwords); + $prefixed = true; + continue; } - if (sizeof($replace_synonym)) + // check word length + $clean_len = strlen(str_replace('*', '', $word)); + if (($clean_len < $config['min_search_chars']) || ($clean_len > $config['max_search_chars'])) + { + if ($prefixed) + { + $this->common_words[] = $this->split_words[$i - 1]; + unset($this->split_words[$i - 1]); + } + $this->common_words[] = $this->split_words[$i]; + unset($this->split_words[$i]); + } + else if (strpos($word, '*') === false) { - $this->split_words = str_replace($replace_synonym, $match_synonym, $this->split_words); + $in_words .= (($in_words) ? ', ' : '') . '\'' . $db->sql_escape($word) . '\''; } + + $prefixed = false; } - if ($this->old_split_words && sizeof($this->old_split_words)) + if ($in_words) { - $this->split_words = (sizeof($this->split_words)) ? array_diff($this->split_words, $this->old_split_words) : $this->old_split_words; + // identify common words and ignore them + $sql = 'SELECT word_text + FROM ' . SEARCH_WORD_TABLE . " + WHERE word_text IN ($in_words) + AND word_common = 1"; + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + $key = array_search($row['word_text'], $this->split_words); + + if (isset($this->split_words[$key - 1]) && (in_array($this->split_words[$key - 1], $prefixes))) + { + $this->common_words[] = $this->split_words[$key - 1]; + unset($this->split_words[$key - 1]); + } + $this->common_words[] = $row['word_text']; + unset($this->split_words[$key]); + } + $db->sql_freeresult($result); } if (sizeof($this->split_words)) { - $bool = ($terms == 'all') ? 'AND' : 'OR'; - $sql_words = ''; - foreach ($this->split_words as $word) + $this->split_words = array_values($this->split_words); + return true; + } + return false; + } + + /** + * Turns text into an array of words that can be stored in the word list table + */ + function split_message($text) + { + global $config; + + static $drop_char_match, $drop_char_replace; + + $this->get_ignore_words(); + $this->get_synonyms(); + + if (!is_array($drop_char_match)) + { + $drop_char_match = array('-', '^', '$', ';', '#', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '\'', '!', '*', '+'); + $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', ' ', ' ', ' ', ' ', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' , ' ', ' ', ' ', ' ', ' ', ' '); + } + + $match = array(); + // Comments for hardcoded bbcode elements (urls, smilies, html) + $match[] = '#<!\-\- .* \-\->(.*?)<!\-\- .* \-\->#is'; + // New lines, carriage returns + $match[] = "#[\n\r]+#"; + // NCRs like etc. + $match[] = '#(&|&)[\#a-z0-9]+?;#i'; + // Do not index code + $match[] = '#\[code=?.*?(\:?[0-9a-z]{5,})\].*?\[\/code(\:?[0-9a-z]{5,})\]#is'; + // BBcode + $match[] = '#\[\/?[a-z\*\+\-]+=?.*?(\:?[0-9a-z]{5,})\]#'; + // Filter out ; and # but not &#[0-9]+; + //$match[] = '#(&\#[0-9]+;)|;|\#|&#'; + + $text = preg_replace($match, ' ', ' ' . strtolower(trim($text)) . ' '); + + // Filter out non-alphabetical chars + $text = str_replace($drop_char_match, $drop_char_replace, $text); + + // Split words + $text = explode(' ', preg_replace('#\s+#', ' ', trim($text))); + + if (sizeof($this->ignore_words)) + { + $stopped_words = array_intersect($text, $this->ignore_words); + $text = array_diff($text, $this->ignore_words); + } + + if (sizeof($this->replace_synonym)) + { + $text = str_replace($this->replace_synonym, $this->match_synonym, $text); + } + + // remove too short or too long words + $text = array_values($text); + for ($i = 0, $n = sizeof($text); $i < $n; $i++) + { + $text[$i] = trim($text[$i]); + if (strlen($text[$i]) < $config['min_search_chars'] || strlen($text[$i]) > $config['max_search_chars']) { - switch ($word) - { - case '-': - $bool = 'NOT'; - continue; - case '+': - $bool = 'AND'; - continue; - case '|': - $bool = 'OR'; - continue; - default: - $bool = ($terms != 'all') ? 'OR' : $bool; - $sql_words[$bool][] = "'" . preg_replace('#\*+#', '%', trim($word)) . "'"; - $bool = ($terms == 'all') ? 'AND' : 'OR'; - } + unset($text[$i]); } + } + + return $text; + } + + /** + * Performs a search on keywords depending 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 indicated 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 keyword_search($type, &$fields, &$terms, &$sort_by_sql, &$sort_key, &$sort_dir, &$sort_days, &$ex_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; + } - // Build some display specific variable strings - $sql_author = ($author) ? 'AND p.poster_id = ' . $author : ''; - $sql_fora = (sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : ''; - $sql_time = ($sort_days) ? 'AND p.post_time >= ' . ($current_time - ($sort_days * 86400)) : ''; - $sql_select = ($type == 'posts') ? 'm.post_id' : 'DISTINCT t.topic_id'; - $sql_from = ($type == 'posts') ? '' : TOPICS_TABLE . ' t, '; - $sql_topic = ($type == 'posts') ? '' : 'AND t.topic_id = p.topic_id'; - $field = ($type == 'posts') ? 'm.post_id' : 't.topic_id'; + // generate a search_key from all the options to identify the results + $search_key = md5(implode('#', array( + implode(',', $this->split_words), + $type, + $fields, + $terms, + $sort_days, + $sort_key, + $topic_id, + implode(',', $ex_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; + } + + $result_count = 0; + $id_ary = array(); + + // 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; + } + + // Build some display specific sql strings + switch ($fields) + { + case 'titleonly': + $sql_match = ' AND m.title_match = 1'; + break; + case 'msgonly': + $sql_match = ' AND m.title_match = 0'; + break; + default: + $sql_match = ''; + } + + $sql_select = ($type == 'posts') ? 'm.post_id' : 'DISTINCT t.topic_id'; + $sql_from = ($type == 'posts') ? '' : TOPICS_TABLE . ' t, '; + $field = ($type == 'posts') ? 'm.post_id' : 't.topic_id'; + $sql_author = (sizeof($author_ary) == 1) ? ' = ' . $author_ary[0] : 'IN (' . implode(',', $author_ary) . ')'; + + $sql_where_options = $sql_sort_join; + $sql_where_options .= ($topic_id) ? 'AND p.topic_id = ' . $topic_id : ''; + $sql_where_options .= ($type == 'posts') ? '' : 'AND t.topic_id = p.topic_id'; + $sql_where_options .= (sizeof($ex_fid_ary)) ? ' AND p.forum_id NOT IN (' . implode(',', $ex_fid_ary) . ')' : ''; + $sql_where_options .= (sizeof($author_ary)) ? 'AND p.poster_id ' . $sql_author : ''; + $sql_where_options .= ($sort_days) ? 'AND p.post_time >= ' . (time() - ($sort_days * 86400)) : ''; + $sql_where_options .= $sql_match; + + // split the words into three arrays (AND, OR, NOT) + $sql_words = array('AND' => array(), 'OR' => array(), 'NOT' => array()); + $bool = ($terms == 'all') ? 'AND' : 'OR'; - switch ($fields) + foreach ($this->split_words as $word) + { + switch ($word) { - case 'titleonly': - $sql_match = ' AND m.title_match = 1'; - break; - case 'msgonly': - $sql_match = ' AND m.title_match = 0'; - break; + case '-': + $bool = 'NOT'; + continue; + case '+': + $bool = 'AND'; + continue; + case '|': + $bool = 'OR'; + continue; default: - $sql_match = ''; + $bool = ($terms != 'all') ? 'OR' : $bool; + $sql_words[$bool][] = "'" . $db->sql_escape(preg_replace('#\*+#', '%', trim($word))) . "'"; + $bool = ($terms == 'all') ? 'AND' : 'OR'; } + } - // Are we searching within an existing search set? Yes, then include the old ids - $sql_find_in = (sizeof($pid_ary)) ? 'AND ' . (($type == 'topics') ? 't.topic_id' : 'm.post_id') . ' IN (' . implode(', ', $pid_ary) . ')' : ''; - - $result_ary = array(); - $_bool = array('AND', 'OR', 'NOT'); - foreach ($_bool as $bool) + // Select all post_ids that contain all AND-words + $result_ary= array('AND' => array(), 'OR' => array(), 'NOT' => array()); + if (sizeof($sql_words['AND'])) + { + $sql_in = ''; + foreach ($sql_words['AND'] as $word) { - if (isset($sql_words[$bool]) && is_array($sql_words[$bool])) + // first select all post ids that match a word containing a wildcard + if (strstr($word, '%')) { - switch ($bool) + $sql = "SELECT $sql_select + FROM $sql_from$sql_sort_table" . POSTS_TABLE . ' p, ' . SEARCH_MATCH_TABLE . ' m, ' . SEARCH_WORD_TABLE . " w + WHERE w.word_text LIKE $word + AND m.word_id = w.word_id + AND w.word_common <> 1 + AND p.post_id = m.post_id + $sql_where_options + GROUP BY $field + ORDER BY $sql_sort"; + $result = $db->sql_query($sql); + + if (!($row = $db->sql_fetchrow($result))) { - case 'AND': - case 'NOT': - foreach ($sql_words[$bool] as $word) - { - if (strlen($word) < 4) - { - continue; - } - - $sql_where = (strstr($word, '%')) ? "LIKE $word" : "= $word"; - - $sql_and = (isset($result_ary['AND']) && sizeof($result_ary['AND'])) ? "AND $field IN (" . implode(', ', $result_ary['AND']) . ')' : ''; - - $sql = "SELECT $sql_select - FROM $sql_from" . POSTS_TABLE . ' p, ' . SEARCH_MATCH_TABLE . ' m, ' . SEARCH_WORD_TABLE . " w - WHERE w.word_text $sql_where - AND m.word_id = w.word_id - AND w.word_common <> 1 - AND p.post_id = m.post_id - $sql_topic - $sql_fora - $sql_author - $sql_and - $sql_time - $sql_match - $sql_find_in"; - $result = $db->sql_query_limit($sql, 1000); - - if ($db->sql_numrows() > 999) - { - trigger_error($user->lang['TOO_MANY_SEARCH_RESULTS']); - } - - if (!($row = $db->sql_fetchrow($result)) && $bool == 'AND') - { - trigger_error($user->lang['NO_SEARCH_RESULTS']); - } - - if ($bool == 'AND') - { - $result_ary['AND'] = array(); - } - - do - { - $result_ary[$bool][] = ($type == 'topics') ? $row['topic_id'] : $row['post_id']; - } - while ($row = $db->sql_fetchrow($result)); - $db->sql_freeresult($result); - } - break; - - case 'OR': - $sql_where = $sql_in = ''; - foreach ($sql_words[$bool] as $word) - { - if (strlen($word) < 4) - { - continue; - } - - if (strstr($word, '%')) - { - $sql_where .= (($sql_where) ? ' OR w.word_text ' : 'w.word_text ') . "LIKE $word"; - } - else - { - $sql_in .= (($sql_in) ? ', ' : '') . $word; - } - } - $sql_where = ($sql_in) ? (($sql_where) ? ' OR ' : '') . 'w.word_text IN (' . $sql_in . ')' : $sql_where; - - $sql_and = (isset($result_ary['AND']) && sizeof($result_ary['AND'])) ? "AND $field IN (" . implode(', ', $result_ary['AND']) . ')' : ''; - $sql = "SELECT $sql_select - FROM $sql_from" . POSTS_TABLE . ' p, ' . SEARCH_MATCH_TABLE . ' m, ' . SEARCH_WORD_TABLE . " w - WHERE ($sql_where) - AND m.word_id = w.word_id - AND w.word_common <> 1 - AND p.post_id = m.post_id - $sql_topic - $sql_fora - $sql_author - $sql_and - $sql_time - $sql_match - $sql_find_in"; - $result = $db->sql_query_limit($sql, 1000); - - while ($row = $db->sql_fetchrow($result)) - { - $result_ary[$bool][] = ($type == 'topics') ? $row['topic_id'] : $row['post_id']; - } - $db->sql_freeresult($result); - break; + $id_ary = array(); + return false; } + + $ids = array(); + do + { + $ids[] = ($type == 'topics') ? $row['topic_id'] : $row['post_id']; + } + while ($row = $db->sql_fetchrow($result)); + $db->sql_freeresult($result); + + // remove ids that are not present in all AND-word results + if (sizeof($result_ary['AND'])) + { + $result_ary['AND'] = array_intersect($result_ary['AND'], $ids); + } + else + { + $result_ary['AND'] = $ids; + } + unset($ids); } else { - $sql_words[$bool] = array(); + $sql_in .= (($sql_in) ? ', ' : '') . $word; } } - if (isset($result_ary['OR']) && sizeof($result_ary['OR'])) + if ($sql_in) { - $pid_ary = (isset($result_ary['AND']) && sizeof($result_ary['AND'])) ? array_diff($result_ary['AND'], $result_ary['OR']) : $result_ary['OR']; + $sql = "SELECT $sql_select, COUNT(DISTINCT m.word_id) as matches + FROM $sql_from$sql_sort_table" . POSTS_TABLE . ' p, ' . SEARCH_MATCH_TABLE . ' m, ' . SEARCH_WORD_TABLE . " w + WHERE w.word_text IN ($sql_in) + AND m.word_id = w.word_id + AND w.word_common <> 1 + AND p.post_id = m.post_id + $sql_where_options + GROUP BY $field + ORDER BY $sql_sort"; + $result = $db->sql_query($sql); + + if (!($row = $db->sql_fetchrow($result))) + { + $id_ary = array(); + return false; + } + + // A little trick so we only need one query: using DISTINCT makes every word unique so if the + // number of all words for one post_id equals the number of AND-words it has to contain all + // AND-words + $ids = array(); + do + { + if ($row['matches'] == sizeof($sql_words['AND'])) + { + $ids[] = ($type == 'topics') ? $row['topic_id'] : $row['post_id']; + } + } + while ($row = $db->sql_fetchrow($result)); + $db->sql_freeresult($result); + + // remove ids that are not present in all AND-word results + if (sizeof($result_ary['AND'])) + { + $result_ary['AND'] = array_intersect($result_ary['AND'], $ids); + } + else + { + $result_ary['AND'] = $ids; + } + unset($ids); } - else + } + + // Select all post_ids that contain one of the OR-words + if (sizeof($sql_words['OR'])) + { + $sql_where = $sql_in = ''; + foreach ($sql_words['OR'] as $word) { - $pid_ary = (isset($result_ary['AND'])) ? $result_ary['AND'] : array(); + if (strstr($word, '%')) + { + $sql_where .= (($sql_where) ? ' OR w.word_text ' : 'w.word_text ') . "LIKE $word"; + } + else + { + $sql_in .= (($sql_in) ? ', ' : '') . $word; + } } + $sql_where = ($sql_in) ? $sql_where . (($sql_where) ? ' OR ' : '') . 'w.word_text IN (' . $sql_in . ')' : $sql_where; + + $sql_and = (sizeof($result_ary['AND'])) ? "AND $field IN (" . implode(', ', $result_ary['AND']) . ')' : ''; + $sql = "SELECT $sql_select + FROM $sql_from$sql_sort_table" . POSTS_TABLE . ' p, ' . SEARCH_MATCH_TABLE . ' m, ' . SEARCH_WORD_TABLE . " w + WHERE ($sql_where) + AND m.word_id = w.word_id + AND w.word_common <> 1 + AND p.post_id = m.post_id + $sql_where_options + ORDER BY $sql_sort"; + $result = $db->sql_query($sql); - if (isset($result_ary['NOT']) && sizeof($result_ary['NOT'])) + while ($row = $db->sql_fetchrow($result)) { - $pid_ary = (sizeof($pid_ary)) ? array_diff($pid_ary, $result_ary['NOT']) : array(); + $result_ary['OR'][] = ($type == 'topics') ? $row['topic_id'] : $row['post_id']; } - unset($result_ary); + $db->sql_freeresult($result); + } + + // remove post_ids that do not contain any OR-word + if (sizeof($result_ary['OR'])) + { + $id_ary = (sizeof($result_ary['AND'])) ? array_intersect($result_ary['AND'], $result_ary['OR']) : $result_ary['OR']; + } + else + { + $id_ary = (sizeof($result_ary['AND'])) ? $result_ary['AND'] : array(); + } - $pid_ary = array_unique($pid_ary); + unset($result_ary['AND']); + unset($result_ary['OR']); - if (!sizeof($pid_ary)) + // remove all post_ids that contain a NOT-word + if (sizeof($sql_words['NOT']) && sizeof($id_ary)) + { + $sql_where = $sql_in = ''; + foreach ($sql_words['NOT'] as $word) { - trigger_error($user->lang['NO_SEARCH_RESULTS']); + if (strstr($word, '%')) + { + $sql_where .= (($sql_where) ? ' OR w.word_text ' : 'w.word_text ') . "LIKE $word"; + } + else + { + $sql_in .= (($sql_in) ? ', ' : '') . $word; + } } - - $sql = 'SELECT word_text - FROM ' . SEARCH_WORD_TABLE . ' - WHERE word_text IN (' . implode(', ', array_unique(array_merge($sql_words['AND'], $sql_words['OR'], $sql_words['NOT']))) . ') - AND word_common = 1'; + $sql_where = ($sql_in) ? $sql_where . (($sql_where) ? ' OR ' : '') . 'w.word_text IN (' . $sql_in . ')' : $sql_where; + + $sql_and = "AND $field IN (" . implode(', ', $id_ary) . ')'; + $sql = "SELECT $sql_select + FROM $sql_from" . POSTS_TABLE . ' p, ' . SEARCH_MATCH_TABLE . ' m, ' . SEARCH_WORD_TABLE . " w + WHERE ($sql_where) + AND m.word_id = w.word_id + AND w.word_common <> 1 + AND p.post_id = m.post_id + $sql_where_options"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { - $common_words[] = $row['word_text']; + $result_ary['NOT'][] = ($type == 'topics') ? $row['topic_id'] : $row['post_id']; } $db->sql_freeresult($result); } - else if ($author) + + if (sizeof($result_ary['NOT'])) + { + $id_ary = (sizeof($id_ary)) ? array_diff($id_ary, $result_ary['NOT']) : array(); + } + unset($result_ary); + + if (!sizeof($id_ary)) + { + return false; + } + + $result_count = sizeof($id_ary); + + // store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page + $id_ary = array_slice($id_ary, $start); + $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 indicated 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, &$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(',', $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) { - $sql_author = ($author) ? 'p.poster_id = ' . $author : ''; - $sql_fora = (sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : ''; + return $result_count; + } + + $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 NOT IN (' . implode(',', $ex_fid_ary) . ')' : ''; + $sql_topic_id = (sizeof($ex_fid_ary)) ? ' AND p.forum_id NOT IN (' . implode(',', $ex_fid_ary) . ')' : ''; + $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 = '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 the cache was completely empty count the results + if (!$result_count) + { if ($type == 'posts') { - $sql = 'SELECT p.post_id + $sql = 'SELECT COUNT(p.post_id) as result_count FROM ' . POSTS_TABLE . " p WHERE $sql_author - $sql_fora"; - $field = 'post_id'; + $sql_fora + $sql_time"; } else { - $sql = 'SELECT t.topic_id + $sql = 'SELECT COUNT(DISTINCT t.topic_id) as result_count FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p WHERE $sql_author $sql_fora AND t.topic_id = p.topic_id - GROUP BY t.topic_id"; - $field = 'topic_id'; + $sql_time"; } - $result = $db->sql_query_limit($sql, 1000); + $result = $db->sql_query($sql); - while ($row = $db->sql_fetchrow($result)) + if ($row = $db->sql_fetchrow()) { - $pid_ary[] = $row[$field]; + $result_count = $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 + WHERE $sql_author + $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_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); + + 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; } + + /** + * Updates wordlist and wordmatch tables when a message is posted or changed + * + * @param string $mode contains the post mode: edit, post, reply, quote ... + */ + function index($mode, $post_id, &$message, &$subject) + { + global $config, $db; + + // Is the fulltext indexer disabled? If yes then we need not + // carry on ... it's okay ... I know when I'm not wanted boo hoo + if (!$config['load_search_upd']) + { + return; + } + + // 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(); + $cur_words = array('post' => array(), 'title' => array()); + + $words = array(); + if ($mode == 'edit') + { + $words['add']['post'] = array(); + $words['add']['title'] = array(); + $words['del']['post'] = array(); + $words['del']['title'] = array(); + + $sql = 'SELECT w.word_id, w.word_text, m.title_match + FROM ' . SEARCH_WORD_TABLE . ' w, ' . SEARCH_MATCH_TABLE . " m + WHERE m.post_id = $post_id + AND w.word_id = m.word_id"; + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + $which = ($row['title_match']) ? 'title' : 'post'; + $cur_words[$which][$row['word_text']] = $row['word_id']; + } + $db->sql_freeresult($result); + + $words['add']['post'] = array_diff($split_text, array_keys($cur_words['post'])); + $words['add']['title'] = array_diff($split_title, array_keys($cur_words['title'])); + $words['del']['post'] = array_diff(array_keys($cur_words['post']), $split_text); + $words['del']['title'] = array_diff(array_keys($cur_words['title']), $split_title); + } + else + { + $words['add']['post'] = $split_text; + $words['add']['title'] = $split_title; + $words['del']['post'] = array(); + $words['del']['title'] = array(); + } + unset($split_text); + unset($split_title); + + // Get unique words from the above arrays + $unique_add_words = array_unique(array_merge($words['add']['post'], $words['add']['title'])); + + // We now have unique arrays of all words to be added and removed and + // individual arrays of added and removed words for text and title. What + // we need to do now is add the new words (if they don't already exist) + // and then add (or remove) matches between the words and this post + if (sizeof($unique_add_words)) + { + $sql = 'SELECT word_id, word_text + FROM ' . SEARCH_WORD_TABLE . ' + WHERE word_text IN (' . implode(', ', preg_replace('#^(.*)$#', '\'$1\'', $unique_add_words)) . ')'; + $result = $db->sql_query($sql); + + $word_ids = array(); + while ($row = $db->sql_fetchrow($result)) + { + $word_ids[$row['word_text']] = $row['word_id']; + } + $db->sql_freeresult($result); + + $new_words = array_diff($unique_add_words, array_keys($word_ids)); + + if (sizeof($new_words)) + { + switch (SQL_LAYER) + { + case 'mysql': + $sql = 'INSERT INTO ' . SEARCH_WORD_TABLE . ' (word_text) + VALUES ' . implode(', ', preg_replace('#^(.*)$#', '(\'$1\')', $new_words)); + $db->sql_query($sql); + break; + + case 'mysql4': + case 'mysqli': + case 'mssql': + case 'mssql_odbc': + case 'sqlite': + $sql = 'INSERT INTO ' . SEARCH_WORD_TABLE . ' (word_text) ' . implode(' UNION ALL ', preg_replace('#^(.*)$#', "SELECT '\$1'", $new_words)); + $db->sql_query($sql); + break; + + default: + foreach ($new_words as $word) + { + $sql = 'INSERT INTO ' . SEARCH_WORD_TABLE . " (word_text) + VALUES ('$word')"; + $db->sql_query($sql); + } + break; + } + } + unset($new_words); + } + + // now update the search match table, remove links to removed words and add links to new words + foreach ($words['del'] as $word_in => $word_ary) + { + $title_match = ($word_in == 'title') ? 1 : 0; + + if (sizeof($word_ary)) + { + $sql_in = array(); + foreach ($word_ary as $word) + { + $sql_in[] = $cur_words[$word_in][$word]; + } + + $sql = 'DELETE FROM ' . SEARCH_MATCH_TABLE . ' + WHERE word_id IN (' . implode(', ', $sql_in) . ') + AND post_id = ' . intval($post_id) . " + AND title_match = $title_match"; + $db->sql_query($sql); + unset($sql_in); + } + } + + foreach ($words['add'] as $word_in => $word_ary) + { + $title_match = ($word_in == 'title') ? 1 : 0; + + if (sizeof($word_ary)) + { + $sql = 'INSERT INTO ' . SEARCH_MATCH_TABLE . " (post_id, word_id, title_match) + SELECT $post_id, word_id, $title_match + FROM " . SEARCH_WORD_TABLE . ' + WHERE word_text IN (' . implode(', ', preg_replace('#^(.*)$#', '\'$1\'', $word_ary)) . ')'; + $db->sql_query($sql); + } + } + + // destroy cached search results containing any of the words removed or added + $this->destroy_cache(array_unique(array_merge($words['add']['post'], $words['add']['title'], $words['del']['post'], $words['del']['post']))); + + unset($unique_add_words); + unset($words); + unset($cur_words); + } + + /** + * Removes entries from the wordmatch table for the specified post_ids + */ + function index_remove($post_ids) + { + global $db; + + $sql = 'DELETE FROM ' . SEARCH_MATCH_TABLE . ' + WHERE post_id IN (' . implode(', ', $post_ids) . ')'; + $db->sql_query($sql); + + // SEARCH_WORD_TABLE will be updated by tidy() + } + + /** + * Tidy up indexes: Tag 'common words' and remove + * words no longer referenced in the match table + */ + function tidy() + { + global $db, $config; + + // Is the fulltext indexer disabled? If yes then we need not + // carry on ... it's okay ... I know when I'm not wanted boo hoo + if (!$config['load_search_upd']) + { + return; + } + + $destroy_cache_words = array(); + + // Remove common (> 50% of posts ) words + if ($config['num_posts'] >= 100) + { + $sql = 'SELECT word_id + FROM ' . SEARCH_MATCH_TABLE . ' + GROUP BY word_id + HAVING COUNT(word_id) > ' . floor($config['num_posts'] * 0.5); + $result = $db->sql_query($sql); + + if ($row = $db->sql_fetchrow($result)) + { + $sql_in = array(); + do + { + $sql_in[] = $row['word_id']; + } + while ($row = $db->sql_fetchrow($result)); + + $destroy_cache_words = $sql_in; + + $sql_in = implode(', ', $sql_in); + + $sql = 'UPDATE ' . SEARCH_WORD_TABLE . " + SET word_common = 1 + WHERE word_id IN ($sql_in)"; + $db->sql_query($sql); + + $sql = 'DELETE FROM ' . SEARCH_MATCH_TABLE . " + WHERE word_id IN ($sql_in)"; + $db->sql_query($sql); + unset($sql_in); + } + $db->sql_freeresult($result); + } + + // Remove words with no matches ... this is a potentially nasty query + $sql = 'SELECT w.word_id + FROM ' . SEARCH_WORD_TABLE . ' w + LEFT JOIN ' . SEARCH_MATCH_TABLE . ' m ON (w.word_id = m.word_id) + WHERE w.word_common = 0 AND m.word_id IS NULL + GROUP BY w.word_id'; + $result = $db->sql_query($sql); + + if ($row = $db->sql_fetchrow($result)) + { + $sql_in = array(); + do + { + $sql_in[] = $row['word_id']; + } + while ($row = $db->sql_fetchrow($result)); + + $destroy_cache_words = array_merge($destroy_cache_words, $sql_in); + + $sql = 'DELETE FROM ' . SEARCH_WORD_TABLE . ' + WHERE word_id IN (' . implode(', ', $sql_in) . ')'; + $db->sql_query($sql); + unset($sql_in); + } + $db->sql_freeresult($result); + + // destroy cached search results containing any of the words that are now common or were removed + $this->destroy_cache(array_unique($destroy_cache_words)); + } } ?>
\ No newline at end of file diff --git a/phpBB/includes/search/search.php b/phpBB/includes/search/search.php new file mode 100755 index 0000000000..6f31e88ca4 --- /dev/null +++ b/phpBB/includes/search/search.php @@ -0,0 +1,264 @@ +<?php +/** +* +* @package search +* @version $Id$ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +define('SEARCH_RESULT_NOT_IN_CACHE', 2); +define('SEARCH_RESULT_IN_CACHE', 1); +define('SEARCH_RESULT_INCOMPLETE', 2); + +/** +* @package search +* search_backend +* optional base class for search plugins providing simple caching based on ACM +* and functions to retrieve ignore_words and synonyms +*/ +class search_backend +{ + var $ignore_words = array(); + var $match_synonym = array(); + var $replace_synonym = array(); + var $split_words = array(); + var $common_words = array(); + + function search_backend(&$error) + { + // This class cannot be used as a search plugin + $error = true; + } + + /** + * Stores a list of common words that should be ignored in $this->ignore_words and caches them + */ + function get_ignore_words() + { + if (!sizeof($this->ignore_words)) + { + global $user, $cache; + + $ignore_words = $cache->get('_ignore_words'); + + if (!$ignore_words) + { + $ignore_words = array(); + } + + if (!isset($ignore_words[$user->lang_name])) + { + $ignore_words[$user->lang_name] = explode("\n", str_replace("\n\n", "\n", str_replace("\r", "\n", file_get_contents($user->lang_path . '/search_ignore_words.txt')))); + + $cache->put('_ignore_words', $ignore_words, 7200); + } + + $this->ignore_words = $ignore_words[$user->lang_name]; + + unset($ignore_words); + } + } + + /** + * Stores a list of synonyms that should be replaced in $this->match_synonym and $this->replace_synonym and caches them + */ + function get_synonyms() + { + if (!sizeof($this->match_synonym)) + { + global $user, $cache; + + $match_synonym = $cache->get('_match_synonym'); + + if (!$match_synonym) + { + $match_synonym = array(); + } + + if (!isset($match_synonym[$user->lang_name])) + { + preg_match_all('#^\s+(\S+)\s+(\S+)\s+$#m', file_get_contents($user->lang_path . '/search_synonyms.txt'), $match); + $match_synonym[$user->lang_name]['replace']= &$match[1]; + $match_synonym[$user->lang_name]['match'] = &$match[2]; + + $cache->put('_match_synonym', $match_synonym, 7200); + } + + $this->replace_synonym = $match_synonym[$user->lang_name]['replace']; + $this->match_synonym = $match_synonym[$user->lang_name]['match']; + + unset($match_synonym); + } + } + + /** + * Retrieves cached search results + * + * @param int result_count will contain the number of all results for the search (not only for the current page) + * @param array id_ary is filled with the ids belonging to the requested page that are stored in the cache + * + * @return SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE + */ + function obtain_ids($search_key, &$result_count, &$id_ary, $start, $per_page, $sort_dir) + { + global $cache; + + if (!($stored_ids = $cache->get('_search_results_' . $search_key))) + { + // no search results cached for this search_key + return SEARCH_RESULT_NOT_IN_CACHE; + } + else + { + $result_count = $stored_ids[-1]; + $reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false; + $complete = true; + + // change the start to the actual end of the current request if the sort direction differs + // from the dirction in the cache and reverse the ids later + if ($reverse_ids) + { + $start = $result_count - $start - $per_page; + + // the user requested a page past the last index + if ($start < 0) + { + return SEARCH_RESULT_NOT_IN_CACHE; + } + } + + for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++) + { + if (!isset($stored_ids[$i])) + { + $complete = false; + } + else + { + $id_ary[] = $stored_ids[$i]; + } + } + unset($stored_ids); + + if ($reverse_ids) + { + $id_ary = array_reverse($id_ary); + } + + if (!$complete) + { + return SEARCH_RESULT_INCOMPLETE; + } + return SEARCH_RESULT_IN_CACHE; + } + } + + /** + * Caches post/topic ids + * + * @param array id_ary contains a list of post or topic ids that shall be cached, the first element + * must have the absolute index $start in the result set. + */ + function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir) + { + global $cache, $config, $db; + + $length = min(sizeof($id_ary), $config['search_block_size']); + + $store_ids = array_slice($id_ary, 0, $length); + + // create a new resultset if there is none for this search_key yet + // or add the ids to the existing resultset + if (!($store = $cache->get('_search_results_' . $search_key))) + { + // add the current keywords to the recent searches in the cache which are listed on the search page + if (!empty($keywords) || sizeof($author_ary)) + { + $sql = 'SELECT search_time + FROM ' . SEARCH_TABLE . ' + WHERE search_key = \'' . $db->sql_escape($search_key) . '\''; + $result = $db->sql_query($sql); + + if (!$db->sql_fetchrow($result)) + { + $sql_ary = array( + 'search_key' => $search_key, + 'search_time' => time(), + 'search_keywords' => $keywords, + 'search_authors' => implode(' ', $author_ary) + ); + + $sql = 'INSERT INTO ' . SEARCH_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary); + $db->sql_query($sql); + } + $db->sql_freeresult($result); + } + set_config('last_search_time', time()); + + $store = array(-1 => $result_count, -2 => $sort_dir); + $id_range = range($start, $start + $length - 1); + } + else + { + // we use one set of resuts for both sort directions so we have to calculate the indizes + // for the reversed array and we also have to reverse the ids themselves + if ($store[-2] != $sort_dir) + { + $store_ids = array_reverse($store_ids); + $id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1); + } + else + { + $id_range = range($start, $start + $length - 1); + } + } + + // append the ids + $store += array_combine($id_range, $store_ids); + $cache->put('_search_results_' . $search_key, $store, $config['search_store_results']); + + unset($store); + unset($store_ids); + unset($id_range); + } + + /** + * Removes old entries from the search results table and removes searches with keywords that contain a word in $words. + */ + function destroy_cache($words) + { + global $db, $cache, $config; + + if (sizeof($words)) + { + $sql_where = ''; + foreach ($words as $word) + { + $sql_where .= ' OR search_keywords LIKE \'%' . $db->sql_escape($word) . '%\''; + } + + $sql = 'SELECT search_key + FROM ' . SEARCH_TABLE . " + WHERE search_keywords LIKE '%*%' $sql_where"; + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + $cache->destroy('_search_results_' . $row['search_key']); + } + $db->sql_freeresult(); + } + + $sql = 'DELETE + FROM ' . SEARCH_TABLE . ' + WHERE search_time < ' . (time() - $config['search_store_results']); + $db->sql_query($sql); + } +} + +?>
\ No newline at end of file diff --git a/phpBB/install/schemas/firebird_schema.sql b/phpBB/install/schemas/firebird_schema.sql index 74d8c64965..91f23238cd 100644 --- a/phpBB/install/schemas/firebird_schema.sql +++ b/phpBB/install/schemas/firebird_schema.sql @@ -481,10 +481,10 @@ CREATE TABLE phpbb_reports_reasons ( # phpbb_search_results CREATE TABLE phpbb_search_results ( - search_id INTEGER DEFAULT 0 NOT NULL, - session_id VARCHAR(32) NOT NULL, + search_key VARCHAR(32) NOT NULL, search_time INTEGER DEFAULT 0 NOT NULL, - search_array BLOB SUB_TYPE TEXT NOT NULL + search_keywords BLOB SUB_TYPE TEXT NOT NULL, + search_authors BLOB SUB_TYPE TEXT NOT NULL );; # phpbb_search_wordlist @@ -1242,12 +1242,7 @@ ADD PRIMARY KEY ( ALTER TABLE phpbb_search_results ADD PRIMARY KEY ( - search_id -);; - -CREATE INDEX session_id54 -ON phpbb_search_results( - session_id + search_key );; ALTER TABLE phpbb_search_wordlist diff --git a/phpBB/install/schemas/mssql_schema.sql b/phpBB/install/schemas/mssql_schema.sql index 9a877e785c..2065f86f43 100644 --- a/phpBB/install/schemas/mssql_schema.sql +++ b/phpBB/install/schemas/mssql_schema.sql @@ -486,10 +486,10 @@ CREATE TABLE [phpbb_reports_reasons] ( GO CREATE TABLE [phpbb_search_results] ( - [search_id] [int] NOT NULL , - [session_id] [varchar] (32) NOT NULL , + [search_key] [varchar] (32) NOT NULL , [search_time] [int] NOT NULL , - [search_array] [text] NOT NULL + [search_keywords] [text] NOT NULL , + [search_authors] [text] NOT NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO @@ -1058,7 +1058,7 @@ GO ALTER TABLE [phpbb_search_results] WITH NOCHECK ADD CONSTRAINT [PK_phpbb_search_results] PRIMARY KEY CLUSTERED ( - [search_id] + [search_key] ) ON [PRIMARY] GO @@ -1461,7 +1461,6 @@ ALTER TABLE [phpbb_reports_reasons] WITH NOCHECK ADD GO ALTER TABLE [phpbb_search_results] WITH NOCHECK ADD - CONSTRAINT [DF_search_search_id] DEFAULT (0) FOR [search_id], CONSTRAINT [DF_search_search_time] DEFAULT (0) FOR [search_time] GO @@ -1785,9 +1784,6 @@ GO CREATE INDEX [field_order] ON [phpbb_profile_fields]([field_order]) ON [PRIMARY] GO -CREATE INDEX [session_id] ON [phpbb_search_results]([session_id]) ON [PRIMARY] -GO - CREATE INDEX [word_id] ON [phpbb_search_wordlist]([word_id]) ON [PRIMARY] GO diff --git a/phpBB/install/schemas/mysql_schema.sql b/phpBB/install/schemas/mysql_schema.sql index 13c914ff51..eb60923afc 100644 --- a/phpBB/install/schemas/mysql_schema.sql +++ b/phpBB/install/schemas/mysql_schema.sql @@ -563,17 +563,16 @@ CREATE TABLE phpbb_reports ( PRIMARY KEY (report_id) ); -# Table: phpbb_search_results -CREATE TABLE phpbb_search_results ( - search_id int(11) UNSIGNED DEFAULT '0' NOT NULL, - session_id varchar(32) DEFAULT '' NOT NULL, +# Table: 'phpbb_search_results' +CREATE TABLE new_search_results ( + search_key varchar(32) DEFAULT '' NOT NULL, search_time int(11) DEFAULT '0' NOT NULL, - search_array mediumtext NOT NULL, - PRIMARY KEY (search_id), - KEY session_id (session_id) + search_keywords mediumtext NOT NULL, + search_authors mediumtext NOT NULL, + PRIMARY KEY (search_key) ); -# Table: phpbb_search_wordlist +# Table: 'phpbb_search_wordlist' CREATE TABLE phpbb_search_wordlist ( word_text varchar(50) BINARY DEFAULT '' NOT NULL, word_id mediumint(8) UNSIGNED NOT NULL auto_increment, @@ -582,7 +581,7 @@ CREATE TABLE phpbb_search_wordlist ( KEY word_id (word_id) ); -# Table: phpbb_search_wordmatch +# Table: 'phpbb_search_wordmatch' CREATE TABLE phpbb_search_wordmatch ( post_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, word_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, diff --git a/phpBB/install/schemas/oracle_schema.sql b/phpBB/install/schemas/oracle_schema.sql index 791ede23bd..eb076921d9 100644 --- a/phpBB/install/schemas/oracle_schema.sql +++ b/phpBB/install/schemas/oracle_schema.sql @@ -1141,17 +1141,14 @@ END; Table: phpbb_search_results */ CREATE TABLE phpbb_search_results ( - search_id number(11) DEFAULT '0' NOT NULL, - session_id varchar2(32) DEFAULT '', + session_key varchar2(32) DEFAULT '', search_time number(11) DEFAULT '0' NOT NULL, - search_array clob, - CONSTRAINT pk_phpbb_search_results PRIMARY KEY (search_id) + search_keywords clob, + search_authors clob, + CONSTRAINT pk_phpbb_search_results PRIMARY KEY (search_key) ) / -CREATE INDEX session_id on phpbb_search_results (session_id) -/ - /* Table: phpbb_search_wordlist */ diff --git a/phpBB/install/schemas/postgres_schema.sql b/phpBB/install/schemas/postgres_schema.sql index 98c335644f..e840387efe 100644 --- a/phpBB/install/schemas/postgres_schema.sql +++ b/phpBB/install/schemas/postgres_schema.sql @@ -775,16 +775,13 @@ SELECT SETVAL('phpbb_reports_report_id_seq',(select case when max(report_id)>0 t /* Table: phpbb_search_results */ CREATE TABLE phpbb_search_results ( - search_id INT4 DEFAULT '0' NOT NULL, - session_id varchar(32) DEFAULT '' NOT NULL, + search_key varchar(32) DEFAULT '' NOT NULL, search_time INT4 DEFAULT '0' NOT NULL, - search_array TEXT DEFAULT '' NOT NULL, - PRIMARY KEY (search_id), - CHECK (search_id>=0) + search_keywords TEXT DEFAULT '' NOT NULL, + search_authors TEXT DEFAULT '' NOT NULL, + PRIMARY KEY (search_key) ); -CREATE INDEX session_id_phpbb_search_results_index ON phpbb_search_results (session_id); - /* Table: phpbb_search_wordlist */ CREATE SEQUENCE phpbb_search_wordlist_word_i; diff --git a/phpBB/install/schemas/schema_data.sql b/phpBB/install/schemas/schema_data.sql index af64f7dbb7..b890e470a4 100644 --- a/phpBB/install/schemas/schema_data.sql +++ b/phpBB/install/schemas/schema_data.sql @@ -108,6 +108,7 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('ldap_base_dn', '') INSERT INTO phpbb_config (config_name, config_value) VALUES ('ldap_server', ''); INSERT INTO phpbb_config (config_name, config_value) VALUES ('ldap_uid', ''); INSERT INTO phpbb_config (config_name, config_value) VALUES ('limit_load', '0'); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('limit_search_load', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_birthdays', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_cpf_memberlist', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_cpf_viewprofile', '1'); @@ -121,7 +122,6 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_online', '1') INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_online_guests', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_online_time', '5'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_search', '1'); -INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_search_phr', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_search_upd', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('load_tplcompile', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_attachments', '3'); @@ -138,12 +138,13 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_post_smilies', INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_post_urls', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_quote_depth', '3'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_reg_attempts', '5'); -INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_search_chars', '10'); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_search_chars', '14'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_sig_chars', '255'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_sig_smilies', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_sig_urls', '5'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('min_name_chars', '3'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('min_pass_chars', '6'); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('min_search_author_chars', '3'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('min_search_chars', '3'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('override_user_style', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('pm_edit_time', '0'); @@ -156,9 +157,11 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('ranks_path', 'imag INSERT INTO phpbb_config (config_name, config_value) VALUES ('require_activation', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('save_passwd', '3'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('script_path', ''); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_block_size', '250'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_gc', '7200'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_interval', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_type', 'fulltext_phpbb'); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_store_results', '1800'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('secure_allow_deny', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('secure_allow_empty_referer', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('secure_downloads', '0'); @@ -184,6 +187,7 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('version', '2.1.2') INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('cache_last_gc', '0', 1); INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('database_last_gc', '0', 1); INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('last_queue_run', '0', 1); +INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('last_search_time', '0', 1); INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('newest_user_id', '2', 1); INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('newest_username', '', 1); INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('num_files', '0', 1); diff --git a/phpBB/language/en/search.php b/phpBB/language/en/search.php index 58d08849ee..1f4b6c2ff0 100644 --- a/phpBB/language/en/search.php +++ b/phpBB/language/en/search.php @@ -36,6 +36,7 @@ $lang = array_merge($lang, array( 'FOUND_SEARCH_MATCH' => 'Search found %d match', 'FOUND_SEARCH_MATCHES' => 'Search found %d matches', + 'FOUND_MORE_SEARCH_MATCHES' => 'Search found more than %d matches', 'IGNORED_TERMS' => 'ignored', @@ -43,6 +44,8 @@ $lang = array_merge($lang, array( 'NO_SEARCH' => 'Sorry but you are not permitted to use the search system.', 'NO_SEARCH_RESULTS' => 'No suitable matches were found.', 'NO_SEARCH_TIME' => 'Sorry but you cannot use search at this time. Please try again in a few minutes.', + 'NO_KEYWORDS' => 'You must specify at least one word to search for. Each word must consist of at least %d characters and must not contain more than %d characters excluding wildcards.', + 'TOO_FEW_AUTHOR_CHARS' => 'You must specify at least %d characters of the authors name.', 'POST_CHARACTERS' => 'characters of posts', @@ -52,6 +55,7 @@ $lang = array_merge($lang, array( 'RETURN_FIRST' => 'Return first', 'SEARCHED_FOR' => 'Search term used', + 'SEARCHED_TOPIC' => 'Searched topic', 'SEARCH_ALL_TERMS' => 'Search for all terms or use query as entered', 'SEARCH_ANY_TERMS' => 'Search for any terms', 'SEARCH_AUTHOR' => 'Search for Author', diff --git a/phpBB/language/en/search_stopwords.txt b/phpBB/language/en/search_ignore_words.txt index 131cf67622..131cf67622 100644..100755 --- a/phpBB/language/en/search_stopwords.txt +++ b/phpBB/language/en/search_ignore_words.txt diff --git a/phpBB/memberlist.php b/phpBB/memberlist.php index 791c40a63f..1c5e78b5de 100644 --- a/phpBB/memberlist.php +++ b/phpBB/memberlist.php @@ -1235,7 +1235,7 @@ function show_profile($data) 'ICQ_STATUS_IMG'=> (!empty($data['user_icq'])) ? '<img src="http://web.icq.com/whitepages/online?icq=' . $data['user_icq'] . '&img=5" width="18" height="18" border="0" />' : '', 'U_PROFILE' => "{$phpbb_root_path}memberlist.$phpEx$SID&mode=viewprofile&u=$user_id", - 'U_SEARCH_USER' => ($auth->acl_get('u_search')) ? "{$phpbb_root_path}search.$phpEx$SID&search_author=" . urlencode($username) . "&show_results=posts" : '', + 'U_SEARCH_USER' => ($auth->acl_get('u_search')) ? "{$phpbb_root_path}search.$phpEx$SID&author=" . urlencode($username) . "&sr=posts" : '', 'U_NOTES' => $auth->acl_gets('m_', 'a_') ? "{$phpbb_root_path}mcp.$phpEx$SID&i=notes&mode=user_notes&u=$user_id" : '', 'U_WARN' => $auth->acl_gets('m_', 'a_') ? "{$phpbb_root_path}mcp.$phpEx$SID&i=warn&mode=warn_user&u=$user_id" : '', 'U_PM' => ($auth->acl_get('u_sendpm')) ? "{$phpbb_root_path}ucp.$phpEx$SID&i=pm&mode=compose&u=$user_id" : '', diff --git a/phpBB/posting.php b/phpBB/posting.php index 54427e6e0d..b798f5c119 100644 --- a/phpBB/posting.php +++ b/phpBB/posting.php @@ -1812,11 +1812,28 @@ function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $u WHERE topic_moved_id = ' . $data['topic_id']); } - // Fulltext parse + // Index message contents if ($update_message && $data['enable_indexing']) { - $search = new fulltext_search(); - $result = $search->add($mode, $data['post_id'], $data['message'], $subject); + // Select the search method and do some additional checks to ensure it can actually be utilised + $search_type = $config['search_type']; + + if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx)) + { + trigger_error('NO_SUCH_SEARCH_MODULE'); + } + + require("{$phpbb_root_path}includes/search/$search_type.$phpEx"); + + $error = false; + $search = new $search_type($error); + + if ($error) + { + trigger_error($error); + } + + $search->index($mode, $data['post_id'], $data['message'], $subject); } $db->sql_transaction('commit'); diff --git a/phpBB/search.php b/phpBB/search.php index fcf4eacf0b..fceab40459 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -21,26 +21,32 @@ $auth->acl($user->data); $user->setup('search'); // Define initial vars -$mode = request_var('mode', ''); -$search_id = request_var('search_id', ''); -$search_session_id = request_var('search_session_id', 0); -$start = request_var('start', 0); -$post_id = request_var('p', 0); -$view = request_var('view', ''); +$mode = request_var('mode', ''); +$search_id = request_var('search_id', ''); +$start = max(request_var('start', 0), 0); +$post_id = request_var('p', 0); +$topic_id = request_var('t', 0); +$view = request_var('view', ''); $keywords = request_var('keywords', ''); +$add_keywords = request_var('add_keywords', ''); $author = request_var('author', ''); -$show_results = request_var('show_results', 'topics'); -$search_terms = request_var('search_terms', 'all'); -$search_fields = request_var('search_fields', 'all'); -$search_child = request_var('search_child', true); +$show_results = ($topic_id) ? 'posts' : request_var('sr', 'topics'); +$search_terms = request_var('terms', 'all'); +$search_fields = request_var('sf', 'all'); +$search_child = request_var('sc', true); -$return_chars = request_var('return_chars', 200); -$search_forum = request_var('search_forum', array(0)); +$sort_days = request_var('st', 0); +$sort_key = request_var('sk', 't'); +$sort_dir = request_var('sd', 'd'); -$sort_days = request_var('st', 0); -$sort_key = request_var('sk', 't'); -$sort_dir = request_var('sd', 'd'); +$return_chars = request_var('ch', ($topic_id) ? -1 : 200); +$search_forum = request_var('fid', array(0)); + +if ($search_forum == array(0)) +{ + $search_forum = array(); +} // Is user able to search? Has search been disabled? if (!$auth->acl_get('u_search') || !$config['load_search']) @@ -48,116 +54,188 @@ if (!$auth->acl_get('u_search') || !$config['load_search']) trigger_error($user->lang['NO_SEARCH']); } -// Define some vars -$limit_days = array(0 => $user->lang['ALL_RESULTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 364 => $user->lang['1_YEAR']); -$sort_by_text = array('a' => $user->lang['SORT_AUTHOR'], 't' => $user->lang['SORT_TIME'], 'f' => $user->lang['SORT_FORUM'], 'i' => $user->lang['SORT_TOPIC_TITLE'], 's' => $user->lang['SORT_POST_SUBJECT']); - -$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = ''; -gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param); - -$store_vars = array('sort_key', 'sort_dir', 'sort_days', 'show_results', 'return_chars', 'total_match_count'); -$current_time = time(); +// Check search load limit +if ($user->load && $config['limit_search_load'] && ($user->load > doubleval($config['limit_search_load']))) +{ + trigger_error($user->lang['NO_SEARCH_TIME']); +} // Check last search time ... if applicable if ($config['search_interval']) { - $sql = 'SELECT MAX(search_time) as last_time - FROM ' . SEARCH_TABLE; - $result = $db->sql_query($sql); - - if ($row = $db->sql_fetchrow($result)) + if ($config['last_search_time'] > time() - $config['search_interval']) { - if ($row['last_time'] > time() - $config['search_interval']) - { - trigger_error($user->lang['NO_SEARCH_TIME']); - } + trigger_error($user->lang['NO_SEARCH_TIME']); } } -if ($keywords || $author || $search_id || $search_session_id) +// Define some vars +$limit_days = array(0 => $user->lang['ALL_RESULTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 364 => $user->lang['1_YEAR']); +$sort_by_text = array('a' => $user->lang['SORT_AUTHOR'], 't' => $user->lang['SORT_TIME'], 'f' => $user->lang['SORT_FORUM'], 'i' => $user->lang['SORT_TOPIC_TITLE'], 's' => $user->lang['SORT_POST_SUBJECT']); + +$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = ''; +gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param); + +if ($keywords || $author || $search_id) { // clear arrays - $pid_ary = $fid_ary = array(); + $id_ary = array(); + + // Which forums should not be searched? + $ex_fid_ary = array_keys($auth->acl_getf('!f_read', true)); - // Which forums can we view? - $sql_where = (sizeof($search_forum) && !$search_child) ? 'WHERE f.forum_id IN (' . implode(', ', $search_forum) . ')' : ''; $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.right_id, f.forum_password, fa.user_id FROM ' . FORUMS_TABLE . ' f LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id AND fa.session_id = '" . $db->sql_escape($user->data['session_id']) . "') - $sql_where - ORDER BY f.left_id"; + WHERE f.forum_id NOT IN (" . implode(', ', $ex_fid_ary) . ") + OR (f.forum_password <> '' AND fa.user_id <> " . (int) $user->data['user_id'] . ') + ORDER BY f.left_id'; $result = $db->sql_query($sql); $right_id = 0; + $reset_search_forum = true; while ($row = $db->sql_fetchrow($result)) { - if ($search_child) + if ($row['forum_password'] && ($row['user_id'] != $user->data['user_id'])) + { + $ex_fid_ary[] = $row['forum_id']; + continue; + } + + if (sizeof($search_forum)) { - if (!$search_forum || (in_array($row['forum_id'], $search_forum) && $row['right_id'] > $right_id)) + if ($search_child) { - $right_id = $row['right_id']; + if (in_array($row['forum_id'], $search_forum) && $row['right_id'] > $right_id) + { + $right_id = $row['right_id']; + } + else if ($row['right_id'] < $right_id) + { + continue; + } } - else if ($row['right_id'] > $right_id) + + if (!in_array($row['forum_id'], $search_forum)) { - continue; + $ex_fid_ary[] = $row['forum_id']; + $reset_search_forum = false; } } - - if ($auth->acl_get('f_read', $row['forum_id']) && (!$row['forum_password'] || $row['user_id'] == $user->data['user_id'])) - { - $fid_ary[] = $row['forum_id']; - } } $db->sql_freeresult($result); - unset($search_forum); - if (!sizeof($fid_ary)) + if ($reset_search_forum) { - trigger_error($user->lang['NO_SEARCH_RESULTS']); + $search_forum = array(); } + // egosearch is an author search if ($search_id == 'egosearch') { $author = $user->data['username']; } - // Are we looking for a user? - $author_id = 0; + // If we are looking for authors get their ids + $author_id_ary = array(); if ($author) { + if ((strstr($author, '*') !== false) && (str_replace(array('*', '%'), '', $author) < $config['min_search_author_chars'])) + { + trigger_error(sprintf($user->lang['TOO_FEW_AUTHOR_CHARS'], $config['min_search_author_chars'])); + } + $sql_where = (strstr($author, '*') !== false) ? ' LIKE ' : ' = '; $sql = 'SELECT user_id FROM ' . USERS_TABLE . " WHERE username $sql_where '" . $db->sql_escape(preg_replace('#\*+#', '%', $author)) . "' AND user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')'; - $result = $db->sql_query($sql); + $result = $db->sql_query_limit($sql, 100); - if (!$row = $db->sql_fetchrow($result)) + while ($row = $db->sql_fetchrow($result)) { - trigger_error($user->lang['NO_SEARCH_RESULTS']); + $author_id_ary[] = (int) $row['user_id']; } + $db->sql_freeresult($result); - $author_id = (int) $row['user_id']; + if (!sizeof($author_id_ary)) + { + trigger_error($user->lang['NO_SEARCH_RESULTS']); + } + } + + // if we search in an existing search result just add the additional keywords. But we need to use "all search terms"-mode + // so we can keep the old keywords in their old mode, but add the new ones as required words + if ($add_keywords) + { + if ($search_terms == 'all') + { + $keywords .= ' ' . $add_keywords; + } + else + { + $search_terms = 'all'; + $keywords = implode(' |', explode(' ', preg_replace('#\s+#', ' ', $keywords))) . ' ' .$add_keywords; + } + } + + // Select which method we'll use to obtain the post_id or topic_id information + $search_type = $config['search_type']; + + if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx)) + { + trigger_error('NO_SUCH_SEARCH_MODULE'); } + require("{$phpbb_root_path}includes/search/$search_type.$phpEx"); + // We do some additional checks in the module to ensure it can actually be utilised + $error = false; + $search = new $search_type($error); + + if ($error) + { + trigger_error($error); + } + + // let the search module split up the keywords + if ($keywords) + { + $search->split_keywords($keywords, $search_terms); + if (!sizeof($search->split_words) && !sizeof($author_id_ary) && !$search_id) + { + trigger_error(sprintf($user->lang['NO_KEYWORDS'], $config['min_search_chars'], $config['max_search_chars'])); + } + } + + // define some variables needed for retrieving post_id/topic_id information + $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page']; + $sort_by_sql = array('a' => (($show_results == 'posts') ? 'u.username' : 't.topic_poster'), 't' => (($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time'), 'f' => 'f.forum_id', 'i' => 't.topic_title', 's' => (($show_results == 'posts') ? 'p.post_subject' : 't.topic_title')); + + // pre-made searches + $sql = $field = ''; if ($search_id) { - $sql_in = $sql_where = ''; + // Build sql string for sorting + $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC'); switch ($search_id) { // Oh holy Bob, bring us some activity... case 'active_topics': $show_results = 'topics'; + $sort_key = 't'; + $sort_dir = 'd'; + $sort_by_sql['t'] = 't.topic_last_post_time'; if (!$sort_days) { $sort_days = 1; - gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param); } + gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param); + $s_sort_key = $s_sort_dir = $u_sort_param = ''; $last_post_time = (time() - ($sort_days * 24 * 3600)); @@ -165,214 +243,114 @@ if ($keywords || $author || $search_id || $search_session_id) FROM ' . POSTS_TABLE . ' p LEFT JOIN ' . TOPICS_TABLE . " t ON (t.topic_approved = 1 AND p.topic_id = t.topic_id) WHERE p.post_time > $last_post_time - " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : '') . ' + " . ((sizeof($ex_fid_ary)) ? ' AND p.forum_id NOT IN (' . implode(',', $ex_fid_ary) . ')' : '') . ' ORDER BY t.topic_last_post_time DESC'; - $result = $db->sql_query_limit($sql, 1000); - - while ($row = $db->sql_fetchrow($result)) - { - $pid_ary[] = $row['topic_id']; - } - $db->sql_freeresult($result); - break; - - case 'egosearch': + $field = 'topic_id'; break; case 'unanswered': + $sort_join = ($sort_key == 'f') ? FORUMS_TABLE . ' f, ' : ''; + $sql_sort = ($sort_key == 'f') ? ' AND f.forum_id = p.forum_id ' . $sql_sort : $sql_sort; if ($show_results == 'posts') { - $sql = 'SELECT p.post_id - FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t + if ($sort_key == 'a') + { + $sort_join = USERS_TABLE . ' u, '; + $sql_sort = ' AND u.user_id = p.poster_id ' . $sql_sort; + } + $sql = "SELECT p.post_id + FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t WHERE t.topic_replies = 0 AND p.topic_id = t.topic_id - " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : ''); + " . ((sizeof($ex_fid_ary)) ? ' AND p.forum_id NOT IN (' . implode(',', $ex_fid_ary) . ')' : '') . " + $sql_sort"; $field = 'post_id'; } else { - $sql = 'SELECT t.topic_id - FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t + $sql = "SELECT DISTINCT p.topic_id + FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t WHERE t.topic_replies = 0 AND p.topic_id = t.topic_id - " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : '') . ' - GROUP BY p.topic_id'; + " . ((sizeof($ex_fid_ary)) ? ' AND p.forum_id NOT IN (' . implode(',', $ex_fid_ary) . ')' : '') . " + $sql_sort"; $field = 'topic_id'; } - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - $pid_ary[] = $row[$field]; - } - $db->sql_freeresult($result); - - if (!sizeof($pid_ary)) - { - trigger_error($user->lang['NO_SEARCH_RESULTS']); - } break; case 'newposts': + $sort_join = ($sort_key == 'f') ? FORUMS_TABLE . ' f, ' : ''; + $sql_sort = ($sort_key == 'f') ? ' AND f.forum_id = p.forum_id ' . $sql_sort : $sql_sort; if ($show_results == 'posts') { - $sql = 'SELECT p.post_id - FROM ' . POSTS_TABLE . ' p + if ($sort_key == 'i') + { + $sort_join = TOPICS_TABLE . ' t, '; + $sql_sort = ' AND t.topic_id = p.topic_id ' . $sql_sort; + } + elseif ($sort_key == 'a') + { + $sort_join = USERS_TABLE . ' u, '; + $sql_sort = ' AND u.user_id = p.poster_id ' . $sql_sort; + } + + $sql = "SELECT p.post_id + FROM $sort_join" . POSTS_TABLE . ' p WHERE p.post_time > ' . $user->data['user_lastvisit'] . " - " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : ''); + " . ((sizeof($ex_fid_ary)) ? ' AND p.forum_id NOT IN (' . implode(',', $ex_fid_ary) . ')' : '') . " + $sql_sort"; $field = 'post_id'; } else { - $sql = 'SELECT t.topic_id - FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p + $sql = "SELECT DISTINCT p.topic_id + FROM $sort_join" . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p WHERE p.post_time > ' . $user->data['user_lastvisit'] . " AND t.topic_id = p.topic_id - " . ((sizeof($fid_ary)) ? ' AND p.forum_id IN (' . implode(',', $fid_ary) . ')' : '') . ' - GROUP by p.topic_id'; + " . ((sizeof($ex_fid_ary)) ? ' AND p.forum_id NOT IN (' . implode(',', $ex_fid_ary) . ')' : '') . " + $sql_sort"; $field = 'topic_id'; } - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - $pid_ary[] = $row[$field]; - } - $db->sql_freeresult($result); - - if (!sizeof($pid_ary)) - { - trigger_error($user->lang['NO_SEARCH_RESULTS']); - } break; } - } - - /** - * @todo add to config - */ - $config['search_type'] = 'phpbb'; - - // Select which method we'll use to obtain the post_id information - $smid = ''; - switch ($config['search_type']) - { - case 'phpbb': - $smid = 'fulltext_phpbb'; - break; - case 'mysql': - $smid = 'fulltext_mysql'; - break; -/* case 'mssql': - case 'pgsql': - $smid = 'fulltext_pgmssql'; - break; - case 'like': - $smid = 'like'; - break; - case 'preg': - $smid = 'preg_mysql'; - break;*/ - default: - trigger_error('NO_SUCH_SEARCH_MODULE'); - } - - require($phpbb_root_path . 'includes/search/' . $smid . '.' . $phpEx); - - // We do some additional checks in each module to ensure it can actually be utilised - $error = false; - $search = new $smid($error); - - if ($error) - { - trigger_error($error); - } - - if ($search_session_id) - { - $sql = 'SELECT search_array - FROM ' . SEARCH_TABLE . " - WHERE search_id = $search_session_id - AND session_id = '" . $db->sql_escape($user->data['session_id']) . "'"; - $result = $db->sql_query($sql); - if ($row = $db->sql_fetchrow($result)) + if ($sql) { - $pid_ary = explode('#', $row['search_array']); + // only return up to 1000 ids (the last one will be removed later) + $result = $db->sql_query_limit($sql, 1001 - $start, $start); - $search->split_words = unserialize(array_shift($pid_ary)); - if ($keywords) - { - // If we're wanting to search on these results we store the existing split word array - $search->old_split_words = $search->split_words; - } - $search->common_words = unserialize(array_shift($pid_ary)); - - foreach ($store_vars as $var) + while ($row = $db->sql_fetchrow($result)) { - $$var = array_shift($pid_ary); + $id_ary[] = $row[$field]; } - } - $db->sql_freeresult($result); - } - - $total_match_count = 0; - $search->search($show_results, $search_fields, $search_terms, $fid_ary, $keywords, $author_id, $pid_ary, $sort_days); - - if ($pid_ary) - { - // Finish building query (for all combinations) and run it ... - $sql = 'SELECT session_id - FROM ' . SESSIONS_TABLE; - $result = $db->sql_query($sql); - - $delete_search_ids = array(); - while ($row = $db->sql_fetchrow($result)) - { - $delete_search_ids[] = "'" . $db->sql_escape($row['session_id']) . "'"; - } + $db->sql_freeresult($result); - if (sizeof($delete_search_ids)) - { - $sql = 'DELETE FROM ' . SEARCH_TABLE . ' - WHERE session_id NOT IN (' . implode(", ", $delete_search_ids) . ')'; - $db->sql_query($sql); + $total_match_count = sizeof($id_ary) + $start; + $id_ary = array_slice($id_ary, 0, $per_page); } - - $total_match_count = sizeof($pid_ary); - $sql_where = (($show_results == 'posts') ? 'p.post_id' : 't.topic_id') . ' IN (' . implode(', ', $pid_ary) . ')'; - - if (sizeof($search->old_split_words) && array_diff($search->split_words, $search->old_split_words)) + else { - $search->split_words = array_merge($search->split_words, $search->old_split_words); + $search_id = ''; } + } - $data = serialize($search->split_words); - $data .= '#' . serialize($search->common_words); - - foreach ($store_vars as $var) - { - $data .= '#' . $$var; - } - $data .= '#' . implode('#', $pid_ary); - - unset($pid_ary); - - srand ((double) microtime() * 1000000); - $search_session_id = rand(); - - $sql_ary = array( - 'search_id' => $search_session_id, - 'session_id' => $user->data['session_id'], - 'search_time' => $current_time, - 'search_array' => $data - ); + if (sizeof($search->split_words)) + { + $total_match_count = $search->keyword_search($show_results, $search_fields, $search_terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $topic_id, $author_id_ary, $id_ary, $start, $per_page); + } + else if (sizeof($author_id_ary)) + { + $total_match_count = $search->author_search($show_results, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $topic_id, $author_id_ary, $id_ary, $start, $per_page); + } - $sql = 'INSERT INTO ' . SEARCH_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary); - $db->sql_query($sql); - unset($data); + if (!sizeof($id_ary)) + { + trigger_error($user->lang['NO_SEARCH_RESULTS']); } + $sql_where = (($show_results == 'posts') ? 'p.post_id' : 't.topic_id') . ' IN (' . implode(', ', $id_ary) . ')'; + $sql_where .= (sizeof($ex_fid_ary)) ? ' AND f.forum_id NOT IN (' . implode(',', $ex_fid_ary) . ')' : ''; + if ($show_results == 'posts') { include($phpbb_root_path . 'includes/functions_posting.' . $phpEx); @@ -382,53 +360,72 @@ if ($keywords || $author || $search_id || $search_session_id) include($phpbb_root_path . 'includes/functions_display.' . $phpEx); } - // Look up data ... - $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page']; - // Grab icons $icons = array(); $cache->obtain_icons($icons); // Output header - $l_search_matches = ($total_match_count == 1) ? sprintf($user->lang['FOUND_SEARCH_MATCH'], $total_match_count) : sprintf($user->lang['FOUND_SEARCH_MATCHES'], $total_match_count); + if ($search_id && ($total_match_count > 1000)) + { + // limit the number to 1000 for pre-made searches + $total_match_count--; + $l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count); + } + else + { + $l_search_matches = ($total_match_count == 1) ? sprintf($user->lang['FOUND_SEARCH_MATCH'], $total_match_count) : sprintf($user->lang['FOUND_SEARCH_MATCHES'], $total_match_count); + } + // define some vars for urls $hilit = htmlspecialchars(implode('|', str_replace(array('+', '-', '|'), '', $search->split_words))); $split_words = (sizeof($search->split_words)) ? htmlspecialchars(implode(' ', $search->split_words)) : ''; + $u_hilit = urlencode($split_words); + $u_show_results = ($show_results != 'topic') ? '&sr=' . $show_results : ''; + $u_search_forum = implode('&fid%5B%5D=', $search_forum); + + $u_search = "{$phpbb_root_path}search.$phpEx$SID"; + $u_search .= ($search_id) ? '&search_id=' . $search_id : ''; + $u_search .= ($u_hilit) ? '&keywords=' . $u_hilit : ''; + $u_search .= ($topic_id) ? '&ch=' . $topic_id : ''; + $u_search .= ($author) ? '&author=' . urlencode($author) : ''; + $u_search .= ($u_search_forum) ? '&fid%5B%5D=' . $u_search_forum : ''; + $u_search .= (!$search_child) ? '&sc=0' : ''; + $u_search .= ($search_fields != 'all') ? '&sf=' . $search_fields : ''; + $u_search .= '&' . $u_sort_param . $u_show_results; + $u_search .= ($return_chars != 200) ? '&ch=' . $return_chars : ''; + $template->assign_vars(array( 'SEARCH_MATCHES' => $l_search_matches, - 'SEARCH_WORDS' => $split_words, - 'IGNORED_WORDS' => (sizeof($search->common_words)) ? htmlspecialchars(implode(' ', $search->common_words)) : '', - 'PAGINATION' => generate_pagination("{$phpbb_root_path}search.$phpEx$SID&search_session_id=$search_session_id&search_id=$search_id&hilit=$hilit&$u_sort_param", $total_match_count, $per_page, $start), + 'SEARCH_WORDS' => $split_words, + 'IGNORED_WORDS' => (sizeof($search->common_words)) ? htmlspecialchars(implode(' ', $search->common_words)) : '', + 'PAGINATION' => generate_pagination($u_search, $total_match_count, $per_page, $start), 'PAGE_NUMBER' => on_page($total_match_count, $per_page, $start), 'TOTAL_MATCHES' => $total_match_count, + 'SEARCH_IN_RESULTS' => ($search_id) ? false : true, 'S_SELECT_SORT_DIR' => $s_sort_dir, 'S_SELECT_SORT_KEY' => $s_sort_key, 'S_SELECT_SORT_DAYS' => $s_limit_days, - 'S_SEARCH_ACTION' => "{$phpbb_root_path}search.$phpEx$SID&search_session_id=$search_session_id&search_id=$search_id", + 'S_SEARCH_ACTION' => $u_search, 'S_SHOW_TOPICS' => ($show_results == 'posts') ? false : true, 'REPORTED_IMG' => $user->img('icon_reported', 'TOPIC_REPORTED'), 'UNAPPROVED_IMG' => $user->img('icon_unapproved', 'TOPIC_UNAPPROVED'), 'GOTO_PAGE_IMG' => $user->img('icon_post', 'GOTO_PAGE'), - 'U_SEARCH_WORDS' => "{$phpbb_root_path}search.$phpEx$SID&show_results=$show_results&keywords=" . urlencode($split_words)) + 'U_SEARCH_WORDS' => "{$phpbb_root_path}search.$phpEx$SID$u_show_results&keywords=$u_hilit") ); - $u_hilit = urlencode($split_words); - - // Define ordering sql field, do it here because the order may be defined - // within an existing search result set - $sort_by_sql = array('a' => (($show_results == 'posts') ? 'u.username' : 't.topic_poster'), 't' => (($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time'), 'f' => 'f.forum_id', 'i' => 't.topic_title', 's' => (($show_results == 'posts') ? 'pt.post_subject' : 't.topic_title')); - if ($sql_where) { if ($show_results == 'posts') { - // Not joining this query to the one below at present ... may do in future + /** + * @todo Joining this query to the one below? + */ $sql = 'SELECT zebra_id, friend, foe - FROM ' . ZEBRA_TABLE . ' + FROM ' . ZEBRA_TABLE . ' WHERE user_id = ' . $user->data['user_id']; $result = $db->sql_query($sql); @@ -440,8 +437,8 @@ if ($keywords || $author || $search_id || $search_session_id) $db->sql_freeresult($result); $sql = 'SELECT p.*, f.forum_id, f.forum_name, t.*, u.username, u.user_sig, u.user_sig_bbcode_uid - FROM ' . FORUMS_TABLE . ' f, ' . TOPICS_TABLE . ' t, ' . USERS_TABLE . ' u, ' . POSTS_TABLE . " p - WHERE $sql_where + FROM ' . FORUMS_TABLE . ' f, ' . TOPICS_TABLE . ' t, ' . USERS_TABLE . ' u, ' . POSTS_TABLE . " p + WHERE $sql_where AND f.forum_id = p.forum_id AND p.topic_id = t.topic_id AND p.poster_id = u.user_id"; @@ -449,19 +446,21 @@ if ($keywords || $author || $search_id || $search_session_id) else { $sql = 'SELECT t.*, f.forum_id, f.forum_name - FROM ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . " f - WHERE $sql_where + FROM ' . TOPICS_TABLE . ' t, ' . FORUMS_TABLE . " f + WHERE $sql_where AND f.forum_id = t.forum_id"; } $sql .= ' ORDER BY ' . $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC'); - $result = $db->sql_query_limit($sql, $per_page, $start); + $result = $db->sql_query($sql); + $result_topic_id = 0; while ($row = $db->sql_fetchrow($result)) { $forum_id = $row['forum_id']; - $topic_id = $row['topic_id']; + $result_topic_id = $row['topic_id']; + $topic_title = censor_text($row['topic_title']); - $view_topic_url = "{$phpbb_root_path}viewtopic.$phpEx$SID&f=$forum_id&t=$topic_id&hilit=$u_hilit"; + $view_topic_url = "{$phpbb_root_path}viewtopic.$phpEx$SID&f=$forum_id&t=$result_topic_id&hilit=$u_hilit"; if ($show_results == 'topics') { @@ -497,8 +496,8 @@ if ($keywords || $author || $search_id || $search_session_id) 'U_LAST_POST' => $view_topic_url . '&p=' . $row['topic_last_post_id'] . '#' . $row['topic_last_post_id'], 'U_LAST_POST_AUTHOR'=> ($row['topic_last_poster_id'] != ANONYMOUS && $row['topic_last_poster_id']) ? "{$phpbb_root_path}memberlist.$phpEx$SID&mode=viewprofile&u={$row['topic_last_poster_id']}" : '', - 'U_MCP_REPORT' => "{$phpbb_root_path}mcp.$phpEx?sid={$user->session_id}&mode=reports&t=$topic_id", - 'U_MCP_QUEUE' => "{$phpbb_root_path}mcp.$phpEx?sid={$user->session_id}&i=queue&mode=approve_details&t=$topic_id" + 'U_MCP_REPORT' => "{$phpbb_root_path}mcp.$phpEx?sid={$user->session_id}&mode=reports&t=$result_topic_id", + 'U_MCP_QUEUE' => "{$phpbb_root_path}mcp.$phpEx?sid={$user->session_id}&i=queue&mode=approve_details&t=$result_topic_id" ); } else @@ -506,11 +505,11 @@ if ($keywords || $author || $search_id || $search_session_id) if ((isset($zebra['foe']) && in_array($row['poster_id'], $zebra['foe'])) && (!$view || $view != 'show' || $post_id != $row['post_id'])) { $template->assign_block_vars('searchresults', array( - 'S_IGNORE_POST' => true, + 'S_IGNORE_POST' => true, - 'L_IGNORE_POST' => sprintf($user->lang['POST_BY_FOE'], $row['username'], "<a href=\"search.$phpEx$SID&search_session_id=$search_session_id&$u_sort_param&p=" . $row['post_id'] . '&view=show#' . $row['post_id'] . '">', '</a>')) + 'L_IGNORE_POST' => sprintf($user->lang['POST_BY_FOE'], $row['username'], "<a href=\"$u_search&p=" . $row['post_id'] . '&view=show#' . $row['post_id'] . '">', '</a>')) ); - + continue; } @@ -519,48 +518,55 @@ if ($keywords || $author || $search_id || $search_session_id) $row['post_text'] = preg_replace('#(<!\-\- h \-\-><)([\/]?.*?)(><!\-\- h \-\->)#is', "<\\2>", $row['post_text']); } - $row['post_text'] = censor_text($row['post_text']); decode_message($row['post_text'], $row['bbcode_uid']); - - if ($return_chars) + + if ($return_chars != -1) { $row['post_text'] = (strlen($row['post_text']) < $return_chars + 3) ? $row['post_text'] : substr($row['post_text'], 0, $return_chars) . '...'; } + // Replace naughty words such as farty pants + $row['post_subject'] = censor_text($row['post_subject']); + $row['post_text'] = str_replace("\n", '<br />', censor_text($row['post_text'])); + if ($hilit) { - $row['post_text'] = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*>)#i', '<span class="posthilit">\1</span>', $row['post_text']); + $row['post_text'] = preg_replace('#(?!<.*)(?<!\w)(' . preg_quote($hilit) . ')(?!\w|[^<>]*>)#i', '<span class="posthilit">$1</span>', $row['post_text']); } $row['post_text'] = smiley_text($row['post_text']); - // Replace naughty words such as farty pants - $row['post_subject'] = censor_text($row['post_subject']); - $row['post_text'] = str_replace("\n", '<br />', censor_text($row['post_text'])); - $tpl_ary = array( - 'POSTER_NAME' => ($row['poster_id'] == ANONYMOUS) ? ((!empty($row['post_username'])) ? $row['post_username'] : $user->lang['GUEST']) : $row['username'], + 'POSTER_NAME' => ($row['poster_id'] == ANONYMOUS) ? ((!empty($row['post_username'])) ? $row['post_username'] : $user->lang['GUEST']) : $row['username'], 'U_PROFILE' => ($row['poster_id'] != ANONYMOUS) ? "{$phpbb_root_path}memberlist.$phpEx$SID&mode=viewprofile&u={$row['poster_id']}" : '', - 'POST_SUBJECT' => censor_text($row['post_subject']), - 'POST_DATE' => (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '', + 'POST_SUBJECT' => $row['post_subject'], + 'POST_DATE' => (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '', 'MESSAGE' => $row['post_text'] ); } $template->assign_block_vars('searchresults', array_merge($tpl_ary, array( 'FORUM_ID' => $forum_id, - 'TOPIC_ID' => $topic_id, - 'POST_ID' => ($show_results == 'posts') ? $row['post_id'] : false, + 'TOPIC_ID' => $result_topic_id, + 'POST_ID' => ($show_results == 'posts') ? $row['post_id'] : false, - 'FORUM_TITLE' => $row['forum_name'], - 'TOPIC_TITLE' => censor_text($row['topic_title']), + 'FORUM_TITLE' => $row['forum_name'], + 'TOPIC_TITLE' => $topic_title, 'U_VIEW_TOPIC' => $view_topic_url, - 'U_VIEW_FORUM' => "viewforum.$phpEx$SID&f=$forum_id", + 'U_VIEW_FORUM' => "viewforum.$phpEx$SID&f=$forum_id", 'U_VIEW_POST' => (!empty($row['post_id'])) ? "viewtopic.$phpEx$SID&f=$forum_id&t=" . $row['topic_id'] . '&p=' . $row['post_id'] . '&hilit=' . $u_hilit . '#' . $row['post_id'] : '') )); } $db->sql_freeresult($result); + + if ($topic_id && ($topic_id == $result_topic_id)) + { + $template->assign_vars(array( + 'SEARCH_TOPIC' => $topic_title, + 'U_SEARCH_TOPIC' => $view_topic_url + )); + } } else { @@ -592,7 +598,6 @@ $result = $db->sql_query($sql); $right = $cat_right = $padding_inc = 0; $padding = $forum_list = $holding = ''; $pad_store = array('0' => ''); -$search_forums = array(); while ($row = $db->sql_fetchrow($result)) { @@ -620,7 +625,7 @@ while ($row = $db->sql_fetchrow($result)) $right = $row['right_id']; - $selected = (!sizeof($search_forums) || in_array($row['forum_id'], $search_forums)) ? ' selected="selected"' : ''; + $selected = (!sizeof($search_forum) || in_array($row['forum_id'], $search_forum)) ? ' selected="selected"' : ''; if ($row['left_id'] > $cat_right) { @@ -655,7 +660,8 @@ for ($i = 100; $i <= 1000 ; $i += 100) } $template->assign_vars(array( - 'S_SEARCH_ACTION' => "{$phpbb_root_path}search.$phpEx$SID&mode=results", + 'S_SEARCH_ACTION' => "{$phpbb_root_path}search.$phpEx", + 'S_HIDDEN_FIELDS' => build_hidden_fields(array('sid' => $user->session_id)), 'S_CHARACTER_OPTIONS' => $s_characters, 'S_FORUM_OPTIONS' => $s_forums, 'S_SELECT_SORT_DIR' => $s_sort_dir, @@ -663,38 +669,22 @@ $template->assign_vars(array( 'S_SELECT_SORT_DAYS' => $s_limit_days) ); -$sql = 'SELECT search_id, search_time, search_array +$sql = 'SELECT search_time, search_keywords FROM ' . SEARCH_TABLE . ' + WHERE search_keywords <> \'\' ORDER BY search_time DESC'; -$result = $db->sql_query($sql); +$result = $db->sql_query_limit($sql, 5); -$i = 0; while ($row = $db->sql_fetchrow($result)) { - if ($i == 5) - { - break; - } - - $data = explode('#', $row['search_array']); - $split_words = htmlspecialchars(implode(' ', unserialize(array_shift($data)))); - - if (!$split_words) - { - continue; - } - - $common_words = htmlspecialchars(implode(' ', unserialize(array_shift($data)))); - unset($data); + $keywords = htmlspecialchars($row['search_keywords']); $template->assign_block_vars('recentsearch', array( - 'KEYWORDS' => $split_words, + 'KEYWORDS' => $keywords, 'TIME' => $user->format_date($row['search_time']), - 'U_KEYWORDS' => "{$phpbb_root_path}search.$phpEx$SID&keywords=" . urlencode($split_words)) + 'U_KEYWORDS' => "{$phpbb_root_path}search.$phpEx$SID&keywords=" . urlencode($keywords)) ); - - $i++; } $db->sql_freeresult($result); diff --git a/phpBB/styles/subSilver/template/search_body.html b/phpBB/styles/subSilver/template/search_body.html index f3811ff140..11a7cdc93c 100644 --- a/phpBB/styles/subSilver/template/search_body.html +++ b/phpBB/styles/subSilver/template/search_body.html @@ -4,13 +4,13 @@ <div id="pagecontent"> - <form method="post" action="{S_SEARCH_ACTION}"><table class="tablebg" width="100%" cellspacing="1"> + <form method="get" action="{S_SEARCH_ACTION}"><table class="tablebg" width="100%" cellspacing="1"> <tr> <th colspan="4">{L_SEARCH_QUERY}</th> </tr> <tr> <td class="row1" colspan="2" width="50%"><b class="genmed">{L_SEARCH_KEYWORDS}: </b><br /><span class="gensmall">{L_SEARCH_KEYWORDS_EXPLAIN}</span></td> - <td class="row2" colspan="2" valign="top"><input type="text" style="width: 300px" class="post" name="keywords" size="30" /><br /><input type="radio" name="search_terms" value="all" checked="checked" /> <span class="genmed">{L_SEARCH_ALL_TERMS}</span><br /><input type="radio" name="search_terms" value="any" /> <span class="genmed">{L_SEARCH_ANY_TERMS}</span></td> + <td class="row2" colspan="2" valign="top"><input type="text" style="width: 300px" class="post" name="keywords" size="30" /><br /><input type="radio" name="terms" value="all" checked="checked" /> <span class="genmed">{L_SEARCH_ALL_TERMS}</span><br /><input type="radio" name="terms" value="any" /> <span class="genmed">{L_SEARCH_ANY_TERMS}</span></td> </tr> <tr> <td class="row1" colspan="2"><b class="genmed">{L_SEARCH_AUTHOR}:</b><br /><span class="gensmall">{L_SEARCH_AUTHOR_EXPLAIN}</span></td> @@ -18,28 +18,28 @@ </tr> <tr> <td class="row1" colspan="2"><b class="genmed">{L_SEARCH_FORUMS}: </b><br /><span class="gensmall">{L_SEARCH_FORUMS_EXPLAIN}</span></td> - <td class="row2" colspan="2"><select name="search_forum[]" multiple="multiple" size="5">{S_FORUM_OPTIONS}</select></td> + <td class="row2" colspan="2"><select name="fid[]" multiple="multiple" size="5">{S_FORUM_OPTIONS}</select></td> </tr> <tr> <th colspan="4">{L_SEARCH_OPTIONS}</th> </tr> <tr> <td class="row1" width="25%" nowrap="nowrap"><b class="genmed">{L_SEARCH_SUBFORUMS}: </b></td> - <td class="row2" width="25%" nowrap="nowrap"><input type="radio" name="search_child" value="1" checked="checked" /> <span class="genmed">{L_YES}</span> <input type="radio" name="search_child" value="0" /> <span class="genmed">{L_NO}</span></td> + <td class="row2" width="25%" nowrap="nowrap"><input type="radio" name="sc" value="1" checked="checked" /> <span class="genmed">{L_YES}</span> <input type="radio" name="sc" value="0" /> <span class="genmed">{L_NO}</span></td> <td class="row1" width="25%" nowrap="nowrap"><b class="genmed">{L_SEARCH_WITHIN}: </b></td> - <td class="row2" width="25%" nowrap="nowrap"><input type="radio" name="search_fields" value="all" checked="checked" /> <span class="genmed">{L_SEARCH_TITLE_MSG}</span><br /><input type="radio" name="search_fields" value="msgonly" /> <span class="genmed">{L_SEARCH_MSG_ONLY}</span> <br /><input type="radio" name="search_fields" value="titleonly" /> <span class="genmed">{L_SEARCH_TITLE_ONLY}</span></td> + <td class="row2" width="25%" nowrap="nowrap"><input type="radio" name="sf" value="all" checked="checked" /> <span class="genmed">{L_SEARCH_TITLE_MSG}</span><br /><input type="radio" name="sf" value="msgonly" /> <span class="genmed">{L_SEARCH_MSG_ONLY}</span> <br /><input type="radio" name="sf" value="titleonly" /> <span class="genmed">{L_SEARCH_TITLE_ONLY}</span></td> </tr> <tr> <td class="row1"><b class="genmed">{L_RESULT_SORT}: </b></td> <td class="row2" nowrap="nowrap">{S_SELECT_SORT_KEY}<br /><input type="radio" name="sd" value="a" /> <span class="genmed">{L_SORT_ASCENDING}</span><br /><input type="radio" name="sd" value="d" checked="checked" /> <span class="genmed">{L_SORT_DESCENDING}</span></td> <td class="row1" nowrap="nowrap"><b class="genmed">{L_DISPLAY_RESULTS}: </b></td> - <td class="row2" nowrap="nowrap"><input type="radio" name="show_results" value="posts" /> <span class="genmed">{L_POSTS}</span> <input type="radio" name="show_results" value="topics" checked="checked" /> <span class="genmed">{L_TOPICS}</td> + <td class="row2" nowrap="nowrap"><input type="radio" name="sr" value="posts" /> <span class="genmed">{L_POSTS}</span> <input type="radio" name="sr" value="topics" checked="checked" /> <span class="genmed">{L_TOPICS}</td> </tr> <tr> <td class="row1" width="25%"><b class="genmed">{L_RESULT_DAYS}: </b></td> <td class="row2" width="25%" nowrap="nowrap">{S_SELECT_SORT_DAYS}</td> <td class="row1" nowrap="nowrap"><b class="genmed">{L_RETURN_FIRST}: </b></td> - <td class="row2" nowrap="nowrap"><select name="return_chars">{S_CHARACTER_OPTIONS}</select> <span class="genmed">{L_POST_CHARACTERS}</span></td> + <td class="row2" nowrap="nowrap"><select name="ch">{S_CHARACTER_OPTIONS}</select> <span class="genmed">{L_POST_CHARACTERS}</span></td> </tr> <tr> <td class="cat" colspan="4" align="center">{S_HIDDEN_FIELDS}<input class="btnlite" type="submit" value="{L_SEARCH}" /> <input class="btnlite" type="reset" value="{L_RESET}" /></td> diff --git a/phpBB/styles/subSilver/template/search_results.html b/phpBB/styles/subSilver/template/search_results.html index 8ed1f327f6..b801da453e 100644 --- a/phpBB/styles/subSilver/template/search_results.html +++ b/phpBB/styles/subSilver/template/search_results.html @@ -9,8 +9,8 @@ <td colspan="2"><span class="titles">{SEARCH_MATCHES}</span><br /></td> </tr> <tr> - <td class="genmed"><!-- IF SEARCH_WORDS -->{L_SEARCHED_FOR}: <a href="{U_SEARCH_WORDS}"><b>{SEARCH_WORDS}</b></a><!-- ENDIF --><!-- IF IGNORED_WORDS --> {L_IGNORED_TERMS}: <b>{IGNORED_WORDS}</b><!-- ENDIF --></td> - <td align="right"><span class="genmed">{L_SEARCH_IN_RESULTS}: </span><input type="text" name="keywords" value="" /> <input class="btnlite" type="submit" name="submit" value="{L_GO}" /></td> + <td class="genmed"><!-- IF SEARCH_TOPIC -->{L_SEARCHED_TOPIC}: <a href="{U_SEARCH_TOPIC}"><b>{SEARCH_TOPIC}</b></a><br /><!-- ENDIF --><!-- IF SEARCH_WORDS -->{L_SEARCHED_FOR}: <a href="{U_SEARCH_WORDS}"><b>{SEARCH_WORDS}</b></a><!-- ENDIF --><!-- IF IGNORED_WORDS --> {L_IGNORED_TERMS}: <b>{IGNORED_WORDS}</b><!-- ENDIF --></td> + <td align="right"><!-- IF SEARCH_IN_RESULTS --><span class="genmed">{L_SEARCH_IN_RESULTS}: </span><input type="text" name="add_keywords" value="" /> <input class="btnlite" type="submit" name="submit" value="{L_GO}" /><!-- ENDIF --></td> </tr> </table> @@ -64,7 +64,7 @@ </tr> <!-- END searchresults --> <tr> - <td class="cat" colspan="7" valign="middle" align="center"><span class="gensmall">{L_DISPLAY_POSTS}:</span> {S_SELECT_SORT_DAYS} <span class="gensmall">{L_SORT_BY}:</span> {S_SELECT_SORT_KEY} {S_SELECT_SORT_DIR} <input class="btnlite" type="submit" value="{L_GO}" name="sort" /></td> + <td class="cat" colspan="7" valign="middle" align="center"><span class="gensmall">{L_DISPLAY_POSTS}:</span> {S_SELECT_SORT_DAYS}<!-- IF S_SELECT_SORT_KEY --> <span class="gensmall">{L_SORT_BY}:</span> {S_SELECT_SORT_KEY} {S_SELECT_SORT_DIR} <input class="btnlite" type="submit" value="{L_GO}" name="sort" /><!-- ENDIF --></td> </tr> </table> |