4 # Copyright (C) 2004-2007 Frank Lichtenheld <frank@lichtenheld.de>
6 # The code is based on the old search_packages.pl script that
9 # Copyright (C) 1998 James Treacy
10 # Copyright (C) 2000, 2001 Josip Rodin
11 # Copyright (C) 2001 Adam Heath
12 # Copyright (C) 2004 Martin Schulze
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 1 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
41 package Packages::Search;
49 use Lingua::Stem v0.82;
50 use Search::Xapian qw(:ops);
56 our @ISA = qw( Exporter );
58 our @EXPORT_OK = qw( read_entry read_entry_all read_entry_simple
59 read_src_entry read_src_entry_all find_binaries
60 do_names_search do_fulltext_search do_xapian_search
63 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
67 our $too_many_hits = 0;
70 my ($hash, $key, $results, $non_results, $opts) = @_;
71 my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-", 2;
73 my %virt = split /\01/o, $virt;
74 while (my ($suite, $provides) = each %virt) {
75 next if $suite eq '-';
76 if ($opts->{h_suites}{$suite}) {
77 push @$results, [ $key, "-", $suite, 'virtual', 'v', 'v', 'v', 'v', 'v',
80 push @$non_results, [ $key, "-", $suite, 'virtual', 'v', 'v', 'v', 'v', 'v',
85 foreach (split(/\000/o, $result||'')) {
86 my @data = split ( /\s/o, $_, 9 );
87 debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
88 if ($opts->{h_suites}{$data[1]}
89 && ($opts->{h_archs}{$data[2]} || $data[2] eq 'all')
90 && $opts->{h_sections}{$data[3]}) {
91 debug( "Using entry ".join( ':', @data), 2) if DEBUG;
92 push @$results, [ $key, @data ];
94 push @$non_results, [ $key, @data ];
99 my ($hash, $key, $results, $opts) = @_;
101 read_entry_all( $hash, $key, $results, \@non_results, $opts );
104 #FIXME: make configurable
105 my %fallback_suites = (
106 'etch-backports' => 'etch',
107 'etch-volatile' => 'etch',
108 experimental => 'sid' );
110 sub read_entry_simple {
111 my ($hash, $key, $archives, $suite) = @_;
112 # FIXME: drop $archives
114 my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-\0", 2;
115 my %virt = split /\01/o, $virt;
116 debug( "read_entry_simple: key=$key, archives=".
117 join(" ",(keys %$archives)).", suite=$suite", 1) if DEBUG;
118 debug( "read_entry_simple: virt=".join(" ",(%virt)), 2) if DEBUG;
119 # FIXME: not all of the 2^4=16 combinations of empty(results),
120 # empty(virt{suite}), empty(fb_result), empty(virt{fb_suite}) are dealt
121 # with correctly, but it's adequate enough for now
122 return [ $virt{$suite} ] unless defined $result;
123 foreach (split /\000/o, $result) {
124 my @data = split ( /\s/o, $_, 9 );
125 debug( "use entry: @data", 2 ) if DEBUG && $data[1] eq $suite;
126 return [ $virt{$suite}, @data ] if $data[1] eq $suite;
128 if (my $fb_suite = $fallback_suites{$suite}) {
129 my $fb_result = read_entry_simple( $hash, $key, $archives, $fb_suite );
130 my $fb_virt = shift(@$fb_result);
131 $virt{$suite} .= $virt{$suite} ? " $fb_virt" : $fb_virt if $fb_virt;
132 return [ $virt{$suite}, @$fb_result ] if @$fb_result;
134 return [ $virt{$suite} ];
137 sub read_src_entry_all {
138 my ($hash, $key, $results, $non_results, $opts) = @_;
139 my $result = $hash->{$key} || '';
140 debug( "read_src_entry_all: key=$key", 1) if DEBUG;
141 foreach (split /\000/o, $result) {
142 my @data = split ( /\s/o, $_, 6 );
143 debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
144 if ($opts->{h_archives}{$data[0]}
145 && $opts->{h_suites}{$data[1]}
146 && $opts->{h_sections}{$data[2]}) {
147 debug( "Using entry ".join( ':', @data), 2) if DEBUG;
148 push @$results, [ $key, @data ];
150 push @$non_results, [ $key, @data ];
155 my ($hash, $key, $results, $opts) = @_;
157 read_src_entry_all( $hash, $key, $results, \@non_results, $opts );
159 sub do_names_search {
160 my ($keywords, $packages, $postfixes, $read_entry, $opts,
161 $results, $non_results) = @_;
163 my $first_keyword = lc shift @$keywords;
164 @$keywords = map { lc $_ } @$keywords;
166 my ($key, $prefixes) = ($first_keyword, '');
167 my (%pkgs, %pkgs_min);
168 $postfixes->seq( $key, $prefixes, R_CURSOR );
169 while (index($key, $first_keyword) >= 0) {
170 if ($prefixes =~ /^(\^)?\001(\d+)/o) {
171 debug("$key has too many hits", 2 ) if DEBUG;
172 $too_many_hits += $2;
173 if ($1) { # use the empty prefix
174 foreach my $k (@$keywords) {
175 next unless $key =~ /\Q$k\E/;
177 debug("add key $key", 2) if DEBUG;
183 foreach (split /\000/o, $prefixes) {
184 $_ = '' if $_ eq '^';
186 foreach my $k (@$keywords) {
187 next PREFIX unless $word =~ /\Q$k\E/;
189 debug("add word $word", 2) if DEBUG;
191 $pkgs_min{$word}++ if $_ eq '';
194 last if $postfixes->seq( $key, $prefixes, R_NEXT ) != 0;
195 last if keys %pkgs_min >= 100;
199 my $min_nr = keys %pkgs_min;
200 debug("nr=$nr min_nr=$min_nr too_many_hits=$too_many_hits", 1) if DEBUG;
202 $too_many_hits += $nr - $min_nr + 1;
205 foreach my $pkg (sort keys %pkgs) {
206 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
210 sub do_xapian_search {
211 my ($keywords, $dbpath, $did2pkg, $packages, $read_entry, $opts,
212 $results, $non_results) = @_;
214 # NOTE: this needs to correspond with parse-packages!
216 foreach my $keyword (@$keywords) {
217 $keyword =~ s;[^\w/+]+; ;og;
220 my $stemmer = Lingua::Stem->new();
221 my @stemmed_keywords = grep { length($_) } @{$stemmer->stem( @tmp )};
223 my $db = Search::Xapian::Database->new( $dbpath );
224 my $enq = $db->enquire( OP_OR, @$keywords, @stemmed_keywords );
225 debug( "Xapian Query was: ".$enq->get_query()->get_description(), 1) if DEBUG;
226 my @matches = $enq->matches(0, 999);
228 my (@order, %tmp_results);
229 foreach my $match ( @matches ) {
230 my $id = $match->get_docid();
231 my $result = $did2pkg->{$id};
233 foreach (split /\000/o, $result) {
234 my @data = split /\s/, $_, 3;
235 debug ("Considering $data[0], arch = $data[2], relevance=".$match->get_percent(), 3) if DEBUG;
236 # next unless $data[2] eq 'all' || $opts->{h_archs}{$data[2]};
237 # debug ("Ok", 3) if DEBUG;
238 unless ($tmp_results{$data[0]}++) {
239 push @order, $data[0];
242 last if @order > 100;
245 $too_many_hits++ if @order > 100;
247 debug ("ORDER: @order", 2) if DEBUG;
248 foreach my $pkg (@order) {
249 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
254 my ($pkg, $dbpath, $did2pkg) = @_;
256 my $db = Search::Xapian::Database->new( $dbpath );
257 my $enq = $db->enquire( "P$pkg" );
258 debug( "Xapian Query was: ".$enq->get_query()->get_description(), 1) if DEBUG;
259 my $first_match = ($enq->matches(0,1))[0]->get_document();
262 my $term_it = $first_match->termlist_begin();
263 my $term_end = $first_match->termlist_end();
265 for (; $term_it ne $term_end; $term_it++) {
266 debug( "TERM: ".$term_it->get_termname(), 3);
267 push @terms, $term_it->get_termname();
270 my $rel_enq = $db->enquire( OP_OR, @terms );
271 debug( "Xapian Query was: ".$rel_enq->get_query()->get_description(), 1) if DEBUG;
272 my @rel_pkg = $rel_enq->matches(2,20);
275 # debug(Dumper(\@rel_pkg),1);
277 my (@order, %tmp_results);
278 foreach my $match ( @rel_pkg ) {
279 my $id = $match->get_docid();
280 my $result = $did2pkg->{$id};
282 foreach (split /\000/o, $result) {
283 my @data = split /\s/, $_, 3;
284 debug ("Considering $data[0], arch = $data[2], relevance=".$match->get_percent(), 3) if DEBUG;
285 next if $data[0] eq $pkg;
286 unless ($tmp_results{$data[0]}++) {
287 push @order, $data[0];
293 debug ("ORDER: @order", 2) if DEBUG;
295 $last = $#order if $#order < $last;
296 return @order[0..$last];
300 my ($pkg, $archive, $suite, $src2bin) = @_;
302 my $bins = $src2bin->{$pkg} || '';
304 foreach (split /\000/o, $bins) {
305 my @data = split /\s/, $_, 5;
307 debug( "find_binaries: considering @data", 3 ) if DEBUG;
308 if (($data[0] eq $archive)
309 && ($data[1] eq $suite)) {
311 debug( "find_binaries: using @data", 3 ) if DEBUG;
315 return [ keys %bins ];