]> git.deb.at Git - deb/packages.git/blobdiff - lib/Packages/DoSearch.pm
We welcome DoSearchContents.pm to the "multiple keywords" age
[deb/packages.git] / lib / Packages / DoSearch.pm
index 64160c13b52183081957b38f45749242adb636ad..5b41162ddd2e3518459148d6c392f421d1009b83 100644 (file)
@@ -28,24 +28,21 @@ sub do_search {
 
     if ($params->{errors}{keywords}) {
        fatal_error( _g( "keyword not valid or missing" ) );
-    } elsif (length($opts->{keywords}) < 2) {
+    } elsif (grep { length($_) < 2 } @{$opts->{keywords}}) {
        fatal_error( _g( "keyword too short (keywords need to have at least two characters)" ) );
     }
 
     $$menu = "";
     
-    my $keyword = $opts->{keywords};
+    my @keywords = @{$opts->{keywords}};
     my $searchon = $opts->{searchon};
 
     # for URL construction
-    my $keyword_esc = uri_escape( $keyword );
-    my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
-    my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
-    my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
-    $opts->{common_params} = "suite=$suites_param&section=$sections_param&keywords=$keyword_esc&searchon=$searchon&arch=$archs_param";
+    my $keyword_esc = uri_escape( "@keywords" );
+    $opts->{keywords_esc} = $keyword_esc;
 
     # for output
-    my $keyword_enc = encode_entities $keyword || '';
+    my $keyword_enc = encode_entities "@keywords" || '';
     my $searchon_enc = encode_entities $searchon;
     my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
     my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
@@ -58,22 +55,19 @@ sub do_search {
 
        if ($searchon eq 'names') {
            if ($opts->{source}) {
-               do_names_search( $keyword, \%sources, $sp_obj,
+               do_names_search( \@keywords, \%sources, $sp_obj,
                                 \&read_src_entry_all, $opts,
                                 \@results, \@non_results );
            } else {
-               do_names_search( $keyword, \%packages, $p_obj,
+               do_names_search( \@keywords, \%packages, $p_obj,
                                 \&read_entry_all, $opts,
                                 \@results, \@non_results );
            }
-#      } elsif ($searchon eq 'contents') {
-#          require "./search_contents.pl";
-#          &contents($input);
        } else {
-           do_names_search( $keyword, \%packages, $p_obj,
+           do_names_search( \@keywords, \%packages, $p_obj,
                             \&read_entry_all, $opts,
                             \@results, \@non_results );
-           do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
+           do_fulltext_search( \@keywords, "$DBDIR/descriptions.txt",
                                \%did2pkg, \%packages,
                                \&read_entry_all, $opts,
                                \@results, \@non_results );
@@ -81,24 +75,29 @@ sub do_search {
     }
     
 #    use Data::Dumper;
-#    debug( join( "", Dumper( \@results, \@non_results )) );
+#    debug( join( "", Dumper( \@results, \@non_results )) ) if DEBUG;
     my $st1 = new Benchmark;
     my $std = timediff($st1, $st0);
-    debug( "Search took ".timestr($std) );
+    debug( "Search took ".timestr($std) ) if DEBUG;
     
-    my $suite_wording = $suites_enc eq "all" ? "all suites"
-       : "suite(s) <em>$suites_enc</em>";
-    my $section_wording = $sections_enc eq 'all' ? "all sections"
-       : "section(s) <em>$sections_enc</em>";
-    my $arch_wording = $archs_enc eq 'any' ? "all architectures"
-       : "architecture(s) <em>$archs_enc</em>";
+    my $suite_wording = $suites_enc =~ /^(default|all)$/ ? _g("all suites")
+       : sprintf(_g("suite(s) <em>%s</em>", $suites_enc) );
+    my $section_wording = $sections_enc eq 'all' ? _g("all sections")
+       : sprintf(_g("section(s) <em>%s</em>", $sections_enc) );
+    my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
+       : sprintf(_g("architecture(s) <em>%s</em>", $archs_enc) );
     if ($searchon eq "names") {
-       my $source_wording = $opts->{source} ? "source " : "";
-       my $exact_wording = $opts->{exact} ? "named" : "that names contain";
-       msg( "You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
+       my $source_wording = $opts->{source} ? _g("source packages") : _g("packages");
+       # sorry to all translators for that one... (patches welcome)
+       msg( sprintf( _g( "You have searched for %s that names contain <em>%s</em> in %s, %s, and %s." ),
+                     $source_wording, $keyword_enc,
+                     $suite_wording, $section_wording, $arch_wording ) );
     } else {
-       my $exact_wording = $opts->{exact} ? "" : " (including subword matching)";
-       msg( "You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording." );
+       my $exact_wording = $opts->{exact} ? "" : _g(" (including subword matching)");
+       msg( sprintf( _g( "You have searched for <em>%s</em> in packages names and descriptions in %s, %s, and %s%s." ),
+                     $keyword_enc,
+                     $suite_wording, $section_wording, $arch_wording,
+                     $exact_wording ) );
     }
 
     if ($Packages::Search::too_many_hits) {
@@ -129,7 +128,7 @@ sub do_search {
            
            if ($opts->{exact}) {
                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>.' ),
-                              encode_entities("$SEARCH_URL?exact=0&$opts->{common_params}") ) );
+                              encode_entities(make_search_url('',"keywords=$keyword_esc",{exact => 0})) ) );
            }
        }
        hint( sprintf( _g( 'You can try a different search on the <a href="%s">Packages search page</a>.' ), "$SEARCH_PAGE#search_packages" ) );
@@ -163,27 +162,21 @@ sub do_search {
                
                my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
                if ($arch ne 'virtual') {
-                   my $real_archive;
-                   if ($archive =~ /^(security|non-US)$/) {
-                       $real_archive = $archive;
-                       $archive = 'us';
-                   }
-
-                   $pkgs{$pkg}{$suite}{$archive}{$version}{$arch} = 1;
-                   $subsect{$pkg}{$suite}{$archive}{$version} = $subsection;
-                   $sect{$pkg}{$suite}{$archive}{$version} = $section
+                   $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
+                   $subsect{$pkg}{$suite}{$version} = $subsection;
+                   $sect{$pkg}{$suite}{$version} = $section
                        unless $section eq 'main';
-                   $archives{$pkg}{$suite}{$archive}{$version} = $real_archive
-                       if $real_archive;
+                   $archives{$pkg}{$suite}{$version} ||= $archive;
                    
-                   $desc{$pkg}{$suite}{$archive}{$version} = $desc;
+                   $desc{$pkg}{$suite}{$version} = $desc;
                } else {
-                   $provided_by{$pkg}{$suite}{$archive} = [ split /\s+/, $desc ];
+                   $provided_by{$pkg}{$suite} = [ split /\s+/, $desc ];
                }
            }
 
-           my @pkgs = sort(keys %pkgs, keys %provided_by);
-           $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword,
+           my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by);
+           my @pkgs = sort keys %uniq_pkgs;
+           $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, \@keywords,
                                              \&print_package, \%provided_by,
                                              \%archives, \%sect, \%subsect,
                                              \%desc );
@@ -214,7 +207,7 @@ sub do_search {
            }
 
            my @pkgs = sort keys %pkgs;
-           $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword,
+           $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, \@keywords,
                                              \&print_src_package, \%archives,
                                              \%sect, \%subsect, \%binaries );
        } # else unless $opts->{source}
@@ -222,16 +215,18 @@ sub do_search {
 } # sub do_search
 
 sub print_packages {
-    my ($pkgs, $pkgs_list, $opts, $keyword, $print_func, @func_args) = @_;
+    my ($pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_;
 
     #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts );
     my $str = '<div id="psearchres">';
     $str .= "<p>".sprintf( _g( "Found <em>%s</em> matching packages." ),
                           scalar @$pkgs_list )."</p>";
     #my $count = 0;
+    my $keyword;
+    $keyword = $keywords->[0] if @$keywords == 1;
            
     my $have_exact;
-    if (grep { $_ eq $keyword } @$pkgs_list) {
+    if ($keyword && grep { $_ eq $keyword } @$pkgs_list) {
        $have_exact = 1;
        $str .= '<h2>'._g( "Exact hits" ).'</h2>';
        $str .= &$print_func( $keyword, $pkgs->{$keyword}||{},
@@ -251,7 +246,7 @@ sub print_packages {
        }
     } elsif (@$pkgs_list) {
        $str .= "<p>".sprintf( _g( '<a href="%s">%s</a> results have not been displayed because you requested only exact matches.' ),
-                              encode_entities("$SEARCH_URL?exact=0&$opts->{common_params}"),
+                              encode_entities(make_search_url('',"keywords=$opts->{keyword_esc}",{exact => 0})),
                               scalar @$pkgs_list )."</p>";
     }
     $str .= '</div>';
@@ -265,45 +260,43 @@ sub print_package {
     my $str = '<h3>'.sprintf( _g( 'Package %s' ), $pkg ).'</h3>';
     $str .= '<ul>';
     foreach my $suite (@SUITES) {
-       foreach my $archive (@ARCHIVES) {
-           next if $archive eq 'security';
-           next if $archive eq 'non-US';
-           my $path = $suite.(($archive ne 'us')?"/$archive":'');
-           if (exists $pkgs->{$suite}{$archive}) {
+           my $override = { suite => $suite };
+           if (exists $pkgs->{$suite}) {
                my %archs_printed;
-               my @versions = version_sort keys %{$pkgs->{$suite}{$archive}};
+               my @versions = version_sort keys %{$pkgs->{$suite}};
                my $origin_str = "";
-               if ($sect->{$suite}{$archive}{$versions[0]}) {
-                   $origin_str .= " ".marker($sect->{$suite}{$archive}{$versions[0]});
+               if ($sect->{$suite}{$versions[0]}) {
+                   $origin_str .= " ".marker($sect->{$suite}{$versions[0]});
                }
-               $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s   %s\n",
-                                $path, $pkg, $path, $subsect->{$suite}{$archive}{$versions[0]},
-                                $desc->{$suite}{$archive}{$versions[0]}, $origin_str );
+               $str .= sprintf( "<li><a href=\"%s\">%s</a> (%s): %s   %s\n",
+                                make_url($pkg,'',$override), $suite, $subsect->{$suite}{$versions[0]},
+                                $desc->{$suite}{$versions[0]}, $origin_str );
                
                foreach my $v (@versions) {
                    my $archive_str = "";
-                   if ($archives->{$suite}{$archive}{$v}) {
-                       $archive_str .= " ".marker($archives->{$suite}{$archive}{$v});
+                   if ($archives->{$suite}{$v} ne 'us') {
+                       $archive_str .= " ".marker($archives->{$suite}{$v});
                    }
                    
-                   my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$archive}{$v}};
+                   my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs->{$suite}{$v}};
                    $str .= sprintf( "<br>%s$archive_str: %s\n",
                                     $v, join (" ", @archs_to_print ))
                        if @archs_to_print;
                    $archs_printed{$_}++ foreach @archs_to_print;
                }
-               if (my $p =  $provided_by->{$suite}{$archive}) {
+               if (my $p =  $provided_by->{$suite}) {
                    $str .= '<br>'._g( 'also provided by: ' ).
-                       join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>"  } @$p);
+                       join( ', ', map { "<a href=\"".
+                                             make_url($_,'',$override)."\">$_</a>"  } @$p);
                }
                $str .= "</li>\n";
-           } elsif (my $p =  $provided_by->{$suite}{$archive}) {
-               $str .= sprintf( "<li><a href=\"$ROOT/%s/%s\">%s</a>: Virtual package<br>",
-                                $path, $pkg, $path );
+           } elsif (my $p =  $provided_by->{$suite}) {
+               $str .= sprintf( "<li><a href=\"%s\">%s</a>: "._g('Virtual package').'<br>',
+                                make_url($pkg,'',$override), $suite );
                $str .= _g( 'provided by: ' ).
-                   join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>"  } @$p);
+                   join( ', ', map { "<a href=\"".
+                                         make_url($_,'',$override)."\">$_</a>"  } @$p);
            }
-       }
     }
     $str .= "</ul>\n";
     return $str;