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( _( "keyword not valid or missing" ) );
31 } elsif (length($opts->{keywords}) < 2) {
32 fatal_error( _( "keyword too short (keywords need to have at least two characters)" ) );
37 my $keyword = $opts->{keywords};
38 my $searchon = $opts->{searchon};
40 # for URL construction
41 my $keyword_esc = uri_escape( $keyword );
42 my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
43 my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
44 my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
45 $opts->{common_params} = "suite=$suites_param§ion=$sections_param&keywords=$keyword_esc&searchon=$searchon&arch=$archs_param";
48 my $keyword_enc = encode_entities $keyword || '';
49 my $searchon_enc = encode_entities $searchon;
50 my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
51 my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
52 my $archs_enc = encode_entities( join( ', ', @{$params->{values}{arch}{no_replace}} ) );
54 my $st0 = new Benchmark;
55 my (@results, @non_results);
57 unless (@Packages::CGI::fatal_errors) {
59 if ($searchon eq 'names') {
60 if ($opts->{source}) {
61 do_names_search( $keyword, \%sources, $sp_obj,
62 \&read_src_entry_all, $opts,
63 \@results, \@non_results );
65 do_names_search( $keyword, \%packages, $p_obj,
66 \&read_entry_all, $opts,
67 \@results, \@non_results );
69 # } elsif ($searchon eq 'contents') {
70 # require "./search_contents.pl";
73 do_names_search( $keyword, \%packages, $p_obj,
74 \&read_entry_all, $opts,
75 \@results, \@non_results );
76 do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
77 \%did2pkg, \%packages,
78 \&read_entry_all, $opts,
79 \@results, \@non_results );
84 # debug( join( "", Dumper( \@results, \@non_results )) );
85 my $st1 = new Benchmark;
86 my $std = timediff($st1, $st0);
87 debug( "Search took ".timestr($std) );
89 my $suite_wording = $suites_enc eq "all" ? "all suites"
90 : "suite(s) <em>$suites_enc</em>";
91 my $section_wording = $sections_enc eq 'all' ? "all sections"
92 : "section(s) <em>$sections_enc</em>";
93 my $arch_wording = $archs_enc eq 'any' ? "all architectures"
94 : "architecture(s) <em>$archs_enc</em>";
95 if ($searchon eq "names") {
96 my $source_wording = $opts->{source} ? "source " : "";
97 my $exact_wording = $opts->{exact} ? "named" : "that names contain";
98 msg( "You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
100 my $exact_wording = $opts->{exact} ? "" : " (including subword matching)";
101 msg( "You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording." );
104 if ($Packages::Search::too_many_hits) {
105 error( sprintf( _( "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( _( "Can't find that package." ) );
113 hint( _( "Can't find that package." )." ".
114 sprintf( _( '<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( _( "Can't find that string." ) );
126 error( sprintf( _( "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( _( '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("$SEARCH_URL?exact=0&$opts->{common_params}") ) );
135 hint( sprintf( _( 'You can try a different search on the <a href="%s">Packages search page</a>.' ), "$SEARCH_PAGE#search_packages" ) );
139 %$html_header = ( title => _( 'Package Search Results' ) ,
140 lang => $opts->{lang},
141 title_tag => _( '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') {
167 if ($archive =~ /^(security|non-US)$/) {
168 $real_archive = $archive;
172 $pkgs{$pkg}{$suite}{$archive}{$version}{$arch} = 1;
173 $subsect{$pkg}{$suite}{$archive}{$version} = $subsection;
174 $sect{$pkg}{$suite}{$archive}{$version} = $section
175 unless $section eq 'main';
176 $archives{$pkg}{$suite}{$archive}{$version} = $real_archive
179 $desc{$pkg}{$suite}{$archive}{$version} = $desc;
181 $provided_by{$pkg}{$suite}{$archive} = [ split /\s+/, $desc ];
185 my @pkgs = sort(keys %pkgs, keys %provided_by);
186 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword,
187 \&print_package, \%provided_by,
188 \%archives, \%sect, \%subsect,
191 } else { # unless $opts->{source}
193 my ($pkg, $archive, $suite, $section, $subsection, $priority,
196 my $real_archive = '';
197 if ($archive =~ /^(security|non-US)$/) {
198 $real_archive = $archive;
201 if (($real_archive eq $archive) &&
202 $pkgs{$pkg}{$suite}{$archive} &&
203 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
206 $pkgs{$pkg}{$suite}{$archive} = $version;
207 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
208 $sect{$pkg}{$suite}{$archive}{source} = $section
209 unless $section eq 'main';
210 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
213 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
216 my @pkgs = sort keys %pkgs;
217 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword,
218 \&print_src_package, \%archives,
219 \%sect, \%subsect, \%binaries );
220 } # else unless $opts->{source}
225 my ($pkgs, $pkgs_list, $opts, $keyword, $print_func, @func_args) = @_;
227 #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts );
228 my $str = '<div id="psearchres">';
229 $str .= "<p>".sprintf( _( "Found <em>%s</em> matching packages." ),
230 scalar @$pkgs_list )."</p>";
234 if (grep { $_ eq $keyword } @$pkgs_list) {
236 $str .= '<h2>'._( "Exact hits" ).'</h2>';
237 $str .= &$print_func( $keyword, $pkgs->{$keyword}||{},
238 map { $_->{$keyword}||{} } @func_args );
239 @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
242 if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
243 $str .= '<h2>'._( 'Other hits' ).'</h2>'
246 foreach my $pkg (@$pkgs_list) {
248 #next if $count < $start or $count > $end;
249 $str .= &$print_func( $pkg, $pkgs->{$pkg}||{},
250 map { $_->{$pkg}||{} } @func_args );
252 } elsif (@$pkgs_list) {
253 $str .= "<p>".sprintf( _( '<a href="%s">%s</a> results have not been displayed because you requested only exact matches.' ),
254 encode_entities("$SEARCH_URL?exact=0&$opts->{common_params}"),
255 scalar @$pkgs_list )."</p>";
263 my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
265 my $str = '<h3>'.sprintf( _( 'Package %s' ), $pkg ).'</h3>';
267 foreach my $suite (@SUITES) {
268 foreach my $archive (@ARCHIVES) {
269 next if $archive eq 'security';
270 next if $archive eq 'non-US';
271 my $path = $suite.(($archive ne 'us')?"/$archive":'');
272 if (exists $pkgs->{$suite}{$archive}) {
274 my @versions = version_sort keys %{$pkgs->{$suite}{$archive}};
276 if ($sect->{$suite}{$archive}{$versions[0]}) {
277 $origin_str .= " ".marker($sect->{$suite}{$archive}{$versions[0]});
279 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s %s\n",
280 $path, $pkg, $path, $subsect->{$suite}{$archive}{$versions[0]},
281 $desc->{$suite}{$archive}{$versions[0]}, $origin_str );
283 foreach my $v (@versions) {
284 my $archive_str = "";
285 if ($archives->{$suite}{$archive}{$v}) {
286 $archive_str .= " ".marker($archives->{$suite}{$archive}{$v});
289 my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$archive}{$v}};
290 $str .= sprintf( "<br>%s$archive_str: %s\n",
291 $v, join (" ", @archs_to_print ))
293 $archs_printed{$_}++ foreach @archs_to_print;
295 if (my $p = $provided_by->{$suite}{$archive}) {
296 $str .= '<br>'._( 'also provided by: ' ).
297 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
300 } elsif (my $p = $provided_by->{$suite}{$archive}) {
301 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a>: Virtual package<br>",
302 $path, $pkg, $path );
303 $str .= _( 'provided by: ' ).
304 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
312 sub print_src_package {
313 my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
315 my $str = '<h3>'.sprintf( _( 'Source package %s' ), $pkg ).'</h3>';
317 foreach my $suite (@SUITES) {
318 foreach my $archive (@ARCHIVES) {
319 if (exists $pkgs->{$suite}{$archive}) {
321 if ($sect->{$suite}{$archive}{source}) {
322 $origin_str .= " ".marker($sect->{$suite}{$archive}{source});
324 if ($archives->{$suite}{$archive}{source}) {
325 $origin_str .= " ".marker($archives->{$suite}{$archive}{source});
327 $str .= sprintf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s",
328 $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect->{$suite}{$archive}{source},
329 $pkgs->{$suite}{$archive}, $origin_str );
331 $str .= "<br>"._( 'Binary packages: ' );
333 foreach my $bp (@{$binaries->{$suite}{$archive}}) {
334 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
335 $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ), $bp );
336 push @bp_links, $bp_link;
338 $str .= join( ", ", @bp_links );