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;section=$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 keywords. You can try to search <a href=\"$SEARCH_URL?exact=0;$opts->{common_params}\">allowing subword matching</a>." );
132 hint( ( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$SEARCH_PAGE#search_packages\">Packages search page</a>." );
136 %$html_header = ( title => 'Package Search Results' ,
138 title_tag => 'Debian Package Search Results',
140 print_search_field => 'packages',
141 search_field_values => {
142 keywords => $keyword_enc,
143 searchon => $opts->{searchon_form},
145 suite => $suites_enc,
146 section => $sections_enc,
147 exact => $opts->{exact},
148 debug => $opts->{debug},
154 my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
156 unless ($opts->{source}) {
158 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
159 $priority, $version, $desc) = @$_;
161 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
162 if ($arch ne 'virtual') {
164 if ($archive =~ /^(security|non-US)$/) {
165 $real_archive = $archive;
169 $pkgs{$pkg}{$suite}{$archive}{$version}{$arch} = 1;
170 $subsect{$pkg}{$suite}{$archive}{$version} = $subsection;
171 $sect{$pkg}{$suite}{$archive}{$version} = $section
172 unless $section eq 'main';
173 $archives{$pkg}{$suite}{$archive}{$version} = $real_archive
176 $desc{$pkg}{$suite}{$archive}{$version} = $desc;
178 $provided_by{$pkg}{$suite}{$archive} = [ split /\s+/, $desc ];
182 my @pkgs = sort(keys %pkgs, keys %provided_by);
183 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword,
184 \&print_package, \%provided_by,
185 \%archives, \%sect, \%subsect,
188 } else { # unless $opts->{source}
190 my ($pkg, $archive, $suite, $section, $subsection, $priority,
193 my $real_archive = '';
194 if ($archive =~ /^(security|non-US)$/) {
195 $real_archive = $archive;
198 if (($real_archive eq $archive) &&
199 $pkgs{$pkg}{$suite}{$archive} &&
200 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
203 $pkgs{$pkg}{$suite}{$archive} = $version;
204 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
205 $sect{$pkg}{$suite}{$archive}{source} = $section
206 unless $section eq 'main';
207 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
210 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
213 my @pkgs = sort keys %pkgs;
214 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword,
215 \&print_src_package, \%archives,
216 \%sect, \%subsect, \%binaries );
217 } # else unless $opts->{source}
222 my ($pkgs, $pkgs_list, $opts, $keyword, $print_func, @func_args) = @_;
224 #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts );
225 my $str = '<div id="psearchres">';
226 $str .= "<p>Found <em>".(scalar @$pkgs_list)."</em> matching packages.";
230 if (grep { $_ eq $keyword } @$pkgs_list) {
232 $str .= '<h2>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>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><a href=\"$SEARCH_URL?exact=0;$opts->{common_params}\">".
250 ($#{$pkgs_list}+1)."</a> results have not been displayed because you requested only exact matches.</p>";
258 my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
260 my $str = sprintf "<h3>Package %s</h3>\n", $pkg;
262 foreach my $suite (@SUITES) {
263 foreach my $archive (@ARCHIVES) {
264 next if $archive eq 'security';
265 next if $archive eq 'non-US';
266 my $path = $suite.(($archive ne 'us')?"/$archive":'');
267 if (exists $pkgs->{$suite}{$archive}) {
269 my @versions = version_sort keys %{$pkgs->{$suite}{$archive}};
271 if ($sect->{$suite}{$archive}{$versions[0]}) {
272 $origin_str .= " ".marker($sect->{$suite}{$archive}{$versions[0]});
274 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s %s\n",
275 $path, $pkg, $path, $subsect->{$suite}{$archive}{$versions[0]},
276 $desc->{$suite}{$archive}{$versions[0]}, $origin_str );
278 foreach my $v (@versions) {
279 my $archive_str = "";
280 if ($archives->{$suite}{$archive}{$v}) {
281 $archive_str .= " ".marker($archives->{$suite}{$archive}{$v});
284 my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$archive}{$v}};
285 $str .= sprintf( "<br>%s$archive_str: %s\n",
286 $v, join (" ", @archs_to_print ))
288 $archs_printed{$_}++ foreach @archs_to_print;
290 if (my $p = $provided_by->{$suite}{$archive}) {
291 $str .= '<br>also provided by: '.
292 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
295 } elsif (my $p = $provided_by->{$suite}{$archive}) {
296 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a>: Virtual package<br>",
297 $path, $pkg, $path );
298 $str .= 'provided by: '.
299 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
307 sub print_src_package {
308 my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
310 my $str = sprintf "<h3>Source package %s</h3>\n", $pkg;
312 foreach my $suite (@SUITES) {
313 foreach my $archive (@ARCHIVES) {
314 if (exists $pkgs->{$suite}{$archive}) {
316 if ($sect->{$suite}{$archive}{source}) {
317 $origin_str .= " ".marker($sect->{$suite}{$archive}{source});
319 if ($archives->{$suite}{$archive}{source}) {
320 $origin_str .= " ".marker($archives->{$suite}{$archive}{source});
322 $str .= sprintf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s",
323 $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect->{$suite}{$archive}{source},
324 $pkgs->{$suite}{$archive}, $origin_str );
326 $str .= "<br>Binary packages: ";
328 foreach my $bp (@{$binaries->{$suite}{$archive}}) {
329 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
330 $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ), $bp );
331 push @bp_links, $bp_link;
333 $str .= join( ", ", @bp_links );