1 package Packages::DoSearch;
6 use Benchmark ':hireswallclock';
11 our @ISA = qw( Exporter );
12 our @EXPORT = qw( do_search );
15 use Packages::I18N::Locale;
16 use Packages::Search qw( :all );
19 use Packages::HTML qw(marker);
20 use Packages::Config qw( $DBDIR $SEARCH_URL $SEARCH_PAGE
21 @SUITES @ARCHIVES $ROOT );
25 my ($params, $opts, $html_header, $menu, $page_content) = @_;
27 $Params::Search::too_many_hits = 0;
29 if ($params->{errors}{keywords}) {
30 fatal_error( _g( "keyword not valid or missing" ) );
31 $opts->{keywords} = [];
32 } elsif (grep { length($_) < 2 } @{$opts->{keywords}}) {
33 fatal_error( _g( "keyword too short (keywords need to have at least two characters)" ) );
38 my @keywords = @{$opts->{keywords}};
39 my $searchon = $opts->{searchon};
41 # for URL construction
42 my $keyword_esc = uri_escape( "@keywords" );
43 $opts->{keywords_esc} = $keyword_esc;
46 my $keyword_enc = encode_entities "@keywords" || '';
47 my $searchon_enc = encode_entities $searchon;
48 my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
49 my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
50 my $archs_enc = encode_entities( join( ', ', @{$params->{values}{arch}{no_replace}} ) );
52 my $st0 = new Benchmark;
53 my (@results, @non_results);
55 unless (@Packages::CGI::fatal_errors) {
57 if ($searchon eq 'names') {
58 if ($opts->{source}) {
59 do_names_search( \@keywords, \%sources, $sp_obj,
60 \&read_src_entry_all, $opts,
61 \@results, \@non_results );
63 do_names_search( \@keywords, \%packages, $p_obj,
64 \&read_entry_all, $opts,
65 \@results, \@non_results );
68 do_names_search( \@keywords, \%packages, $p_obj,
69 \&read_entry_all, $opts,
70 \@results, \@non_results );
71 do_fulltext_search( \@keywords, "$DBDIR/descriptions.txt",
72 \%did2pkg, \%packages,
73 \&read_entry_all, $opts,
74 \@results, \@non_results );
79 # debug( join( "", Dumper( \@results, \@non_results )) ) if DEBUG;
80 my $st1 = new Benchmark;
81 my $std = timediff($st1, $st0);
82 debug( "Search took ".timestr($std) ) if DEBUG;
84 my $suite_wording = $suites_enc =~ /^(default|all)$/ ? _g("all suites")
85 : sprintf(_g("suite(s) <em>%s</em>", $suites_enc) );
86 my $section_wording = $sections_enc eq 'all' ? _g("all sections")
87 : sprintf(_g("section(s) <em>%s</em>", $sections_enc) );
88 my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
89 : sprintf(_g("architecture(s) <em>%s</em>", $archs_enc) );
90 if ($searchon eq "names") {
91 my $source_wording = $opts->{source} ? _g("source packages") : _g("packages");
92 # sorry to all translators for that one... (patches welcome)
93 msg( sprintf( _g( "You have searched for %s that names contain <em>%s</em> in %s, %s, and %s." ),
94 $source_wording, $keyword_enc,
95 $suite_wording, $section_wording, $arch_wording ) );
97 my $exact_wording = $opts->{exact} ? "" : _g(" (including subword matching)");
98 msg( sprintf( _g( "You have searched for <em>%s</em> in packages names and descriptions in %s, %s, and %s%s." ),
100 $suite_wording, $section_wording, $arch_wording,
104 if ($Packages::Search::too_many_hits) {
105 error( sprintf( _g( "Your search was too wide so we will only display exact matches. At least <em>%s</em> results have been omitted and will not be displayed. Please consider using a longer keyword or more keywords." ), $Packages::Search::too_many_hits ) );
108 if (!@Packages::CGI::fatal_errors && !@results) {
109 if ($searchon eq "names") {
110 unless (@non_results) {
111 error( _g( "Can't find that package." ) );
113 hint( _g( "Can't find that package." )." ".
114 sprintf( _g( '<a href="%s">%s</a>'.
115 " results have not been displayed due to the".
116 " search parameters." ), "$SEARCH_URL/$keyword_esc" ,
121 if (($suites_enc eq 'all')
122 && ($archs_enc eq 'any')
123 && ($sections_enc eq 'all')) {
124 error( _g( "Can't find that string." ) );
126 error( sprintf( _g( "Can't find that string, at least not in that suite (%s, section %s) and on that architecture (%s)." ),
127 $suites_enc, $sections_enc, $archs_enc ) );
130 if ($opts->{exact}) {
131 hint( sprintf( _g( 'You have searched only for words exactly matching your keywords. You can try to search <a href="%s">allowing subword matching</a>.' ),
132 encode_entities(make_search_url('',"keywords=$keyword_esc",{exact => 0})) ) );
135 hint( sprintf( _g( 'You can try a different search on the <a href="%s">Packages search page</a>.' ), "$SEARCH_PAGE#search_packages" ) );
139 %$html_header = ( title => _g( 'Package Search Results' ) ,
140 lang => $opts->{lang},
141 title_tag => _g( 'Debian Package Search Results' ),
143 print_search_field => 'packages',
144 search_field_values => {
145 keywords => $keyword_enc,
146 searchon => $opts->{searchon_form},
148 suite => $suites_enc,
149 section => $sections_enc,
150 exact => $opts->{exact},
151 debug => $opts->{debug},
157 my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
159 unless ($opts->{source}) {
161 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
162 $priority, $version, $desc) = @$_;
164 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
165 if ($arch ne 'virtual') {
166 $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
167 $subsect{$pkg}{$suite}{$version} = $subsection;
168 $sect{$pkg}{$suite}{$version} = $section
169 unless $section eq 'main';
170 $archives{$pkg}{$suite}{$version} ||= $archive;
172 $desc{$pkg}{$suite}{$version} = $desc;
174 $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
178 my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
179 my @pkgs = sort keys %uniq_pkgs;
180 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, \@keywords,
181 \&print_package, \%provided_by,
182 \%archives, \%sect, \%subsect,
185 } else { # unless $opts->{source}
187 my ($pkg, $archive, $suite, $section, $subsection, $priority,
190 my $real_archive = '';
191 if ($archive =~ /^(security|non-US)$/) {
192 $real_archive = $archive;
195 if (($real_archive eq $archive) &&
196 $pkgs{$pkg}{$suite}{$archive} &&
197 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
200 $pkgs{$pkg}{$suite}{$archive} = $version;
201 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
202 $sect{$pkg}{$suite}{$archive}{source} = $section
203 unless $section eq 'main';
204 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
207 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
210 my @pkgs = sort keys %pkgs;
211 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, \@keywords,
212 \&print_src_package, \%archives,
213 \%sect, \%subsect, \%binaries );
214 } # else unless $opts->{source}
219 my ($pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
221 #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts );
222 my $str = '<div id="psearchres">';
223 $str .= "<p>".sprintf( _g( "Found <em>%s</em> matching packages." ),
224 scalar @$pkgs_list )."</p>";
227 $keyword = $keywords->[0] if @$keywords == 1;
230 if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
232 $str .= '<h2>'._g( "Exact hits" ).'</h2>';
233 $str .= &$print_func( $keyword, $pkgs->{$keyword}||{},
234 map { $_->{$keyword}||{} } @func_args );
235 @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
238 if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
239 $str .= '<h2>'._g( 'Other hits' ).'</h2>'
242 foreach my $pkg (@$pkgs_list) {
244 #next if $count < $start or $count > $end;
245 $str .= &$print_func( $pkg, $pkgs->{$pkg}||{},
246 map { $_->{$pkg}||{} } @func_args );
248 } elsif (@$pkgs_list) {
249 $str .= "<p>".sprintf( _g( '<a href="%s">%s</a> results have not been displayed because you requested only exact matches.' ),
250 encode_entities(make_search_url('',"keywords=$opts->{keyword_esc}",{exact => 0})),
251 scalar @$pkgs_list )."</p>";
259 my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
261 my $str = '<h3>'.sprintf( _g( 'Package %s' ), $pkg ).'</h3>';
263 foreach my $suite (@SUITES) {
264 my $override = { suite => $suite };
265 if (exists $pkgs->{$suite}) {
267 my @versions = version_sort keys %{$pkgs->{$suite}};
269 if ($sect->{$suite}{$versions[0]}) {
270 $origin_str .= " ".marker($sect->{$suite}{$versions[0]});
272 $str .= sprintf( "<li><a href=\"%s\">%s</a> (%s): %s %s\n",
273 make_url($pkg,'',$override), $suite, $subsect->{$suite}{$versions[0]},
274 $desc->{$suite}{$versions[0]}, $origin_str );
276 foreach my $v (@versions) {
277 my $archive_str = "";
278 if ($archives->{$suite}{$v} ne 'us') {
279 $archive_str .= " ".marker($archives->{$suite}{$v});
282 my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}};
283 $str .= sprintf( "<br>%s$archive_str: %s\n",
284 $v, join (" ", @archs_to_print ))
286 $archs_printed{$_}++ foreach @archs_to_print;
288 if (my $p = $provided_by->{$suite}) {
289 $str .= '<br>'._g( 'also provided by: ' ).
290 join( ', ', map { "<a href=\"".
291 make_url($_,'',$override)."\">$_</a>" } @$p);
294 } elsif (my $p = $provided_by->{$suite}) {
295 $str .= sprintf( "<li><a href=\"%s\">%s</a>: "._g('Virtual package').'<br>',
296 make_url($pkg,'',$override), $suite );
297 $str .= _g( 'provided by: ' ).
298 join( ', ', map { "<a href=\"".
299 make_url($_,'',$override)."\">$_</a>" } @$p);
306 sub print_src_package {
307 my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
309 my $str = '<h3>'.sprintf( _g( 'Source package %s' ), $pkg ).'</h3>';
311 foreach my $suite (@SUITES) {
312 foreach my $archive (@ARCHIVES) {
313 if (exists $pkgs->{$suite}{$archive}) {
315 if ($sect->{$suite}{$archive}{source}) {
316 $origin_str .= " ".marker($sect->{$suite}{$archive}{source});
318 if ($archives->{$suite}{$archive}{source}) {
319 $origin_str .= " ".marker($archives->{$suite}{$archive}{source});
321 $str .= sprintf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s",
322 $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect->{$suite}{$archive}{source},
323 $pkgs->{$suite}{$archive}, $origin_str );
325 $str .= "<br>"._g( 'Binary packages: ' );
327 foreach my $bp (@{$binaries->{$suite}{$archive}}) {
328 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
329 $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ), $bp );
330 push @bp_links, $bp_link;
332 $str .= join( ", ", @bp_links );