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;
55 our @ISA = qw( Exporter );
57 our @EXPORT_OK = qw( read_entry read_entry_all read_entry_simple
58 read_src_entry read_src_entry_all find_binaries
59 do_names_search do_fulltext_search
61 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
65 our $USE_PAGED_MODE = 1;
66 use constant DEFAULT_PAGE => 1;
67 use constant DEFAULT_RES_PER_PAGE => 50;
68 our %page_params = ( page => { default => DEFAULT_PAGE,
70 number => { default => DEFAULT_RES_PER_PAGE,
73 our $too_many_hits = 0;
76 my ( $cgi, $params_def, $opts ) = @_;
78 my %params_ret = ( values => {}, errors => {} );
80 if ($USE_PAGED_MODE) {
81 debug( "Use PAGED_MODE", 2 ) if DEBUG;
82 %params = %$params_def;
83 foreach (keys %page_params) {
86 %params = ( %params, %page_params );
88 %params = %$params_def;
91 foreach my $param ( keys %params ) {
93 debug( "Param <strong>$param</strong>", 2 ) if DEBUG;
95 my $p_value_orig = $cgi->param($param);
97 if (!defined($p_value_orig)
98 && defined $params_def->{$param}{alias}
99 && defined $cgi->param($params_def->{$param}{alias})) {
100 $p_value_orig = $cgi->param($params_def->{$param}{alias});
101 debug( "Used alias <strong>$params_def->{$param}{alias}</strong>",
105 my @p_value = ($p_value_orig);
107 debug( "Value (Orig) ".($p_value_orig||""), 2 ) if DEBUG;
109 if ($params_def->{$param}{array} && defined $p_value_orig) {
110 @p_value = split /$params_def->{$param}{array}/, $p_value_orig;
111 debug( "Value (Array Split) ". join('##',@p_value), 2 ) if DEBUG;
114 if ($params_def->{$param}{match} && defined $p_value_orig) {
116 { $_ =~ m/$params_def->{$param}{match}/; $_ = $1 }
119 @p_value = grep { defined $_ } @p_value;
121 debug( "Value (Match) ". join('##',@p_value), 2 ) if DEBUG;
124 if (defined $params{$param}{default}) {
125 @p_value = ($params{$param}{default});
128 $params_ret{errors}{$param} = "undef";
133 debug( "Value (Default) ". join('##',@p_value), 2 ) if DEBUG;
134 my @p_value_no_replace = @p_value;
136 if ($params{$param}{replace} && @p_value) {
137 foreach my $pattern (keys %{$params{$param}{replace}}) {
138 my @p_value_tmp = @p_value;
140 foreach (@p_value_tmp) {
141 if ($_ eq $pattern) {
142 my $replacement = $params{$param}{replace}{$_};
143 if (ref $replacement) {
144 push @p_value, @$replacement;
146 push @p_value, $replacement;
155 debug( "Value (Final) ". join('##',@p_value), 2 ) if DEBUG;
157 if ($params_def->{$param}{array}) {
158 $params_ret{values}{$param} = {
159 orig => $p_value_orig,
160 no_replace => \@p_value_no_replace,
163 @{$params_def->{$param}{var}} = @p_value
164 if $params_def->{$param}{var};
166 $params_ret{values}{$param} = {
167 orig => $p_value_orig,
168 no_replace => $p_value_no_replace[0],
169 final => $p_value[0],
171 ${$params_def->{$param}{var}} = $p_value[0]
172 if $params_def->{$param}{var};
174 $opts->{$param} = $params_ret{values}{$param}{final} if $opts;
177 if ($USE_PAGED_MODE) {
178 $cgi->delete( "page" );
179 $cgi->delete( "number" );
188 my $page = $params->{values}{page}{final}
190 my $res_per_page = $params->{values}{number}{final}
191 || DEFAULT_RES_PER_PAGE;
193 return 1 if $res_per_page =~ /^all$/i;
194 return $res_per_page * ($page - 1) + 1;
201 debug( "end: ".Dumper($params) ) if DEBUG;
202 my $page = $params->{page}
204 my $res_per_page = $params->{number}
205 || DEFAULT_RES_PER_PAGE;
207 return $page * $res_per_page;
211 my ($cgi, $params, $num_res) = @_;
214 my $page = $params->{page}
216 my $res_per_page = $params->{number}
217 || DEFAULT_RES_PER_PAGE;
218 my $numpages = ceil($num_res /
220 for (my $i = 1; $i <= $numpages; $i++) {
224 $index_line .= "<a href=\"".encode_entities($cgi->self_url).
225 "&page=$i&number=$res_per_page\">".
228 if ($i < $numpages) {
229 $index_line .= " | ";
236 my ($cgi, $params, $no_results ) = @_;
238 my $page = $params->{page}
241 my $res_per_page = $params->{number}
242 || DEFAULT_RES_PER_PAGE;
244 if ((($page-1)*$res_per_page + 1) > $no_results) {
248 return "<a href=\"".encode_entities($cgi->self_url).
249 "&page=$page&number=$res_per_page\">>></a>";
253 my ($cgi, $params ) = @_;
255 my $page = $params->{page}
262 my $res_per_page = $params->{number}
263 || DEFAULT_RES_PER_PAGE;
265 return "<a href=\"".encode_entities($cgi->self_url).
266 "&page=$page&number=$res_per_page\"><<</a>";
270 my ($cgi, $params, $res_per_page ) = @_;
273 if ($res_per_page =~ /^all$/i) {
276 $page = ceil(start( $params ) / $res_per_page);
279 return "<a href=\"".encode_entities($cgi->self_url).
280 "&page=$page&number=$res_per_page\">$res_per_page</a>";
284 my ( $input, $no_results, $opts ) = @_;
287 if ($no_results > $opts->{number}) {
289 $index_line = prevlink( $input, $opts)." | ".
290 indexline( $input, $opts, $no_results)." | ".
291 nextlink( $input, $opts, $no_results);
293 print "<p style=\"text-align:center\">$index_line</p>";
297 #sub multipageheader {
298 # my ( $input, $no_results, $opts ) = @_;
301 # if ($opts->{number} =~ /^all$/i) {
303 # $end = $no_results;
304 # $opts->{number} = $no_results;
305 # $opts->{number_all}++;
307 # $start = Packages::Search::start( $opts );
308 # $end = Packages::Search::end( $opts );
309 # if ($end > $no_results) { $end = $no_results; }
312 # print "<p>Found <em>$no_results</em> matching packages,";
313 # if ($end == $start) {
314 # print " displaying package $end.</p>";
316 # print " displaying packages $start to $end.</p>";
319 # printindexline( $input, $no_results, $opts );
321 # if ($no_results > 100) {
322 # print "<p>Results per page: ";
323 # my @resperpagelinks;
324 # for (50, 100, 200) {
325 # if ($opts->{number} == $_) {
326 # push @resperpagelinks, $_;
328 # push @resperpagelinks, resperpagelink($input,$opts,$_);
331 # if ($opts->{number_all}) {
332 # push @resperpagelinks, "all";
334 # push @resperpagelinks, resperpagelink($input, $opts, "all");
336 # print join( " | ", @resperpagelinks )."</p>";
338 # return ( $start, $end );
342 my ($hash, $key, $results, $non_results, $opts) = @_;
343 my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-", 2;
345 my %virt = split /\01/o, $virt;
346 while (my ($suite, $provides) = each %virt) {
347 next if $suite eq '-';
348 if ($opts->{h_suites}{$suite}) {
349 push @$results, [ $key, "-", $suite, 'virtual', 'v', 'v', 'v', 'v',
352 push @$non_results, [ $key, "-", $suite, 'virtual', 'v', 'v', 'v', 'v',
357 foreach (split /\000/o, $result) {
358 my @data = split ( /\s/o, $_, 8 );
359 debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
360 if ($opts->{h_suites}{$data[1]}
361 && ($opts->{h_archs}{$data[2]} || $data[2] eq 'all')
362 && $opts->{h_sections}{$data[3]}) {
363 debug( "Using entry ".join( ':', @data), 2) if DEBUG;
364 push @$results, [ $key, @data ];
366 push @$non_results, [ $key, @data ];
371 my ($hash, $key, $results, $opts) = @_;
373 read_entry_all( $hash, $key, $results, \@non_results, $opts );
376 #FIXME: make configurable
377 my %fallback_suites = (
378 'stable-backports' => 'stable',
379 'stable-volatile' => 'stable',
380 experimental => 'unstable' );
382 sub read_entry_simple {
383 my ($hash, $key, $archives, $suite) = @_;
384 # FIXME: drop $archives
386 my ($virt, $result) = split /\000/o, $hash->{$key} || "-\01-\0", 2;
387 my %virt = split /\01/o, $virt;
388 debug( "read_entry_simple: key=$key, archives=".
389 join(" ",(keys %$archives)).", suite=$suite", 1) if DEBUG;
390 # FIXME: not all of the 2^4=16 combinations of empty(results),
391 # empty(virt{suite}), empty(fb_result), empty(virt{fb_suite}) are dealt
392 # with correctly, but it's adequate enough for now
393 return [ $virt{$suite} ] unless defined $result;
394 foreach (split /\000/o, $result) {
395 my @data = split ( /\s/o, $_, 8 );
396 debug( "use entry: @data", 2 ) if DEBUG && $data[1] eq $suite;
397 return [ $virt{$suite}, @data ] if $data[1] eq $suite;
399 if (my $fb_suite = $fallback_suites{$suite}) {
400 my $fb_result = read_entry_simple( $hash, $key, $archives, $fb_suite );
401 my $fb_virt = shift(@$fb_result);
402 $virt{$suite} .= $fb_virt if $fb_virt;
403 return [ $virt{$suite}, @$fb_result ] if @$fb_result;
405 return [ $virt{$suite} ];
408 sub read_src_entry_all {
409 my ($hash, $key, $results, $non_results, $opts) = @_;
410 my $result = $hash->{$key} || '';
411 debug( "read_src_entry_all: key=$key", 1) if DEBUG;
412 foreach (split /\000/o, $result) {
413 my @data = split ( /\s/o, $_, 6 );
414 debug( "Considering entry ".join( ':', @data), 2) if DEBUG;
415 if ($opts->{h_archives}{$data[0]}
416 && $opts->{h_suites}{$data[1]}
417 && $opts->{h_sections}{$data[2]}) {
418 debug( "Using entry ".join( ':', @data), 2) if DEBUG;
419 push @$results, [ $key, @data ];
421 push @$non_results, [ $key, @data ];
426 my ($hash, $key, $results, $opts) = @_;
428 read_src_entry_all( $hash, $key, $results, \@non_results, $opts );
430 sub do_names_search {
431 my ($keyword, $packages, $postfixes, $read_entry, $opts,
432 $results, $non_results) = @_;
434 $keyword = lc $keyword;
436 my ($key, $prefixes) = ($keyword, '');
438 $postfixes->seq( $key, $prefixes, R_CURSOR );
439 while (index($key, $keyword) >= 0) {
440 if ($prefixes =~ /^\001(\d+)/o) {
441 debug( "$key has too many hits", 2 ) if DEBUG;
442 $too_many_hits += $1;
444 foreach (split /\000/o, $prefixes) {
445 $_ = '' if $_ eq '^';
446 debug( "add word $_$key", 2) if DEBUG;
450 last if $postfixes->seq( $key, $prefixes, R_NEXT ) != 0;
451 last if $too_many_hits or keys %pkgs >= 100;
454 my $no_results = keys %pkgs;
455 if ($too_many_hits || ($no_results >= 100)) {
456 $too_many_hits += $no_results;
457 %pkgs = ( $keyword => 1 );
459 foreach my $pkg (sort keys %pkgs) {
460 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
463 sub do_fulltext_search {
464 my ($keyword, $file, $did2pkg, $packages, $read_entry, $opts,
465 $results, $non_results) = @_;
467 # NOTE: this needs to correspond with parse-packages!
468 $keyword =~ tr [A-Z] [a-z];
469 if ($opts->{exact}) {
470 $keyword = " $keyword ";
472 $keyword =~ s/[(),.-]+//og;
473 $keyword =~ s#[^a-z0-9_/+]+# #og;
477 # fgrep is seriously faster than using perl
478 open DESC, '-|', 'fgrep', '-n', '--', $keyword, $file
479 or die "couldn't open $file: $!";
483 debug( "Matched line $_", 2) if DEBUG;
484 my $result = $did2pkg->{$nr};
485 foreach (split /\000/o, $result) {
486 my @data = split /\s/, $_, 3;
487 # debug ("Considering $data[0], arch = $data[2]", 3) if DEBUG;
488 # next unless $data[2] eq 'all' || $opts->{h_archs}{$data[2]};
489 # debug ("Ok", 3) if DEBUG;
490 $numres++ unless $tmp_results{$data[0]}++;
492 last if $numres > 100;
495 $too_many_hits++ if $numres > 100;
498 foreach my $pkg (keys %tmp_results) {
499 &$read_entry( $packages, $pkg, $results, $non_results, $opts );
504 my ($pkg, $archive, $suite, $src2bin) = @_;
506 my $bins = $src2bin->{$pkg} || '';
508 foreach (split /\000/o, $bins) {
509 my @data = split /\s/, $_, 5;
511 debug( "find_binaries: considering @data", 3 ) if DEBUG;
512 if (($data[0] eq $archive)
513 && ($data[1] eq $suite)) {
515 debug( "find_binaries: using @data", 3 ) if DEBUG;
519 return [ keys %bins ];