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 );
19 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES $ROOT );
22 my ($params, $opts, $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};
35 $page_content->{search_keywords} = \@keywords;
37 my $st0 = new Benchmark;
38 my (@results, @non_results);
40 unless (@Packages::CGI::fatal_errors) {
42 if ($searchon eq 'names') {
43 if ($opts->{source}) {
44 do_names_search( [ @keywords ], \%sources, $sp_obj,
45 \&read_src_entry_all, $opts,
46 \@results, \@non_results );
48 do_names_search( [ @keywords ], \%packages, $p_obj,
49 \&read_entry_all, $opts,
50 \@results, \@non_results );
53 do_names_search( [ @keywords ], \%packages, $p_obj,
54 \&read_entry_all, $opts,
55 \@results, \@non_results );
56 my $fts1 = new Benchmark;
57 do_xapian_search( [ @keywords ], "$DBDIR/xapian/",
58 \%did2pkg, \%packages,
59 \&read_entry_all, $opts,
60 \@results, \@non_results );
61 my $fts2 = new Benchmark;
62 my $fts_xapian = timediff($fts2,$fts1);
63 debug( "Fulltext search took ".timestr($fts_xapian) )
69 # debug( join( "", Dumper( \@results, \@non_results )) ) if DEBUG;
70 my $st1 = new Benchmark;
71 my $std = timediff($st1, $st0);
72 debug( "Search took ".timestr($std) ) if DEBUG;
74 $page_content->{too_many_hits} = $Packages::Search::too_many_hits;
75 #FIXME: non_results can't be compared to results since it is
76 # not normalized to unique packages
77 $page_content->{non_results} = scalar @non_results;
80 my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
82 my %sort_by_relevance;
83 for (1 ... scalar @results) {
84 # debug("$results[$_][0] => $_", 4) if DEBUG;
85 $sort_by_relevance{$results[$_-1][0]} = $_;
88 # debug( "sort_by_relevance=".Dumper(\%sort_by_relevance), 4);
90 unless ($opts->{source}) {
92 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
93 $priority, $version, $desc) = @$_;
95 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
96 if ($arch ne 'virtual') {
97 $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
98 $subsect{$pkg}{$suite}{$version} = $subsection;
99 $sect{$pkg}{$suite}{$version} = $section;
100 $archives{$pkg}{$suite}{$version} ||= $archive;
102 $desc{$pkg}{$suite}{$version} = $desc;
104 $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
108 my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
110 if ($searchon eq 'names') {
111 @pkgs = sort keys %uniq_pkgs;
113 @pkgs = sort { $sort_by_relevance{$a} <=> $sort_by_relevance{$b} } keys %uniq_pkgs;
115 process_packages( $page_content, 'packages', \%pkgs, \@pkgs, $opts, \@keywords,
116 \&process_package, \%provided_by,
117 \%archives, \%sect, \%subsect,
120 } else { # unless $opts->{source}
122 my ($pkg, $archive, $suite, $section, $subsection, $priority,
125 my $real_archive = '';
126 if ($archive eq 'security') {
127 $real_archive = $archive;
130 if (($real_archive eq $archive) &&
131 $pkgs{$pkg}{$suite}{$archive} &&
132 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
135 $pkgs{$pkg}{$suite}{$archive} = $version;
136 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
137 $sect{$pkg}{$suite}{$archive}{source} = $section
138 unless $section eq 'main';
139 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
142 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
145 my @pkgs = sort keys %pkgs;
146 process_packages( $page_content, 'src_packages', \%pkgs, \@pkgs, $opts, \@keywords,
147 \&process_src_package, \%archives,
148 \%sect, \%subsect, \%binaries );
149 } # else unless $opts->{source}
153 sub process_packages {
154 my ($content, $target, $pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
157 $content->{results} = scalar @$pkgs_list;
160 $keyword = $keywords->[0] if @$keywords == 1;
163 if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
165 $categories[0]{name} = _g( "Exact hits" );
167 $categories[0]{$target} = [ &$print_func( $keyword, $pkgs->{$keyword}||{},
168 map { $_->{$keyword}||{} } @func_args ) ];
169 @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
172 if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
174 $cat{name} = _g( 'Other hits' ) if $have_exact;
177 foreach my $pkg (@$pkgs_list) {
178 push @{$cat{$target}}, &$print_func( $pkg, $pkgs->{$pkg}||{},
179 map { $_->{$pkg}||{} } @func_args );
181 push @categories, \%cat;
182 } elsif (@$pkgs_list) {
183 $content->{skipped} = scalar @$pkgs_list;
186 $content->{categories} = \@categories;
189 sub process_package {
190 my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
192 my %pkg = ( pkg => $pkg,
195 foreach my $suite (@SUITES) {
196 my %suite = ( suite => $suite );
197 if (exists $pkgs->{$suite}) {
199 my @versions = version_sort keys %{$pkgs->{$suite}};
200 $suite{section} = $sect->{$suite}{$versions[0]};
201 $suite{subsection} = $subsect->{$suite}{$versions[0]};
202 $suite{desc} = $desc->{$suite}{$versions[0]};
203 $suite{versions} = [];
205 foreach my $v (@versions) {
207 $version{version} = $v;
208 $version{archive} = $archives->{$suite}{$v};
210 $version{architectures} = [ grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}} ];
211 push @{$suite{versions}}, \%version if @{$version{architectures}};
213 $archs_printed{$_}++ foreach @{$version{architectures}};
215 if (my $p = $provided_by->{$suite}) {
216 $suite{providers} = $p;
218 } elsif (my $p = $provided_by->{$suite}) {
219 $suite{desc} = _g('Virtual package');
220 $suite{providers} = $p;
222 push @{$pkg{suites}}, \%suite if $suite{versions} || $suite{providers};
228 sub process_src_package {
229 my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
231 my %pkg = ( pkg => $pkg,
234 foreach my $suite (@SUITES) {
235 foreach my $archive (@ARCHIVES) {
236 if (exists $pkgs->{$suite}{$archive}) {
238 $origin{version} = $pkgs->{$suite}{$archive};
239 $origin{suite} = $suite;
240 $origin{archive} = $archive;
241 $origin{section} = $sect->{$suite}{$archive}{source};
242 $origin{subsection} = $subsect->{$suite}{$archive}{source};
243 $origin{real_archive} = $archives->{$suite}{$archive}{source};
245 $origin{binaries} = $binaries->{$suite}{$archive};
246 push @{$pkg{origins}}, \%origin;