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 @ARCHITECTURES $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 msg( _g("Search in other suite:")." ".
130 join( ' ', map { '[<a href="'.make_search_url('',"keywords=$keyword_esc",{suite=>$_}).
131 "\">$_</a>]" } @SUITES ) );
133 if ($Packages::Search::too_many_hits) {
134 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." ) );
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},
154 my (%results,%archs);
155 foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
156 my $file = shift @$result;
159 my ($pkg, $arch) = split /:/, $_;
160 next unless $opts->{h_archs}{$arch};
161 $pkgs{$pkg}{$arch}++;
162 $archs{$arch}++ unless $arch eq 'all';
164 next unless keys %pkgs;
165 $results{$file} = \%pkgs;
167 my @all_archs = keys %archs;
168 @all_archs = @ARCHITECTURES unless @all_archs;
169 debug( "all_archs = @all_archs", 1 ) if DEBUG;
170 msg(_g("Limit search to a specific architecture:")." ".
171 join( ' ', map { '[<a href="'.make_search_url('',"keywords=$keyword_esc",{arch=>$_}).
172 "\">$_</a>]" } @all_archs ) )
173 unless (@{$opts->{arch}} == 1) || (@all_archs == 1);
174 msg(sprintf(_g('Search in <a href="%s">all architectures</a>'),
175 make_search_url('',"keywords=$keyword_esc",{arch=>undef})))
176 if @{$opts->{arch}} == 1;
178 if (!@Packages::CGI::fatal_errors && !keys(%results)) {
179 error( _g( "Nothing found" ) );
183 $$page_content .= "<p>".sprintf( _g( 'Found %s results' ),
184 scalar keys %results )."</p>";
185 $$page_content .= '<div
186 id="pcontentsres"><table><colgroup><col><col></colgroup><tr><th>'._g('File').'</th><th>'._g('Packages')
188 foreach my $file (sort keys %results) {
189 $$page_content .= "<tr><td class=\"file\">/$file</td><td>";
191 foreach my $pkg (sort keys %{$results{$file}}) {
193 my @archs = keys %{$results{$file}{$pkg}};
194 unless ($results{$file}{$pkg}{all} ||
195 (@archs == @all_archs)) {
196 if (@archs < @all_archs/2) {
197 $arch_str = ' ['.join(' ',sort @archs).']';
199 $arch_str = ' ['._g('not').' '.
200 join(' ', grep { !$results{$file}{$pkg}{$_} } @all_archs).']';
203 push @pkgs, "<a href=\"".make_url($pkg,'',{suite=>$suite})."\">$pkg</a>$arch_str";
205 $$page_content .= join( ", ", @pkgs);
206 $$page_content .= '</td>';
208 $$page_content .= '<tr><th>'._g('File').'</th><th>'._g('Packages').'</th></tr>' if @results > 20;
209 $$page_content .= '</table></div>';
211 } # sub do_search_contents
215 my ($results, $kw, $nres, $reverses) = @_;
217 my ($key, $value) = ($kw, "");
218 debug( "searchfile: kw=$kw", 1 ) if DEBUG;
219 for (my $status = $reverses->seq($key, $value, R_CURSOR);
221 $status = $reverses->seq( $key, $value, R_NEXT)) {
223 # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
224 # whether $kw is or is not a prefix of $key
225 last unless index($key, $kw) == 0;
226 debug( "found $key", 2 ) if DEBUG;
228 my @hits = split /\0/o, $value;
229 push @$results, [ scalar reverse($key), @hits ];
230 last if ($$nres)++ > 100;
233 $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;