X-Git-Url: https://git.deb.at/?p=deb%2Fpackages.git;a=blobdiff_plain;f=lib%2FPackages%2FDoSearch.pm;h=2a59937b84423ad9ec04573417fc2150053f6865;hp=a889ef5ca89870fc2d8f35430e22906f772262fa;hb=078ddbf7006d957bbef2daa9735ec9a8bf3d8fcd;hpb=ab47ae363dddbc35743572c62fae6350dcb7cf96 diff --git a/lib/Packages/DoSearch.pm b/lib/Packages/DoSearch.pm index a889ef5..2a59937 100644 --- a/lib/Packages/DoSearch.pm +++ b/lib/Packages/DoSearch.pm @@ -14,35 +14,31 @@ our @EXPORT = qw( do_search ); use Deb::Versions; use Packages::I18N::Locale; use Packages::Search qw( :all ); -use Packages::CGI; +use Packages::CGI qw( :DEFAULT msg error ); use Packages::DB; -use Packages::HTML qw(marker); -use Packages::Config qw( $DBDIR $SEARCH_URL $SEARCH_PAGE - @SUITES @ARCHIVES $ROOT ); -use Packages::HTML; +use Packages::Config qw( $DBDIR @SUITES @ARCHIVES $ROOT ); sub do_search { - my ($params, $opts, $html_header, $menu, $page_content) = @_; + my ($params, $opts, $html_header, $page_content) = @_; $Params::Search::too_many_hits = 0; if ($params->{errors}{keywords}) { fatal_error( _g( "keyword not valid or missing" ) ); - } elsif (length($opts->{keywords}) < 2) { + $opts->{keywords} = []; + } 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 $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}} ) ); @@ -55,22 +51,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 ); @@ -83,7 +76,7 @@ sub do_search { my $std = timediff($st1, $st0); debug( "Search took ".timestr($std) ) if DEBUG; - my $suite_wording = $suites_enc eq "all" ? _g("all suites") + my $suite_wording = $suites_enc =~ /^(default|all)$/ ? _g("all suites") : sprintf(_g("suite(s) %s", $suites_enc) ); my $section_wording = $sections_enc eq 'all' ? _g("all sections") : sprintf(_g("section(s) %s", $sections_enc) ); @@ -112,11 +105,11 @@ sub do_search { unless (@non_results) { error( _g( "Can't find that package." ) ); } else { - hint( _g( "Can't find that package." )." ". - sprintf( _g( '%s'. - " results have not been displayed due to the". - " search parameters." ), "$SEARCH_URL/$keyword_esc" , - $#non_results+1 ) ); +# hint( _g( "Can't find that package." )." ". +# sprintf( _g( '%s'. +# " results have not been displayed due to the". +# " search parameters." ), "$SEARCH_URL/$keyword_esc" , +# $#non_results+1 ) ); } } else { @@ -134,27 +127,23 @@ sub do_search { encode_entities(make_search_url('',"keywords=$keyword_esc",{exact => 0})) ) ); } } - hint( sprintf( _g( 'You can try a different search on the Packages search page.' ), "$SEARCH_PAGE#search_packages" ) ); +# hint( sprintf( _g( 'You can try a different search on the Packages search page.' ), "$SEARCH_PAGE#search_packages" ) ); } - %$html_header = ( title => _g( 'Package Search Results' ) , - lang => $opts->{lang}, - title_tag => _g( 'Debian Package Search Results' ), - print_title => 1, - print_search_field => 'packages', - search_field_values => { - keywords => $keyword_enc, - searchon => $opts->{searchon_form}, - arch => $archs_enc, - suite => $suites_enc, - section => $sections_enc, - exact => $opts->{exact}, - debug => $opts->{debug}, - }, - ); - - $$page_content = ''; + $page_content->{make_url} = sub { return &Packages::CGI::make_url(@_) }; + $page_content->{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) }; + + $page_content->{search_field_values} = { + keywords => $keyword_enc, + searchon => $opts->{searchon_form}, + arch => $archs_enc, + suite => $suites_enc, + section => $sections_enc, + exact => $opts->{exact}, + debug => $opts->{debug}, + }; + if (@results) { my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by); @@ -167,8 +156,7 @@ sub do_search { if ($arch ne 'virtual') { $pkgs{$pkg}{$suite}{$version}{$arch} = 1; $subsect{$pkg}{$suite}{$version} = $subsection; - $sect{$pkg}{$suite}{$version} = $section - unless $section eq 'main'; + $sect{$pkg}{$suite}{$version} = $section; $archives{$pkg}{$suite}{$version} ||= $archive; $desc{$pkg}{$suite}{$version} = $desc; @@ -179,10 +167,10 @@ sub do_search { my %uniq_pkgs = map { $_ => 1 } (keys %pkgs, keys %provided_by); my @pkgs = sort keys %uniq_pkgs; - $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword, - \&print_package, \%provided_by, - \%archives, \%sect, \%subsect, - \%desc ); + process_packages( $page_content, 'packages', \%pkgs, \@pkgs, $opts, \@keywords, + \&process_package, \%provided_by, + \%archives, \%sect, \%subsect, + \%desc ); } else { # unless $opts->{source} foreach (@results) { @@ -210,132 +198,112 @@ sub do_search { } my @pkgs = sort keys %pkgs; - $$page_content .= print_packages( \%pkgs, \@pkgs, $opts, $keyword, - \&print_src_package, \%archives, - \%sect, \%subsect, \%binaries ); + process_packages( $page_content, 'src_packages', \%pkgs, \@pkgs, $opts, \@keywords, + \&process_src_package, \%archives, + \%sect, \%subsect, \%binaries ); } # else unless $opts->{source} } # if @results } # sub do_search -sub print_packages { - my ($pkgs, $pkgs_list, $opts, $keyword, $print_func, @func_args) = @_; +sub process_packages { + my ($content, $target, $pkgs, $pkgs_list, $opts, $keywords, $print_func, @func_args) = @_; - #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts ); - my $str = '
'; - $str .= "

".sprintf( _g( "Found %s matching packages." ), - scalar @$pkgs_list )."

"; - #my $count = 0; + my @categories; + $content->{results} = scalar @$pkgs_list; + + 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 .= '

'._g( "Exact hits" ).'

'; - $str .= &$print_func( $keyword, $pkgs->{$keyword}||{}, - map { $_->{$keyword}||{} } @func_args ); + $categories[0]{name} = _g( "Exact hits" ); + + $categories[0]{$target} = [ &$print_func( $keyword, $pkgs->{$keyword}||{}, + map { $_->{$keyword}||{} } @func_args ) ]; @$pkgs_list = grep { $_ ne $keyword } @$pkgs_list; } if (@$pkgs_list && (($opts->{searchon} ne 'names') || !$opts->{exact})) { - $str .= '

'._g( 'Other hits' ).'

' - if $have_exact; + my %cat; + $cat{name} = _g( 'Other hits' ) if $have_exact; + $cat{packages} = []; foreach my $pkg (@$pkgs_list) { - #$count++; - #next if $count < $start or $count > $end; - $str .= &$print_func( $pkg, $pkgs->{$pkg}||{}, - map { $_->{$pkg}||{} } @func_args ); + push @{$cat{$target}}, &$print_func( $pkg, $pkgs->{$pkg}||{}, + map { $_->{$pkg}||{} } @func_args ); } + push @categories, \%cat; } elsif (@$pkgs_list) { - $str .= "

".sprintf( _g( '%s results have not been displayed because you requested only exact matches.' ), - encode_entities(make_search_url('',"keywords=$opts->{keyword_esc}",{exact => 0})), - scalar @$pkgs_list )."

"; + $content->{skipped} = scalar @$pkgs_list; } - $str .= '
'; - return $str; + $content->{categories} = \@categories; } -sub print_package { +sub process_package { my ($pkg, $pkgs, $provided_by, $archives, $sect, $subsect, $desc) = @_; - my $str = '

'.sprintf( _g( 'Package %s' ), $pkg ).'

'; - $str .= '\n"; - return $str; + + return \%pkg; } -sub print_src_package { +sub process_src_package { my ($pkg, $pkgs, $archives, $sect, $subsect, $binaries) = @_; - my $str = '

'.sprintf( _g( 'Source package %s' ), $pkg ).'

'; - $str .= "\n"; - return $str; + + return \%pkg; } 1;