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