1 package Packages::DoSearchContents;
6 use Benchmark ':hireswallclock';
11 our @ISA = qw( Exporter );
12 our @EXPORT = qw( do_search_contents );
15 use Packages::I18N::Locale;
16 use Packages::Search qw( :all );
17 use Packages::CGI qw( :DEFAULT error );
19 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES @ARCHITECTURES $ROOT );
21 sub do_search_contents {
22 my ($params, $opts, $page_content) = @_;
24 if ($params->{errors}{keywords}) {
25 fatal_error( _g( "keyword not valid or missing" ) );
26 $opts->{keywords} = [];
27 } elsif (grep { length($_) < 2 } @{$opts->{keywords}}) {
28 fatal_error( _g( "keyword too short (keywords need to have at least two characters)" ) );
30 if ($params->{errors}{suite}) {
31 fatal_error( _g( "suite not valid or not specified" ) );
34 #FIXME: that's extremely hacky atm
35 if ($params->{values}{suite}{no_replace}[0] eq 'default') {
36 $params->{values}{suite}{no_replace} =
37 $params->{values}{suite}{final} = $opts->{suite} = [ 'etch' ];
40 if (@{$opts->{suite}} > 1) {
41 fatal_error( sprintf( _g( "more than one suite specified for contents search (%s)" ), "@{$opts->{suite}}" ) );
44 my @keywords = @{$opts->{keywords}};
45 my $mode = $opts->{mode} || '';
46 my $suite = $opts->{suite}[0];
47 my $archive = $opts->{archive}[0] ||'';
48 $Packages::Search::too_many_hits = 0;
50 my $st0 = new Benchmark;
53 unless (@Packages::CGI::fatal_errors) {
57 my $first_kw = lc shift @keywords;
58 # full filename search is tricky
59 my $ffn = $mode eq 'filename';
61 my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
62 O_RDONLY, 0666, $DB_BTREE
63 or die "Failed opening reverse DB: $!";
66 open FILENAMES, '-|', 'fgrep', '--', $first_kw, "$DBDIR/contents/filenames_$suite.txt"
67 or die "Failed opening filename table: $!";
72 foreach my $kw (@keywords) {
73 next FILENAME unless /\Q$kw\E/;
75 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
76 last if $Packages::Search::too_many_hits;
78 close FILENAMES or warn "fgrep error: $!\n";
81 error(_g("The search mode you selected doesn't support more than one keyword."))
84 my $kw = reverse $first_kw;
87 # exact filename searching follows trivially:
88 $kw = "$kw/" if $mode eq 'exactfilename';
90 &searchfile(\@results, $kw, \$nres, $reverses);
96 my $st1 = new Benchmark;
97 my $std = timediff($st1, $st0);
98 debug( "Search took ".timestr($std) ) if DEBUG;
101 my (%results,%archs);
102 foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
103 my $file = shift @$result;
106 my ($pkg, $arch) = split /:/, $_;
107 next unless $opts->{h_archs}{$arch};
108 $pkgs{$pkg}{$arch}++;
109 $archs{$arch}++ unless $arch eq 'all';
111 next unless keys %pkgs;
112 $results{$file} = \%pkgs;
114 my @all_archs = sort keys %archs;
115 @all_archs = sort @ARCHITECTURES unless @all_archs;
116 $page_content->{suite} = $suite;
117 $page_content->{archive} = $archive;
118 $page_content->{all_architectures} = \@all_archs;
119 $page_content->{all_suites} = \@SUITES;
120 $page_content->{mode} = $mode;
121 $page_content->{search_architectures} = $opts->{arch};
122 $page_content->{search_keywords} = $opts->{keywords};
123 $page_content->{sections} = $opts->{section};
124 $page_content->{too_many_hits} = $Packages::Search::too_many_hits;
126 debug( "all_archs = @all_archs", 1 ) if DEBUG;
129 my $sort_func = sub { $_[0] cmp $_[1] };
130 $sort_func = sub { (sort keys %{$results{$_[0]}})[0]
132 (sort keys %{$results{$_[1]}})[0]
133 } if $opts->{sort_by} eq 'pkg';
135 $page_content->{results} = [];
136 foreach my $file (sort {&$sort_func($a,$b)} keys %results) {
138 $result{file} = "/$file";
139 $result{packages} = [];
140 foreach my $pkg (sort keys %{$results{$file}}) {
142 my @archs = keys %{$results{$file}{$pkg}};
144 unless ($results{$file}{$pkg}{all} ||
145 (@archs == @all_archs)) {
146 if (@archs >= @all_archs/2) {
147 @archs = grep { !$results{$file}{$pkg}{$_} } @all_archs;
153 push @{$result{packages}}, { pkg => $pkg, architectures => \@archs, architectures_are_rev => $arch_neg };
155 push @{$page_content->{results}}, \%result;
158 } # sub do_search_contents
162 my ($results, $kw, $nres, $reverses) = @_;
164 my ($key, $value) = ($kw, "");
165 debug( "searchfile: kw=$kw", 1 ) if DEBUG;
166 for (my $status = $reverses->seq($key, $value, R_CURSOR);
168 $status = $reverses->seq( $key, $value, R_NEXT)) {
170 # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
171 # whether $kw is or is not a prefix of $key
172 last unless index($key, $kw) == 0;
173 debug( "found $key", 2 ) if DEBUG;
175 my @hits = split /\0/o, $value;
176 push @$results, [ scalar reverse($key), @hits ];
177 last if ($$nres)++ > 100;
180 $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;