]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoSearch.pm
Merge commit 'origin/master' into debian-master
[deb/packages.git] / lib / Packages / DoSearch.pm
1 package Packages::DoSearch;
2
3 use strict;
4 use warnings;
5
6 use Benchmark ':hireswallclock';
7 use DB_File;
8 use URI::Escape;
9 use HTML::Entities;
10 use Exporter;
11 our @ISA = qw( Exporter );
12 our @EXPORT = qw( do_search );
13
14 use Deb::Versions;
15 use Packages::I18N::Locale;
16 use Packages::Search qw( :all );
17 use Packages::CGI qw( :DEFAULT );
18 use Packages::DB;
19 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES $ROOT );
20
21 sub do_search {
22     my ($params, $opts, $page_content) = @_;
23
24     $Params::Search::too_many_hits = 0;
25
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)" ) );
31     }
32
33     my @keywords = @{$opts->{keywords}};
34     my $searchon = $opts->{searchon};
35     $page_content->{search_keywords} = \@keywords;
36
37     my $st0 = new Benchmark;
38     my (@results, @non_results);
39
40     unless (@Packages::CGI::fatal_errors) {
41
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 );
47             } else {
48                 do_names_search( [ @keywords ], \%packages, $p_obj,
49                                  \&read_entry_all, $opts,
50                                  \@results, \@non_results );
51             }
52         } else {
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) )
64                 if DEBUG;
65         }
66     }
67
68 #    use Data::Dumper;
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;
73
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;
78
79     if (@results) {
80         my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
81
82         my %sort_by_relevance;
83         for (1 ... scalar @results) {
84 #           debug("$results[$_][0] => $_", 4) if DEBUG;
85             $sort_by_relevance{$results[$_-1][0]} = $_;
86         }
87 #       use Data::Dumper;
88 #       debug( "sort_by_relevance=".Dumper(\%sort_by_relevance), 4);
89
90         unless ($opts->{source}) {
91             foreach (@results) {
92                 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
93                     $priority, $version, $desc) = @$_;
94
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;
101
102                     $desc{$pkg}{$suite}{$version} = $desc;
103                 } else {
104                     $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
105                 }
106             }
107
108             my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
109             my @pkgs;
110             if ($searchon eq 'names') {
111                 @pkgs = sort keys %uniq_pkgs;
112             } else {
113                 @pkgs = sort { $sort_by_relevance{$a} <=> $sort_by_relevance{$b} } keys %uniq_pkgs;
114             }
115             process_packages( $page_content, 'packages', \%pkgs, \@pkgs, $opts, \@keywords,
116                               \&process_package, \%provided_by,
117                               \%archives, \%sect, \%subsect,
118                               \%desc );
119
120         } else { # unless $opts->{source}
121             foreach (@results) {
122                 my ($pkg, $archive, $suite, $section, $subsection, $priority,
123                     $version) = @$_;
124
125                 my $real_archive = '';
126                 if ($archive =~ /^(security|non-US)$/) {
127                     $real_archive = $archive;
128                     $archive = 'us';
129                 }
130                 if (($real_archive eq $archive) &&
131                     $pkgs{$pkg}{$suite}{$archive} &&
132                     (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
133                     next;
134                 }
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
140                     if $real_archive;
141
142                 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
143             }
144
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}
150     } # if @results
151 } # sub do_search
152
153 sub process_packages {
154     my ($content, $target, $pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
155
156     my @categories;
157     $content->{results} = scalar @$pkgs_list;
158
159     my $keyword;
160     $keyword = $keywords->[0] if @$keywords == 1;
161             
162     my $have_exact;
163     if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
164         $have_exact = 1;
165         $categories[0]{name} = _g( "Exact hits" );
166
167         $categories[0]{$target} = [ &$print_func( $keyword, $pkgs->{$keyword}||{},
168                                                    map { $_->{$keyword}||{} } @func_args ) ];
169         @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
170     }
171             
172     if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
173         my %cat;
174         $cat{name} = _g( 'Other hits' ) if $have_exact;
175         
176         $cat{packages} = [];
177         foreach my $pkg (@$pkgs_list) {
178             push @{$cat{$target}}, &$print_func( $pkg, $pkgs->{$pkg}||{},
179                                                  map { $_->{$pkg}||{} } @func_args );
180         }
181         push @categories, \%cat;
182     } elsif (@$pkgs_list) {
183         $content->{skipped} = scalar @$pkgs_list;
184     }
185
186     $content->{categories} = \@categories;
187 }
188
189 sub process_package {
190     my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
191
192     my %pkg = ( pkg => $pkg,
193                 suites => [] );
194
195     foreach my $suite (@SUITES) {
196         my %suite = ( suite => $suite );
197         if (exists $pkgs->{$suite}) {
198             my %archs_printed;
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} = [];
204                 
205             foreach my $v (@versions) {
206                 my %version;
207                 $version{version} = $v;
208                 $version{archive} = $archives->{$suite}{$v};
209                     
210                 $version{architectures} = [ grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}} ];
211                 push @{$suite{versions}}, \%version if @{$version{architectures}};
212
213                 $archs_printed{$_}++ foreach @{$version{architectures}};
214             }
215             if (my $p =  $provided_by->{$suite}) {
216                 $suite{providers} = $p;
217             }
218         } elsif (my $p =  $provided_by->{$suite}) {
219             $suite{desc} = _g('Virtual package');
220             $suite{providers} = $p;
221         }
222         push @{$pkg{suites}}, \%suite if $suite{versions} || $suite{providers};
223     }
224
225     return \%pkg;
226 }
227
228 sub process_src_package {
229     my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
230
231     my %pkg = ( pkg => $pkg,
232                 origins => [] );
233
234     foreach my $suite (@SUITES) {
235         foreach my $archive (@ARCHIVES) {
236             if (exists $pkgs->{$suite}{$archive}) {
237                 my %origin;
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};
244
245                 $origin{binaries} = $binaries->{$suite}{$archive};
246                 push @{$pkg{origins}}, \%origin;
247             }
248         }
249     }
250
251     return \%pkg;
252 }
253
254 1;