home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 21 / CTROM21B.mdf / win95 / zakelijk / shopease / _setup.1 / search.pl < prev    next >
Encoding:
Perl Script  |  1999-06-21  |  10.1 KB  |  433 lines

  1. #!{|PerlScript|}
  2.  
  3. # Location of your index file:
  4.  
  5. $Index_File = "{|CGIDirectory|}data/index.txt";
  6.  
  7. # Words that are generally ignored in a search query:
  8.  
  9. $Ignore .= " what how who which when where do you find site get and ";
  10. $Ignore .= " or if not a the for an it of from by the one two to he ";
  11. $Ignore .= " most all about i me search is are be been with why ";
  12.  
  13. # The URL to your main search page with tips and so on:
  14.  
  15. $Search_Page = "{|HTTPAddress|}search.htm";
  16.  
  17. # How many links should the viewer see per page?
  18.  
  19. $Hits_Per_Page = 10;
  20.  
  21. # This forms a hyperlink back to your main web page:
  22.  
  23. ($Link_URL,$Link_Title) = ("{|HTTPAddress|}main.htm","{|ShopName|} Main Page");
  24.  
  25. # The HTML for the top of the search results page.  Ours is pretty 
  26. # generic and should work if you aren't too creative.  Edit between 
  27. # the lines containing "EOM":
  28.  
  29. $Header = <<EOM;
  30. <HTML>
  31. <HEAD>
  32. <TITLE>Search Results</TITLE>
  33. </HEAD>
  34. <BODY BGCOLOR="#FFFFFF">
  35. <TITLE>{|ShopName|} Search Engine</TITLE>
  36. <META NAME="description" CONTENT="Try out our internal search engine 
  37.     for the fastest way to find what you're looking for!">
  38. </head>
  39. <body TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#006F08" VLINK="#00BB0E" ALINK="#006F08">
  40. <!-- START LAYOUT TABLE Defined in Definitions.txt file in common files folder --> 
  41. <!-- END of START LAYOUT TABLE --> </p>
  42. EOM
  43.  
  44. # The HTML to give searchers who can't seem to find anything. Usually 
  45. # this should include yet another link back to the tips page, and, if 
  46. # you're a business that is not overly annoyed by people sending you 
  47. # email, a mailto: link for them to request information from a human 
  48. # would be in order here:
  49.  
  50. $No_Documents_Found = <<EOM;
  51. <BLOCKQUOTE>
  52. <B>Unfortunately, we didn't find any products which matched your 
  53. search terms.</B>
  54. </BLOCKQUOTE>
  55. EOM
  56.  
  57. # The location of your summaries file.  It holds information about who 
  58. # searched for what, what they found, and so on.  This provides pretty 
  59. # good feedback on your visitors.  Make this file writable (chmod 777):
  60.  
  61. $summary_file = "{|CGIDirectory|}data/summaries.html";
  62.  
  63. # No further editing is necessary, but feel free to play around...
  64. # __________________________________________________________________
  65.  
  66.  
  67. if ($ENV{'QUERY_STRING'} =~ /^Range=([^\&]*)\&Format=([^\&]*)\&Terms=([^\&]*)\&Rank=(.*)/i)
  68.     {
  69.     ($Range,$Format,$terms,$rank) = ($1,$2,$3,$4);
  70.     $Encoded_Terms = $terms;
  71.     $terms =~ tr/+/ /;
  72.     $terms =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  73.     $Saved_Terms = $terms;
  74.     }
  75. elsif ($ENV{'QUERY_STRING'} =~ /^Range=([^\&]*)\&Format=([^\&]*)\&Terms=([^\&]*)/i)
  76.     {
  77.     ($Range,$Format,$terms,$rank) = ($1,$2,$3,1);
  78.     $Encoded_Terms = $terms;
  79.     $terms =~ tr/+/ /;
  80.     $terms =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  81.     $Saved_Terms = $terms;
  82.     }
  83. else
  84.     {
  85.     print "Location: $Search_Page\n\n";
  86.     exit;
  87.     }
  88. $terms = " " . $terms . " ";
  89. $terms =~ s/\s+/ /g;
  90. @Raw_Terms = split(/\"/,$terms);
  91. $terms = "";
  92. foreach $term (@Raw_Terms)
  93.     {
  94.     if ($i == 1)
  95.         {$i--;}
  96.     else
  97.         {$i++;}
  98.     $term =~ s/ /_/g unless $i;
  99.     $terms .= $term;
  100.     }
  101. $terms =~ s/ not / -/ig;
  102. $terms =~ s/ and / \+/ig;
  103. $terms =~ s/ or / \|/ig;
  104. $WildCard = "thewildcardisaveryspecialcharacter";
  105. $terms =~ s/(\*+)/$WildCard/g;
  106. @terms = split(/\s+/,$terms);
  107. foreach $term (@terms)
  108.     {
  109.     next if ($term eq "");
  110.     if (($Ignore =~ / $term /i) && ($term !~ /^\W/))
  111.         {
  112.         $Ignored_Terms .= $term . ", ";
  113.         next;
  114.         }
  115.     if ($term =~ /$WildCard/)
  116.         {
  117.         $Beta = $term;
  118.         $Beta =~ s/$WildCard//g;
  119.         $Beta =~ s/\W//g;
  120.         $Beta =~ s/_//g;
  121.         if (length($Beta) < 3)
  122.             {
  123.             $Ignored_Terms .= $term . ", ";
  124.             next;
  125.             }
  126.         }
  127.     $term =~ s/_/ /g;
  128.     if ($term =~ /^-/)
  129.         {
  130.         if ($term =~ / /)
  131.             {
  132.             $Ignored_Terms .= "\"" . $term . "\", ";
  133.             }
  134.         else
  135.             {
  136.             $Ignored_Terms .= $term . ", ";
  137.             }
  138.         push(@forbidden,&Format_Term($term));
  139.         }
  140.     elsif (($term =~ /^\|/) || (($Range eq "Any") && ($term !~ /^\+/)))
  141.         {
  142.         if ($term =~ / /)
  143.             {
  144.             $Important_Terms .= "\"" . $term . "\", ";
  145.             }
  146.         else
  147.             {
  148.             $Important_Terms .= $term . ", ";
  149.             }
  150.         push(@optional,&Format_Term($term));
  151.         }
  152.     else
  153.         {
  154.         if ($term =~ / /)
  155.             {
  156.             $Important_Terms .= "\"" . $term . "\", ";
  157.             }
  158.         else
  159.             {
  160.             $Important_Terms .= $term . ", ";
  161.             }
  162.         push(@required,&Format_Term($term));
  163.         }
  164.     }
  165. $delimiter = '%%==%%';
  166. open(ALLFILE,"$Index_File") || &Fatal;
  167. foreach $LINE (<ALLFILE>)
  168.     {
  169.     ($Crank,$URL,$Title,$Description,$Size,$Last_Modified,$string) = split(/$delimiter/,$LINE);
  170.     $string =~ s/\n/ /g;
  171.     ($Relevance,$Kill) = (0,0);
  172.     foreach (@forbidden)
  173.         {
  174.         if (($string =~ /$_/i) && ($_ !~ /[A-Z]/))
  175.             {
  176.             $Kill++;
  177.             last;
  178.             }
  179.         elsif ($string =~ /$_/)
  180.             {
  181.             $Kill++;
  182.             last;
  183.             }
  184.         }
  185.     next if ($Kill);
  186.     foreach (@required)
  187.         {
  188.         if (($string =~ /$_/) && ($_ =~ /[A-Z]/))
  189.             {
  190.             $Relevance += (($string =~ s/$_/$_/og) + 20);
  191.             }
  192.         elsif (($string =~ /$_/i) && ($_ !~ /[A-Z]/))
  193.             {
  194.             $Relevance += (($string =~ s/$_/$_/oig) + 10);
  195.             }
  196.         else
  197.             {
  198.             $Kill++;
  199.             last;
  200.             }
  201.         }
  202.     next if ($Kill);
  203.     foreach (@optional)
  204.         {
  205.         if (($string =~ /$_/) && ($_ =~ /[A-Z]/))
  206.             {
  207.             $Relevance += ($string =~ s/$_/$_/og);
  208.             }
  209.         elsif (($string =~ /$_/i) && ($_ !~ /[A-Z]/))
  210.             {
  211.             $Relevance += ($string =~ s/$_/$_/oig);
  212.             }
  213.         }
  214.     &Include if ($Relevance);
  215.     }
  216. close(ALLFILE);
  217. if ($Ignored_Terms =~ /(.*)\, /)
  218.     {
  219.     $Ignored_Terms = " " . $1;
  220.     $Ignored_Terms =~ s/$WildCard/\*/g;
  221.     $Ignored_Terms =~ s/ (\"?)\-/ $1/g;
  222.     $summary .= "Ignored: <TT>$Ignored_Terms</TT>.<BR>\n";
  223.     }
  224. else
  225.     {
  226.     $summary .= "<BR>\n";
  227.     }
  228. if ($Important_Terms =~ /(.*)\, /)
  229.     {
  230.     $Important_Terms = " " . $1;
  231.     $Important_Terms =~ s/$WildCard/\*/g;
  232.     $Important_Terms =~ s/ (\"?)\+/ $1/g;
  233.     $Important_Terms =~ s/ (\"?)\|/ $1/g;
  234.     $summary .= "Searched for: <TT>$Important_Terms</TT>.<BR>\n";
  235.     }
  236. $Remaining = ($hitcount - $rank) - ($Hits_Per_Page - 1);
  237. if ($Remaining > $Hits_Per_Page)
  238.     {
  239.     $Next = $Hits_Per_Page;
  240.     $New_Rank = $rank + $Hits_Per_Page;
  241.     }
  242. elsif ($Remaining > 0)
  243.     {
  244.     $Next = $Remaining % $Hits_Per_Page;
  245.     $New_Rank = $rank + $Hits_Per_Page;
  246.     }
  247. else
  248.     {
  249.     $New_Rank = $hitcount + 1;
  250.     }
  251. $summary .= "Displaying documents " . $rank . "-" . ($New_Rank - 1);
  252. $summary .= " of $hitcount, with best matches first.<BR>\n";
  253. if (($summary_file) && (-e $summary_file))
  254.     {
  255.     open(SUMMARY,">>$summary_file");
  256.     print SUMMARY "Search by $ENV{'REMOTE_HOST'}:<BR>\n";
  257.     print SUMMARY "Raw Query was: $Saved_Terms<BR>\n";
  258.     print SUMMARY "$summary\n<BR><HR><BR>\n";
  259.     close(SUMMARY);
  260.     }
  261. print "Content-type: text/html\n\n";
  262. print $Header;
  263. print $summary;
  264. if ($hitcount > 0)
  265.     {
  266.     if ($Format eq "Standard")
  267.         {
  268.         print "<BR>\n<BLOCKQUOTE>\n<DL>\n";
  269.         }
  270.     else
  271.         {
  272.         print "<BR>\n<PRE>\n";
  273.         }
  274.     $i = 0;
  275.     foreach $key (reverse sort keys %HITS)
  276.         {
  277.         $i++;
  278.         next if ($i < $rank);
  279.         next if ($i > ($rank + ($Hits_Per_Page - 1)));
  280.         ($URL,$Title,$Description,$Size,$Last_Modified) = split(/$delimiter/,$HITS{$key});
  281.         if ($Format eq "Standard")
  282.             {
  283.             if ($Size < 1500)
  284.                 {
  285.                 $Size = "$Size bytes";
  286.                 }
  287.             else
  288.                 {
  289.                 $Size = int($Size/1000) . " K";
  290.                 }
  291.             print "<P><DT><A HREF=\"$URL\"><STRONG>$Title</STRONG></A></DT>";
  292.             print "<DD>$Description<BR>\n<CITE><A HREF=\"$URL\">$URL</A>";
  293.             print "<FONT SIZE=-1> - $Size - $Last_Modified</FONT></CITE></DD>\n";
  294.             }
  295.         else
  296.             {
  297.             &Compact_Print;
  298.             }
  299.         }
  300.     if ($Format eq "Standard")
  301.         {
  302.         print "</DL>\n";
  303.         }
  304.     else
  305.         {
  306.         print "</PRE>\n";
  307.         }
  308.  
  309.     print "Documents " . $rank . "-" . ($New_Rank - 1);
  310.     print " of $hitcount displayed.<BR>\n";
  311.     print "</BLOCKQUOTE>\n" if ($Format eq "Standard");
  312.     print "<CENTER>\n";
  313.     if ($Remaining > 0)
  314.         {
  315.         print "<A HREF=\"$ENV{'SCRIPT_NAME'}?Range=$Range\&Format=$Format\&Terms=$Encoded_Terms\&Rank=$New_Rank\">";
  316.         print "<H4>Next $Next Matches</H4></A>\n";
  317.         }
  318.     }
  319. else
  320.     {
  321.     print $No_Documents_Found;
  322.     }
  323.  
  324. $Saved_Terms =~ s/\"/\"/g;
  325. print <<EOM;
  326. <BR>
  327. <CENTER>
  328. <FORM METHOD=GET ACTION="$ENV{'SCRIPT_NAME'}">
  329. <B>Match 
  330. <SELECT NAME="Range">
  331. EOM
  332.  
  333. if (($Range eq "All") && ($hitcount))
  334.     {
  335.     print "<OPTION VALUE=\"All\" SELECTED>All Terms";
  336.     print "<OPTION VALUE=\"Any\">Any Term";
  337.     }
  338. else
  339.     {
  340.     print "<OPTION VALUE=\"All\">All Terms";
  341.     print "<OPTION VALUE=\"Any\" SELECTED>Any Term";
  342.     }
  343.  
  344. print <<EOM;
  345. </SELECT>
  346. and show results 
  347. <SELECT NAME="Format">
  348. EOM
  349. if ($Format eq "Standard")
  350.     {
  351.     print "<OPTION VALUE=\"Standard\" SELECTED>in Standard Form";
  352.     print "<OPTION VALUE=\"Compact\">in Compact Form";
  353.     }
  354. else
  355.     {
  356.     print "<OPTION VALUE=\"Standard\">in Standard Form";
  357.     print "<OPTION VALUE=\"Compact\" SELECTED>in Compact Form";
  358.     }
  359. print <<EOM;
  360. </SELECT></B><BR>
  361. <INPUT NAME="Terms" SIZE=45 MAXLENGTH=800 VALUE="$Saved_Terms">
  362. <INPUT TYPE=submit VALUE=" New Search ">
  363. </FORM></CENTER>
  364. <BR><H5 ALIGN=CENTER>
  365. <A HREF="$Link_URL">$Link_Title</A><HR SIZE=1 NOSHADE WIDTH=50\%>
  366. </BODY></HTML>
  367. EOM
  368. sub Include
  369. {
  370. $Relevance = ($Relevance * $Crank) if ($Crank > 1);
  371. $Relevance = 100000 if ($Relevance > 100000);
  372. $Relevance = sprintf("%.5f",($Relevance/100000));
  373. $HITS{"$Relevance$URL"} = $URL . $delimiter . $Title . $delimiter . $Description . $delimiter. $Size . $delimiter . $Last_Modified;
  374. $hitcount++;
  375. }
  376.  
  377. sub Compact_Print
  378. {
  379. if (length($Title) < 25)
  380.     {
  381.     while (length($Title) < 25)
  382.         {
  383.         $Title .= " ";
  384.         }
  385.     }
  386. else
  387.     {
  388.     @Title_Characters = split(//,$Title);
  389.     $Title = "";
  390.     for ($x=0; $x<25; $x++)
  391.         {
  392.         $Title .= $Title_Characters[$x];
  393.         }
  394.     }
  395. if (length($Description) < 36)
  396.     {
  397.     while (length($Description) < 36)
  398.         {
  399.         $Description .= " ";
  400.         }
  401.     }
  402. else
  403.     {
  404.     @Descript_Characters = split(//,$Description);
  405.     $Description = "";
  406.     for ($x=0; $x<36; $x++)
  407.         {
  408.         $Description .= $Descript_Characters[$x];
  409.         }
  410.     }
  411. if (length($Last_Modified) == 8)
  412.     {
  413.     $Last_Modified = "0" . $Last_Modified;
  414.     }
  415. print "<A HREF=\"$URL\">$Title</A> [$Last_Modified] $Description\n";
  416. }
  417. sub Format_Term
  418. {
  419. $_ = shift;
  420. $_ = " " . $_ . " ";
  421. $_ =~ s/\W/ /g;
  422. $_ =~ s/\s+/ /g;
  423. $_ =~ s/$WildCard/\(\[\^\\s\+\]\{0,4\}\)/g;
  424. return $_;
  425. }
  426. sub Fatal
  427. {
  428. print "Content-type: text/plain\n\n";
  429. print "Fatal Error: Could not read from the index file.\n";
  430. print "Check location at $Index_File.\n\n";
  431. exit;
  432. }