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