1 package Packages::DoSearch;
6 use Benchmark ':hireswallclock';
9 our @ISA = qw( Exporter );
10 our @EXPORT = qw( do_search );
13 use Packages::Search qw( :all );
14 use Packages::CGI qw( :DEFAULT );
16 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES @ARCHITECTURES $ROOT );
19 my ($params, $opts, $page_content) = @_;
20 my $cat = $opts->{cat};
22 $Params::Search::too_many_hits = 0;
24 if ($params->{errors}{keywords}) {
25 fatal_error( $cat->g( "keyword not valid or missing" ) );
26 $opts->{keywords} = [];
27 } elsif (grep { length($_) < 2 } @{$opts->{keywords}}) {
28 fatal_error( $cat->g( "keyword too short (keywords need to have at least two characters)" ) );
31 my @keywords = @{$opts->{keywords}};
32 my $searchon = $opts->{searchon};
33 $page_content->{search_keywords} = $opts->{keywords};
34 $page_content->{all_architectures} = \@ARCHITECTURES;
35 $page_content->{all_suites} = \@SUITES;
36 $page_content->{search_architectures} = $opts->{arch};
37 $page_content->{search_suites} = $opts->{suite};
38 $page_content->{sections} = $opts->{section};
40 my $st0 = new Benchmark;
41 my (@results, @non_results);
43 unless (@Packages::CGI::fatal_errors) {
45 if ($searchon eq 'names') {
46 if ($opts->{source}) {
47 do_names_search( [ @keywords ], \%sources, $sp_obj,
48 \&read_src_entry_all, $opts,
49 \@results, \@non_results );
51 do_names_search( [ @keywords ], \%packages, $p_obj,
52 \&read_entry_all, $opts,
53 \@results, \@non_results );
56 do_names_search( [ @keywords ], \%packages, $p_obj,
57 \&read_entry_all, $opts,
58 \@results, \@non_results );
59 my $fts1 = new Benchmark;
60 do_xapian_search( [ @keywords ], "$DBDIR/xapian/",
61 \%did2pkg, \%packages,
62 \&read_entry_all, $opts,
63 \@results, \@non_results );
64 my $fts2 = new Benchmark;
65 my $fts_xapian = timediff($fts2,$fts1);
66 debug( "Fulltext search took ".timestr($fts_xapian) )
72 # debug( join( "", Dumper( \@results, \@non_results )) ) if DEBUG;
73 my $st1 = new Benchmark;
74 my $std = timediff($st1, $st0);
75 debug( "Search took ".timestr($std) ) if DEBUG;
77 $page_content->{too_many_hits} = $Packages::Search::too_many_hits;
78 #FIXME: non_results can't be compared to results since it is
79 # not normalized to unique packages
80 $page_content->{non_results} = scalar @non_results;
83 my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
85 my %sort_by_relevance;
86 for (1 ... scalar @results) {
87 # debug("$results[$_][0] => $_", 4) if DEBUG;
88 $sort_by_relevance{$results[$_-1][0]} = $_;
91 # debug( "sort_by_relevance=".Dumper(\%sort_by_relevance), 4);
93 unless ($opts->{source}) {
95 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
96 $priority, $version, $desc_md5, $desc) = @$_;
98 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
99 if ($arch ne 'virtual') {
100 $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
101 $subsect{$pkg}{$suite}{$version} = $subsection;
102 $sect{$pkg}{$suite}{$version} = $section;
103 $archives{$pkg}{$suite}{$version} ||= $archive;
105 $desc{$pkg}{$suite}{$version} = [ $desc_md5, $desc ];
107 $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
111 my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
113 if ($searchon eq 'names') {
114 @pkgs = sort keys %uniq_pkgs;
116 @pkgs = sort { $sort_by_relevance{$a} <=> $sort_by_relevance{$b} } keys %uniq_pkgs;
118 process_packages( $page_content, 'packages', \%pkgs, \@pkgs,
120 \&process_package, \%provided_by,
121 \%archives, \%sect, \%subsect,
124 } else { # unless $opts->{source}
126 my ($pkg, $archive, $suite, $section, $subsection, $priority,
129 my $real_archive = '';
130 if ($archive eq 'security') {
131 $real_archive = $archive;
134 if (($real_archive eq $archive) &&
135 $pkgs{$pkg}{$suite}{$archive} &&
136 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
139 $pkgs{$pkg}{$suite}{$archive} = $version;
140 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
141 $sect{$pkg}{$suite}{$archive}{source} = $section
142 unless $section eq 'main';
143 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
146 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
149 my @pkgs = sort keys %pkgs;
150 process_packages( $page_content, 'src_packages', \%pkgs, \@pkgs,
152 \&process_src_package, \%archives,
153 \%sect, \%subsect, \%binaries );
154 } # else unless $opts->{source}
158 sub process_packages {
159 my ($content, $target, $pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
162 $content->{results} = scalar @$pkgs_list;
165 $keyword = $keywords->[0] if @$keywords == 1;
168 if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
170 $categories[0]{name} = $opts->{cat}->g( "Exact hits" );
172 $categories[0]{$target} = [ &$print_func( $opts, $keyword,
173 $pkgs->{$keyword}||{},
174 map { $_->{$keyword}||{} } @func_args ) ];
175 @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
178 if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
180 $cat{name} = $opts->{cat}->g( 'Other hits' ) if $have_exact;
183 foreach my $pkg (@$pkgs_list) {
184 push @{$cat{$target}}, &$print_func( $opts, $pkg, $pkgs->{$pkg}||{},
185 map { $_->{$pkg}||{} } @func_args );
187 push @categories, \%cat;
188 } elsif (@$pkgs_list) {
189 $content->{skipped} = scalar @$pkgs_list;
192 $content->{categories} = \@categories;
195 sub process_package {
196 my ($opts, $pkg, $pkgs, $provided_by,
197 $archives, $sect, $subsect, $desc) = @_;
199 my %pkg = ( pkg => $pkg,
202 foreach my $suite (@SUITES) {
203 my %suite = ( suite => $suite );
204 if (exists $pkgs->{$suite}) {
206 my @versions = version_sort keys %{$pkgs->{$suite}};
207 $suite{section} = $sect->{$suite}{$versions[0]};
208 $suite{subsection} = $subsect->{$suite}{$versions[0]};
209 my $desc_md5 = $desc->{$suite}{$versions[0]}[0];
210 $suite{desc} = $desc->{$suite}{$versions[0]}[1];
211 $suite{versions} = [];
213 my $trans_desc = $desctrans{$desc_md5};
216 my %trans_desc = split /\000|\001/, $trans_desc;
217 while (my ($l, $d) = each %trans_desc) {
222 $suite{trans_desc} = \%sdescs;
225 foreach my $v (@versions) {
227 $version{version} = $v;
228 $version{archive} = $archives->{$suite}{$v};
230 $version{architectures} = [ grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}} ];
231 push @{$suite{versions}}, \%version if @{$version{architectures}};
233 $archs_printed{$_}++ foreach @{$version{architectures}};
235 if (my $p = $provided_by->{$suite}) {
236 $suite{providers} = $p;
238 } elsif (my $p = $provided_by->{$suite}) {
239 $suite{desc} = $opts->{cat}->g('Virtual package');
240 $suite{providers} = $p;
242 push @{$pkg{suites}}, \%suite if $suite{versions} || $suite{providers};
248 sub process_src_package {
249 my ($opts, $pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
251 my %pkg = ( pkg => $pkg,
254 foreach my $suite (@SUITES) {
255 foreach my $archive (@ARCHIVES) {
256 if (exists $pkgs->{$suite}{$archive}) {
258 $origin{version} = $pkgs->{$suite}{$archive};
259 $origin{suite} = $suite;
260 $origin{archive} = $archive;
261 $origin{section} = $sect->{$suite}{$archive}{source};
262 $origin{subsection} = $subsect->{$suite}{$archive}{source};
263 $origin{real_archive} = $archives->{$suite}{$archive}{source};
265 $origin{binaries} = $binaries->{$suite}{$archive};
266 push @{$pkg{origins}}, \%origin;