1 package Packages::DoSearch;
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_CGI $SEARCH_PAGE
19 @SUITES @ARCHIVES $ROOT );
22 my ($params, $opts, $html_header, $menu, $page_content) = @_;
24 if ($params->{errors}{keywords}) {
25 fatal_error( "keyword not valid or missing" );
26 } elsif (length($opts->{keywords}) < 2) {
27 fatal_error( "keyword too short (keywords need to have at least two characters)" );
32 my $keyword = $opts->{keywords};
33 my $searchon = $opts->{searchon};
35 # for URL construction
36 my $keyword_esc = uri_escape( $keyword );
37 my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
38 my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
39 my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
42 my $keyword_enc = encode_entities $keyword || '';
43 my $searchon_enc = encode_entities $searchon;
44 my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
45 my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
46 my $archs_enc = encode_entities( join( ', ', @{$params->{values}{arch}{no_replace}} ) );
48 my $st0 = new Benchmark;
49 my (@results, @non_results);
51 unless (@Packages::CGI::fatal_errors) {
53 if ($searchon eq 'names') {
54 if ($opts->{source}) {
55 do_names_search( $keyword, \%sources, $sp_obj,
56 \&read_src_entry_all, $opts,
57 \@results, \@non_results );
59 do_names_search( $keyword, \%packages, $p_obj,
60 \&read_entry_all, $opts,
61 \@results, \@non_results );
63 # } elsif ($searchon eq 'contents') {
64 # require "./search_contents.pl";
67 do_names_search( $keyword, \%packages, $p_obj,
68 \&read_entry_all, $opts,
69 \@results, \@non_results );
70 do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
71 \%did2pkg, \%packages,
72 \&read_entry_all, $opts,
73 \@results, \@non_results );
78 # debug( join( "", Dumper( \@results, \@non_results )) );
79 my $st1 = new Benchmark;
80 my $std = timediff($st1, $st0);
81 debug( "Search took ".timestr($std) );
83 my $suite_wording = $suites_enc eq "all" ? "all suites"
84 : "suite(s) <em>$suites_enc</em>";
85 my $section_wording = $sections_enc eq 'all' ? "all sections"
86 : "section(s) <em>$sections_enc</em>";
87 my $arch_wording = $archs_enc eq 'any' ? "all architectures"
88 : "architecture(s) <em>$archs_enc</em>";
89 if ($searchon eq "names") {
90 my $source_wording = $opts->{source} ? "source " : "";
91 my $exact_wording = $opts->{exact} ? "named" : "that names contain";
92 msg( "You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
94 my $exact_wording = $opts->{exact} ? "" : " (including subword matching)";
95 msg( "You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording." );
98 if ($Packages::Search::too_many_hits) {
99 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." );
102 if (!@Packages::CGI::fatal_errors && !@results) {
104 if ($searchon eq "names") {
105 unless (@non_results) {
106 error( "Can't find that package." );
108 hint( "Can't find that package. ".
109 "<a href=\"$SEARCH_URL/$keyword_esc\">".
110 ($#non_results+1)."</a>".
111 " results have not been displayed due to the".
112 " search parameters." );
116 if (($suites_enc eq 'all')
117 && ($archs_enc eq 'any')
118 && ($sections_enc eq 'all')) {
119 error( "Can't find that string." );
121 error( "Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc)." );
124 if ($opts->{exact}) {
126 hint( "You have searched only for words exactly matching your keywords. You can try to search <a href=\"$SEARCH_CGI?exact=0;searchon=$searchon;suite=$suites_param;section=$sections_param;keywords=$keyword_esc;arch=$archs_param\">allowing subword matching</a>." );
129 hint( ( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$SEARCH_PAGE#search_packages\">Packages search page</a>." );
133 %$html_header = ( title => 'Package Search Results' ,
135 title_tag => 'Debian Package Search Results',
137 print_search_field => 'packages',
138 search_field_values => {
139 keywords => $keyword_enc,
140 searchon => $opts->{searchon_form},
142 suite => $suites_enc,
143 section => $sections_enc,
144 exact => $opts->{exact},
145 debug => $opts->{debug},
151 my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
153 unless ($opts->{source}) {
155 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
156 $priority, $version, $desc) = @$_;
158 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
159 if ($arch ne 'virtual') {
161 if ($archive =~ /^(security|non-US)$/) {
162 $real_archive = $archive;
166 $pkgs{$pkg}{$suite}{$archive}{$version}{$arch} = 1;
167 $subsect{$pkg}{$suite}{$archive}{$version} = $subsection;
168 $sect{$pkg}{$suite}{$archive}{$version} = $section
169 unless $section eq 'main';
170 $archives{$pkg}{$suite}{$archive}{$version} = $real_archive
173 $desc{$pkg}{$suite}{$archive}{$version} = $desc;
175 $provided_by{$pkg}{$suite}{$archive} = [ split /\s+/, $desc ];
179 my @pkgs = sort(keys %pkgs, keys %provided_by);
180 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword,
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, $keyword,
212 \&print_src_package, \%archives,
213 \%sect, \%subsect, \%binaries );
214 } # else unless $opts->{source}
219 my ($pkgs, $pkgs_list, $opts, $keyword, $print_func, @func_args) = @_;
221 #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts );
222 my $str .= "<p>Found <em>".(scalar @$pkgs_list)."</em> matching packages.";
226 if (grep { $_ eq $keyword } @$pkgs_list) {
228 $str .= '<h2 style="padding:.3em;border-top:solid grey thin;border-bottom:solid grey thin;background-color:#bdf">Exact hits</h2>';
229 $str .= &$print_func( $keyword, $pkgs->{$keyword}||{},
230 map { $_->{$keyword}||{} } @func_args );
231 @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
234 if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
235 $str .= '<h2 style="padding:.3em;border-top:solid grey thin;border-bottom:solid grey thin;background-color:#bdf">Other hits</h2>'
238 foreach my $pkg (@$pkgs_list) {
240 #next if $count < $start or $count > $end;
241 $str .= &$print_func( $pkg, $pkgs->{$pkg}||{},
242 map { $_->{$pkg}||{} } @func_args );
244 } elsif (@$pkgs_list) {
245 $str .= "<p><a href=\"$SEARCH_URL/FIXME\">".
246 ($#{$pkgs_list}+1)."</a> results have not been displayed because you requested only exact matches.</p>";
253 my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
255 my $str = sprintf "<h3>Package %s</h3>\n", $pkg;
257 foreach my $suite (@SUITES) {
258 foreach my $archive (@ARCHIVES) {
259 next if $archive eq 'security';
260 next if $archive eq 'non-US';
261 my $path = $suite.(($archive ne 'us')?"/$archive":'');
262 if (exists $pkgs->{$suite}{$archive}) {
264 my @versions = version_sort keys %{$pkgs->{$suite}{$archive}};
266 if ($sect->{$suite}{$archive}{$versions[0]}) {
267 $origin_str .= " [<span style=\"color:red\">$sect->{$suite}{$archive}{$versions[0]}</span>]";
269 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s %s\n",
270 $path, $pkg, $path, $subsect->{$suite}{$archive}{$versions[0]},
271 $desc->{$suite}{$archive}{$versions[0]}, $origin_str );
273 foreach my $v (@versions) {
274 my $archive_str = "";
275 if ($archives->{$suite}{$archive}{$v}) {
276 $archive_str .= " [<span style=\"color:red\">$archives->{$suite}{$archive}{$v}</span>]";
279 my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$archive}{$v}};
280 $str .= sprintf( "<br>%s$archive_str: %s\n",
281 $v, join (" ", @archs_to_print ))
283 $archs_printed{$_}++ foreach @archs_to_print;
285 if (my $p = $provided_by->{$suite}{$archive}) {
286 $str .= '<br>also provided by: '.
287 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
290 } elsif (my $p = $provided_by->{$suite}{$archive}) {
291 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a>: Virtual package<br>",
292 $path, $pkg, $path );
293 $str .= 'provided by: '.
294 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
302 sub print_src_package {
303 my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
305 my $str = sprintf "<h3>Source package %s</h3>\n", $pkg;
307 foreach my $suite (@SUITES) {
308 foreach my $archive (@ARCHIVES) {
309 if (exists $pkgs->{$suite}{$archive}) {
311 if ($sect->{$suite}{$archive}{source}) {
312 $origin_str .= " [<span style=\"color:red\">$sect->{$suite}{$archive}{source}</span>]";
314 if ($archives->{$suite}{$archive}{source}) {
315 $origin_str .= " [<span style=\"color:red\">$archives->{$suite}{$archive}{source}</span>]";
317 $str .= sprintf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s",
318 $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect->{$suite}{$archive}{source},
319 $pkgs->{$suite}{$archive}, $origin_str );
321 $str .= "<br>Binary packages: ";
323 foreach my $bp (@{$binaries->{$suite}{$archive}}) {
324 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
325 $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ), $bp );
326 push @bp_links, $bp_link;
328 $str .= join( ", ", @bp_links );