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 'sarge-backports' => 'sarge',
107 'sarge-volatile' => 'sarge',
108 'etch-backports' => 'etch',
109 'etch-volatile' => 'etch',
110 experimental => 'sid' );
112 sub read_entry_simple {
113 my ($hash, $key, $archives, $suite) = @_;
114 # FIXME: drop $archives
116 my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-\0", 2;
117 my %virt = split /\01/o, $virt;
118 debug( "read_entry_simple: key=$key, archives=".
119 join(" ",(keys %$archives)).", suite=$suite", 1) if DEBUG;
120 debug( "read_entry_simple: virt=".join(" ",(%virt)), 2) if DEBUG;
121 # FIXME: not all of the 2^4=16 combinations of empty(results),
122 # empty(virt{suite}), empty(fb_result), empty(virt{fb_suite}) are dealt
123 # with correctly, but it's adequate enough for now
124 return [ $virt{$suite} ] unless defined $result;
125 foreach (split /\000/o, $result) {
126 my @data = split ( /\s/o, $_, 9 );
127 debug( "use entry: @data", 2 ) if DEBUG && $data[1] eq $suite;
128 return [ $virt{$suite}, @data ] if $data[1] eq $suite;
130 if (my $fb_suite = $fallback_suites{$suite}) {
131 my $fb_result = read_entry_simple( $hash, $key, $archives, $fb_suite );
132 my $fb_virt = shift(@$fb_result);
133 $virt{$suite} .= $virt{$suite} ? " $fb_virt" : $fb_virt if $fb_virt;
134 return [ $virt{$suite}, @$fb_result ] if @$fb_result;
136 return [ $virt{$suite} ];
139 sub read_src_entry_all {
140 my ($hash, $key, $results, $non_results, $opts) = @_;
141 my $result = $hash->{$key} || '';
142 debug( "read_src_entry_all: key=$key", 1) if DEBUG;
143 foreach (split /\000/o, $result) {
144 my @data = split ( /\s/o, $_, 6 );
145 debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
146 if ($opts->{h_archives}{$data[0]}
147 && $opts->{h_suites}{$data[1]}
148 && $opts->{h_sections}{$data[2]}) {
149 debug( "Using entry ".join( ':', @data), 2) if DEBUG;
150 push @$results, [ $key, @data ];
152 push @$non_results, [ $key, @data ];
157 my ($hash, $key, $results, $opts) = @_;
159 read_src_entry_all( $hash, $key, $results, \@non_results, $opts );
161 sub do_names_search {
162 my ($keywords, $packages, $postfixes, $read_entry, $opts,
163 $results, $non_results) = @_;
165 my $first_keyword = lc shift @$keywords;
166 @$keywords = map { lc $_ } @$keywords;
168 my ($key, $prefixes) = ($first_keyword, '');
170 $postfixes->seq( $key, $prefixes, R_CURSOR );
171 while (index($key, $first_keyword) >= 0) {
172 if ($prefixes =~ /^\001(\d+)/o) {
173 debug( "$key has too many hits", 2 ) if DEBUG;
174 $too_many_hits += $1;
177 foreach (split /\000/o, $prefixes) {
178 $_ = '' if $_ eq '^';
180 foreach my $k (@$keywords) {
181 next PREFIX unless $word =~ /\Q$k\E/;
183 debug( "add word $word", 2) if DEBUG;
187 last if $postfixes->seq( $key, $prefixes, R_NEXT ) != 0;
188 last if $too_many_hits or keys %pkgs >= 100;
191 my $no_results = keys %pkgs;
192 if ($too_many_hits || ($no_results >= 100)) {
193 $too_many_hits += $no_results;
194 %pkgs = ( $first_keyword => 1 ) unless @$keywords;
196 foreach my $pkg (sort keys %pkgs) {
197 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
201 sub do_xapian_search {
202 my ($keywords, $dbpath, $did2pkg, $packages, $read_entry, $opts,
203 $results, $non_results) = @_;
205 # NOTE: this needs to correspond with parse-packages!
207 foreach my $keyword (@$keywords) {
208 $keyword =~ s;[^\w/+]+; ;og;
211 my $stemmer = Lingua::Stem->new();
212 my @stemmed_keywords = grep { length($_) } @{$stemmer->stem( @tmp )};
214 my $db = Search::Xapian::Database->new( $dbpath );
215 my $enq = $db->enquire( OP_OR, @$keywords, @stemmed_keywords );
216 debug( "Xapian Query was: ".$enq->get_query()->get_description(), 1) if DEBUG;
217 my @matches = $enq->matches(0, 999);
219 my (@order, %tmp_results);
220 foreach my $match ( @matches ) {
221 my $id = $match->get_docid();
222 my $result = $did2pkg->{$id};
224 foreach (split /\000/o, $result) {
225 my @data = split /\s/, $_, 3;
226 debug ("Considering $data[0], arch = $data[2], relevance=".$match->get_percent(), 3) if DEBUG;
227 # next unless $data[2] eq 'all' || $opts->{h_archs}{$data[2]};
228 # debug ("Ok", 3) if DEBUG;
229 unless ($tmp_results{$data[0]}++) {
230 push @order, $data[0];
233 last if @order > 100;
236 $too_many_hits++ if @order > 100;
238 debug ("ORDER: @order", 2) if DEBUG;
239 foreach my $pkg (@order) {
240 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
245 my ($pkg, $dbpath, $did2pkg) = @_;
247 my $db = Search::Xapian::Database->new( $dbpath );
248 my $enq = $db->enquire( "P$pkg" );
249 debug( "Xapian Query was: ".$enq->get_query()->get_description(), 1) if DEBUG;
250 my $first_match = ($enq->matches(0,1))[0]->get_document();
253 my $term_it = $first_match->termlist_begin();
254 my $term_end = $first_match->termlist_end();
256 for (; $term_it ne $term_end; $term_it++) {
257 debug( "TERM: ".$term_it->get_termname(), 3);
258 push @terms, $term_it->get_termname();
261 my $rel_enq = $db->enquire( OP_OR, @terms );
262 debug( "Xapian Query was: ".$rel_enq->get_query()->get_description(), 1) if DEBUG;
263 my @rel_pkg = $rel_enq->matches(2,20);
266 # debug(Dumper(\@rel_pkg),1);
268 my (@order, %tmp_results);
269 foreach my $match ( @rel_pkg ) {
270 my $id = $match->get_docid();
271 my $result = $did2pkg->{$id};
273 foreach (split /\000/o, $result) {
274 my @data = split /\s/, $_, 3;
275 debug ("Considering $data[0], arch = $data[2], relevance=".$match->get_percent(), 3) if DEBUG;
276 next if $data[0] eq $pkg;
277 unless ($tmp_results{$data[0]}++) {
278 push @order, $data[0];
284 debug ("ORDER: @order", 2) if DEBUG;
285 return @order[0..10];
289 my ($pkg, $archive, $suite, $src2bin) = @_;
291 my $bins = $src2bin->{$pkg} || '';
293 foreach (split /\000/o, $bins) {
294 my @data = split /\s/, $_, 5;
296 debug( "find_binaries: considering @data", 3 ) if DEBUG;
297 if (($data[0] eq $archive)
298 && ($data[1] eq $suite)) {
300 debug( "find_binaries: using @data", 3 ) if DEBUG;
304 return [ keys %bins ];