]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoSearchContents.pm
Small adjustments
[deb/packages.git] / lib / Packages / DoSearchContents.pm
1 package Packages::DoSearchContents;
2
3 use strict;
4 use warnings;
5
6 use Benchmark ':hireswallclock';
7 use DB_File;
8 use URI::Escape;
9 use HTML::Entities;
10 use Exporter;
11 our @ISA = qw( Exporter );
12 our @EXPORT = qw( do_search_contents );
13
14 use Deb::Versions;
15 use Packages::I18N::Locale;
16 use Packages::Search qw( :all );
17 use Packages::CGI;
18 use Packages::DB;
19 use Packages::Config qw( $DBDIR $SEARCH_URL $SEARCH_PAGE
20                          @SUITES @ARCHIVES @ARCHITECTURES $ROOT );
21
22 sub do_search_contents {
23     my ($params, $opts, $html_header, $menu, $page_content) = @_;
24
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)" ) );
29     }
30     if ($params->{errors}{suite}) {
31         fatal_error( _g( "suite not valid or not specified" ) );
32     }
33
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' ];
38     }
39
40     if (@{$opts->{suite}} > 1) {
41         fatal_error( sprintf( _g( "more than one suite specified for contents search (%s)" ), "@{$opts->{suite}}" ) );
42     }
43
44     $$menu = "";
45     
46     my $keyword = $opts->{keywords};
47     my $mode = $opts->{mode} || '';
48     my $suite = $opts->{suite}[0];
49     my $archive = $opts->{archive}[0] ||'';
50     $Packages::Search::too_many_hits = 0;
51
52     # for URL construction
53     my $keyword_esc = uri_escape( $keyword );
54     my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
55     my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
56     my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
57
58     # for output
59     my $keyword_enc = encode_entities $keyword || '';
60     my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ), '&<>"' );
61     my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ), '&<>"' );
62     my $archs_enc = encode_entities( join( ', ',  @{$params->{values}{arch}{no_replace}} ), '&<>"' );
63     
64     my $st0 = new Benchmark;
65     my (@results);
66
67     unless (@Packages::CGI::fatal_errors) {
68
69         my $nres = 0;
70
71         my $kw = lc $keyword;
72         # full filename search is tricky
73         my $ffn = $mode eq 'filename';
74
75         my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
76             O_RDONLY, 0666, $DB_BTREE
77             or die "Failed opening reverse DB: $!";
78
79         if ($ffn) {
80             open FILENAMES, '-|', 'fgrep', '--', $kw, "$DBDIR/contents/filenames_$suite.txt"
81                 or die "Failed opening filename table: $!";
82
83             while (<FILENAMES>) {
84                 chomp;
85                 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
86                 last if $Packages::Search::too_many_hits;
87             }
88             close FILENAMES or warn "fgrep error: $!\n";
89         } else {
90
91             $kw = reverse $kw;
92             
93             # exact filename searching follows trivially:
94             $kw = "$kw/" if $mode eq 'exactfilename';
95
96             &searchfile(\@results, $kw, \$nres, $reverses);
97         }
98         $reverses = undef;
99         untie %reverses;
100
101     
102         my $st1 = new Benchmark;
103         my $std = timediff($st1, $st0);
104         debug( "Search took ".timestr($std) ) if DEBUG;
105     }
106     
107     my $suite_wording = sprintf(_g("suite <em>%s</em>"), $suites_enc );
108     my $section_wording = $sections_enc eq 'all' ? _g("all sections")
109         : sprintf(_g("section(s) <em>%s</em>"), $sections_enc );
110     my $arch_wording = $archs_enc eq 'any' ? _g("all architectures")
111         : sprintf(_g("architecture(s) <em>%s</em>"), $archs_enc );
112     my $wording = _g("paths that end with");
113     if ($mode eq 'filename') {
114         $wording =  _g("files named");
115     } elsif ($mode eq 'exactfilename') {
116         $wording = _g("filenames that contain");
117     }
118     msg( sprintf( _g("You have searched for %s <em>%s</em> in %s, %s, and %s." ),
119                   $wording, $keyword_enc,
120                   $suite_wording, $section_wording, $arch_wording ) );
121
122     if ($mode ne 'filename') {
123         msg( '<a href="'.make_search_url('',"keywords=$keyword_esc",{mode=>'filename'}).
124               "\">"._g("Search within filenames")."</a>");
125     }
126     if ($mode ne 'exactfilename') {
127         msg( '<a href="'.make_search_url('',"keywords=$keyword_esc",{mode=>'exactfilename'}).
128               "\">"._g("Search exact filename")."</a>");
129     }
130     if ($mode eq 'exactfilename' || $mode eq 'filename') {
131         msg( '<a href="'.make_search_url('',"keywords=$keyword_esc",{mode=>undef}).
132               "\">"._g("Search for paths ending with")."</a>");
133     }
134
135     msg( _g("Search in other suite:")." ".
136          join( ' ', map { '[<a href="'.make_search_url('',"keywords=$keyword_esc",{suite=>$_}).
137                               "\">$_</a>]" } @SUITES ) );
138
139     if ($Packages::Search::too_many_hits) {
140         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." ) );
141     }
142     
143     %$html_header = ( title => _g( 'Package Contents Search Results' ),
144                       lang => $opts->{lang},
145                       title_tag => _g( 'Debian Package Contents Search Results' ),
146                       print_title => 1,
147                       print_search_field => 'packages',
148                       search_field_values => { 
149                           keywords => $keyword_enc,
150                           searchon => 'contents',
151                           arch => $archs_enc,
152                           suite => $suites_enc,
153                           section => $sections_enc,
154                           exact => $opts->{exact},
155                           debug => $opts->{debug},
156                       },
157                       );
158
159     $$page_content = '';
160     my (%results,%archs);
161     foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
162         my $file = shift @$result;
163         my %pkgs;
164         foreach (@$result) {
165             my ($pkg, $arch) = split /:/, $_;
166             next unless $opts->{h_archs}{$arch};
167             $pkgs{$pkg}{$arch}++;
168             $archs{$arch}++ unless $arch eq 'all';
169         }
170         next unless keys %pkgs;
171         $results{$file} = \%pkgs;
172     }
173     my @all_archs = keys %archs;
174     @all_archs = @ARCHITECTURES unless @all_archs;
175     debug( "all_archs = @all_archs", 1 ) if DEBUG;
176     msg(_g("Limit search to a specific architecture:")." ".
177         join( ' ', map { '[<a href="'.make_search_url('',"keywords=$keyword_esc",{arch=>$_}).
178                              "\">$_</a>]" } @all_archs ) )
179         unless (@{$opts->{arch}} == 1) || (@all_archs == 1);
180     msg(sprintf(_g('Search in <a href="%s">all architectures</a>'),
181                 make_search_url('',"keywords=$keyword_esc",{arch=>undef})))
182         if @{$opts->{arch}} == 1;
183
184     if (!@Packages::CGI::fatal_errors && !keys(%results)) {
185         error( _g( "Nothing found" ) );
186     }
187
188     if (keys %results) {
189         $$page_content .= "<p>".sprintf( _g( 'Found %s results' ),
190                                          scalar keys %results )."</p>";
191         $$page_content .= '<div
192         id="pcontentsres"><table><colgroup><col><col></colgroup><tr><th>'._g('File').'</th><th>'._g('Packages')
193             ."</th></tr>\n";
194         foreach my $file (sort keys %results) {
195                 my $file_enc = encode_entities($file);
196                 $file_enc =~ s#(\Q$keyword_enc\E)#<span class="keyword">$1</span>#g;
197             $$page_content .= "<tr><td class=\"file\">/$file_enc</td><td>";
198             my @pkgs;
199             foreach my $pkg (sort keys %{$results{$file}}) {
200                 my $arch_str = '';
201                 my @archs = keys %{$results{$file}{$pkg}};
202                 unless ($results{$file}{$pkg}{all} ||
203                         (@archs == @all_archs)) {
204                     if (@archs < @all_archs/2) {
205                         $arch_str = ' ['.join(' ',sort @archs).']';
206                     } else {
207                         $arch_str = ' ['._g('not').' '.
208                             join(' ', grep { !$results{$file}{$pkg}{$_} } @all_archs).']';
209                     }
210                 }
211                 push @pkgs, "<a href=\"".make_url($pkg,'',{suite=>$suite})."\">$pkg</a>$arch_str";
212             }
213             $$page_content .= join( ", ", @pkgs);
214             $$page_content .= "</td></tr>\n";
215         }
216         $$page_content .= '<tr><th>'._g('File').'</th><th>'._g('Packages')."</th></tr>\n" if @results > 20;
217         $$page_content .= '</table></div>';
218     }
219 } # sub do_search_contents
220
221 sub searchfile
222 {
223     my ($results, $kw, $nres, $reverses) = @_;
224
225     my ($key, $value) = ($kw, "");
226     debug( "searchfile: kw=$kw", 1 ) if DEBUG;
227     for (my $status = $reverses->seq($key, $value, R_CURSOR);
228         $status == 0;
229         $status =  $reverses->seq( $key, $value, R_NEXT)) {
230
231         # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
232         # whether $kw is or is not a prefix of $key
233         last unless index($key, $kw) == 0;
234         debug( "found $key", 2 ) if DEBUG;
235
236         my @hits = split /\0/o, $value;
237         push @$results, [ scalar reverse($key), @hits ];
238         last if ($$nres)++ > 100;
239     }
240
241     $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;
242 }
243
244
245 1;