1 package Packages::DoSearch;
6 use Benchmark ':hireswallclock';
11 our @ISA = qw( Exporter );
12 our @EXPORT = qw( do_search );
15 use Packages::Search qw( :all );
18 use Packages::Config qw( $DBDIR $SEARCH_URL $SEARCH_PAGE
19 @SUITES @ARCHIVES $ROOT );
22 my ($params, $opts, $html_header, $menu, $page_content) = @_;
24 $Params::Search::too_many_hits = 0;
26 if ($params->{errors}{keywords}) {
27 fatal_error( "keyword not valid or missing" );
28 } elsif (length($opts->{keywords}) < 2) {
29 fatal_error( "keyword too short (keywords need to have at least two characters)" );
34 my $keyword = $opts->{keywords};
35 my $searchon = $opts->{searchon};
37 # for URL construction
38 my $keyword_esc = uri_escape( $keyword );
39 my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
40 my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
41 my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
42 $opts->{common_params} = "suite=$suites_param§ion=$sections_param&keywords=$keyword_esc&searchon=$searchon&arch=$archs_param";
45 my $keyword_enc = encode_entities $keyword || '';
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( $keyword, \%sources, $sp_obj,
59 \&read_src_entry_all, $opts,
60 \@results, \@non_results );
62 do_names_search( $keyword, \%packages, $p_obj,
63 \&read_entry_all, $opts,
64 \@results, \@non_results );
66 # } elsif ($searchon eq 'contents') {
67 # require "./search_contents.pl";
70 do_names_search( $keyword, \%packages, $p_obj,
71 \&read_entry_all, $opts,
72 \@results, \@non_results );
73 do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
74 \%did2pkg, \%packages,
75 \&read_entry_all, $opts,
76 \@results, \@non_results );
81 # debug( join( "", Dumper( \@results, \@non_results )) );
82 my $st1 = new Benchmark;
83 my $std = timediff($st1, $st0);
84 debug( "Search took ".timestr($std) );
86 my $suite_wording = $suites_enc eq "all" ? "all suites"
87 : "suite(s) <em>$suites_enc</em>";
88 my $section_wording = $sections_enc eq 'all' ? "all sections"
89 : "section(s) <em>$sections_enc</em>";
90 my $arch_wording = $archs_enc eq 'any' ? "all architectures"
91 : "architecture(s) <em>$archs_enc</em>";
92 if ($searchon eq "names") {
93 my $source_wording = $opts->{source} ? "source " : "";
94 my $exact_wording = $opts->{exact} ? "named" : "that names contain";
95 msg( "You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
97 my $exact_wording = $opts->{exact} ? "" : " (including subword matching)";
98 msg( "You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording." );
101 if ($Packages::Search::too_many_hits) {
102 error( "Your search was too wide so we will only display exact matches. At least <em>$Packages::Search::too_many_hits</em> results have been omitted and will not be displayed. Please consider using a longer keyword or more keywords." );
105 if (!@Packages::CGI::fatal_errors && !@results) {
107 if ($searchon eq "names") {
108 unless (@non_results) {
109 error( "Can't find that package." );
111 hint( "Can't find that package. ".
112 "<a href=\"$SEARCH_URL/$keyword_esc\">".
113 ($#non_results+1)."</a>".
114 " results have not been displayed due to the".
115 " search parameters." );
119 if (($suites_enc eq 'all')
120 && ($archs_enc eq 'any')
121 && ($sections_enc eq 'all')) {
122 error( "Can't find that string." );
124 error( "Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc)." );
127 if ($opts->{exact}) {
129 hint( "You have searched only for words exactly matching your
130 keywords. You can try to search <a href=\"".
131 encode_entities("$SEARCH_URL?exact=0&$opts->{common_params}")."\">allowing
132 subword matching</a>." );
135 hint( ( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$SEARCH_PAGE#search_packages\">Packages search page</a>." );
139 %$html_header = ( title => 'Package Search Results' ,
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>Found <em>".(scalar @$pkgs_list)."</em> matching packages.";
233 if (grep { $_ eq $keyword } @$pkgs_list) {
235 $str .= '<h2>Exact hits</h2>';
236 $str .= &$print_func( $keyword, $pkgs->{$keyword}||{},
237 map { $_->{$keyword}||{} } @func_args );
238 @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
241 if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
242 $str .= '<h2>Other hits</h2>'
245 foreach my $pkg (@$pkgs_list) {
247 #next if $count < $start or $count > $end;
248 $str .= &$print_func( $pkg, $pkgs->{$pkg}||{},
249 map { $_->{$pkg}||{} } @func_args );
251 } elsif (@$pkgs_list) {
252 $str .= "<p><a href=\"".encode_entities("$SEARCH_URL?exact=0&$opts->{common_params}")."\">".
253 ($#{$pkgs_list}+1)."</a> results have not been displayed because you requested only exact matches.</p>";
261 my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
263 my $str = sprintf "<h3>Package %s</h3>\n", $pkg;
265 foreach my $suite (@SUITES) {
266 foreach my $archive (@ARCHIVES) {
267 next if $archive eq 'security';
268 next if $archive eq 'non-US';
269 my $path = $suite.(($archive ne 'us')?"/$archive":'');
270 if (exists $pkgs->{$suite}{$archive}) {
272 my @versions = version_sort keys %{$pkgs->{$suite}{$archive}};
274 if ($sect->{$suite}{$archive}{$versions[0]}) {
275 $origin_str .= " ".marker($sect->{$suite}{$archive}{$versions[0]});
277 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s %s\n",
278 $path, $pkg, $path, $subsect->{$suite}{$archive}{$versions[0]},
279 $desc->{$suite}{$archive}{$versions[0]}, $origin_str );
281 foreach my $v (@versions) {
282 my $archive_str = "";
283 if ($archives->{$suite}{$archive}{$v}) {
284 $archive_str .= " ".marker($archives->{$suite}{$archive}{$v});
287 my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$archive}{$v}};
288 $str .= sprintf( "<br>%s$archive_str: %s\n",
289 $v, join (" ", @archs_to_print ))
291 $archs_printed{$_}++ foreach @archs_to_print;
293 if (my $p = $provided_by->{$suite}{$archive}) {
294 $str .= '<br>also provided by: '.
295 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
298 } elsif (my $p = $provided_by->{$suite}{$archive}) {
299 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a>: Virtual package<br>",
300 $path, $pkg, $path );
301 $str .= 'provided by: '.
302 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
310 sub print_src_package {
311 my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
313 my $str = sprintf "<h3>Source package %s</h3>\n", $pkg;
315 foreach my $suite (@SUITES) {
316 foreach my $archive (@ARCHIVES) {
317 if (exists $pkgs->{$suite}{$archive}) {
319 if ($sect->{$suite}{$archive}{source}) {
320 $origin_str .= " ".marker($sect->{$suite}{$archive}{source});
322 if ($archives->{$suite}{$archive}{source}) {
323 $origin_str .= " ".marker($archives->{$suite}{$archive}{source});
325 $str .= sprintf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s",
326 $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect->{$suite}{$archive}{source},
327 $pkgs->{$suite}{$archive}, $origin_str );
329 $str .= "<br>Binary packages: ";
331 foreach my $bp (@{$binaries->{$suite}{$archive}}) {
332 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
333 $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ), $bp );
334 push @bp_links, $bp_link;
336 $str .= join( ", ", @bp_links );