]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoSearch.pm
Unfuzzy sections.de.po
[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 ($pkgs{$pkg}{$suite}{$archive} &&
135                     (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
136                     next;
137                 }
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
143                     if $real_archive;
144
145                 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
146             }
147
148             my @pkgs = sort keys %pkgs;
149             process_packages( $page_content, 'src_packages', \%pkgs, \@pkgs,
150                               $opts, \@keywords,
151                               \&process_src_package, \%archives,
152                               \%sect, \%subsect, \%binaries );
153         } # else unless $opts->{source}
154     } # if @results
155 } # sub do_search
156
157 sub process_packages {
158     my ($content, $target, $pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
159
160     my @categories;
161     $content->{results} = scalar @$pkgs_list;
162
163     my $keyword;
164     $keyword = $keywords->[0] if @$keywords == 1;
165             
166     my $have_exact;
167     if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
168         $have_exact = 1;
169         $categories[0]{name} = $opts->{cat}->g( "Exact hits" );
170
171         $categories[0]{$target} = [ &$print_func( $opts, $keyword,
172                                                   $pkgs->{$keyword}||{},
173                                                   map { $_->{$keyword}||{} } @func_args ) ];
174         @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
175     }
176             
177     if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
178         my %cat;
179         $cat{name} = $opts->{cat}->g( 'Other hits' ) if $have_exact;
180         
181         $cat{packages} = [];
182         foreach my $pkg (@$pkgs_list) {
183             push @{$cat{$target}}, &$print_func( $opts, $pkg, $pkgs->{$pkg}||{},
184                                                  map { $_->{$pkg}||{} } @func_args );
185         }
186         push @categories, \%cat;
187     } elsif (@$pkgs_list) {
188         $content->{skipped} = scalar @$pkgs_list;
189     }
190
191     $content->{categories} = \@categories;
192 }
193
194 sub process_package {
195     my ($opts, $pkg, $pkgs, $provided_by,
196         $archives, $sect, $subsect, $desc) = @_;
197
198     my %pkg = ( pkg => $pkg,
199                 suites => [] );
200
201     foreach my $suite (@SUITES) {
202         my %suite = ( suite => $suite );
203         if (exists $pkgs->{$suite}) {
204             my %archs_printed;
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} = [];
211
212             my $trans_desc = $desctrans{$desc_md5};
213             my %sdescs;
214             if ($trans_desc) {
215                 my %trans_desc = split /\000|\001/, $trans_desc;
216                 while (my ($l, $d) = each %trans_desc) {
217                     $d =~ s/\n.*//os;
218
219                     $sdescs{$l} = $d;
220                 }
221                 $suite{trans_desc} = \%sdescs;
222             }
223
224             foreach my $v (@versions) {
225                 my %version;
226                 $version{version} = $v;
227                 $version{archive} = $archives->{$suite}{$v};
228                     
229                 $version{architectures} = [ grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}} ];
230                 push @{$suite{versions}}, \%version if @{$version{architectures}};
231
232                 $archs_printed{$_}++ foreach @{$version{architectures}};
233             }
234             if (my $p =  $provided_by->{$suite}) {
235                 $suite{providers} = $p;
236             }
237         } elsif (my $p =  $provided_by->{$suite}) {
238             $suite{desc} = $opts->{cat}->g('Virtual package');
239             $suite{providers} = $p;
240         }
241         push @{$pkg{suites}}, \%suite if $suite{versions} || $suite{providers};
242     }
243
244     return \%pkg;
245 }
246
247 sub process_src_package {
248     my ($opts, $pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
249
250     my %pkg = ( pkg => $pkg,
251                 origins => [] );
252
253     foreach my $suite (@SUITES) {
254         foreach my $archive (@ARCHIVES) {
255             if (exists $pkgs->{$suite}{$archive}) {
256                 my %origin;
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};
263
264                 $origin{binaries} = $binaries->{$suite}{$archive};
265                 push @{$pkg{origins}}, \%origin;
266             }
267         }
268     }
269
270     return \%pkg;
271 }
272
273 1;