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" ? _g("all suites")
106 : sprintf(_g("suite(s) <em>%s</em>", $suites_enc) );
107 my $section_wording = $sections_enc eq 'all' ? _g("all sections")
108 : sprintf(_g("section(s) <em>%s</em>", $sections_enc) );
109 my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
110 : sprintf(_g("architecture(s) <em>%s</em>", $archs_enc) );
111 my $wording = $opts->{exact} ? _g("exact filenames") : _g("filenames that contain");
112 $wording = _g("paths that end with") if $searchon eq "contents";
113 msg( sprintf( _g("You have searched for %s <em>%s</em> in %s, %s, and %s." ),
114 $wording, $keyword_enc,
115 $suite_wording, $section_wording, $arch_wording ) );
117 if ($Packages::Search::too_many_hits) {
118 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." ) );
121 if (!@Packages::CGI::fatal_errors && !@results) {
122 error( _g( "Nothing found" ) );
125 %$html_header = ( title => _g( 'Package Contents Search Results' ),
126 lang => $opts->{lang},
127 title_tag => _g( 'Debian Package Contents Search Results' ),
129 print_search_field => 'packages',
130 search_field_values => {
131 keywords => $keyword_enc,
132 searchon => 'contents',
134 suite => $suites_enc,
135 section => $sections_enc,
136 exact => $opts->{exact},
137 debug => $opts->{debug},
143 $$page_content .= "<p>".sprintf( _g( 'Found %s results' ),
144 scalar @results )."</p>";
145 $$page_content .= '<div
146 id="pcontentsres"><table><colgroup><col><col></colgroup><tr><th>'._g('File').'</th><th>'._g('Packages')
148 foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
149 my $file = shift @$result;
150 $$page_content .= "<tr><td class=\"file\">/$file</td><td>";
153 my ($pkg, $arch) = split /:/, $_;
154 $pkgs{$pkg}{$arch}++;
156 $$page_content .= join( ", ", map { "<a href=\"$ROOT/$suite/$_\">$_</a>" } sort keys %pkgs);
157 $$page_content .= '</td>';
159 $$page_content .= '<tr><th>'._g('File').'</th><th>'._g('Packages').'</th></tr>' if @results > 20;
160 $$page_content .= '</table></div>';
162 } # sub do_search_contents
166 my ($results, $kw, $nres, $reverses) = @_;
168 my ($key, $value) = ($kw, "");
169 debug( "searchfile: kw=$kw", 1 ) if DEBUG;
170 for (my $status = $reverses->seq($key, $value, R_CURSOR);
172 $status = $reverses->seq( $key, $value, R_NEXT)) {
174 # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
175 # whether $kw is or is not a prefix of $key
176 last unless index($key, $kw) == 0;
177 debug( "found $key", 2 ) if DEBUG;
179 my @hits = split /\0/o, $value;
180 push @$results, [ scalar reverse($key), @hits ];
181 last if ($$nres)++ > 100;
184 $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;