]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoSearchContents.pm
e86fc789783ef06dcecd7e9442535db7530f4c78
[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 $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     if (@{$opts->{suite}} > 1) {
34         fatal_error( sprintf( _g( "more than one suite specified for contents search (%s)" ), "@{$opts->{suite}}" ) );
35     }
36
37     $$menu = "";
38     
39     my $keyword = $opts->{keywords};
40     my $searchon = $opts->{searchon};
41     my $exact = $opts->{exact};
42     my $suite = $opts->{suite}[0];
43     my $archive = $opts->{archive}[0] ||'';
44     $Packages::Search::too_many_hits = 0;
45
46     # for URL construction
47     my $keyword_esc = uri_escape( $keyword );
48     my $suites_param = join ',', @{$params->{values}{suite}{no_replace}};
49     my $sections_param = join ',', @{$params->{values}{section}{no_replace}};
50     my $archs_param = join ',', @{$params->{values}{arch}{no_replace}};
51
52     # for output
53     my $keyword_enc = encode_entities $keyword || '';
54     my $searchon_enc = encode_entities $searchon;
55     my $suites_enc = encode_entities( join( ', ', @{$params->{values}{suite}{no_replace}} ) );
56     my $sections_enc = encode_entities( join( ', ', @{$params->{values}{section}{no_replace}} ) );
57     my $archs_enc = encode_entities( join( ', ',  @{$params->{values}{arch}{no_replace}} ) );
58     
59     my $st0 = new Benchmark;
60     my (@results);
61
62     unless (@Packages::CGI::fatal_errors) {
63
64         my $nres = 0;
65
66         my $kw = lc $keyword;
67         # full filename search is tricky
68         my $ffn = $searchon eq 'filenames';
69
70         my $reverses = tie my %reverses, 'DB_File', "$DBDIR/contents/reverse_$suite.db",
71             O_RDONLY, 0666, $DB_BTREE
72             or die "Failed opening reverse DB: $!";
73
74         if ($ffn) {
75             open FILENAMES, '-|', 'fgrep', '--', $kw, "$DBDIR/contents/filenames_$suite.txt"
76                 or die "Failed opening filename table: $!";
77
78             error( _g( "Exact and fullfilenamesearch don't go along" ) )
79                 if $ffn and $exact;
80
81             while (<FILENAMES>) {
82                 chomp;
83                 &searchfile(\@results, reverse($_)."/", \$nres, $reverses);
84                 last if $Packages::Search::too_many_hits;
85             }
86             close FILENAMES or warn "fgrep error: $!\n";
87         } else {
88
89             $kw = reverse $kw;
90             
91             # exact filename searching follows trivially:
92             $kw = "$kw/" if $exact;
93
94             &searchfile(\@results, $kw, \$nres, $reverses);
95         }
96         $reverses = undef;
97         untie %reverses;
98
99     
100         my $st1 = new Benchmark;
101         my $std = timediff($st1, $st0);
102         debug( "Search took ".timestr($std) ) if DEBUG;
103     }
104     
105     my $suite_wording = $suites_enc eq "all" ? "all suites"
106         : "suite(s) <em>$suites_enc</em>";
107     my $section_wording = $sections_enc eq 'all' ? "all sections"
108         : "section(s) <em>$sections_enc</em>";
109     my $arch_wording = $archs_enc eq 'any' ? "all architectures"
110         : "architecture(s) <em>$archs_enc</em>";
111     my $wording = $opts->{exact} ? "exact filenames" : "filenames that contain";
112     $wording = "paths that end with" if $searchon eq "contents";
113     msg( "You have searched for ${wording} <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
114
115     if ($Packages::Search::too_many_hits) {
116         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." ) );
117     }
118     
119     if (!@Packages::CGI::fatal_errors && !@results) {
120         error( _g( "Nothing found" ) );
121     }
122
123     %$html_header = ( title => _g( 'Package Contents Search Results' ),
124                       lang => $opts->{lang},
125                       title_tag => _g( 'Debian Package Contents Search Results' ),
126                       print_title => 1,
127                       print_search_field => 'packages',
128                       search_field_values => { 
129                           keywords => $keyword_enc,
130                           searchon => 'contents',
131                           arch => $archs_enc,
132                           suite => $suites_enc,
133                           section => $sections_enc,
134                           exact => $opts->{exact},
135                           debug => $opts->{debug},
136                       },
137                       );
138
139     $$page_content = '';
140     if (@results) {
141         $$page_content .= "<p>".sprintf( _g( 'Found %s results' ),
142                                          scalar @results )."</p>";
143         $$page_content .= '<div
144         id="pcontentsres"><table><colgroup><col><col></colgroup><tr><th>'._g('File').'</th><th>'._g('Packages')
145             .'</th></tr>';
146         foreach my $result (sort { $a->[0] cmp $b->[0] } @results) {
147             my $file = shift @$result;
148             $$page_content .= "<tr><td class=\"file\">/$file</td><td>";
149             my %pkgs;
150             foreach (@$result) {
151                 my ($pkg, $arch) = split /:/, $_;
152                 $pkgs{$pkg}{$arch}++;
153             }
154             $$page_content .= join( ", ", map { "<a href=\"$ROOT/$suite/$_\">$_</a>" } sort keys %pkgs);
155             $$page_content .= '</td>';
156         }
157         $$page_content .= '<tr><th>'._g('File').'</th><th>'._g('Packages').'</th></tr>' if @results > 20;
158         $$page_content .= '</table></div>';
159     }
160 } # sub do_search_contents
161
162 sub searchfile
163 {
164     my ($results, $kw, $nres, $reverses) = @_;
165
166     my ($key, $value) = ($kw, "");
167     debug( "searchfile: kw=$kw", 1 ) if DEBUG;
168     for (my $status = $reverses->seq($key, $value, R_CURSOR);
169         $status == 0;
170         $status =  $reverses->seq( $key, $value, R_NEXT)) {
171
172         # FIXME: what's the most efficient "is prefix of" thingy? We only want to know
173         # whether $kw is or is not a prefix of $key
174         last unless index($key, $kw) == 0;
175         debug( "found $key", 2 ) if DEBUG;
176
177         my @hits = split /\0/o, $value;
178         push @$results, [ scalar reverse($key), @hits ];
179         last if ($$nres)++ > 100;
180     }
181
182     $Packages::Search::too_many_hits += $$nres - 100 if $$nres > 100;
183 }
184
185
186 1;