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 );
19 use Packages::Config qw( $DBDIR $SEARCH_URL $SEARCH_PAGE
20 @SUITES @ARCHIVES $ROOT );
22 sub do_search_contents {
23 my ($params, $opts, $html_header, $menu, $page_content) = @_;
25 if ($params->{errors}{keywords}) {
26 fatal_error( _g( "keyword not valid or missing" ) );
27 } elsif (length($opts->{keywords}) < 2) {
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} = [ 'stable' ];
40 if (@{$opts->{suite}} > 1) {
41 fatal_error( sprintf( _g( "more than one suite specified for contents search (%s)" ), "@{$opts->{suite}}" ) );
46 my $keyword = $opts->{keywords};
47 my $searchon = $opts->{searchon};
48 my $exact = $opts->{exact};
49 my $suite = $opts->{suite}[0];
50 my $archive = $opts->{archive}[0] ||'';
51 $Packages::Search::too_many_hits = 0;
53 # for URL construction
54 my $keyword_esc = uri_escape( $keyword );
55 my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
56 my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
57 my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
60 my $keyword_enc = encode_entities $keyword || '';
61 my $searchon_enc = encode_entities $searchon;
62 my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ), '&<>"' );
63 my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ), '&<>"' );
64 my $archs_enc = encode_entities( join( ', ', @{$params->{values}{arch}{no_replace}} ), '&<>"' );
66 my $st0 = new Benchmark;
69 unless (@Packages::CGI::fatal_errors) {
74 # full filename search is tricky
75 my $ffn = $searchon eq 'filenames';
77 my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
78 O_RDONLY, 0666, $DB_BTREE
79 or die "Failed opening reverse DB: $!";
82 open FILENAMES, '-|', 'fgrep', '--', $kw, "$DBDIR/contents/filenames_$suite.txt"
83 or die "Failed opening filename table: $!";
85 error( _g( "Exact and fullfilenamesearch don't go along" ) )
90 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
91 last if $Packages::Search::too_many_hits;
93 close FILENAMES or warn "fgrep error: $!\n";
98 # exact filename searching follows trivially:
99 $kw = "$kw/" if $exact;
101 &searchfile(\@results, $kw, \$nres, $reverses);
107 my $st1 = new Benchmark;
108 my $std = timediff($st1, $st0);
109 debug( "Search took ".timestr($std) ) if DEBUG;
112 my $suite_wording = sprintf(_g("suite <em>%s</em>"), $suites_enc );
113 my $section_wording = $sections_enc eq 'all' ? _g("all sections")
114 : sprintf(_g("section(s) <em>%s</em>"), $sections_enc );
115 my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
116 : sprintf(_g("architecture(s) <em>%s</em>"), $archs_enc );
117 my $wording = _g("filenames that contain");
118 if ($searchon eq 'contents') {
119 if ($opts->{exact}) {
120 $wording = _g("files named");
122 $wording = _g("paths that end with");
125 msg( sprintf( _g("You have searched for %s <em>%s</em> in %s, %s, and %s." ),
126 $wording, $keyword_enc,
127 $suite_wording, $section_wording, $arch_wording ) );
129 if ($Packages::Search::too_many_hits) {
130 error( _g( "Your search was too wide so we will only display only the first about 100 matches. Please consider using a longer keyword or more keywords." ) );
133 if (!@Packages::CGI::fatal_errors && !@results) {
134 error( _g( "Nothing found" ) );
137 %$html_header = ( title => _g( 'Package Contents Search Results' ),
138 lang => $opts->{lang},
139 title_tag => _g( 'Debian Package Contents Search Results' ),
141 print_search_field => 'packages',
142 search_field_values => {
143 keywords => $keyword_enc,
144 searchon => 'contents',
146 suite => $suites_enc,
147 section => $sections_enc,
148 exact => $opts->{exact},
149 debug => $opts->{debug},
155 my (%results,%archs);
156 foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
157 my $file = shift @$result;
160 my ($pkg, $arch) = split /:/, $_;
161 next unless $opts->{h_archs}{$arch};
162 $pkgs{$pkg}{$arch}++;
163 $archs{$arch}++ unless $arch eq 'all';
165 next unless keys %pkgs;
166 $results{$file} = \%pkgs;
168 my @all_archs = keys %archs;
169 debug( "all_archs = @all_archs", 1 ) if DEBUG;
171 $$page_content .= "<p>".sprintf( _g( 'Found %s results' ),
172 scalar @results )."</p>";
173 $$page_content .= '<div
174 id="pcontentsres"><table><colgroup><col><col></colgroup><tr><th>'._g('File').'</th><th>'._g('Packages')
176 foreach my $file (sort keys %results) {
177 $$page_content .= "<tr><td class=\"file\">/$file</td><td>";
179 foreach my $pkg (sort keys %{$results{$file}}) {
181 my @archs = keys %{$results{$file}{$pkg}};
182 unless ($results{$file}{$pkg}{all} ||
183 (@archs == @all_archs)) {
184 if (@archs < @all_archs/2) {
185 $arch_str = ' ['.join(' ',sort @archs).']';
187 $arch_str = ' ['._g('not').' '.
188 join(' ', grep { !$results{$file}{$pkg}{$_} } @all_archs).']';
191 push @pkgs, "<a href=\"$ROOT/$suite/$pkg\">$pkg</a>$arch_str";
193 $$page_content .= join( ", ", @pkgs);
194 $$page_content .= '</td>';
196 $$page_content .= '<tr><th>'._g('File').'</th><th>'._g('Packages').'</th></tr>' if @results > 20;
197 $$page_content .= '</table></div>';
199 } # sub do_search_contents
203 my ($results, $kw, $nres, $reverses) = @_;
205 my ($key, $value) = ($kw, "");
206 debug( "searchfile: kw=$kw", 1 ) if DEBUG;
207 for (my $status = $reverses->seq($key, $value, R_CURSOR);
209 $status = $reverses->seq( $key, $value, R_NEXT)) {
211 # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
212 # whether $kw is or is not a prefix of $key
213 last unless index($key, $kw) == 0;
214 debug( "found $key", 2 ) if DEBUG;
216 my @hits = split /\0/o, $value;
217 push @$results, [ scalar reverse($key), @hits ];
218 last if ($$nres)++ > 100;
221 $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;