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 $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 )) ) if DEBUG;
85 my $st1 = new Benchmark;
86 my $std = timediff($st1, $st0);
87 debug( "Search took ".timestr($std) ) if DEBUG;
89 my $suite_wording = $suites_enc eq "all" ? _g("all suites")
90 : sprintf(_g("suite(s) <em>%s</em>", $suites_enc) );
91 my $section_wording = $sections_enc eq 'all' ? _g("all sections")
92 : sprintf(_g("section(s) <em>%s</em>", $sections_enc) );
93 my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
94 : sprintf(_g("architecture(s) <em>%s</em>", $archs_enc) );
95 if ($searchon eq "names") {
96 my $source_wording = $opts->{source} ? _g("source packages") : _g("packages");
97 # sorry to all translators for that one... (patches welcome)
98 msg( sprintf( _g( "You have searched for %s that names contain <em>%s</em> in %s, %s, and %s." ),
99 $source_wording, $keyword_enc,
100 $suite_wording, $section_wording, $arch_wording ) );
102 my $exact_wording = $opts->{exact} ? "" : _g(" (including subword matching)");
103 msg( sprintf( _g( "You have searched for <em>%s</em> in packages names and descriptions in %s, %s, and %s%s." ),
105 $suite_wording, $section_wording, $arch_wording,
109 if ($Packages::Search::too_many_hits) {
110 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 ) );
113 if (!@Packages::CGI::fatal_errors && !@results) {
114 if ($searchon eq "names") {
115 unless (@non_results) {
116 error( _g( "Can't find that package." ) );
118 hint( _g( "Can't find that package." )." ".
119 sprintf( _g( '<a href="%s">%s</a>'.
120 " results have not been displayed due to the".
121 " search parameters." ), "$SEARCH_URL/$keyword_esc" ,
126 if (($suites_enc eq 'all')
127 && ($archs_enc eq 'any')
128 && ($sections_enc eq 'all')) {
129 error( _g( "Can't find that string." ) );
131 error( sprintf( _g( "Can't find that string, at least not in that suite (%s, section %s) and on that architecture (%s)." ),
132 $suites_enc, $sections_enc, $archs_enc ) );
135 if ($opts->{exact}) {
136 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>.' ),
137 encode_entities("$SEARCH_URL?exact=0&$opts->{common_params}") ) );
140 hint( sprintf( _g( 'You can try a different search on the <a href="%s">Packages search page</a>.' ), "$SEARCH_PAGE#search_packages" ) );
144 %$html_header = ( title => _g( 'Package Search Results' ) ,
145 lang => $opts->{lang},
146 title_tag => _g( 'Debian Package Search Results' ),
148 print_search_field => 'packages',
149 search_field_values => {
150 keywords => $keyword_enc,
151 searchon => $opts->{searchon_form},
153 suite => $suites_enc,
154 section => $sections_enc,
155 exact => $opts->{exact},
156 debug => $opts->{debug},
162 my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
164 unless ($opts->{source}) {
166 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
167 $priority, $version, $desc) = @$_;
169 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
170 if ($arch ne 'virtual') {
171 $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
172 $subsect{$pkg}{$suite}{$version} = $subsection;
173 $sect{$pkg}{$suite}{$version} = $section
174 unless $section eq 'main';
175 $archives{$pkg}{$suite}{$version} ||= $archive;
177 $desc{$pkg}{$suite}{$version} = $desc;
179 $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
183 my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
184 my @pkgs = sort keys %uniq_pkgs;
185 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword,
186 \&print_package, \%provided_by,
187 \%archives, \%sect, \%subsect,
190 } else { # unless $opts->{source}
192 my ($pkg, $archive, $suite, $section, $subsection, $priority,
195 my $real_archive = '';
196 if ($archive =~ /^(security|non-US)$/) {
197 $real_archive = $archive;
200 if (($real_archive eq $archive) &&
201 $pkgs{$pkg}{$suite}{$archive} &&
202 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
205 $pkgs{$pkg}{$suite}{$archive} = $version;
206 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
207 $sect{$pkg}{$suite}{$archive}{source} = $section
208 unless $section eq 'main';
209 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
212 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
215 my @pkgs = sort keys %pkgs;
216 $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword,
217 \&print_src_package, \%archives,
218 \%sect, \%subsect, \%binaries );
219 } # else unless $opts->{source}
224 my ($pkgs, $pkgs_list, $opts, $keyword, $print_func, @func_args) = @_;
226 #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts );
227 my $str = '<div id="psearchres">';
228 $str .= "<p>".sprintf( _g( "Found <em>%s</em> matching packages." ),
229 scalar @$pkgs_list )."</p>";
233 if (grep { $_ eq $keyword } @$pkgs_list) {
235 $str .= '<h2>'._g( "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>'._g( '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>".sprintf( _g( '<a href="%s">%s</a> results have not been displayed because you requested only exact matches.' ),
253 encode_entities("$SEARCH_URL?exact=0&$opts->{common_params}"),
254 scalar @$pkgs_list )."</p>";
262 my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
264 my $str = '<h3>'.sprintf( _g( 'Package %s' ), $pkg ).'</h3>';
266 foreach my $suite (@SUITES) {
268 if (exists $pkgs->{$suite}) {
270 my @versions = version_sort keys %{$pkgs->{$suite}};
272 if ($sect->{$suite}{$versions[0]}) {
273 $origin_str .= " ".marker($sect->{$suite}{$versions[0]});
275 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s %s\n",
276 $path, $pkg, $path, $subsect->{$suite}{$versions[0]},
277 $desc->{$suite}{$versions[0]}, $origin_str );
279 foreach my $v (@versions) {
280 my $archive_str = "";
281 if ($archives->{$suite}{$v} ne 'us') {
282 $archive_str .= " ".marker($archives->{$suite}{$v});
285 my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}};
286 $str .= sprintf( "<br>%s$archive_str: %s\n",
287 $v, join (" ", @archs_to_print ))
289 $archs_printed{$_}++ foreach @archs_to_print;
291 if (my $p = $provided_by->{$suite}) {
292 $str .= '<br>'._g( 'also provided by: ' ).
293 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
296 } elsif (my $p = $provided_by->{$suite}) {
297 $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a>: Virtual package<br>",
298 $path, $pkg, $path );
299 $str .= _g( 'provided by: ' ).
300 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$p);
307 sub print_src_package {
308 my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
310 my $str = '<h3>'.sprintf( _g( 'Source package %s' ), $pkg ).'</h3>';
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>"._g( '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 );