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 $opts->{keywords} = [];
28 } elsif (grep { length($_) < 2 } @{$opts->{keywords}}) {
29 fatal_error( _g( "keyword too short (keywords need to have at least two characters)" ) );
31 if ($params->{errors}{suite}) {
32 fatal_error( _g( "suite not valid or not specified" ) );
35 #FIXME: that's extremely hacky atm
36 if ($params->{values}{suite}{no_replace}[0] eq 'default') {
37 $params->{values}{suite}{no_replace} =
38 $params->{values}{suite}{final} = $opts->{suite} = [ 'stable' ];
41 if (@{$opts->{suite}} > 1) {
42 fatal_error( sprintf( _g( "more than one suite specified for contents search (%s)" ), "@{$opts->{suite}}" ) );
47 my @keywords = @{$opts->{keywords}};
48 my $mode = $opts->{mode} || '';
49 my $suite = $opts->{suite}[0];
50 my $archive = $opts->{archive}[0] ||'';
51 $Packages::Search::too_many_hits = 0;
53 # for URL construction
54 my $keyword_esc = uri_escape( "@keywords" );
55 my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
56 my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
57 my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
60 my $keyword_enc = encode_entities "@keywords" || '';
61 my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ), '&<>"' );
62 my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ), '&<>"' );
63 my $archs_enc = encode_entities( join( ', ', @{$params->{values}{arch}{no_replace}} ), '&<>"' );
65 my $st0 = new Benchmark;
68 unless (@Packages::CGI::fatal_errors) {
72 my $first_kw = lc shift @keywords;
73 # full filename search is tricky
74 my $ffn = $mode eq 'filename';
76 my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
77 O_RDONLY, 0666, $DB_BTREE
78 or die "Failed opening reverse DB: $!";
81 open FILENAMES, '-|', 'fgrep', '--', $first_kw, "$DBDIR/contents/filenames_$suite.txt"
82 or die "Failed opening filename table: $!";
87 foreach my $kw (@keywords) {
88 next FILENAME unless /\Q$kw\E/;
90 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
91 last if $Packages::Search::too_many_hits;
93 close FILENAMES or warn "fgrep error: $!\n";
96 error(_g("The search mode you selected doesn't support more than one keyword."))
99 my $kw = reverse $first_kw;
101 # exact filename searching follows trivially:
102 $kw = "$kw/" if $mode eq 'exactfilename';
104 &searchfile(\@results, $kw, \$nres, $reverses);
110 my $st1 = new Benchmark;
111 my $std = timediff($st1, $st0);
112 debug( "Search took ".timestr($std) ) if DEBUG;
115 my $suite_wording = sprintf(_g("suite <em>%s</em>"), $suites_enc );
116 my $section_wording = $sections_enc eq 'all' ? _g("all sections")
117 : sprintf(_g("section(s) <em>%s</em>"), $sections_enc );
118 my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
119 : sprintf(_g("architecture(s) <em>%s</em>"), $archs_enc );
120 my $wording = _g("paths that end with");
121 if ($mode eq 'filename') {
122 $wording = _g("files named");
123 } elsif ($mode eq 'exactfilename') {
124 $wording = _g("filenames that contain");
126 msg( sprintf( _g("You have searched for %s <em>%s</em> in %s, %s, and %s." ),
127 $wording, $keyword_enc,
128 $suite_wording, $section_wording, $arch_wording ) );
130 if ($mode ne 'filename') {
131 msg( '<a href="'.make_search_url('',"keywords=$keyword_esc",{mode=>'filename'}).
132 "\">"._g("Search within filenames")."</a>");
134 if ($mode ne 'exactfilename') {
135 msg( '<a href="'.make_search_url('',"keywords=$keyword_esc",{mode=>'exactfilename'}).
136 "\">"._g("Search exact filename")."</a>");
138 if ($mode eq 'exactfilename' || $mode eq 'filename') {
139 msg( '<a href="'.make_search_url('',"keywords=$keyword_esc",{mode=>undef}).
140 "\">"._g("Search for paths ending with")."</a>");
143 msg( _g("Search in other suite:")." ".
144 join( ' ', map { '[<a href="'.make_search_url('',"keywords=$keyword_esc",{suite=>$_}).
145 "\">$_</a>]" } @SUITES ) );
147 if ($Packages::Search::too_many_hits) {
148 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." ) );
151 %$html_header = ( title => _g( 'Package Contents Search Results' ),
152 lang => $opts->{lang},
153 title_tag => _g( 'Debian Package Contents Search Results' ),
155 print_search_field => 'packages',
156 search_field_values => {
157 keywords => $keyword_enc,
158 searchon => 'contents',
160 suite => $suites_enc,
161 section => $sections_enc,
162 exact => $opts->{exact},
163 debug => $opts->{debug},
168 my (%results,%archs);
169 foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
170 my $file = shift @$result;
173 my ($pkg, $arch) = split /:/, $_;
174 next unless $opts->{h_archs}{$arch};
175 $pkgs{$pkg}{$arch}++;
176 $archs{$arch}++ unless $arch eq 'all';
178 next unless keys %pkgs;
179 $results{$file} = \%pkgs;
181 my @all_archs = keys %archs;
182 @all_archs = @ARCHITECTURES unless @all_archs;
183 debug( "all_archs = @all_archs", 1 ) if DEBUG;
184 msg(_g("Limit search to a specific architecture:")." ".
185 join( ' ', map { '[<a href="'.make_search_url('',"keywords=$keyword_esc",{arch=>$_}).
186 "\">$_</a>]" } @all_archs ) )
187 unless (@{$opts->{arch}} == 1) || (@all_archs == 1);
188 msg(sprintf(_g('Search in <a href="%s">all architectures</a>'),
189 make_search_url('',"keywords=$keyword_esc",{arch=>undef})))
190 if @{$opts->{arch}} == 1;
192 if (!@Packages::CGI::fatal_errors && !keys(%results)) {
193 error( _g( "Nothing found" ) );
197 $$page_content .= "<p>".sprintf( _g( 'Found %s results' ),
198 scalar keys %results )."</p>";
199 $$page_content .= '<div
200 id="pcontentsres"><table><colgroup><col><col></colgroup><tr><th>'._g('File').'</th><th>'._g('Packages')
202 foreach my $file (sort keys %results) {
203 my $file_enc = encode_entities($file);
204 foreach my $kw (@{$opts->{keywords}}) {
205 my $kw_enc = encode_entities($kw);
206 $file_enc =~ s#(\Q$kw_enc\E)#<span class="keyword">$1</span>#g;
208 $$page_content .= "<tr><td class=\"file\">/$file_enc</td><td>";
210 foreach my $pkg (sort keys %{$results{$file}}) {
212 my @archs = keys %{$results{$file}{$pkg}};
213 unless ($results{$file}{$pkg}{all} ||
214 (@archs == @all_archs)) {
215 if (@archs < @all_archs/2) {
216 $arch_str = ' ['.join(' ',sort @archs).']';
218 $arch_str = ' ['._g('not').' '.
219 join(' ', grep { !$results{$file}{$pkg}{$_} } @all_archs).']';
222 push @pkgs, "<a href=\"".make_url($pkg,'',{suite=>$suite})."\">$pkg</a>$arch_str";
224 $$page_content .= join( ", ", @pkgs);
225 $$page_content .= "</td></tr>\n";
227 $$page_content .= '<tr><th>'._g('File').'</th><th>'._g('Packages')."</th></tr>\n" if @results > 20;
228 $$page_content .= '</table></div>';
230 } # sub do_search_contents
234 my ($results, $kw, $nres, $reverses) = @_;
236 my ($key, $value) = ($kw, "");
237 debug( "searchfile: kw=$kw", 1 ) if DEBUG;
238 for (my $status = $reverses->seq($key, $value, R_CURSOR);
240 $status = $reverses->seq( $key, $value, R_NEXT)) {
242 # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
243 # whether $kw is or is not a prefix of $key
244 last unless index($key, $kw) == 0;
245 debug( "found $key", 2 ) if DEBUG;
247 my @hits = split /\0/o, $value;
248 push @$results, [ scalar reverse($key), @hits ];
249 last if ($$nres)++ > 100;
252 $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;