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