1 package Packages::DoSearchContents;
6 use Benchmark ':hireswallclock';
11 our @ISA = qw( Exporter );
12 our @EXPORT = qw( do_search_contents );
15 use Packages::I18N::Locale;
16 use Packages::Search qw( :all );
19 use Packages::Config qw( $DBDIR $SEARCH_URL $SEARCH_PAGE
20 @SUITES @ARCHIVES $ROOT );
22 sub do_search_contents {
23 my ($params, $opts, $html_header, $menu, $page_content) = @_;
25 if ($params->{errors}{keywords}) {
26 fatal_error( _g( "keyword not valid or missing" ) );
27 } elsif (length($opts->{keywords}) < 2) {
28 fatal_error( _g( "keyword too short (keywords need to have at least two characters)" ) );
30 if ($params->{errors}{suite}) {
31 fatal_error( _g( "suite not valid or not specified" ) );
33 if (@{$opts->{suite}} > 1) {
34 fatal_error( sprintf( _g( "more than one suite specified for contents search (%s)" ), "@{$opts->{suite}}" ) );
39 my $keyword = $opts->{keywords};
40 my $searchon = $opts->{searchon};
41 my $exact = $opts->{exact};
42 my $suite = $opts->{suite}[0];
43 my $archive = $opts->{archive}[0] ||'';
44 $Packages::Search::too_many_hits = 0;
46 # for URL construction
47 my $keyword_esc = uri_escape( $keyword );
48 my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
49 my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
50 my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
53 my $keyword_enc = encode_entities $keyword || '';
54 my $searchon_enc = encode_entities $searchon;
55 my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
56 my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
57 my $archs_enc = encode_entities( join( ', ', @{$params->{values}{arch}{no_replace}} ) );
59 my $st0 = new Benchmark;
62 unless (@Packages::CGI::fatal_errors) {
67 # full filename search is tricky
68 my $ffn = $searchon eq 'filenames';
70 my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
71 O_RDONLY, 0666, $DB_BTREE
72 or die "Failed opening reverse DB: $!";
75 open FILENAMES, '-|', 'fgrep', '--', $kw, "$DBDIR/contents/filenames_$suite.txt"
76 or die "Failed opening filename table: $!";
78 error( _g( "Exact and fullfilenamesearch don't go along" ) )
83 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
84 last if $Packages::Search::too_many_hits;
86 close FILENAMES or warn "fgrep error: $!\n";
91 # exact filename searching follows trivially:
92 $kw = "$kw/" if $exact;
94 &searchfile(\@results, $kw, \$nres, $reverses);
100 my $st1 = new Benchmark;
101 my $std = timediff($st1, $st0);
102 debug( "Search took ".timestr($std) ) if DEBUG;
105 my $suite_wording = $suites_enc eq "all" ? "all suites"
106 : "suite(s) <em>$suites_enc</em>";
107 my $section_wording = $sections_enc eq 'all' ? "all sections"
108 : "section(s) <em>$sections_enc</em>";
109 my $arch_wording = $archs_enc eq 'any' ? "all architectures"
110 : "architecture(s) <em>$archs_enc</em>";
111 my $wording = $opts->{exact} ? "exact filenames" : "filenames that contain";
112 $wording = "paths that end with" if $searchon eq "contents";
113 msg( "You have searched for ${wording} <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
115 if ($Packages::Search::too_many_hits) {
116 error( _g( "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." ) );
119 if (!@Packages::CGI::fatal_errors && !@results) {
120 error( _g( "Nothing found" ) );
123 %$html_header = ( title => _g( 'Package Contents Search Results' ),
124 lang => $opts->{lang},
125 title_tag => _g( 'Debian Package Contents Search Results' ),
127 print_search_field => 'packages',
128 search_field_values => {
129 keywords => $keyword_enc,
130 searchon => 'contents',
132 suite => $suites_enc,
133 section => $sections_enc,
134 exact => $opts->{exact},
135 debug => $opts->{debug},
141 $$page_content .= "<p>".sprintf( _g( 'Found %s results' ),
142 scalar @results )."</p>";
143 $$page_content .= '<div
144 id="pcontentsres"><table><colgroup><col><col></colgroup><tr><th>'._g('File').'</th><th>'._g('Packages')
146 foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
147 my $file = shift @$result;
148 $$page_content .= "<tr><td class=\"file\">/$file</td><td>";
151 my ($pkg, $arch) = split /:/, $_;
152 $pkgs{$pkg}{$arch}++;
154 $$page_content .= join( ", ", map { "<a href=\"$ROOT/$suite/$_\">$_</a>" } sort keys %pkgs);
155 $$page_content .= '</td>';
157 $$page_content .= '<tr><th>'._g('File').'</th><th>'._g('Packages').'</th></tr>' if @results > 20;
158 $$page_content .= '</table></div>';
160 } # sub do_search_contents
164 my ($results, $kw, $nres, $reverses) = @_;
166 my ($key, $value) = ($kw, "");
167 debug( "searchfile: kw=$kw", 1 ) if DEBUG;
168 for (my $status = $reverses->seq($key, $value, R_CURSOR);
170 $status = $reverses->seq( $key, $value, R_NEXT)) {
172 # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
173 # whether $kw is or is not a prefix of $key
174 last unless index($key, $kw) == 0;
175 debug( "found $key", 2 ) if DEBUG;
177 my @hits = split /\0/o, $value;
178 push @$results, [ scalar reverse($key), @hits ];
179 last if ($$nres)++ > 100;
182 $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;