]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoSearchContents.pm
Make result display of DoSearchContents actually usable
[deb/packages.git] / lib / Packages / DoSearchContents.pm
1 package Packages::DoSearchContents;
2
3 use strict;
4 use warnings;
5
6 use Benchmark;
7 use DB_File;
8 use URI::Escape;
9 use HTML::Entities;
10 use Exporter;
11 our @ISA = qw( Exporter );
12 our @EXPORT = qw( do_search_contents );
13
14 use Deb::Versions;
15 use Packages::Search qw( :all );
16 use Packages::CGI;
17 use Packages::DB;
18 use Packages::Config qw( $DBDIR $SEARCH_URL $SEARCH_CGI $SEARCH_PAGE
19                          @SUITES @ARCHIVES $ROOT );
20
21 sub do_search_contents {
22     my ($params, $opts, $html_header, $menu, $page_content) = @_;
23
24     if ($params->{errors}{keywords}) {
25         fatal_error( "keyword not valid or missing" );
26     } elsif (length($opts->{keywords}) < 2) {
27         fatal_error( "keyword too short (keywords need to have at least two characters)" );
28     }
29     if ($params->{errors}{suite}) {
30         fatal_error( "suite not valid or not specified" );
31     }
32     if (@{$opts->{suite}} > 1) {
33         fatal_error( "more than one suite specified for contents search (@{$opts->{suite}})" );
34     }
35
36     $$menu = "";
37     
38     my $keyword = $opts->{keywords};
39     my $searchon = $opts->{searchon};
40     my $exact = $opts->{exact};
41     my $suite = $opts->{suite}[0];
42     my $archive = $opts->{archive}[0] ||'';
43     $Packages::Search::too_many_hits = 0;
44
45     # for URL construction
46     my $keyword_esc = uri_escape( $keyword );
47     my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
48     my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
49     my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
50
51     # for output
52     my $keyword_enc = encode_entities $keyword || '';
53     my $searchon_enc = encode_entities $searchon;
54     my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
55     my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
56     my $archs_enc = encode_entities( join( ', ',  @{$params->{values}{arch}{no_replace}} ) );
57     
58     my $st0 = new Benchmark;
59     my (@results);
60
61     unless (@Packages::CGI::fatal_errors) {
62
63         my $nres = 0;
64
65         my $kw = lc $keyword;
66         # full filename search is tricky
67         my $ffn = $searchon eq 'filenames';
68
69         my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
70             O_RDONLY, 0666, $DB_BTREE
71             or die "Failed opening reverse DB: $!";
72
73         if ($ffn) {
74             open FILENAMES, '-|', 'fgrep', '--', $kw, "$DBDIR/contents/filenames_$suite.txt"
75                 or die "Failed opening filename table: $!";
76
77             error( "Exact and fullfilenamesearch don't go along" )
78                 if $ffn and $exact;
79
80             while (<FILENAMES>) {
81                 chomp;
82                 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
83                 last if $Packages::Search::too_many_hits;
84             }
85             close FILENAMES or warn "fgrep error: $!\n";
86         } else {
87
88             $kw = reverse $kw;
89             
90             # exact filename searching follows trivially:
91             $kw = "$kw/" if $exact;
92
93             &searchfile(\@results, $kw, \$nres, $reverses);
94         }
95         $reverses = undef;
96         untie %reverses;
97
98     
99         my $st1 = new Benchmark;
100         my $std = timediff($st1, $st0);
101         debug( "Search took ".timestr($std) );
102     }
103     
104     my $suite_wording = $suites_enc eq "all" ? "all suites"
105         : "suite(s) <em>$suites_enc</em>";
106     my $section_wording = $sections_enc eq 'all' ? "all sections"
107         : "section(s) <em>$sections_enc</em>";
108     my $arch_wording = $archs_enc eq 'any' ? "all architectures"
109         : "architecture(s) <em>$archs_enc</em>";
110     my $wording = $opts->{exact} ? "exact filenames" : "filenames that contain";
111     $wording = "paths that end with" if $searchon eq "contents";
112     msg( "You have searched for ${wording} <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
113
114     if ($Packages::Search::too_many_hits) {
115         error( "Your search was too wide so we will only display only the first about 100 matches. Please consider using a longer keyword or more keywords." );
116     }
117     
118     if (!@Packages::CGI::fatal_errors && !@results) {
119         error( "Nothing found" );
120     }
121
122     %$html_header = ( title => 'Package Contents Search Results' ,
123                       lang => 'en',
124                       title_tag => 'Debian Package Contents Search Results',
125                       print_title => 1,
126                       print_search_field => 'packages',
127                       search_field_values => { 
128                           keywords => $keyword_enc,
129                           searchon => 'contents',
130                           arch => $archs_enc,
131                           suite => $suites_enc,
132                           section => $sections_enc,
133                           exact => $opts->{exact},
134                           debug => $opts->{debug},
135                       },
136                       );
137
138     $$page_content = '';
139     if (@results) {
140         $$page_content .= "<p>Found ".scalar(@results)." results</p>";
141         $$page_content .= "<div  id=\"pcontentsres\"><table><colgroup><col><col></colgroup><tr><th style=\"text-align:center\">File</th><th style=\"text-align:center\">Packages</th></tr>";
142         foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
143             my $file = shift @$result;
144             $$page_content .= "<tr><td class=\"file\">$file</td><td>";
145             my %pkgs;
146             foreach (@$result) {
147                 my ($pkg, $arch) = split /:/, $_;
148                 $pkgs{$pkg}{$arch}++;
149             }
150             $$page_content .= join( ", ", map { "<a href=\"$ROOT/$suite/$_\">$_</a>" } sort keys %pkgs);
151             $$page_content .= '</td>';
152         }
153         $$page_content .= '<tr><th style="text-align:center">File</th><th style="text-align:center">Packages</th></tr>' if @results > 20;
154         $$page_content .= '</table></div>';
155     }
156 } # sub do_search_contents
157
158 sub searchfile
159 {
160     my ($results, $kw, $nres, $reverses) = @_;
161
162     my ($key, $value) = ($kw, "");
163     debug( "searchfile: kw=$kw", 1 );
164     for (my $status = $reverses->seq($key, $value, R_CURSOR);
165         $status == 0;
166         $status =  $reverses->seq( $key, $value, R_NEXT)) {
167
168         # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
169         # whether $kw is or is not a prefix of $key
170         last unless index($key, $kw) == 0;
171         debug( "found $key", 2 );
172
173         my @hits = split /\0/o, $value;
174         push @$results, [ scalar reverse($key), @hits ];
175         last if ($$nres)++ > 100;
176     }
177
178     $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;
179 }
180
181
182 1;