]> git.deb.at Git - deb/packages.git/blobdiff - lib/Packages/Search.pm
Add an additional debug output to track $too_many_hits better
[deb/packages.git] / lib / Packages / Search.pm
index 000dc602d1b71249f58033352c45a92a8f02fbf3..c0555e2bb302b0dfdd241e742c9370ccae205cb4 100644 (file)
@@ -54,11 +54,10 @@ use Exporter;
 
 our @ISA = qw( Exporter );
 
-our @EXPORT_OK = qw( nextlink prevlink indexline
-                     resperpagelink
-                    read_entry read_entry_all read_src_entry find_binaries
+our @EXPORT_OK = qw( read_entry read_entry_all read_entry_simple
+                    read_src_entry read_src_entry_all find_binaries
                     do_names_search do_fulltext_search
-                    printindexline multipageheader );
+                    );
 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
 
 our $VERSION = 0.01;
@@ -294,59 +293,60 @@ sub printindexline {
     }
 }
 
-sub multipageheader {
-    my ( $input, $no_results, $opts ) = @_;
-
-    my ($start, $end);
-    if ($opts->{number} =~ /^all$/i) {
-       $start = 1;
-       $end = $no_results;
-       $opts->{number} = $no_results;
-       $opts->{number_all}++;
-    } else {
-       $start = Packages::Search::start( $opts );
-       $end = Packages::Search::end( $opts );
-       if ($end > $no_results) { $end = $no_results; }
-    }
-
-    print "<p>Found <em>$no_results</em> matching packages,";
-    if ($end == $start) {
-       print " displaying package $end.</p>";
-    } else {
-       print " displaying packages $start to $end.</p>";
-    }
-
-    printindexline( $input, $no_results, $opts );
-
-    if ($no_results > 100) {
-       print "<p>Results per page: ";
-       my @resperpagelinks;
-       for (50, 100, 200) {
-           if ($opts->{number} == $_) {
-               push @resperpagelinks, $_;
-           } else {
-               push @resperpagelinks, resperpagelink($input,$opts,$_);
-           }
-       }
-       if ($opts->{number_all}) {
-           push @resperpagelinks, "all";
-       } else {
-           push @resperpagelinks, resperpagelink($input, $opts, "all");
-       }
-       print join( " | ", @resperpagelinks )."</p>";
-    }
-    return ( $start, $end );
-}
+#sub multipageheader {
+#    my ( $input, $no_results, $opts ) = @_;
+#
+#    my ($start, $end);
+#    if ($opts->{number} =~ /^all$/i) {
+#      $start = 1;
+#      $end = $no_results;
+#      $opts->{number} = $no_results;
+#      $opts->{number_all}++;
+#    } else {
+#      $start = Packages::Search::start( $opts );
+#      $end = Packages::Search::end( $opts );
+#      if ($end > $no_results) { $end = $no_results; }
+#    }
+#
+#      print "<p>Found <em>$no_results</em> matching packages,";
+#    if ($end == $start) {
+#      print " displaying package $end.</p>";
+#    } else {
+#      print " displaying packages $start to $end.</p>";
+#    }
+#
+#    printindexline( $input, $no_results, $opts );
+#
+#    if ($no_results > 100) {
+#      print "<p>Results per page: ";
+#      my @resperpagelinks;
+#      for (50, 100, 200) {
+#          if ($opts->{number} == $_) {
+#              push @resperpagelinks, $_;
+#          } else {
+#              push @resperpagelinks, resperpagelink($input,$opts,$_);
+#          }
+#      }
+#      if ($opts->{number_all}) {
+#          push @resperpagelinks, "all";
+#      } else {
+#          push @resperpagelinks, resperpagelink($input, $opts, "all");
+#      }
+#      print join( " | ", @resperpagelinks )."</p>";
+#    }
+#    return ( $start, $end );
+#}
 
 sub read_entry_all {
     my ($hash, $key, $results, $non_results, $opts) = @_;
     my $result = $hash->{$key} || '';
-    foreach (split /\000/, $result) {
-       my @data = split ( /\s/, $_, 8 );
+    foreach (split /\000/o, $result) {
+       my @data = split ( /\s/o, $_, 8 );
        debug( "Considering entry ".join( ':', @data), 2);
        if ($opts->{h_archives}{$data[0]} && $opts->{h_suites}{$data[1]}
-           && ($opts->{h_archs}{$data[2]} || $data[2] eq 'all')
-           && $opts->{h_sections}{$data[3]}) {
+           && ($opts->{h_archs}{$data[2]} || $data[2] eq 'all'
+               || $data[2] eq 'virtual')
+           && ($opts->{h_sections}{$data[3]} || $data[3] eq 'v')) {
            debug( "Using entry ".join( ':', @data), 2);
            push @$results, [ $key, @data ];
        } else {
@@ -359,100 +359,119 @@ sub read_entry {
     my @non_results;
     read_entry_all( $hash, $key, $results, \@non_results, $opts );
 }
-sub read_src_entry {
-    my ($hash, $key, $results, $opts) = @_;
+sub read_entry_simple {
+    my ($hash, $key, $archives, $suite) = @_;
     my $result = $hash->{$key} || '';
-    foreach (split /\000/, $result) {
-       my @data = split ( /\s/, $_, 6 );
+    debug( "read_entry_simple: key=$key, archives=".
+          join(" ",(keys %$archives)).", suite=$suite", 1);
+    my @data_fuzzy;
+    foreach (split /\000/o, $result) {
+       my @data = split ( /\s/o, $_, 8 );
+       debug( "Considering entry ".join( ':', @data), 2);
+       if ($data[1] eq $suite) {
+           if ($archives->{$data[0]}) {
+               debug( "Using entry ".join( ':', @data), 2);
+               return \@data;
+           } elsif ($data[0] eq 'us') {
+               debug( "Fuzzy entry ".join( ':', @data), 2);
+               @data_fuzzy = @data;
+           }
+       } 
+    }
+    return \@data_fuzzy;
+}
+sub read_src_entry_all {
+    my ($hash, $key, $results, $non_results, $opts) = @_;
+    my $result = $hash->{$key} || '';
+    debug( "read_src_entry_all: key=$key", 1);
+    foreach (split /\000/o, $result) {
+       my @data = split ( /\s/o, $_, 6 );
        debug( "Considering entry ".join( ':', @data), 2);
        if ($opts->{h_archives}{$data[0]}
            && $opts->{h_suites}{$data[1]}
            && $opts->{h_sections}{$data[2]}) {
            debug( "Using entry ".join( ':', @data), 2);
            push @$results, [ $key, @data ];
+       } else {
+           push @$non_results, [ $key, @data ];
        }
     }
 }
+sub read_src_entry {
+    my ($hash, $key, $results, $opts) = @_;
+    my @non_results;
+    read_src_entry_all( $hash, $key, $results, \@non_results, $opts );
+}
 sub do_names_search {
-    my ($keyword, $packages, $postfixes, $read_entry, $opts) = @_;
-    my @results;
+    my ($keyword, $packages, $postfixes, $read_entry, $opts,
+       $results, $non_results) = @_;
 
-    $keyword = lc $keyword unless $opts->{case_bool};
+    $keyword = lc $keyword;
         
-    if ($opts->{exact}) {
-       &$read_entry( $packages, $keyword, \@results, $opts );
-    } else {
-       my ($key, $prefixes) = ($keyword, '');
-       my %pkgs;
-       $postfixes->seq( $key, $prefixes, R_CURSOR );
-       while (index($key, $keyword) >= 0) {
-            if ($prefixes =~ /^\001(\d+)/o) {
-                $too_many_hits += $1;
-            } else {
-               foreach (split /\000/o, $prefixes) {
-                   $_ = '' if $_ eq '^';
-                   debug( "add word $_$key", 2);
-                   $pkgs{$_.$key}++;
-               }
+    my ($key, $prefixes) = ($keyword, '');
+    my %pkgs;
+    $postfixes->seq( $key, $prefixes, R_CURSOR );
+    while (index($key, $keyword) >= 0) {
+       if ($prefixes =~ /^\001(\d+)/o) {
+           debug( "$key has too many hits", 2 );
+           $too_many_hits += $1;
+       } else {
+           foreach (split /\000/o, $prefixes) {
+               $_ = '' if $_ eq '^';
+               debug( "add word $_$key", 2);
+               $pkgs{$_.$key}++;
            }
-           last if $postfixes->seq( $key, $prefixes, R_NEXT ) != 0;
-           last if $too_many_hits or keys %pkgs >= 100;
-       }
-        
-        my $no_results = keys %pkgs;
-        if ($too_many_hits || ($no_results >= 100)) {
-           $too_many_hits += $no_results;
-           %pkgs = ( $keyword => 1 );
-       }
-       foreach my $pkg (sort keys %pkgs) {
-           &$read_entry( $packages, $pkg, \@results, $opts );
        }
+       last if $postfixes->seq( $key, $prefixes, R_NEXT ) != 0;
+       last if $too_many_hits or keys %pkgs >= 100;
+    }
+    
+    my $no_results = keys %pkgs;
+    if ($too_many_hits || ($no_results >= 100)) {
+       $too_many_hits += $no_results;
+       %pkgs = ( $keyword => 1 );
+    }
+    foreach my $pkg (sort keys %pkgs) {
+       &$read_entry( $packages, $pkg, $results, $non_results, $opts );
     }
-    return \@results;
 }
 sub do_fulltext_search {
-    my ($keyword, $file, $did2pkg, $packages, $read_entry, $opts) = @_;
-    my @results;
+    my ($keyword, $file, $did2pkg, $packages, $read_entry, $opts,
+       $results, $non_results) = @_;
 
-    my @lines;
-    my $regex;
-    if ($opts->{case_bool}) {
-       if ($opts->{exact}) {
-           $regex = qr/\b\Q$keyword\E\b/o;
-       } else {
-           $regex = qr/\Q$keyword\E/o;
-       }
-    } else {
-       if ($opts->{exact}) {
-           $regex = qr/\b\Q$keyword\E\b/io;
-       } else {
-           $regex = qr/\Q$keyword\E/io;
-       }
+# NOTE: this needs to correspond with parse-packages!
+    $keyword =~ tr [A-Z] [a-z];
+    if ($opts->{exact}) {
+       $keyword = " $keyword ";
     }
+    $keyword =~ s/[(),.-]+//og;
+    $keyword =~ s#[^a-z0-9_/+]+# #og;
 
+    my $numres = 0;
+    my %tmp_results;
     open DESC, '<', "$file"
        or die "couldn't open $file: $!";
     while (<DESC>) {
-       $_ =~ $regex or next;
-       debug( "Matched line $.", 2);
-       push @lines, $.;
-    }
-    close DESC;
-
-    my %tmp_results;
-    foreach my $l (@lines) {
-       my $result = $did2pkg->{$l};
+       next if (index $_, $keyword) < 0;
+       debug( "Matched line $.: $_", 2);
+       my $result = $did2pkg->{$.};
        foreach (split /\000/o, $result) {
            my @data = split /\s/, $_, 3;
-           next unless $opts->{h_archs}{$data[2]};
-           $tmp_results{$data[0]}++;
+#          debug ("Considering $data[0], arch = $data[2]", 3);
+#          next unless $data[2] eq 'all' || $opts->{h_archs}{$data[2]};
+#          debug ("Ok", 3);
+           $numres++ unless $tmp_results{$data[0]}++;
        }
+       last if $numres > 100;
     }
+    close DESC;
+    $too_many_hits++ if $numres > 100;
+
+    my @results;
     foreach my $pkg (keys %tmp_results) {
-       &$read_entry( $packages, $pkg, \@results, $opts );
+       &$read_entry( $packages, $pkg, $results, $non_results, $opts );
     }
-    return \@results;
-}
+ }
 
 sub find_binaries {
     my ($pkg, $archive, $suite, $src2bin) = @_;
@@ -462,9 +481,11 @@ sub find_binaries {
     foreach (split /\000/o, $bins) {
        my @data = split /\s/, $_, 5;
 
+       debug( "find_binaries: considering @data", 3 );
        if (($data[0] eq $archive)
            && ($data[1] eq $suite)) {
            $bins{$data[2]}++;
+           debug( "find_binaries: using @data", 3 );
        }
     }