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