home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / skeptic / bibliography / search.cgi < prev    next >
Text File  |  2009-08-26  |  2KB  |  123 lines

  1. #!/usr/bin/perl
  2.  
  3. # $rcs = ' $Id: search.cgi,v 1.4 1998/06/20 07:15:03 fitz Exp $ ' ;
  4.  
  5. unshift(@INC, "lib");
  6.  
  7. require 'config.pl';
  8. require 'bib.pl';
  9. require 'db.pl';
  10. require 'web.pl';
  11. require 'cgi-lib.pl';
  12.  
  13. print "Content-type: text/html\n\n";
  14. &ReadParse();
  15.  
  16. if ($ENV{PATH_INFO})
  17. {
  18.     unless ($in{q})
  19.     {
  20.         ($in{q} = $ENV{PATH_INFO}) =~ s!.*/!!;
  21.     }
  22. }
  23.  
  24. &error("No search query was specified.") unless %in;
  25.  
  26. # Initially consider all IDs as "hits"
  27. foreach (&db::get_ids())
  28. {
  29.     $hits{$_} = 1;
  30. }
  31.  
  32. if ($in{q})
  33. {
  34.     if ($in{exact})
  35.     {
  36.         $query = $in{q};
  37.         $in{q} =~ s/(\W)/\\\1/g;
  38.         $words[0] = $in{q};
  39.     }
  40.     else
  41.     {
  42.         $in{q} =~ s/\W+/ /g;
  43.         @words = split(/\s+/, $in{q});
  44.         $query = "$in{q}";
  45.     }
  46. }
  47.  
  48. if ($in{keyword})
  49. {
  50.     $query = "keyword=$in{keyword}";
  51.     $in{keyword} =~ s/(\W)/\\\1/g;
  52.  
  53.     $in{q} = 'bogus';
  54.     $words[0] = $in{q};
  55. }
  56.  
  57. foreach $word (@words)
  58. {
  59.  ID:
  60.     foreach $id (keys %hits)
  61.     {
  62.         %entry = &db::read_entry($id);
  63.  
  64.         $hits{$id} = $entry{TITLE};
  65.         $author{$id} = $entry{AUTHOR};
  66.  
  67.         if ($in{keyword})
  68.         {
  69.             delete($hits{$id})
  70.               unless $entry{KEYWORDS} =~ /\b$in{keyword}\b/i;
  71.             next ID;
  72.         }
  73.  
  74.         # Search the entry - it must contain the word or we remove
  75.         # this ID from the hit list
  76.         foreach $key (keys %entry)
  77.         {
  78.             next ID if $entry{$key} =~ /$word/i;
  79.         }
  80.  
  81.         # Remove the ID from the hits list
  82.         delete $hits{$id};
  83.     }
  84. }
  85.  
  86. foreach $id (sort id_by_title keys %hits)
  87. {
  88.     $series .= "$id,";
  89.     $bib::db{$id}->{TITLE} = $hits{$id};
  90.     $bib::db{$id}->{AUTHOR} = $author{$id};
  91.  
  92.     $list_items .= "<LI> " . &bib::link_to_id($id);
  93. }
  94.  
  95. &error("No hits for search query '<STRONG>", &untaint($query), "</STRONG>'")
  96.   unless $list_items;
  97.  
  98. # Create the HTML page
  99. $data = &bib::template("search.html",
  100.                        "HITS", $list_items,
  101.                        "QUERY", $query,
  102.                        "SERIES", $series,
  103.                        "HEADING", "Search for: " . &untaint($query));
  104.  
  105. print &bib::template("basic.html",
  106.                      "TITLE", "Search Results",
  107.                      "DATA", $data,);
  108.  
  109. exit 0;
  110.  
  111.  
  112. sub id_by_title
  113. # Given a database ID, sort by the corresponding title
  114. {
  115.     my($a2, $b2);
  116.  
  117.     # Remove "The" or "A" from the start of the title
  118.     ($a2 = $hits{$a}) =~ s/^(the|a)\b\s*//i;
  119.     ($b2 = $hits{$b}) =~ s/^(the|a)\b\s*//i;
  120.  
  121.     uc($a2) cmp uc($b2);
  122. }
  123.