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 } elsif (length($opts->{keywords}) < 2) {
32 fatal_error( _g( "keyword too short (keywords need to have at least two characters)" ) );
37 my @keywords = @{$opts->{keywords}};
38 my $searchon = $opts->{searchon};
40 # for URL construction
41 my $keyword_esc = uri_escape( "@keywords" );
42 $opts->{keywords_esc} = $keyword_esc;
45 my $keyword_enc = encode_entities "@keywords" || '';
46 my $searchon_enc = encode_entities $searchon;
47 my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
48 my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
49 my $archs_enc = encode_entities( join( ', ', @{$params->{values}{arch}{no_replace}} ) );
51 my $st0 = new Benchmark;
52 my (@results, @non_results);
54 unless (@Packages::CGI::fatal_errors) {
56 if ($searchon eq 'names') {
57 if ($opts->{source}) {
58 do_names_search( \@keywords, \%sources, $sp_obj,
59 \&read_src_entry_all, $opts,
60 \@results, \@non_results );
62 do_names_search( \@keywords, \%packages, $p_obj,
63 \&read_entry_all, $opts,
64 \@results, \@non_results );
67 do_names_search( \@keywords, \%packages, $p_obj,
68 \&read_entry_all, $opts,
69 \@results, \@non_results );
70 do_fulltext_search( \@keywords, "$DBDIR/descriptions.txt",
71 \%did2pkg, \%packages,
72 \&read_entry_all, $opts,
73 \@results, \@non_results );
78 # debug( join( "", Dumper( \@results, \@non_results )) ) if DEBUG;
79 my $st1 = new Benchmark;
80 my $std = timediff($st1, $st0);
81 debug( "Search took ".timestr($std) ) if DEBUG;
83 my $suite_wording = $suites_enc =~ /^(default|all)$/ ? _g("all suites")
84 : sprintf(_g("suite(s) <em>%s</em>", $suites_enc) );
85 my $section_wording = $sections_enc eq 'all' ? _g("all sections")
86 : sprintf(_g("section(s) <em>%s</em>", $sections_enc) );
87 my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
88 : sprintf(_g("architecture(s) <em>%s</em>", $archs_enc) );
89 if ($searchon eq "names") {
90 my $source_wording = $opts->{source} ? _g("source packages") : _g("packages");
91 # sorry to all translators for that one... (patches welcome)
92 msg( sprintf( _g( "You have searched for %s that names contain <em>%s</em> in %s, %s, and %s." ),
93 $source_wording, $keyword_enc,
94 $suite_wording, $section_wording, $arch_wording ) );
96 my $exact_wording = $opts->{exact} ? "" : _g(" (including subword matching)");
97 msg( sprintf( _g( "You have searched for <em>%s</em> in packages names and descriptions in %s, %s, and %s%s." ),
99 $suite_wording, $section_wording, $arch_wording,
103 if ($Packages::Search::too_many_hits) {
104 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 ) );
107 if (!@Packages::CGI::fatal_errors && !@results) {
108 if ($searchon eq "names") {
109 unless (@non_results) {
110 error( _g( "Can't find that package." ) );
112 hint( _g( "Can't find that package." )." ".
113 sprintf( _g( '<a href="%s">%s</a>'.
114 " results have not been displayed due to the".
115 " search parameters." ), "$SEARCH_URL/$keyword_esc" ,
120 if (($suites_enc eq 'all')
121 && ($archs_enc eq 'any')
122 && ($sections_enc eq 'all')) {
123 error( _g( "Can't find that string." ) );
125 error( sprintf( _g( "Can't find that string, at least not in that suite (%s, section %s) and on that architecture (%s)." ),
126 $suites_enc, $sections_enc, $archs_enc ) );
129 if ($opts->{exact}) {
130 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>.' ),
131 encode_entities(make_search_url('',"keywords=$keyword_esc",{exact => 0})) ) );
134 hint( sprintf( _g( 'You can try a different search on the <a href="%s">Packages search page</a>.' ), "$SEARCH_PAGE#search_packages" ) );
138 %$html_header = ( title => _g( 'Package Search Results' ) ,
139 lang => $opts->{lang},
140 title_tag => _g( 'Debian Package Search Results' ),
142 print_search_field => 'packages',
143 search_field_values => {
144 keywords => $keyword_enc,
145 searchon => $opts->{searchon_form},
147 suite => $suites_enc,
148 section => $sections_enc,
149 exact => $opts->{exact},
150 debug => $opts->{debug},
156 my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
158 unless ($opts->{source}) {
160 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
161 $priority, $version, $desc) = @$_;
163 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
164 if ($arch ne 'virtual') {
165 $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
166 $subsect{$pkg}{$suite}{$version} = $subsection;
167 $sect{$pkg}{$suite}{$version} = $section
168 unless $section eq 'main';
169 $archives{$pkg}{$suite}{$version} ||= $archive;
171 $desc{$pkg}{$suite}{$version} = $desc;
173 $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
177 my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
178 my @pkgs = sort keys %uniq_pkgs;
179 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, \@keywords,
180 \&print_package, \%provided_by,
181 \%archives, \%sect, \%subsect,
184 } else { # unless $opts->{source}
186 my ($pkg, $archive, $suite, $section, $subsection, $priority,
189 my $real_archive = '';
190 if ($archive =~ /^(security|non-US)$/) {
191 $real_archive = $archive;
194 if (($real_archive eq $archive) &&
195 $pkgs{$pkg}{$suite}{$archive} &&
196 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
199 $pkgs{$pkg}{$suite}{$archive} = $version;
200 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
201 $sect{$pkg}{$suite}{$archive}{source} = $section
202 unless $section eq 'main';
203 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
206 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
209 my @pkgs = sort keys %pkgs;
210 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, \@keywords,
211 \&print_src_package, \%archives,
212 \%sect, \%subsect, \%binaries );
213 } # else unless $opts->{source}
218 my ($pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
220 #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts );
221 my $str = '<div id="psearchres">';
222 $str .= "<p>".sprintf( _g( "Found <em>%s</em> matching packages." ),
223 scalar @$pkgs_list )."</p>";
226 $keyword = $keywords->[0] if @$keywords == 1;
229 if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
231 $str .= '<h2>'._g( "Exact hits" ).'</h2>';
232 $str .= &$print_func( $keyword, $pkgs->{$keyword}||{},
233 map { $_->{$keyword}||{} } @func_args );
234 @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
237 if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
238 $str .= '<h2>'._g( 'Other hits' ).'</h2>'
241 foreach my $pkg (@$pkgs_list) {
243 #next if $count < $start or $count > $end;
244 $str .= &$print_func( $pkg, $pkgs->{$pkg}||{},
245 map { $_->{$pkg}||{} } @func_args );
247 } elsif (@$pkgs_list) {
248 $str .= "<p>".sprintf( _g( '<a href="%s">%s</a> results have not been displayed because you requested only exact matches.' ),
249 encode_entities(make_search_url('',"keywords=$opts->{keyword_esc}",{exact => 0})),
250 scalar @$pkgs_list )."</p>";
258 my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
260 my $str = '<h3>'.sprintf( _g( 'Package %s' ), $pkg ).'</h3>';
262 foreach my $suite (@SUITES) {
263 my $override = { suite => $suite };
264 if (exists $pkgs->{$suite}) {
266 my @versions = version_sort keys %{$pkgs->{$suite}};
268 if ($sect->{$suite}{$versions[0]}) {
269 $origin_str .= " ".marker($sect->{$suite}{$versions[0]});
271 $str .= sprintf( "<li><a href=\"%s\">%s</a> (%s): %s %s\n",
272 make_url($pkg,'',$override), $suite, $subsect->{$suite}{$versions[0]},
273 $desc->{$suite}{$versions[0]}, $origin_str );
275 foreach my $v (@versions) {
276 my $archive_str = "";
277 if ($archives->{$suite}{$v} ne 'us') {
278 $archive_str .= " ".marker($archives->{$suite}{$v});
281 my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}};
282 $str .= sprintf( "<br>%s$archive_str: %s\n",
283 $v, join (" ", @archs_to_print ))
285 $archs_printed{$_}++ foreach @archs_to_print;
287 if (my $p = $provided_by->{$suite}) {
288 $str .= '<br>'._g( 'also provided by: ' ).
289 join( ', ', map { "<a href=\"".
290 make_url($_,'',$override)."\">$_</a>" } @$p);
293 } elsif (my $p = $provided_by->{$suite}) {
294 $str .= sprintf( "<li><a href=\"%s\">%s</a>: "._g('Virtual package').'<br>',
295 make_url($pkg,'',$override), $suite );
296 $str .= _g( 'provided by: ' ).
297 join( ', ', map { "<a href=\"".
298 make_url($_,'',$override)."\">$_</a>" } @$p);
305 sub print_src_package {
306 my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
308 my $str = '<h3>'.sprintf( _g( 'Source package %s' ), $pkg ).'</h3>';
310 foreach my $suite (@SUITES) {
311 foreach my $archive (@ARCHIVES) {
312 if (exists $pkgs->{$suite}{$archive}) {
314 if ($sect->{$suite}{$archive}{source}) {
315 $origin_str .= " ".marker($sect->{$suite}{$archive}{source});
317 if ($archives->{$suite}{$archive}{source}) {
318 $origin_str .= " ".marker($archives->{$suite}{$archive}{source});
320 $str .= sprintf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s",
321 $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect->{$suite}{$archive}{source},
322 $pkgs->{$suite}{$archive}, $origin_str );
324 $str .= "<br>"._g( 'Binary packages: ' );
326 foreach my $bp (@{$binaries->{$suite}{$archive}}) {
327 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
328 $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ), $bp );
329 push @bp_links, $bp_link;
331 $str .= join( ", ", @bp_links );