]> git.deb.at Git - deb/packages.git/blob - cgi-bin/search_contents.pl
Update copyright notice -- in this file, no code not my own
[deb/packages.git] / cgi-bin / search_contents.pl
1 #!/usr/bin/perl -wT
2 # $Id$
3 # search_contents.pl -- CGI interface to the Contents files on packages.debian.org
4 #
5 # Copyright (C) 2006 Jeroen van Wolffelaar
6 #
7 # use is allowed under the terms of the GNU Public License (GPL)                              
8 # see http://www.fsf.org/copyleft/gpl.html for a copy of the license
9
10 sub contents() {
11     my $nres = 0;
12
13     my ($cgi) = @_;
14
15     print "Extremely blunt ends-with search results:<br><pre>";
16 # only thing implemented yet: ends-with search
17     my $kw = lc $cgi->param("keywords");
18     # full filename search is tricky
19     my $ffn = $cgi->param("fullfilename");
20     $ffn = $ffn ? 1 : 0;
21
22
23 my $suite = 'stable'; #fixme
24
25     # fixme: I should open $reverses only once per search
26     my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
27         O_RDONLY, 0666, $DB_BTREE
28         or die "Failed opening reverse DB: $!";
29
30     if ($ffn) {
31         open FILENAMES, "$DBDIR/contents/filenames_$suite.txt"
32             or die "Failed opening filename table";
33         while (<FILENAMES>) {
34             next if index($_, $kw)<0;
35             chomp;
36             last unless &dosearch(reverse($_)."/", \$nres, $reverses);
37         }
38         close FILENAMES;
39     } else {
40
41         $kw = reverse $kw;
42         
43         # exact filename searching follows trivially:
44         my $exact = $cgi->param("exact");
45         $kw = "$kw/" if $exact;
46
47         print "ERROR: Exact and fullfilenamesearch don't go along" if $ffn and $exact;
48
49         &dosearch($kw, \$nres, $reverses);
50     }
51     print "</pre>$nres results displayed";
52     $reverses = undef;
53     untie %reverses;
54
55 }
56
57 sub dosearch
58 {
59     my ($kw, $nres, $reverses) = @_;
60
61     my ($key, $rest) = ($kw, "");
62     for (my $status = $reverses->seq($key, $value, R_CURSOR);
63         $status == 0;
64         $status =  $reverses->seq( $key, $value, R_NEXT)) {
65
66         # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
67         # whether $kw is or is not a prefix of $key
68         last unless index($key, $kw) == 0;
69
70         @hits = split /\0/o, $value;
71         print reverse($key)." is found in @hits\n";
72         last if ($$nres)++ > 100;
73     }
74
75     return $$nres<100;
76 }
77
78 1;
79 # vim: ts=8 sw=4