4 # Copyright (C) 2004-2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
62 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
66 our $too_many_hits = 0;
69 my ($hash, $key, $results, $non_results, $opts) = @_;
70 my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-", 2;
72 my %virt = split /\01/o, $virt;
73 while (my ($suite, $provides) = each %virt) {
74 next if $suite eq '-';
75 if ($opts->{h_suites}{$suite}) {
76 push @$results, [ $key, "-", $suite, 'virtual', 'v', 'v', 'v', 'v',
79 push @$non_results, [ $key, "-", $suite, 'virtual', 'v', 'v', 'v', 'v',
84 foreach (split /\000/o, $result) {
85 my @data = split ( /\s/o, $_, 8 );
86 debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
87 if ($opts->{h_suites}{$data[1]}
88 && ($opts->{h_archs}{$data[2]} || $data[2] eq 'all')
89 && $opts->{h_sections}{$data[3]}) {
90 debug( "Using entry ".join( ':', @data), 2) if DEBUG;
91 push @$results, [ $key, @data ];
93 push @$non_results, [ $key, @data ];
98 my ($hash, $key, $results, $opts) = @_;
100 read_entry_all( $hash, $key, $results, \@non_results, $opts );
103 #FIXME: make configurable
104 my %fallback_suites = (
105 'oldstable-backports' => 'oldstable',
106 'oldstable-volatile' => 'oldstable',
107 'stable-backports' => 'stable',
108 'stable-volatile' => 'stable',
109 experimental => 'unstable' );
111 sub read_entry_simple {
112 my ($hash, $key, $archives, $suite) = @_;
113 # FIXME: drop $archives
115 my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-\0", 2;
116 my %virt = split /\01/o, $virt;
117 debug( "read_entry_simple: key=$key, archives=".
118 join(" ",(keys %$archives)).", suite=$suite", 1) if DEBUG;
119 debug( "read_entry_simple: virt=".join(" ",(%virt)), 2) if DEBUG;
120 # FIXME: not all of the 2^4=16 combinations of empty(results),
121 # empty(virt{suite}), empty(fb_result), empty(virt{fb_suite}) are dealt
122 # with correctly, but it's adequate enough for now
123 return [ $virt{$suite} ] unless defined $result;
124 foreach (split /\000/o, $result) {
125 my @data = split ( /\s/o, $_, 8 );
126 debug( "use entry: @data", 2 ) if DEBUG && $data[1] eq $suite;
127 return [ $virt{$suite}, @data ] if $data[1] eq $suite;
129 if (my $fb_suite = $fallback_suites{$suite}) {
130 my $fb_result = read_entry_simple( $hash, $key, $archives, $fb_suite );
131 my $fb_virt = shift(@$fb_result);
132 $virt{$suite} .= $virt{$suite} ? " $fb_virt" : $fb_virt if $fb_virt;
133 return [ $virt{$suite}, @$fb_result ] if @$fb_result;
135 return [ $virt{$suite} ];
138 sub read_src_entry_all {
139 my ($hash, $key, $results, $non_results, $opts) = @_;
140 my $result = $hash->{$key} || '';
141 debug( "read_src_entry_all: key=$key", 1) if DEBUG;
142 foreach (split /\000/o, $result) {
143 my @data = split ( /\s/o, $_, 6 );
144 debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
145 if ($opts->{h_archives}{$data[0]}
146 && $opts->{h_suites}{$data[1]}
147 && $opts->{h_sections}{$data[2]}) {
148 debug( "Using entry ".join( ':', @data), 2) if DEBUG;
149 push @$results, [ $key, @data ];
151 push @$non_results, [ $key, @data ];
156 my ($hash, $key, $results, $opts) = @_;
158 read_src_entry_all( $hash, $key, $results, \@non_results, $opts );
160 sub do_names_search {
161 my ($keywords, $packages, $postfixes, $read_entry, $opts,
162 $results, $non_results) = @_;
164 my $first_keyword = lc shift @$keywords;
165 @$keywords = map { lc $_ } @$keywords;
167 my ($key, $prefixes) = ($first_keyword, '');
169 $postfixes->seq( $key, $prefixes, R_CURSOR );
170 while (index($key, $first_keyword) >= 0) {
171 if ($prefixes =~ /^\001(\d+)/o) {
172 debug( "$key has too many hits", 2 ) if DEBUG;
173 $too_many_hits += $1;
176 foreach (split /\000/o, $prefixes) {
177 $_ = '' if $_ eq '^';
179 foreach my $k (@$keywords) {
180 next PREFIX unless $word =~ /\Q$k\E/;
182 debug( "add word $word", 2) if DEBUG;
186 last if $postfixes->seq( $key, $prefixes, R_NEXT ) != 0;
187 last if $too_many_hits or keys %pkgs >= 100;
190 my $no_results = keys %pkgs;
191 if ($too_many_hits || ($no_results >= 100)) {
192 $too_many_hits += $no_results;
193 %pkgs = ( $first_keyword => 1 ) unless @$keywords;
195 foreach my $pkg (sort keys %pkgs) {
196 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
200 sub do_xapian_search {
201 my ($keywords, $db, $did2pkg, $packages, $read_entry, $opts,
202 $results, $non_results) = @_;
204 # NOTE: this needs to correspond with parse-packages!
206 foreach my $keyword (@$keywords) {
207 $keyword =~ tr [A-Z] [a-z];
208 if ($opts->{exact}) {
209 $keyword = " $keyword ";
211 $keyword =~ s/[(),.-]+//og;
212 $keyword =~ s;[^a-z0-9_/+]+; ;og;
215 my $stemmer = Lingua::Stem->new();
216 $keywords = $stemmer->stem( @tmp );
218 my $db = Search::Xapian::Database->new( $db );
219 my $enq = $db->enquire( OP_AND, @$keywords );
220 debug( "Xapian Query was: ".$enq->get_query()->get_description(), 1) if DEBUG;
221 my @matches = $enq->matches(0, 100);
225 foreach my $match ( @matches ) {
226 my $id = $match->get_docid();
227 my $result = $did2pkg->{$id};
229 foreach (split /\000/o, $result) {
230 my @data = split /\s/, $_, 3;
231 # debug ("Considering $data[0], arch = $data[2]", 3) if DEBUG;
232 # next unless $data[2] eq 'all' || $opts->{h_archs}{$data[2]};
233 # debug ("Ok", 3) if DEBUG;
234 $numres++ unless $tmp_results{$data[0]}++;
236 last if $numres > 100;
239 $too_many_hits++ if $numres > 100;
241 foreach my $pkg (keys %tmp_results) {
242 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
247 my ($pkg, $archive, $suite, $src2bin) = @_;
249 my $bins = $src2bin->{$pkg} || '';
251 foreach (split /\000/o, $bins) {
252 my @data = split /\s/, $_, 5;
254 debug( "find_binaries: considering @data", 3 ) if DEBUG;
255 if (($data[0] eq $archive)
256 && ($data[1] eq $suite)) {
258 debug( "find_binaries: using @data", 3 ) if DEBUG;
262 return [ keys %bins ];