]> git.deb.at Git - deb/packages.git/blob - lib/Packages/Search.pm
fix off-by-one after switching on the brain again
[deb/packages.git] / lib / Packages / Search.pm
1 #
2 # Packages::Search
3 #
4 # Copyright (C) 2004-2007 Frank Lichtenheld <frank@lichtenheld.de>
5 #
6 # The code is based on the old search_packages.pl script that
7 # was:
8 #
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
13 #
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.
18 #
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.
23 #
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.
27 #
28
29 =head1 NAME
30
31 Packages::Search - 
32
33 =head1 SYNOPSIS
34
35 =head1 DESCRIPTION
36
37 =over 4
38
39 =cut
40
41 package Packages::Search;
42
43 use strict;
44 use warnings;
45
46 use POSIX;
47 use HTML::Entities;
48 use DB_File;
49 use Lingua::Stem v0.82;
50 use Search::Xapian qw(:ops);
51
52 use Deb::Versions;
53 use Packages::CGI;
54 use Exporter;
55
56 our @ISA = qw( Exporter );
57
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
61                      find_similar
62                      );
63 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
64
65 our $VERSION = 0.01;
66
67 our $too_many_hits = 0;
68
69 sub read_entry_all {
70     my ($hash, $key, $results, $non_results, $opts) = @_;
71     my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-", 2;
72
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',
78                               $provides];
79         } else {
80             push @$non_results, [ $key, "-", $suite, 'virtual', 'v', 'v', 'v', 'v', 'v',
81                                   $provides];
82         }
83     }
84
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 ];
93         } else {
94             push @$non_results, [ $key, @data ];
95         }
96     }
97 }
98 sub read_entry {
99     my ($hash, $key, $results, $opts) = @_;
100     my @non_results;
101     read_entry_all( $hash, $key, $results, \@non_results, $opts );
102 }
103
104 sub fallback_suite {
105     my $suite = shift;
106     if ($suite =~ /^(\S+)-(?:updates|backports|volatile)/) {
107         return $1;
108     } elsif ($suite eq 'experimental') {
109         return 'sid';
110     } else {
111         return undef;
112     }
113 }
114
115 sub read_entry_simple {
116     my ($hash, $key, $archives, $suite) = @_;
117     # FIXME: drop $archives
118
119     my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-\0", 2;
120     my %virt = split /\01/o, $virt; 
121     debug( "read_entry_simple: key=$key, archives=".
122            join(" ",(keys %$archives)).", suite=$suite", 1) if DEBUG;
123     debug( "read_entry_simple: virt=".join(" ",(%virt)), 2) if DEBUG;
124     # FIXME: not all of the 2^4=16 combinations of empty(results),
125     # empty(virt{suite}), empty(fb_result), empty(virt{fb_suite}) are dealt
126     # with correctly, but it's adequate enough for now
127     return [ $virt{$suite} ] unless defined $result;
128     foreach (split /\000/o, $result) {
129         my @data = split ( /\s/o, $_, 9 );
130         debug( "use entry: @data", 2 ) if DEBUG && $data[1] eq $suite;
131         return [ $virt{$suite}, @data ] if $data[1] eq $suite;
132     }
133     if (my $fb_suite = fallback_suite($suite)) {
134         my $fb_result = read_entry_simple( $hash, $key, $archives, $fb_suite );
135         my $fb_virt = shift(@$fb_result);
136         $virt{$suite} .= $virt{$suite} ? " $fb_virt" : $fb_virt if $fb_virt;
137         return [ $virt{$suite}, @$fb_result ] if @$fb_result;
138     }
139     return [ $virt{$suite} ];
140 }
141
142 sub read_src_entry_all {
143     my ($hash, $key, $results, $non_results, $opts) = @_;
144     my $result = $hash->{$key} || '';
145     debug( "read_src_entry_all: key=$key", 1) if DEBUG;
146     foreach (split /\000/o, $result) {
147         my @data = split ( /\s/o, $_, 6 );
148         debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
149         if ($opts->{h_archives}{$data[0]}
150             && $opts->{h_suites}{$data[1]}
151             && $opts->{h_sections}{$data[2]}) {
152             debug( "Using entry ".join( ':', @data), 2) if DEBUG;
153             push @$results, [ $key, @data ];
154         } else {
155             push @$non_results, [ $key, @data ];
156         }
157     }
158 }
159 sub read_src_entry {
160     my ($hash, $key, $results, $opts) = @_;
161     my @non_results;
162     read_src_entry_all( $hash, $key, $results, \@non_results, $opts );
163 }
164 sub do_names_search {
165     my ($keywords, $packages, $postfixes, $read_entry, $opts,
166         $results, $non_results) = @_;
167
168     my $first_keyword = lc shift @$keywords;
169     @$keywords = map { lc $_ } @$keywords;
170
171     my ($key, $prefixes) = ($first_keyword, '');
172     my (%pkgs, %pkgs_min);
173     $postfixes->seq( $key, $prefixes, R_CURSOR );
174     while (index($key, $first_keyword) >= 0) {
175         if ($prefixes =~ /^(\^)?\001(\d+)/o) {
176             debug("$key has too many hits", 2 ) if DEBUG;
177             $too_many_hits += $2;
178             if ($1) { # use the empty prefix
179                 foreach my $k (@$keywords) {
180                     next unless $key =~ /\Q$k\E/;
181                 }
182                 debug("add key $key", 2) if DEBUG;
183                 $pkgs{$key}++;
184                 $pkgs_min{$key}++;
185             }
186         } else {
187           PREFIX:
188             foreach (split /\000/o, $prefixes) {
189                 $_ = '' if $_ eq '^';
190                 my $word = "$_$key";
191                 foreach my $k (@$keywords) {
192                     next PREFIX unless $word =~ /\Q$k\E/;
193                 }
194                 debug("add word $word", 2) if DEBUG;
195                 $pkgs{$word}++;
196                 $pkgs_min{$word}++ if $_ eq '';
197             }
198         }
199         last if $postfixes->seq( $key, $prefixes, R_NEXT ) != 0;
200         last if keys %pkgs_min >= 100;
201     }
202
203     my $nr = keys %pkgs;
204     my $min_nr = keys %pkgs_min;
205     debug("nr=$nr min_nr=$min_nr too_many_hits=$too_many_hits", 1) if DEBUG;
206     if ($nr >= 100) {
207         $too_many_hits += $nr - $min_nr + 1;
208         %pkgs = %pkgs_min;
209     }
210     foreach my $pkg (sort keys %pkgs) {
211         &$read_entry( $packages, $pkg, $results, $non_results, $opts );
212     }
213 }
214
215 sub do_xapian_search {
216     my ($keywords, $dbpath, $did2pkg, $packages, $read_entry, $opts,
217         $results, $non_results) = @_;
218
219 # NOTE: this needs to correspond with parse-packages!
220     my @tmp;
221     foreach my $keyword (@$keywords) {
222         $keyword =~ s;[^\w/+]+; ;og;
223         push @tmp, $keyword;
224     }
225     my $stemmer = Lingua::Stem->new();
226     my @stemmed_keywords = grep { length($_) } @{$stemmer->stem( @tmp )};
227
228     my $db = Search::Xapian::Database->new( $dbpath );
229     my $enq = $db->enquire( OP_OR, @$keywords, @stemmed_keywords );
230     debug( "Xapian Query was: ".$enq->get_query()->get_description(), 1) if DEBUG;
231     my @matches = $enq->matches(0, 999);
232
233     my (@order, %tmp_results);
234     foreach my $match ( @matches ) {
235         my $id = $match->get_docid();
236         my $result = $did2pkg->{$id};
237
238         foreach (split /\000/o, $result) {
239             my @data = split /\s/, $_, 3;
240             debug ("Considering $data[0], arch = $data[2], relevance=".$match->get_percent(), 3) if DEBUG;
241 #           next unless $data[2] eq 'all' || $opts->{h_archs}{$data[2]};
242 #           debug ("Ok", 3) if DEBUG;
243             unless ($tmp_results{$data[0]}++) {
244                 push @order, $data[0];
245             }
246         }
247         last if @order > 100;
248     }
249     undef $db;
250     $too_many_hits++ if @order > 100;
251
252     debug ("ORDER: @order", 2) if DEBUG;
253     foreach my $pkg (@order) {
254         &$read_entry( $packages, $pkg, $results, $non_results, $opts );
255     }
256 }
257
258 sub find_similar {
259     my ($pkg, $dbpath, $did2pkg) = @_;
260
261     my $db = Search::Xapian::Database->new( $dbpath );
262     my $enq = $db->enquire( "P$pkg" );
263     debug( "Xapian Query was: ".$enq->get_query()->get_description(), 1) if DEBUG;
264     my $first_match = ($enq->matches(0,1))[0]->get_document();
265
266     my @terms;
267     my $term_it = $first_match->termlist_begin();
268     my $term_end = $first_match->termlist_end();
269
270     for (; $term_it ne $term_end; $term_it++) {
271         debug( "TERM: ".$term_it->get_termname(), 3);
272         push @terms, $term_it->get_termname();
273     }
274
275     my $rel_enq = $db->enquire( OP_OR, @terms );
276     debug( "Xapian Query was: ".$rel_enq->get_query()->get_description(), 1) if DEBUG;
277     my @rel_pkg = $rel_enq->matches(2,20);
278
279 #    use Data::Dumper;
280 #    debug(Dumper(\@rel_pkg),1);
281
282     my (@order, %tmp_results);
283     foreach my $match ( @rel_pkg ) {
284         my $id = $match->get_docid();
285         my $result = $did2pkg->{$id};
286
287         foreach (split /\000/o, $result) {
288             my @data = split /\s/, $_, 3;
289             debug ("Considering $data[0], arch = $data[2], relevance=".$match->get_percent(), 3) if DEBUG;
290             next if $data[0] eq $pkg;
291             unless ($tmp_results{$data[0]}++) {
292                 push @order, $data[0];
293             }
294         }
295     }
296     undef $db;
297
298     debug ("ORDER: @order", 2) if DEBUG;
299     my $last = 10;
300     $last = $#order if $#order < $last;
301     return @order[0..$last];
302 }
303
304 sub find_binaries {
305     my ($pkg, $archive, $suite, $src2bin) = @_;
306
307     my $bins = $src2bin->{$pkg} || '';
308     my %bins;
309     foreach (split /\000/o, $bins) {
310         my @data = split /\s/, $_, 5;
311
312         debug( "find_binaries: considering @data", 3 ) if DEBUG;
313         if (($data[0] eq $archive)
314             && ($data[1] eq $suite)) {
315             $bins{$data[2]}++;
316             debug( "find_binaries: using @data", 3 ) if DEBUG;
317         }
318     }
319
320     return [ keys %bins ];
321 }
322
323
324 1;