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 ($pkgs{$pkg}{$suite}{$archive} &&
135 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
138 $pkgs{$pkg}{$suite}{$archive} = $version;
139 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
140 $sect{$pkg}{$suite}{$archive}{source} = $section
141 unless $section eq 'main';
142 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
145 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
148 my @pkgs = sort keys %pkgs;
149 process_packages( $page_content, 'src_packages', \%pkgs, \@pkgs,
151 \&process_src_package, \%archives,
152 \%sect, \%subsect, \%binaries );
153 } # else unless $opts->{source}
157 sub process_packages {
158 my ($content, $target, $pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
161 $content->{results} = scalar @$pkgs_list;
164 $keyword = $keywords->[0] if @$keywords == 1;
167 if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
169 $categories[0]{name} = $opts->{cat}->g( "Exact hits" );
171 $categories[0]{$target} = [ &$print_func( $opts, $keyword,
172 $pkgs->{$keyword}||{},
173 map { $_->{$keyword}||{} } @func_args ) ];
174 @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
177 if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
179 $cat{name} = $opts->{cat}->g( 'Other hits' ) if $have_exact;
182 foreach my $pkg (@$pkgs_list) {
183 push @{$cat{$target}}, &$print_func( $opts, $pkg, $pkgs->{$pkg}||{},
184 map { $_->{$pkg}||{} } @func_args );
186 push @categories, \%cat;
187 } elsif (@$pkgs_list) {
188 $content->{skipped} = scalar @$pkgs_list;
191 $content->{categories} = \@categories;
194 sub process_package {
195 my ($opts, $pkg, $pkgs, $provided_by,
196 $archives, $sect, $subsect, $desc) = @_;
198 my %pkg = ( pkg => $pkg,
201 foreach my $suite (@SUITES) {
202 my %suite = ( suite => $suite );
203 if (exists $pkgs->{$suite}) {
205 my @versions = version_sort keys %{$pkgs->{$suite}};
206 $suite{section} = $sect->{$suite}{$versions[0]};
207 $suite{subsection} = $subsect->{$suite}{$versions[0]};
208 my $desc_md5 = $desc->{$suite}{$versions[0]}[0];
209 $suite{desc} = $desc->{$suite}{$versions[0]}[1];
210 $suite{versions} = [];
212 my $trans_desc = $desctrans{$desc_md5};
215 my %trans_desc = split /\000|\001/, $trans_desc;
216 while (my ($l, $d) = each %trans_desc) {
221 $suite{trans_desc} = \%sdescs;
224 foreach my $v (@versions) {
226 $version{version} = $v;
227 $version{archive} = $archives->{$suite}{$v};
229 $version{architectures} = [ grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}} ];
230 push @{$suite{versions}}, \%version if @{$version{architectures}};
232 $archs_printed{$_}++ foreach @{$version{architectures}};
234 if (my $p = $provided_by->{$suite}) {
235 $suite{providers} = $p;
237 } elsif (my $p = $provided_by->{$suite}) {
238 $suite{desc} = $opts->{cat}->g('Virtual package');
239 $suite{providers} = $p;
241 push @{$pkg{suites}}, \%suite if $suite{versions} || $suite{providers};
247 sub process_src_package {
248 my ($opts, $pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
250 my %pkg = ( pkg => $pkg,
253 foreach my $suite (@SUITES) {
254 foreach my $archive (@ARCHIVES) {
255 if (exists $pkgs->{$suite}{$archive}) {
257 $origin{version} = $pkgs->{$suite}{$archive};
258 $origin{suite} = $suite;
259 $origin{archive} = $archive;
260 $origin{section} = $sect->{$suite}{$archive}{source};
261 $origin{subsection} = $subsect->{$suite}{$archive}{source};
262 $origin{real_archive} = $archives->{$suite}{$archive}{source};
264 $origin{binaries} = $binaries->{$suite}{$archive};
265 push @{$pkg{origins}}, \%origin;