]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoSearch.pm
Fix some smaller issues reported by Thijs Kinkhorst
[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;
18 use Packages::DB;
19 use Packages::HTML qw(marker);
20 use Packages::Config qw( $DBDIR $SEARCH_URL $SEARCH_PAGE
21                          @SUITES @ARCHIVES $ROOT );
22 use Packages::HTML;
23
24 sub do_search {
25     my ($params, $opts, $html_header, $menu, $page_content) = @_;
26
27     $Params::Search::too_many_hits = 0;
28
29     if ($params->{errors}{keywords}) {
30         fatal_error( _g( "keyword not valid or missing" ) );
31         $opts->{keywords} = [];
32     } elsif (grep { length($_) < 2 } @{$opts->{keywords}}) {
33         fatal_error( _g( "keyword too short (keywords need to have at least two characters)" ) );
34     }
35
36     $$menu = "";
37     
38     my @keywords = @{$opts->{keywords}};
39     my $searchon = $opts->{searchon};
40
41     # for URL construction
42     my $keyword_esc = uri_escape( "@keywords" );
43     $opts->{keywords_esc} = $keyword_esc;
44
45     # for output
46     my $keyword_enc = encode_entities "@keywords" || '';
47     my $searchon_enc = encode_entities $searchon;
48     my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
49     my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
50     my $archs_enc = encode_entities( join( ', ',  @{$params->{values}{arch}{no_replace}} ) );
51     
52     my $st0 = new Benchmark;
53     my (@results, @non_results);
54
55     unless (@Packages::CGI::fatal_errors) {
56
57         if ($searchon eq 'names') {
58             if ($opts->{source}) {
59                 do_names_search( \@keywords, \%sources, $sp_obj,
60                                  \&read_src_entry_all, $opts,
61                                  \@results, \@non_results );
62             } else {
63                 do_names_search( \@keywords, \%packages, $p_obj,
64                                  \&read_entry_all, $opts,
65                                  \@results, \@non_results );
66             }
67         } else {
68             do_names_search( \@keywords, \%packages, $p_obj,
69                              \&read_entry_all, $opts,
70                              \@results, \@non_results );
71             do_fulltext_search( \@keywords, "$DBDIR/descriptions.txt",
72                                 \%did2pkg, \%packages,
73                                 \&read_entry_all, $opts,
74                                 \@results, \@non_results );
75         }
76     }
77     
78 #    use Data::Dumper;
79 #    debug( join( "", Dumper( \@results, \@non_results )) ) if DEBUG;
80     my $st1 = new Benchmark;
81     my $std = timediff($st1, $st0);
82     debug( "Search took ".timestr($std) ) if DEBUG;
83     
84     my $suite_wording = $suites_enc =~ /^(default|all)$/ ? _g("all suites")
85         : sprintf(_g("suite(s) <em>%s</em>", $suites_enc) );
86     my $section_wording = $sections_enc eq 'all' ? _g("all sections")
87         : sprintf(_g("section(s) <em>%s</em>", $sections_enc) );
88     my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
89         : sprintf(_g("architecture(s) <em>%s</em>", $archs_enc) );
90     if ($searchon eq "names") {
91         my $source_wording = $opts->{source} ? _g("source packages") : _g("packages");
92         # sorry to all translators for that one... (patches welcome)
93         msg( sprintf( _g( "You have searched for %s that names contain <em>%s</em> in %s, %s, and %s." ),
94                       $source_wording, $keyword_enc,
95                       $suite_wording, $section_wording, $arch_wording ) );
96     } else {
97         my $exact_wording = $opts->{exact} ? "" : _g(" (including subword matching)");
98         msg( sprintf( _g( "You have searched for <em>%s</em> in packages names and descriptions in %s, %s, and %s%s." ),
99                       $keyword_enc,
100                       $suite_wording, $section_wording, $arch_wording,
101                       $exact_wording ) );
102     }
103
104     if ($Packages::Search::too_many_hits) {
105         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 ) );
106     }
107     
108     if (!@Packages::CGI::fatal_errors && !@results) {
109         if ($searchon eq "names") {
110             unless (@non_results) {
111                 error( _g( "Can't find that package." ) );
112             } else {
113                 hint( _g( "Can't find that package." )." ".
114                       sprintf( _g( '<a href="%s">%s</a>'.
115                       " results have not been displayed due to the".
116                       " search parameters." ), "$SEARCH_URL/$keyword_esc" ,
117                       $#non_results+1 ) );
118             }
119             
120         } else {
121             if (($suites_enc eq 'all')
122                 && ($archs_enc eq 'any')
123                 && ($sections_enc eq 'all')) {
124                 error( _g( "Can't find that string." ) );
125             } else {
126                 error( sprintf( _g( "Can't find that string, at least not in that suite (%s, section %s) and on that architecture (%s)." ),
127                                 $suites_enc, $sections_enc, $archs_enc ) );
128             }
129             
130             if ($opts->{exact}) {
131                 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>.' ),
132                                encode_entities(make_search_url('',"keywords=$keyword_esc",{exact => 0})) ) );
133             }
134         }
135         hint( sprintf( _g( 'You can try a different search on the <a href="%s">Packages search page</a>.' ), "$SEARCH_PAGE#search_packages" ) );
136         
137     }
138
139     %$html_header = ( title => _g( 'Package Search Results' ) ,
140                       lang => $opts->{lang},
141                       title_tag => _g( 'Debian Package Search Results' ),
142                       print_title => 1,
143                       print_search_field => 'packages',
144                       search_field_values => { 
145                           keywords => $keyword_enc,
146                           searchon => $opts->{searchon_form},
147                           arch => $archs_enc,
148                           suite => $suites_enc,
149                           section => $sections_enc,
150                           exact => $opts->{exact},
151                           debug => $opts->{debug},
152                       },
153                       );
154
155     $$page_content = '';
156     if (@results) {
157         my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
158
159         unless ($opts->{source}) {
160             foreach (@results) {
161                 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
162                     $priority, $version, $desc) = @$_;
163                 
164                 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
165                 if ($arch ne 'virtual') {
166                     $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
167                     $subsect{$pkg}{$suite}{$version} = $subsection;
168                     $sect{$pkg}{$suite}{$version} = $section
169                         unless $section eq 'main';
170                     $archives{$pkg}{$suite}{$version} ||= $archive;
171                     
172                     $desc{$pkg}{$suite}{$version} = $desc;
173                 } else {
174                     $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
175                 }
176             }
177
178             my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
179             my @pkgs = sort keys %uniq_pkgs;
180             $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, \@keywords,
181                                               \&print_package, \%provided_by,
182                                               \%archives, \%sect, \%subsect,
183                                               \%desc );
184
185         } else { # unless $opts->{source}
186             foreach (@results) {
187                 my ($pkg, $archive, $suite, $section, $subsection, $priority,
188                     $version) = @$_;
189                 
190                 my $real_archive = '';
191                 if ($archive =~ /^(security|non-US)$/) {
192                     $real_archive = $archive;
193                     $archive = 'us';
194                 }
195                 if (($real_archive eq $archive) &&
196                     $pkgs{$pkg}{$suite}{$archive} &&
197                     (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
198                     next;
199                 }
200                 $pkgs{$pkg}{$suite}{$archive} = $version;
201                 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
202                 $sect{$pkg}{$suite}{$archive}{source} = $section
203                     unless $section eq 'main';
204                 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
205                     if $real_archive;
206
207                 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
208             }
209
210             my @pkgs = sort keys %pkgs;
211             $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, \@keywords,
212                                               \&print_src_package, \%archives,
213                                               \%sect, \%subsect, \%binaries );
214         } # else unless $opts->{source}
215     } # if @results
216 } # sub do_search
217
218 sub print_packages {
219     my ($pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
220
221     #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts );
222     my $str = '<div id="psearchres">';
223     $str .= "<p>".sprintf( _g( "Found <em>%s</em> matching packages." ),
224                            scalar @$pkgs_list )."</p>";
225     #my $count = 0;
226     my $keyword;
227     $keyword = $keywords->[0] if @$keywords == 1;
228             
229     my $have_exact;
230     if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
231         $have_exact = 1;
232         $str .= '<h2>'._g( "Exact hits" ).'</h2>';
233         $str .= &$print_func( $keyword, $pkgs->{$keyword}||{},
234                               map { $_->{$keyword}||{} } @func_args );
235         @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list;
236     }
237             
238     if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) {
239         $str .= '<h2>'._g( 'Other hits' ).'</h2>'
240             if $have_exact;
241         
242         foreach my $pkg (@$pkgs_list) {
243             #$count++;
244             #next if $count < $start or $count > $end;
245             $str .= &$print_func( $pkg, $pkgs->{$pkg}||{},
246                                   map { $_->{$pkg}||{} } @func_args );
247         }
248     } elsif (@$pkgs_list) {
249         $str .= "<p>".sprintf( _g( '<a href="%s">%s</a> results have not been displayed because you requested only exact matches.' ),
250                                encode_entities(make_search_url('',"keywords=$opts->{keyword_esc}",{exact => 0})),
251                                scalar @$pkgs_list )."</p>";
252     }
253     $str .= '</div>';
254
255     return $str;
256 }
257
258 sub print_package {
259     my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_;
260
261     my $str = '<h3>'.sprintf( _g( 'Package %s' ), $pkg ).'</h3>';
262     $str .= '<ul>';
263     foreach my $suite (@SUITES) {
264             my $override = { suite => $suite };
265             if (exists $pkgs->{$suite}) {
266                 my %archs_printed;
267                 my @versions = version_sort keys %{$pkgs->{$suite}};
268                 my $origin_str = "";
269                 if ($sect->{$suite}{$versions[0]}) {
270                     $origin_str .= " ".marker($sect->{$suite}{$versions[0]});
271                 }
272                 $str .= sprintf( "<li><a href=\"%s\">%s</a> (%s): %s   %s\n",
273                                  make_url($pkg,'',$override), $suite, $subsect->{$suite}{$versions[0]},
274                                  $desc->{$suite}{$versions[0]}, $origin_str );
275                 
276                 foreach my $v (@versions) {
277                     my $archive_str = "";
278                     if ($archives->{$suite}{$v} ne 'us') {
279                         $archive_str .= " ".marker($archives->{$suite}{$v});
280                     }
281                     
282                     my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}};
283                     $str .= sprintf( "<br>%s$archive_str: %s\n",
284                                      $v, join (" ", @archs_to_print ))
285                         if @archs_to_print;
286                     $archs_printed{$_}++ foreach @archs_to_print;
287                 }
288                 if (my $p =  $provided_by->{$suite}) {
289                     $str .= '<br>'._g( 'also provided by: ' ).
290                         join( ', ', map { "<a href=\"".
291                                               make_url($_,'',$override)."\">$_</a>"  } @$p);
292                 }
293                 $str .= "</li>\n";
294             } elsif (my $p =  $provided_by->{$suite}) {
295                 $str .= sprintf( "<li><a href=\"%s\">%s</a>: "._g('Virtual package').'<br>',
296                                  make_url($pkg,'',$override), $suite );
297                 $str .= _g( 'provided by: ' ).
298                     join( ', ', map { "<a href=\"".
299                                           make_url($_,'',$override)."\">$_</a>"  } @$p);
300             }
301     }
302     $str .= "</ul>\n";
303     return $str;
304 }
305
306 sub print_src_package {
307     my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_;
308
309     my $str = '<h3>'.sprintf( _g( 'Source package %s' ), $pkg ).'</h3>';
310     $str .= "<ul>\n";
311     foreach my $suite (@SUITES) {
312         foreach my $archive (@ARCHIVES) {
313             if (exists $pkgs->{$suite}{$archive}) {
314                 my $origin_str = "";
315                 if ($sect->{$suite}{$archive}{source}) {
316                     $origin_str .= " ".marker($sect->{$suite}{$archive}{source});
317                 }
318                 if ($archives->{$suite}{$archive}{source}) {
319                     $origin_str .= " ".marker($archives->{$suite}{$archive}{source});
320                 }
321                 $str .= sprintf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s   %s",
322                                  $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect->{$suite}{$archive}{source},
323                                  $pkgs->{$suite}{$archive}, $origin_str );
324                 
325                 $str .= "<br>"._g( 'Binary packages: ' );
326                 my @bp_links;
327                 foreach my $bp (@{$binaries->{$suite}{$archive}}) {
328                     my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
329                                            $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ),  $bp );
330                     push @bp_links, $bp_link;
331                 }
332                 $str .= join( ", ", @bp_links );
333                 $str .= "</li>\n";
334             }
335         }
336     }
337     $str .= "</ul>\n";
338     return $str;
339 }
340
341 1;