Search re-write was erm, how can I say this, erm, brown, soft and smelly ... putting old one with required updates back, will do more work soon

git-svn-id: file:///svn/phpbb/trunk@1412 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Paul S. Owen 2001-11-22 01:00:14 +00:00
parent 5e184479e6
commit eb82c68087

View file

@ -88,6 +88,74 @@ function split_words(&$entry)
return $split_entries[1]; return $split_entries[1];
} }
function arrayintersect($array1, $array2)
{
//
// make sure arguments are actually arrays
//
if( !is_array($array1) || !is_array($array2) )
{
return false;
}
if( sizeof($array2) > sizeof($array1) )
{
$temp = $array1;
$array1 = $array2;
$array2 = $temp;
unset($temp);
}
sort($array1);
rsort($array2);
// the greatest element in array2
$eg2 = $array2[0];
for($i = 0; $i < sizeof($array1); $i++)
{
$e1 = $array1[$i];
for($j = 0; $j < sizeof($array2); $j++)
{
$e2 = $array2[$j];
if( $e1 > $e2 )
{
//
// we have passed the match
//
if( $e2 == $eg2 )
{
break(2);
}
else
{
break;
}
}
else if( $e1 == $e2 )
{
//
// we found a match
//
$retArray[] = $e1;
break;
}
}
}
return $retArray;
}
function inarray($needle, $haystack)
{
for($i = 0; $i < count($haystack) && $haystack[$i] != $needle; $i++);
return ( $i != count($haystack) );
}
// //
// End of functions defns // End of functions defns
// ---------------------- // ----------------------
@ -284,128 +352,123 @@ else if( $query_keywords != "" || $query_author != "" || $search_id )
$split_search = array(); $split_search = array();
$split_search = split_words($cleaned_search); $split_search = split_words($cleaned_search);
$current_match_type = ""; $word_match = array();
$word_count = 0; $current_match_type = "and";
$word_store = array();
$post_id_match_list = array();
for($i = 0; $i < min(count($split_search), 10); $i++) for($i = 0; $i < count($split_search); $i++)
{ {
$word_match = str_replace("*", "%", trim($split_search[$i])); if( $split_search[$i] == "and" )
if( $word_match == "and" )
{ {
$current_match_type = "and"; $current_match_type = "and";
} }
else if( $word_match == "or" ) else if( $split_search[$i] == "or" )
{ {
$current_match_type = "or"; $current_match_type = "or";
} }
else if( $word_match == "not" ) else if( $split_search[$i] == "not" )
{ {
$current_match_type = "not"; $current_match_type = "not";
} }
else else
{ {
if( $current_match_type == "" ) if( !empty($search_all_terms) )
{ {
$current_match_type = "and"; $current_match_type = "and";
} }
if( $word_match != "" ) $word_match[$current_match_type][] = $split_search[$i];
}
}
@reset($word_match);
$word_count = 0;
$result_list = array();
while( list($match_type, $match_word_list) = each($word_match) )
{ {
$word_store[] = $word_match; for($i = 0; $i < count($match_word_list); $i++ )
{
$match_word = str_replace("*", "%", $match_word_list[$i]);
$sql = "SELECT m.post_id $sql = "SELECT m.post_id
FROM " . SEARCH_WORD_TABLE . " w, " . SEARCH_MATCH_TABLE . " m FROM " . SEARCH_WORD_TABLE . " w, " . SEARCH_MATCH_TABLE . " m
WHERE w.word_text LIKE '$word_match' WHERE w.word_text LIKE '$match_word'
AND m.word_id = w.word_id AND m.word_id = w.word_id
ORDER BY m.post_id DESC"; ORDER BY m.post_id";
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
if( !$result ) if( !$result )
{ {
message_die(GENERAL_ERROR, "Couldn't matched posts", "", __LINE__, __FILE__, $sql); message_die(GENERAL_ERROR, "Couldn't matched posts", "", __LINE__, __FILE__, $sql);
} }
$row = array();
while( $temp_row = $db->sql_fetchrow($result) )
{
$row['' . $temp_row['post_id'] . ''] = 1;
}
@reset($row);
while( list($post_id, $match_count) = each($row) )
{
if( !$word_count ) if( !$word_count )
{ {
while( $row = $db->sql_fetchrow($result) ) $result_list['' . $post_id . ''] = $match_count;
{
$post_id_match_list[] = $row['post_id'];
} }
else if( $match_type == "and" )
{
$result_list['' . $post_id . ''] = ( $result_list['' . $post_id . ''] ) ? $result_list['' . $post_id . ''] + intval($match_count) : 0;
}
else if( $match_type == "or" )
{
if( $result_list['' . $post_id . ''] )
{
$result_list['' . $post_id . ''] += intval($match_count);
} }
else else
{ {
if( $current_match_type == "or" ) $result_list['' . $post_id . ''] = 0;
{ $result_list['' . $post_id . ''] += intval($match_count);
while( $row = $db->sql_fetchrow($result) )
{
$post_id_match_list[] = $row['post_id'];
} }
} }
else if( $current_match_type == "and" ) else if( $match_type == "not" )
{ {
$rowset = $db->sql_fetchrowset($result); $result_list['' . $post_id . ''] = 0;
for($j = 0; $j < count($post_id_match_list); $j++)
{
$and_match = false;
for($k = 0; $k < count($rowset); $k++)
{
if( $post_id_match_list[$j] == $rowset[$k]['post_id'] )
{
$and_match = true;
} }
} }
if( !$and_match ) if( $match_type == "and" && $word_count )
{ {
$post_id_match_list[$j] = 0; @reset($row);
} @reset($result_list);
}
}
else if( $current_match_type == "not" )
{
$rowset = $db->sql_fetchrowset($result);
for($j = 0; $j < count($post_id_match_list); $j++) while( list($post_id, $match_count) = each($result_list) )
{ {
$not_match = false; if( !$row['' . $post_id . ''] )
for($k = 0; $k < count($rowset); $k++)
{ {
if( $post_id_match_list[$j] == $rowset[$k]['post_id'] ) $result_list['' . $post_id . ''] = 0;
{
$not_match = true;
}
}
if( $not_match )
{
$post_id_match_list[$j] = 0;
} }
} }
} }
}
$word_count++; $word_count++;
}
} }
$current_match_type = ""; @reset($result_list);
}
}
$total_posts = 0; $total_posts = 0;
$sql_post_id_in = ""; $sql_post_id_in = "";
for($i = 0; $i < count($post_id_match_list); $i++) while( list($post_id, $matches) = each($result_list) )
{ {
if( $post_id_match_list[$i] ) if( $matches )
{ {
if( $sql_post_id_in != "" ) if( $sql_post_id_in != "" )
{ {
$sql_post_id_in .= ", "; $sql_post_id_in .= ", ";
} }
$sql_post_id_in .= $post_id_match_list[$i]; $sql_post_id_in .= $post_id;
$total_posts++; $total_posts++;
} }