]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoSearchContents.pm
- Further fixes to the arch handling
[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 $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;
52
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}};
58
59     # for output
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}} ), '&<>"' );
65     
66     my $st0 = new Benchmark;
67     my (@results);
68
69     unless (@Packages::CGI::fatal_errors) {
70
71         my $nres = 0;
72
73         my $kw = lc $keyword;
74         # full filename search is tricky
75         my $ffn = $searchon eq 'filenames';
76
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: $!";
80
81         if ($ffn) {
82             open FILENAMES, '-|', 'fgrep', '--', $kw, "$DBDIR/contents/filenames_$suite.txt"
83                 or die "Failed opening filename table: $!";
84
85             error( _g( "Exact and fullfilenamesearch don't go along" ) )
86                 if $ffn and $exact;
87
88             while (<FILENAMES>) {
89                 chomp;
90                 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
91                 last if $Packages::Search::too_many_hits;
92             }
93             close FILENAMES or warn "fgrep error: $!\n";
94         } else {
95
96             $kw = reverse $kw;
97             
98             # exact filename searching follows trivially:
99             $kw = "$kw/" if $exact;
100
101             &searchfile(\@results, $kw, \$nres, $reverses);
102         }
103         $reverses = undef;
104         untie %reverses;
105
106     
107         my $st1 = new Benchmark;
108         my $std = timediff($st1, $st0);
109         debug( "Search took ".timestr($std) ) if DEBUG;
110     }
111     
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");
121         } else {
122             $wording = _g("paths that end with");
123         }
124     }
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 ) );
128
129     msg( _g("Search in other suite:")." ".
130          join( ' ', map { '[<a href="'.make_search_url('',"keywords=$keyword_esc",{suite=>$_}).
131                               "\">$_</a>]" } @SUITES ) );
132
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." ) );
135     }
136     
137     %$html_header = ( title => _g( 'Package Contents Search Results' ),
138                       lang => $opts->{lang},
139                       title_tag => _g( 'Debian Package Contents Search Results' ),
140                       print_title => 1,
141                       print_search_field => 'packages',
142                       search_field_values => { 
143                           keywords => $keyword_enc,
144                           searchon => 'contents',
145                           arch => $archs_enc,
146                           suite => $suites_enc,
147                           section => $sections_enc,
148                           exact => $opts->{exact},
149                           debug => $opts->{debug},
150                       },
151                       );
152
153     $$page_content = '';
154     my (%results,%archs);
155     foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
156         my $file = shift @$result;
157         my %pkgs;
158         foreach (@$result) {
159             my ($pkg, $arch) = split /:/, $_;
160             next unless $opts->{h_archs}{$arch};
161             $pkgs{$pkg}{$arch}++;
162             $archs{$arch}++ unless $arch eq 'all';
163         }
164         next unless keys %pkgs;
165         $results{$file} = \%pkgs;
166     }
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;
177
178     if (!@Packages::CGI::fatal_errors && !keys(%results)) {
179         error( _g( "Nothing found" ) );
180     }
181
182     if (keys %results) {
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')
187             .'</th></tr>';
188         foreach my $file (sort keys %results) {
189             $$page_content .= "<tr><td class=\"file\">/$file</td><td>";
190             my @pkgs;
191             foreach my $pkg (sort keys %{$results{$file}}) {
192                 my $arch_str = '';
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).']';
198                     } else {
199                         $arch_str = ' ['._g('not').' '.
200                             join(' ', grep { !$results{$file}{$pkg}{$_} } @all_archs).']';
201                     }
202                 }
203                 push @pkgs, "<a href=\"".make_url($pkg,'',{suite=>$suite})."\">$pkg</a>$arch_str";
204             }
205             $$page_content .= join( ", ", @pkgs);
206             $$page_content .= '</td>';
207         }
208         $$page_content .= '<tr><th>'._g('File').'</th><th>'._g('Packages').'</th></tr>' if @results > 20;
209         $$page_content .= '</table></div>';
210     }
211 } # sub do_search_contents
212
213 sub searchfile
214 {
215     my ($results, $kw, $nres, $reverses) = @_;
216
217     my ($key, $value) = ($kw, "");
218     debug( "searchfile: kw=$kw", 1 ) if DEBUG;
219     for (my $status = $reverses->seq($key, $value, R_CURSOR);
220         $status == 0;
221         $status =  $reverses->seq( $key, $value, R_NEXT)) {
222
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;
227
228         my @hits = split /\0/o, $value;
229         push @$results, [ scalar reverse($key), @hits ];
230         last if ($$nres)++ > 100;
231     }
232
233     $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;
234 }
235
236
237 1;