]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoSearch.pm
The Big, the Fat and the Ugly commit ;)
[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 msg );
18 use Packages::DB;
19 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES $ROOT );
20
21 sub do_search {
22     my ($params, $opts, $html_header, $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
36     # for URL construction
37     my $keyword_esc = uri_escape( "@keywords" );
38     $opts->{keywords_esc} = $keyword_esc;
39
40     # for output
41     my $keyword_enc = encode_entities "@keywords" || '';
42     my $searchon_enc = encode_entities $searchon;
43     my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
44     my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
45     my $archs_enc = encode_entities( join( ', ',  @{$params->{values}{arch}{no_replace}} ) );
46     
47     my $st0 = new Benchmark;
48     my (@results, @non_results);
49
50     unless (@Packages::CGI::fatal_errors) {
51
52         if ($searchon eq 'names') {
53             if ($opts->{source}) {
54                 do_names_search( [ @keywords ], \%sources, $sp_obj,
55                                  \&read_src_entry_all, $opts,
56                                  \@results, \@non_results );
57             } else {
58                 do_names_search( [ @keywords ], \%packages, $p_obj,
59                                  \&read_entry_all, $opts,
60                                  \@results, \@non_results );
61             }
62         } else {
63             do_names_search( [ @keywords ], \%packages, $p_obj,
64                              \&read_entry_all, $opts,
65                              \@results, \@non_results );
66             do_fulltext_search( [ @keywords ], "$DBDIR/descriptions.txt",
67                                 \%did2pkg, \%packages,
68                                 \&read_entry_all, $opts,
69                                 \@results, \@non_results );
70         }
71     }
72     
73 #    use Data::Dumper;
74 #    debug( join( "", Dumper( \@results, \@non_results )) ) if DEBUG;
75     my $st1 = new Benchmark;
76     my $std = timediff($st1, $st0);
77     debug( "Search took ".timestr($std) ) if DEBUG;
78     
79     my $suite_wording = $suites_enc =~ /^(default|all)$/ ? _g("all suites")
80         : sprintf(_g("suite(s) <em>%s</em>", $suites_enc) );
81     my $section_wording = $sections_enc eq 'all' ? _g("all sections")
82         : sprintf(_g("section(s) <em>%s</em>", $sections_enc) );
83     my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
84         : sprintf(_g("architecture(s) <em>%s</em>", $archs_enc) );
85     if ($searchon eq "names") {
86         my $source_wording = $opts->{source} ? _g("source packages") : _g("packages");
87         # sorry to all translators for that one... (patches welcome)
88         msg( sprintf( _g( "You have searched for %s that names contain <em>%s</em> in %s, %s, and %s." ),
89                       $source_wording, $keyword_enc,
90                       $suite_wording, $section_wording, $arch_wording ) );
91     } else {
92         my $exact_wording = $opts->{exact} ? "" : _g(" (including subword matching)");
93         msg( sprintf( _g( "You have searched for <em>%s</em> in packages names and descriptions in %s, %s, and %s%s." ),
94                       $keyword_enc,
95                       $suite_wording, $section_wording, $arch_wording,
96                       $exact_wording ) );
97     }
98
99     if ($Packages::Search::too_many_hits) {
100         error( sprintf( _g( "Your search was too wide so we will only display exact matches. At least <em>%s</em> results have been omitted and will not be displayed. Please consider using a longer keyword or more keywords." ), $Packages::Search::too_many_hits ) );
101     }
102     
103     if (!@Packages::CGI::fatal_errors && !@results) {
104         if ($searchon eq "names") {
105             unless (@non_results) {
106                 error( _g( "Can't find that package." ) );
107             } else {
108 #               hint( _g( "Can't find that package." )." ".
109 #                     sprintf( _g( '<a href="%s">%s</a>'.
110 #                     " results have not been displayed due to the".
111 #                     " search parameters." ), "$SEARCH_URL/$keyword_esc" ,
112 #                     $#non_results+1 ) );
113             }
114             
115         } else {
116             if (($suites_enc eq 'all')
117                 && ($archs_enc eq 'any')
118                 && ($sections_enc eq 'all')) {
119                 error( _g( "Can't find that string." ) );
120             } else {
121                 error( sprintf( _g( "Can't find that string, at least not in that suite (%s, section %s) and on that architecture (%s)." ),
122                                 $suites_enc, $sections_enc, $archs_enc ) );
123             }
124             
125             if ($opts->{exact}) {
126                 hint( sprintf( _g( 'You have searched only for words exactly matching your keywords. You can try to search <a href="%s">allowing subword matching</a>.' ),
127                                encode_entities(make_search_url('',"keywords=$keyword_esc",{exact => 0})) ) );
128             }
129         }
130 #       hint( sprintf( _g( 'You can try a different search on the <a href="%s">Packages search page</a>.' ), "$SEARCH_PAGE#search_packages" ) );
131         
132     }
133
134     $page_content->{make_url} = sub { return &Packages::CGI::make_url(@_) };
135     $page_content->{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
136
137     $page_content->{search_field_values} = { 
138         keywords => $keyword_enc,
139         searchon => $opts->{searchon_form},
140         arch => $archs_enc,
141         suite => $suites_enc,
142         section => $sections_enc,
143         exact => $opts->{exact},
144         debug => $opts->{debug},
145     };
146
147     if (@results) {
148         my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
149
150         unless ($opts->{source}) {
151             foreach (@results) {
152                 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
153                     $priority, $version, $desc) = @$_;
154                 
155                 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
156                 if ($arch ne 'virtual') {
157                     $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
158                     $subsect{$pkg}{$suite}{$version} = $subsection;
159                     $sect{$pkg}{$suite}{$version} = $section
160                         unless $section eq 'main';
161                     $archives{$pkg}{$suite}{$version} ||= $archive;
162                     
163                     $desc{$pkg}{$suite}{$version} = $desc;
164                 } else {
165                     $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
166                 }
167             }
168
169             my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
170             my @pkgs = sort keys %uniq_pkgs;
171             process_packages( $page_content, 'packages', \%pkgs, \@pkgs, $opts, \@keywords,
172                               \&process_package, \%provided_by,
173                               \%archives, \%sect, \%subsect,
174                               \%desc );
175
176         } else { # unless $opts->{source}
177             foreach (@results) {
178                 my ($pkg, $archive, $suite, $section, $subsection, $priority,
179                     $version) = @$_;
180                 
181                 my $real_archive = '';
182                 if ($archive =~ /^(security|non-US)$/) {
183                     $real_archive = $archive;
184                     $archive = 'us';
185                 }
186                 if (($real_archive eq $archive) &&
187                     $pkgs{$pkg}{$suite}{$archive} &&
188                     (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
189                     next;
190                 }
191                 $pkgs{$pkg}{$suite}{$archive} = $version;
192                 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
193                 $sect{$pkg}{$suite}{$archive}{source} = $section
194                     unless $section eq 'main';
195                 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
196                     if $real_archive;
197
198                 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
199             }
200
201             my @pkgs = sort keys %pkgs;
202             process_packages( $page_content, 'src_packages', \%pkgs, \@pkgs, $opts, \@keywords,
203                               \&process_src_package, \%archives,
204                               \%sect, \%subsect, \%binaries );
205         } # else unless $opts->{source}
206     } # if @results
207 } # sub do_search
208
209 sub process_packages {
210     my ($content, $target, $pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
211
212     my @categories;
213     $content->{results} = scalar @$pkgs_list;
214
215     my $keyword;
216     $keyword = $keywords->[0] if @$keywords == 1;
217             
218     my $have_exact;
219     if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
220         $have_exact = 1;
221         $categories[0]{name} = _g( "Exact hits" );
222
223         $categories[0]{$target} = [ &$print_func( $keyword, $pkgs->{$keyword}||{},
224                                                    map { $_->{$keyword}||{} } @func_args ) ];
225         @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
226     }
227             
228     if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
229         my %cat;
230         $cat{name} = _g( 'Other hits' ) if $have_exact;
231         
232         $cat{packages} = [];
233         foreach my $pkg (@$pkgs_list) {
234             push @{$cat{$target}}, &$print_func( $pkg, $pkgs->{$pkg}||{},
235                                                  map { $_->{$pkg}||{} } @func_args );
236         }
237         push @categories, \%cat;
238     } elsif (@$pkgs_list) {
239         $content->{skipped} = scalar @$pkgs_list;
240     }
241
242     $content->{categories} = \@categories;
243 }
244
245 sub process_package {
246     my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
247
248     my %pkg = ( pkg => $pkg,
249                 suites => [] );
250
251     foreach my $suite (@SUITES) {
252         my %suite = ( suite => $suite );
253         if (exists $pkgs->{$suite}) {
254             my %archs_printed;
255             my @versions = version_sort keys %{$pkgs->{$suite}};
256             $suite{section} = $sect->{$suite}{$versions[0]};
257             $suite{subsection} = $subsect->{$suite}{$versions[0]};
258             $suite{desc} = $desc->{$suite}{$versions[0]};
259             $suite{versions} = [];
260                 
261             foreach my $v (@versions) {
262                 my %version;
263                 $version{version} = $v;
264                 $version{archive} = $archives->{$suite}{$v};
265                     
266                 $version{architectures} = [ grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}} ];
267                 push @{$suite{versions}}, \%version if @{$version{architectures}};
268
269                 $archs_printed{$_}++ foreach @{$version{architectures}};
270             }
271             if (my $p =  $provided_by->{$suite}) {
272                 $suite{providers} = $p;
273             }
274         } elsif (my $p =  $provided_by->{$suite}) {
275             $suite{desc} = _g('Virtual package');
276             $suite{providers} = $p;
277         }
278         push @{$pkg{suites}}, \%suite if $suite{versions} || $suite{providers};
279     }
280
281     return \%pkg;
282 }
283
284 sub process_src_package {
285     my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
286
287     my %pkg = ( pkg => $pkg,
288                 origins => [] );
289
290     foreach my $suite (@SUITES) {
291         foreach my $archive (@ARCHIVES) {
292             if (exists $pkgs->{$suite}{$archive}) {
293                 my %origin;
294                 $origin{version} = $pkgs->{$suite}{$archive};
295                 $origin{suite} = $suite;
296                 $origin{archive} = $archive; 
297                 $origin{section} = $sect->{$suite}{$archive}{source};
298                 $origin{subsection} = $subsect->{$suite}{$archive}{source};
299                 $origin{real_archive} = $archives->{$suite}{$archive}{source};
300
301                 $origin{binaries} = $binaries->{$suite}{$archive};
302                 push @{$pkg{origins}}, \%origin;
303             }
304         }
305     }
306
307     return \%pkg;
308 }
309
310 1;