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 @ARCHITECTURES $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" ) );
34 #FIXME: that's extremely hacky atm
35 if ($params->{values}{suite}{no_replace}[0] eq 'default') {
36 $params->{values}{suite}{no_replace} =
37 $params->{values}{suite}{final} = $opts->{suite} = [ 'stable' ];
40 if (@{$opts->{suite}} > 1) {
41 fatal_error( sprintf( _g( "more than one suite specified for contents search (%s)" ), "@{$opts->{suite}}" ) );
46 my $keyword = $opts->{keywords};
47 my $mode = $opts->{mode};
48 my $suite = $opts->{suite}[0];
49 my $archive = $opts->{archive}[0] ||'';
50 $Packages::Search::too_many_hits = 0;
52 # for URL construction
53 my $keyword_esc = uri_escape( $keyword );
54 my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
55 my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
56 my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
59 my $keyword_enc = encode_entities $keyword || '';
60 my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ), '&<>"' );
61 my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ), '&<>"' );
62 my $archs_enc = encode_entities( join( ', ', @{$params->{values}{arch}{no_replace}} ), '&<>"' );
64 my $st0 = new Benchmark;
67 unless (@Packages::CGI::fatal_errors) {
72 # full filename search is tricky
73 my $ffn = $mode eq 'filename';
75 my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
76 O_RDONLY, 0666, $DB_BTREE
77 or die "Failed opening reverse DB: $!";
80 open FILENAMES, '-|', 'fgrep', '--', $kw, "$DBDIR/contents/filenames_$suite.txt"
81 or die "Failed opening filename table: $!";
85 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
86 last if $Packages::Search::too_many_hits;
88 close FILENAMES or warn "fgrep error: $!\n";
93 # exact filename searching follows trivially:
94 $kw = "$kw/" if $mode eq 'exactfilename';
96 &searchfile(\@results, $kw, \$nres, $reverses);
102 my $st1 = new Benchmark;
103 my $std = timediff($st1, $st0);
104 debug( "Search took ".timestr($std) ) if DEBUG;
107 my $suite_wording = sprintf(_g("suite <em>%s</em>"), $suites_enc );
108 my $section_wording = $sections_enc eq 'all' ? _g("all sections")
109 : sprintf(_g("section(s) <em>%s</em>"), $sections_enc );
110 my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
111 : sprintf(_g("architecture(s) <em>%s</em>"), $archs_enc );
112 my $wording = _g("paths that end with");
113 if ($mode eq 'filename') {
114 $wording = _g("files named");
115 } elsif ($mode eq 'exactfilename') {
116 $wording = _g("filenames that contain");
118 msg( sprintf( _g("You have searched for %s <em>%s</em> in %s, %s, and %s." ),
119 $wording, $keyword_enc,
120 $suite_wording, $section_wording, $arch_wording ) );
122 if ($mode ne 'filename') {
123 msg( '<a href="'.make_search_url('',"keywords=$keyword_esc",{mode=>'filename'}).
124 "\">"._g("Search within filenames")."</a>");
126 if ($mode ne 'exactfilename') {
127 msg( '<a href="'.make_search_url('',"keywords=$keyword_esc",{mode=>'exactfilename'}).
128 "\">"._g("Search exact filename")."</a>");
130 if ($mode eq 'exactfilename' || $mode eq 'filename') {
131 msg( '<a href="'.make_search_url('',"keywords=$keyword_esc",{mode=>undef}).
132 "\">"._g("Search for paths ending with")."</a>");
135 msg( _g("Search in other suite:")." ".
136 join( ' ', map { '[<a href="'.make_search_url('',"keywords=$keyword_esc",{suite=>$_}).
137 "\">$_</a>]" } @SUITES ) );
139 if ($Packages::Search::too_many_hits) {
140 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." ) );
143 %$html_header = ( title => _g( 'Package Contents Search Results' ),
144 lang => $opts->{lang},
145 title_tag => _g( 'Debian Package Contents Search Results' ),
147 print_search_field => 'packages',
148 search_field_values => {
149 keywords => $keyword_enc,
150 searchon => 'contents',
152 suite => $suites_enc,
153 section => $sections_enc,
154 exact => $opts->{exact},
155 debug => $opts->{debug},
160 my (%results,%archs);
161 foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
162 my $file = shift @$result;
165 my ($pkg, $arch) = split /:/, $_;
166 next unless $opts->{h_archs}{$arch};
167 $pkgs{$pkg}{$arch}++;
168 $archs{$arch}++ unless $arch eq 'all';
170 next unless keys %pkgs;
171 $results{$file} = \%pkgs;
173 my @all_archs = keys %archs;
174 @all_archs = @ARCHITECTURES unless @all_archs;
175 debug( "all_archs = @all_archs", 1 ) if DEBUG;
176 msg(_g("Limit search to a specific architecture:")." ".
177 join( ' ', map { '[<a href="'.make_search_url('',"keywords=$keyword_esc",{arch=>$_}).
178 "\">$_</a>]" } @all_archs ) )
179 unless (@{$opts->{arch}} == 1) || (@all_archs == 1);
180 msg(sprintf(_g('Search in <a href="%s">all architectures</a>'),
181 make_search_url('',"keywords=$keyword_esc",{arch=>undef})))
182 if @{$opts->{arch}} == 1;
184 if (!@Packages::CGI::fatal_errors && !keys(%results)) {
185 error( _g( "Nothing found" ) );
189 $$page_content .= "<p>".sprintf( _g( 'Found %s results' ),
190 scalar keys %results )."</p>";
191 $$page_content .= '<div
192 id="pcontentsres"><table><colgroup><col><col></colgroup><tr><th>'._g('File').'</th><th>'._g('Packages')
194 foreach my $file (sort keys %results) {
195 my $file_enc = encode_entities($file);
196 $file_enc =~ s#(\Q$keyword_enc\E)#<span class=keyword>$1</span>#g;
197 $$page_content .= "<tr><td class=\"file\">/$file_enc</td><td>";
199 foreach my $pkg (sort keys %{$results{$file}}) {
201 my @archs = keys %{$results{$file}{$pkg}};
202 unless ($results{$file}{$pkg}{all} ||
203 (@archs == @all_archs)) {
204 if (@archs < @all_archs/2) {
205 $arch_str = ' ['.join(' ',sort @archs).']';
207 $arch_str = ' ['._g('not').' '.
208 join(' ', grep { !$results{$file}{$pkg}{$_} } @all_archs).']';
211 push @pkgs, "<a href=\"".make_url($pkg,'',{suite=>$suite})."\">$pkg</a>$arch_str";
213 $$page_content .= join( ", ", @pkgs);
214 $$page_content .= "</td></tr>\n";
216 $$page_content .= '<tr><th>'._g('File').'</th><th>'._g('Packages')."</th></tr>\n" if @results > 20;
217 $$page_content .= '</table></div>';
219 } # sub do_search_contents
223 my ($results, $kw, $nres, $reverses) = @_;
225 my ($key, $value) = ($kw, "");
226 debug( "searchfile: kw=$kw", 1 ) if DEBUG;
227 for (my $status = $reverses->seq($key, $value, R_CURSOR);
229 $status = $reverses->seq( $key, $value, R_NEXT)) {
231 # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
232 # whether $kw is or is not a prefix of $key
233 last unless index($key, $kw) == 0;
234 debug( "found $key", 2 ) if DEBUG;
236 my @hits = split /\0/o, $value;
237 push @$results, [ scalar reverse($key), @hits ];
238 last if ($$nres)++ > 100;
241 $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;