#!/usr/bin/perl -w use strict; use DB_File; use CGI; use HTML::Template; #use diagnostics; #use CGI::Carp qw(fatalsToBrowser); #$|++; use vars qw($query_Form $start_Form $keyCount $query); $query = new CGI; $query_Form ="query"; # form variable name 'query' $start_Form = "start"; # form variable name 'start' $keyCount = "Silly"; # only a key name for %result my $MAXHITS = 10; my $tmpl = "show_result.tmpl"; # template filename my (@found,$isfound,@match,%db,%result); my $script_name = $query->script_name(); my ($keywords,$start,$original_Query) = &process_form; dbmopen(%db,"search_index.db",0) or die "dbmopen: $!"; $ENV{HTML_TEMPLATE_ROOT} = '/where/template/folder/located/'; # set template file folder my $template = HTML::Template->new(filename=>$tmpl); $template->param('keywords' =>$original_Query); ################################################################################################### ## store keywords and count of each matching file foreach my $keyword (@$keywords) { if (defined (my $filelist = $db{lc $keyword})) { $isfound =1; # for every matched file, store matched keywords and matching time for # the keyword foreach my $fileno ($filelist=~ /(-\d+)/g) { # the file that matches the most keywords get biggest count $result{$fileno}{$keyCount}++; # remember keywords matched in current file $result{$fileno}{$keyword} = $keyword; } } } ## print the result and matched keywords if found if ($isfound) { $template->param('FOUND' => 1); # Store all matched files info in @found in order from the most matched to the least # Store corresponding matched keywords into @match foreach my $key (sort { hashValueDesc(\%result) } keys(%result) ) { push @found,$db{$key}; # Collect the matched keywords in current file and store whole as array push @match,[grep { $_ ne $keyCount } keys %{ $result{$key} }]; } my $low = $start; $low = 0 if ($low < 0 or $low > $#found); my $high = $low + $MAXHITS -1; $high = $#found if $high > $#found; my $total_found = @found; $template->param('found' => $total_found); $template->param('low' => $low+1,'high' => $high+1); my @loop; foreach ($low..$high) { my ($url,$title) = split /<>/,$found[$_]; my %search_result = ( 'title' => $title, 'match' => (join ' ',@{$match[$_]}), 'url' => $url, ); push (@loop,\%search_result); } $template->param('match_url' => \@loop); if ($low > 0) { $start = $low - $MAXHITS; $start =0 if ($start < 0); $template->param('previous_link' => "$script_name?$query_Form=$original_Query&$start_Form=$start"); } if ($high < $#found) { $template->param('next_link' => "$script_name?$query_Form=$original_Query&$start_Form=".($low+ $MAXHITS)); } } else { #$template->param('NOT_FOUND' => 1); } print $template->output; sub hashValueDesc { my $result=shift; $$result{$b}{$keyCount} <=> $$result{$a}{$keyCount}; } sub process_form { if ($query->param($query_Form)) { # set start value regarding if the start passed from form is set. my $s = $query->param($start_Form); my $start = $s ? $s : 0; my $original_Query = $query->param($query_Form); my $query_Trim = $original_Query; $query_Trim =~ s/\W+/ /g; my @keywords = split /\s+/,$query_Trim; return (\@keywords,$start,$original_Query); } else { print $query->redirect('http://www.easyya.com'); exit; # exit needed since we need to skip return value or error. } } 1;