]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoSearch.pm
Merge branch 'master' into ubuntu-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 Exporter;
9 our @ISA = qw( Exporter );
10 our @EXPORT = qw( do_search );
11
12 use Deb::Versions;
13 use Packages::Search qw( :all );
14 use Packages::CGI qw( :DEFAULT );
15 use Packages::DB;
16 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES @ARCHITECTURES $ROOT );
17
18 sub do_search {
19     my ($params, $opts, $page_content) = @_;
20     my $cat = $opts->{cat};
21
22     $Params::Search::too_many_hits = 0;
23
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)" ) );
29     }
30
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};
39
40     my $st0 = new Benchmark;
41     my (@results, @non_results);
42
43     unless (@Packages::CGI::fatal_errors) {
44
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 );
50             } else {
51                 do_names_search( [ @keywords ], \%packages, $p_obj,
52                                  \&read_entry_all, $opts,
53                                  \@results, \@non_results );
54             }
55         } else {
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) )
67                 if DEBUG;
68         }
69     }
70
71 #    use Data::Dumper;
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;
76
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;
81
82     if (@results) {
83         my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
84
85         my %sort_by_relevance;
86         for (1 ... scalar @results) {
87 #           debug("$results[$_][0] => $_", 4) if DEBUG;
88             $sort_by_relevance{$results[$_-1][0]} = $_;
89         }
90 #       use Data::Dumper;
91 #       debug( "sort_by_relevance=".Dumper(\%sort_by_relevance), 4);
92
93         unless ($opts->{source}) {
94             foreach (@results) {
95                 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
96                     $priority, $version, $desc_md5, $desc) = @$_;
97
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;
104
105                     $desc{$pkg}{$suite}{$version} = [ $desc_md5, $desc ];
106                 } else {
107                     $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
108                 }
109             }
110
111             my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
112             my @pkgs;
113             if ($searchon eq 'names') {
114                 @pkgs = sort keys %uniq_pkgs;
115             } else {
116                 @pkgs = sort { $sort_by_relevance{$a} <=> $sort_by_relevance{$b} } keys %uniq_pkgs;
117             }
118             process_packages( $page_content, 'packages', \%pkgs, \@pkgs,
119                               $opts, \@keywords,
120                               \&process_package, \%provided_by,
121                               \%archives, \%sect, \%subsect,
122                               \%desc );
123
124         } else { # unless $opts->{source}
125             foreach (@results) {
126                 my ($pkg, $archive, $suite, $section, $subsection, $priority,
127                     $version) = @$_;
128
129                 my $real_archive = '';
130                 if ($archive eq 'security') {
131                     $real_archive = $archive;
132                     $archive = 'us';
133                 }
134                 if (($real_archive eq $archive) &&
135                     $pkgs{$pkg}{$suite}{$archive} &&
136                     (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
137                     next;
138                 }
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
144                     if $real_archive;
145
146                 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
147             }
148
149             my @pkgs = sort keys %pkgs;
150             process_packages( $page_content, 'src_packages', \%pkgs, \@pkgs,
151                               $opts, \@keywords,
152                               \&process_src_package, \%archives,
153                               \%sect, \%subsect, \%binaries );
154         } # else unless $opts->{source}
155     } # if @results
156 } # sub do_search
157
158 sub process_packages {
159     my ($content, $target, $pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
160
161     my @categories;
162     $content->{results} = scalar @$pkgs_list;
163
164     my $keyword;
165     $keyword = $keywords->[0] if @$keywords == 1;
166             
167     my $have_exact;
168     if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
169         $have_exact = 1;
170         $categories[0]{name} = $opts->{cat}->g( "Exact hits" );
171
172         $categories[0]{$target} = [ &$print_func( $opts, $keyword,
173                                                   $pkgs->{$keyword}||{},
174                                                   map { $_->{$keyword}||{} } @func_args ) ];
175         @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
176     }
177             
178     if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
179         my %cat;
180         $cat{name} = $opts->{cat}->g( 'Other hits' ) if $have_exact;
181         
182         $cat{packages} = [];
183         foreach my $pkg (@$pkgs_list) {
184             push @{$cat{$target}}, &$print_func( $opts, $pkg, $pkgs->{$pkg}||{},
185                                                  map { $_->{$pkg}||{} } @func_args );
186         }
187         push @categories, \%cat;
188     } elsif (@$pkgs_list) {
189         $content->{skipped} = scalar @$pkgs_list;
190     }
191
192     $content->{categories} = \@categories;
193 }
194
195 sub process_package {
196     my ($opts, $pkg, $pkgs, $provided_by,
197         $archives, $sect, $subsect, $desc) = @_;
198
199     my %pkg = ( pkg => $pkg,
200                 suites => [] );
201
202     foreach my $suite (@SUITES) {
203         my %suite = ( suite => $suite );
204         if (exists $pkgs->{$suite}) {
205             my %archs_printed;
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} = [];
212
213             my $trans_desc = $desctrans{$desc_md5};
214             my %sdescs;
215             if ($trans_desc) {
216                 my %trans_desc = split /\000|\001/, $trans_desc;
217                 while (my ($l, $d) = each %trans_desc) {
218                     $d =~ s/\n.*//os;
219
220                     $sdescs{$l} = $d;
221                 }
222                 $suite{trans_desc} = \%sdescs;
223             }
224
225             foreach my $v (@versions) {
226                 my %version;
227                 $version{version} = $v;
228                 $version{archive} = $archives->{$suite}{$v};
229                     
230                 $version{architectures} = [ grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}} ];
231                 push @{$suite{versions}}, \%version if @{$version{architectures}};
232
233                 $archs_printed{$_}++ foreach @{$version{architectures}};
234             }
235             if (my $p =  $provided_by->{$suite}) {
236                 $suite{providers} = $p;
237             }
238         } elsif (my $p =  $provided_by->{$suite}) {
239             $suite{desc} = $opts->{cat}->g('Virtual package');
240             $suite{providers} = $p;
241         }
242         push @{$pkg{suites}}, \%suite if $suite{versions} || $suite{providers};
243     }
244
245     return \%pkg;
246 }
247
248 sub process_src_package {
249     my ($opts, $pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
250
251     my %pkg = ( pkg => $pkg,
252                 origins => [] );
253
254     foreach my $suite (@SUITES) {
255         foreach my $archive (@ARCHIVES) {
256             if (exists $pkgs->{$suite}{$archive}) {
257                 my %origin;
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};
264
265                 $origin{binaries} = $binaries->{$suite}{$archive};
266                 push @{$pkg{origins}}, \%origin;
267             }
268         }
269     }
270
271     return \%pkg;
272 }
273
274 1;