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 );
17 use Packages::CGI qw( :DEFAULT msg );
19 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES $ROOT );
22 my ($params, $opts, $html_header, $page_content) = @_;
24 $Params::Search::too_many_hits = 0;
26 if ($params->{errors}{keywords}) {
27 fatal_error( _g( "keyword not valid or missing" ) );
28 $opts->{keywords} = [];
29 } elsif (grep { length($_) < 2 } @{$opts->{keywords}}) {
30 fatal_error( _g( "keyword too short (keywords need to have at least two characters)" ) );
33 my @keywords = @{$opts->{keywords}};
34 my $searchon = $opts->{searchon};
36 # for URL construction
37 my $keyword_esc = uri_escape( "@keywords" );
38 $opts->{keywords_esc} = $keyword_esc;
41 my $keyword_enc = encode_entities "@keywords" || '';
42 my $searchon_enc = encode_entities $searchon;
43 my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
44 my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
45 my $archs_enc = encode_entities( join( ', ', @{$params->{values}{arch}{no_replace}} ) );
47 my $st0 = new Benchmark;
48 my (@results, @non_results);
50 unless (@Packages::CGI::fatal_errors) {
52 if ($searchon eq 'names') {
53 if ($opts->{source}) {
54 do_names_search( [ @keywords ], \%sources, $sp_obj,
55 \&read_src_entry_all, $opts,
56 \@results, \@non_results );
58 do_names_search( [ @keywords ], \%packages, $p_obj,
59 \&read_entry_all, $opts,
60 \@results, \@non_results );
63 do_names_search( [ @keywords ], \%packages, $p_obj,
64 \&read_entry_all, $opts,
65 \@results, \@non_results );
66 do_fulltext_search( [ @keywords ], "$DBDIR/descriptions.txt",
67 \%did2pkg, \%packages,
68 \&read_entry_all, $opts,
69 \@results, \@non_results );
74 # debug( join( "", Dumper( \@results, \@non_results )) ) if DEBUG;
75 my $st1 = new Benchmark;
76 my $std = timediff($st1, $st0);
77 debug( "Search took ".timestr($std) ) if DEBUG;
79 my $suite_wording = $suites_enc =~ /^(default|all)$/ ? _g("all suites")
80 : sprintf(_g("suite(s) <em>%s</em>", $suites_enc) );
81 my $section_wording = $sections_enc eq 'all' ? _g("all sections")
82 : sprintf(_g("section(s) <em>%s</em>", $sections_enc) );
83 my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
84 : sprintf(_g("architecture(s) <em>%s</em>", $archs_enc) );
85 if ($searchon eq "names") {
86 my $source_wording = $opts->{source} ? _g("source packages") : _g("packages");
87 # sorry to all translators for that one... (patches welcome)
88 msg( sprintf( _g( "You have searched for %s that names contain <em>%s</em> in %s, %s, and %s." ),
89 $source_wording, $keyword_enc,
90 $suite_wording, $section_wording, $arch_wording ) );
92 my $exact_wording = $opts->{exact} ? "" : _g(" (including subword matching)");
93 msg( sprintf( _g( "You have searched for <em>%s</em> in packages names and descriptions in %s, %s, and %s%s." ),
95 $suite_wording, $section_wording, $arch_wording,
99 if ($Packages::Search::too_many_hits) {
100 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 ) );
103 if (!@Packages::CGI::fatal_errors && !@results) {
104 if ($searchon eq "names") {
105 unless (@non_results) {
106 error( _g( "Can't find that package." ) );
108 # hint( _g( "Can't find that package." )." ".
109 # sprintf( _g( '<a href="%s">%s</a>'.
110 # " results have not been displayed due to the".
111 # " search parameters." ), "$SEARCH_URL/$keyword_esc" ,
112 # $#non_results+1 ) );
116 if (($suites_enc eq 'all')
117 && ($archs_enc eq 'any')
118 && ($sections_enc eq 'all')) {
119 error( _g( "Can't find that string." ) );
121 error( sprintf( _g( "Can't find that string, at least not in that suite (%s, section %s) and on that architecture (%s)." ),
122 $suites_enc, $sections_enc, $archs_enc ) );
125 if ($opts->{exact}) {
126 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>.' ),
127 encode_entities(make_search_url('',"keywords=$keyword_esc",{exact => 0})) ) );
130 # hint( sprintf( _g( 'You can try a different search on the <a href="%s">Packages search page</a>.' ), "$SEARCH_PAGE#search_packages" ) );
134 $page_content->{make_url} = sub { return &Packages::CGI::make_url(@_) };
135 $page_content->{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
137 $page_content->{search_field_values} = {
138 keywords => $keyword_enc,
139 searchon => $opts->{searchon_form},
141 suite => $suites_enc,
142 section => $sections_enc,
143 exact => $opts->{exact},
144 debug => $opts->{debug},
148 my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
150 unless ($opts->{source}) {
152 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
153 $priority, $version, $desc) = @$_;
155 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
156 if ($arch ne 'virtual') {
157 $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
158 $subsect{$pkg}{$suite}{$version} = $subsection;
159 $sect{$pkg}{$suite}{$version} = $section
160 unless $section eq 'main';
161 $archives{$pkg}{$suite}{$version} ||= $archive;
163 $desc{$pkg}{$suite}{$version} = $desc;
165 $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
169 my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
170 my @pkgs = sort keys %uniq_pkgs;
171 process_packages( $page_content, 'packages', \%pkgs, \@pkgs, $opts, \@keywords,
172 \&process_package, \%provided_by,
173 \%archives, \%sect, \%subsect,
176 } else { # unless $opts->{source}
178 my ($pkg, $archive, $suite, $section, $subsection, $priority,
181 my $real_archive = '';
182 if ($archive =~ /^(security|non-US)$/) {
183 $real_archive = $archive;
186 if (($real_archive eq $archive) &&
187 $pkgs{$pkg}{$suite}{$archive} &&
188 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
191 $pkgs{$pkg}{$suite}{$archive} = $version;
192 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
193 $sect{$pkg}{$suite}{$archive}{source} = $section
194 unless $section eq 'main';
195 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
198 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
201 my @pkgs = sort keys %pkgs;
202 process_packages( $page_content, 'src_packages', \%pkgs, \@pkgs, $opts, \@keywords,
203 \&process_src_package, \%archives,
204 \%sect, \%subsect, \%binaries );
205 } # else unless $opts->{source}
209 sub process_packages {
210 my ($content, $target, $pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
213 $content->{results} = scalar @$pkgs_list;
216 $keyword = $keywords->[0] if @$keywords == 1;
219 if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
221 $categories[0]{name} = _g( "Exact hits" );
223 $categories[0]{$target} = [ &$print_func( $keyword, $pkgs->{$keyword}||{},
224 map { $_->{$keyword}||{} } @func_args ) ];
225 @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
228 if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
230 $cat{name} = _g( 'Other hits' ) if $have_exact;
233 foreach my $pkg (@$pkgs_list) {
234 push @{$cat{$target}}, &$print_func( $pkg, $pkgs->{$pkg}||{},
235 map { $_->{$pkg}||{} } @func_args );
237 push @categories, \%cat;
238 } elsif (@$pkgs_list) {
239 $content->{skipped} = scalar @$pkgs_list;
242 $content->{categories} = \@categories;
245 sub process_package {
246 my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
248 my %pkg = ( pkg => $pkg,
251 foreach my $suite (@SUITES) {
252 my %suite = ( suite => $suite );
253 if (exists $pkgs->{$suite}) {
255 my @versions = version_sort keys %{$pkgs->{$suite}};
256 $suite{section} = $sect->{$suite}{$versions[0]};
257 $suite{subsection} = $subsect->{$suite}{$versions[0]};
258 $suite{desc} = $desc->{$suite}{$versions[0]};
259 $suite{versions} = [];
261 foreach my $v (@versions) {
263 $version{version} = $v;
264 $version{archive} = $archives->{$suite}{$v};
266 $version{architectures} = [ grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}} ];
267 push @{$suite{versions}}, \%version if @{$version{architectures}};
269 $archs_printed{$_}++ foreach @{$version{architectures}};
271 if (my $p = $provided_by->{$suite}) {
272 $suite{providers} = $p;
274 } elsif (my $p = $provided_by->{$suite}) {
275 $suite{desc} = _g('Virtual package');
276 $suite{providers} = $p;
278 push @{$pkg{suites}}, \%suite if $suite{versions} || $suite{providers};
284 sub process_src_package {
285 my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
287 my %pkg = ( pkg => $pkg,
290 foreach my $suite (@SUITES) {
291 foreach my $archive (@ARCHIVES) {
292 if (exists $pkgs->{$suite}{$archive}) {
294 $origin{version} = $pkgs->{$suite}{$archive};
295 $origin{suite} = $suite;
296 $origin{archive} = $archive;
297 $origin{section} = $sect->{$suite}{$archive}{source};
298 $origin{subsection} = $subsect->{$suite}{$archive}{source};
299 $origin{real_archive} = $archives->{$suite}{$archive}{source};
301 $origin{binaries} = $binaries->{$suite}{$archive};
302 push @{$pkg{origins}}, \%origin;