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