1 package Packages::DoSearchContents;
6 use Benchmark ':hireswallclock';
9 our @ISA = qw( Exporter );
10 our @EXPORT = qw( do_search_contents );
13 use Packages::I18N::Locale;
14 use Packages::Search qw( :all );
15 use Packages::CGI qw( :DEFAULT error );
17 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES @ARCHITECTURES $ROOT );
19 sub do_search_contents {
20 my ($params, $opts, $page_content) = @_;
21 my $cat = $opts->{cat};
23 if ($params->{errors}{keywords}) {
24 fatal_error( $cat->g( "keyword not valid or missing" ) );
25 $opts->{keywords} = [];
26 } elsif (grep { length($_) < 2 } @{$opts->{keywords}}) {
27 fatal_error( $cat->g( "keyword too short (keywords need to have at least two characters)" ) );
29 if ($params->{errors}{suite}) {
30 fatal_error( $cat->g( "suite not valid or not specified" ) );
33 #FIXME: that's extremely hacky atm
34 if ($params->{values}{suite}{no_replace}[0] eq 'default') {
35 $params->{values}{suite}{no_replace} =
36 $params->{values}{suite}{final} = $opts->{suite} = [ 'squeeze' ];
39 if (@{$opts->{suite}} > 1) {
40 fatal_error( $cat->g( "more than one suite specified for contents search (%s)",
41 "@{$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 unless (-e "$DBDIR/contents/reverse_$suite.db") {
62 fatal_error($cat->g("No contents information available for this suite"));
65 my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
66 O_RDONLY, 0666, $DB_BTREE
67 or die "Failed opening reverse DB: $!";
70 open FILENAMES, '-|', 'fgrep', '--', $first_kw, "$DBDIR/contents/filenames_$suite.txt"
71 or die "Failed opening filename table: $!";
76 foreach my $kw (@keywords) {
77 next FILENAME unless /\Q$kw\E/;
79 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
80 last if $Packages::Search::too_many_hits;
82 while (<FILENAMES>) {};
83 close FILENAMES or warn "fgrep error: $!\n";
86 error($cat->g("The search mode you selected doesn't support more than one keyword."))
89 my $kw = reverse $first_kw;
92 # exact filename searching follows trivially:
93 $kw = "$kw/" if $mode eq 'exactfilename';
95 &searchfile(\@results, $kw, \$nres, $reverses);
101 my $st1 = new Benchmark;
102 my $std = timediff($st1, $st0);
103 debug( "Search took ".timestr($std) ) if DEBUG;
106 my (%results,%archs);
107 foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
108 my $file = shift @$result;
111 my ($pkg, $arch) = split m/:/, $_;
112 next unless $opts->{h_archs}{$arch};
113 $pkgs{$pkg}{$arch}++;
114 $archs{$arch}++ unless $arch eq 'all';
116 next unless keys %pkgs;
117 $results{$file} = \%pkgs;
119 my @all_archs = sort keys %archs;
120 @all_archs = sort @ARCHITECTURES unless @all_archs;
121 $page_content->{suite} = $suite;
122 $page_content->{archive} = $archive;
123 $page_content->{all_architectures} = \@all_archs;
124 $page_content->{all_suites} = \@SUITES;
125 $page_content->{mode} = $mode;
126 $page_content->{search_architectures} = $opts->{arch};
127 $page_content->{search_keywords} = $opts->{keywords};
128 $page_content->{sections} = $opts->{section};
129 $page_content->{too_many_hits} = $Packages::Search::too_many_hits;
131 debug( "all_archs = @all_archs", 1 ) if DEBUG;
134 my $sort_func = sub { $_[0] cmp $_[1] };
135 $sort_func = sub { (sort keys %{$results{$_[0]}})[0]
137 (sort keys %{$results{$_[1]}})[0]
138 } if $opts->{sort_by} eq 'pkg';
140 $page_content->{results} = [];
141 foreach my $file (sort {&$sort_func($a,$b)} keys %results) {
143 $result{file} = "/$file";
144 $result{packages} = [];
145 foreach my $pkg (sort keys %{$results{$file}}) {
147 my @archs = keys %{$results{$file}{$pkg}};
149 unless ($results{$file}{$pkg}{all} ||
150 (@archs == @all_archs)) {
151 if (@archs >= @all_archs/2) {
152 @archs = grep { !$results{$file}{$pkg}{$_} } @all_archs;
158 push @{$result{packages}}, { pkg => $pkg, architectures => \@archs, architectures_are_rev => $arch_neg };
160 push @{$page_content->{results}}, \%result;
163 } # sub do_search_contents
167 my ($results, $kw, $nres, $reverses) = @_;
169 my ($key, $value) = ($kw, "");
170 debug( "searchfile: kw=$kw", 1 ) if DEBUG;
171 for (my $status = $reverses->seq($key, $value, R_CURSOR);
173 $status = $reverses->seq( $key, $value, R_NEXT)) {
175 # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
176 # whether $kw is or is not a prefix of $key
177 last unless index($key, $kw) == 0;
178 debug( "found $key", 2 ) if DEBUG;
180 my @files = split /\001/o, $value;
181 foreach my $f (@files) {
182 my @hits = split /\0/o, $f;
183 my $file = shift @hits;
185 $file = reverse($key);
187 push @$results, [ $file, @hits ];
189 last if ($$nres)++ > 100;
192 $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;