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;
46 use CGI qw( -oldstyle_urls );
55 our @ISA = qw( Exporter );
57 our @EXPORT_OK = qw( nextlink prevlink indexline
59 read_entry read_entry_all read_entry_simple
60 read_src_entry read_src_entry_all find_binaries
61 do_names_search do_fulltext_search
62 printindexline multipageheader );
63 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
67 our $USE_PAGED_MODE = 1;
68 use constant DEFAULT_PAGE => 1;
69 use constant DEFAULT_RES_PER_PAGE => 50;
70 our %page_params = ( page => { default => DEFAULT_PAGE,
72 number => { default => DEFAULT_RES_PER_PAGE,
75 our $too_many_hits = 0;
78 my ( $cgi, $params_def, $opts ) = @_;
80 my %params_ret = ( values => {}, errors => {} );
82 if ($USE_PAGED_MODE) {
83 debug( "Use PAGED_MODE", 2 );
84 %params = %$params_def;
85 foreach (keys %page_params) {
88 %params = ( %params, %page_params );
90 %params = %$params_def;
93 foreach my $param ( keys %params ) {
95 debug( "Param <strong>$param</strong>", 2 );
97 my $p_value_orig = $cgi->param($param);
99 if (!defined($p_value_orig)
100 && defined $params_def->{$param}{alias}
101 && defined $cgi->param($params_def->{$param}{alias})) {
102 $p_value_orig = $cgi->param($params_def->{$param}{alias});
103 debug( "Used alias <strong>$params_def->{$param}{alias}</strong>",
107 my @p_value = ($p_value_orig);
109 debug( "Value (Orig) ".($p_value_orig||""), 2 );
111 if ($params_def->{$param}{array} && defined $p_value_orig) {
112 @p_value = split /$params_def->{$param}{array}/, $p_value_orig;
113 debug( "Value (Array Split) ". join('##',@p_value), 2 );
116 if ($params_def->{$param}{match} && defined $p_value_orig) {
118 { $_ =~ m/$params_def->{$param}{match}/; $_ = $1 }
121 @p_value = grep { defined $_ } @p_value;
123 debug( "Value (Match) ". join('##',@p_value), 2 );
126 if (defined $params{$param}{default}) {
127 @p_value = ($params{$param}{default});
130 $params_ret{errors}{$param} = "undef";
135 debug( "Value (Default) ". join('##',@p_value), 2 );
136 my @p_value_no_replace = @p_value;
138 if ($params{$param}{replace} && @p_value) {
140 foreach my $pattern (keys %{$params{$param}{replace}}) {
141 foreach (@p_value_no_replace) {
142 if ($_ eq $pattern) {
143 my $replacement = $params{$param}{replace}{$_};
144 if (ref $replacement) {
145 push @p_value, @$replacement;
147 push @p_value, $replacement;
156 debug( "Value (Final) ". join('##',@p_value), 2 );
158 if ($params_def->{$param}{array}) {
159 $params_ret{values}{$param} = {
160 orig => $p_value_orig,
161 no_replace => \@p_value_no_replace,
164 @{$params_def->{$param}{var}} = @p_value
165 if $params_def->{$param}{var};
167 $params_ret{values}{$param} = {
168 orig => $p_value_orig,
169 no_replace => $p_value_no_replace[0],
170 final => $p_value[0],
172 ${$params_def->{$param}{var}} = $p_value[0]
173 if $params_def->{$param}{var};
175 $opts->{$param} = $params_ret{values}{$param}{final} if $opts;
178 if ($USE_PAGED_MODE) {
179 $cgi->delete( "page" );
180 $cgi->delete( "number" );
189 my $page = $params->{values}{page}{final}
191 my $res_per_page = $params->{values}{number}{final}
192 || DEFAULT_RES_PER_PAGE;
194 return 1 if $res_per_page =~ /^all$/i;
195 return $res_per_page * ($page - 1) + 1;
202 debug( "end: ".Dumper($params) );
203 my $page = $params->{page}
205 my $res_per_page = $params->{number}
206 || DEFAULT_RES_PER_PAGE;
208 return $page * $res_per_page;
212 my ($cgi, $params, $num_res) = @_;
215 my $page = $params->{page}
217 my $res_per_page = $params->{number}
218 || DEFAULT_RES_PER_PAGE;
219 my $numpages = ceil($num_res /
221 for (my $i = 1; $i <= $numpages; $i++) {
225 $index_line .= "<a href=\"".encode_entities($cgi->self_url).
226 "&page=$i&number=$res_per_page\">".
229 if ($i < $numpages) {
230 $index_line .= " | ";
237 my ($cgi, $params, $no_results ) = @_;
239 my $page = $params->{page}
242 my $res_per_page = $params->{number}
243 || DEFAULT_RES_PER_PAGE;
245 if ((($page-1)*$res_per_page + 1) > $no_results) {
249 return "<a href=\"".encode_entities($cgi->self_url).
250 "&page=$page&number=$res_per_page\">>></a>";
254 my ($cgi, $params ) = @_;
256 my $page = $params->{page}
263 my $res_per_page = $params->{number}
264 || DEFAULT_RES_PER_PAGE;
266 return "<a href=\"".encode_entities($cgi->self_url).
267 "&page=$page&number=$res_per_page\"><<</a>";
271 my ($cgi, $params, $res_per_page ) = @_;
274 if ($res_per_page =~ /^all$/i) {
277 $page = ceil(start( $params ) / $res_per_page);
280 return "<a href=\"".encode_entities($cgi->self_url).
281 "&page=$page&number=$res_per_page\">$res_per_page</a>";
285 my ( $input, $no_results, $opts ) = @_;
288 if ($no_results > $opts->{number}) {
290 $index_line = prevlink( $input, $opts)." | ".
291 indexline( $input, $opts, $no_results)." | ".
292 nextlink( $input, $opts, $no_results);
294 print "<p style=\"text-align:center\">$index_line</p>";
298 sub multipageheader {
299 my ( $input, $no_results, $opts ) = @_;
302 if ($opts->{number} =~ /^all$/i) {
305 $opts->{number} = $no_results;
306 $opts->{number_all}++;
308 $start = Packages::Search::start( $opts );
309 $end = Packages::Search::end( $opts );
310 if ($end > $no_results) { $end = $no_results; }
313 print "<p>Found <em>$no_results</em> matching packages,";
314 if ($end == $start) {
315 print " displaying package $end.</p>";
317 print " displaying packages $start to $end.</p>";
320 printindexline( $input, $no_results, $opts );
322 if ($no_results > 100) {
323 print "<p>Results per page: ";
326 if ($opts->{number} == $_) {
327 push @resperpagelinks, $_;
329 push @resperpagelinks, resperpagelink($input,$opts,$_);
332 if ($opts->{number_all}) {
333 push @resperpagelinks, "all";
335 push @resperpagelinks, resperpagelink($input, $opts, "all");
337 print join( " | ", @resperpagelinks )."</p>";
339 return ( $start, $end );
343 my ($hash, $key, $results, $non_results, $opts) = @_;
344 my $result = $hash->{$key} || '';
345 foreach (split /\000/o, $result) {
346 my @data = split ( /\s/o, $_, 8 );
347 debug( "Considering entry ".join( ':', @data), 2);
348 if ($opts->{h_archives}{$data[0]} && $opts->{h_suites}{$data[1]}
349 && ($opts->{h_archs}{$data[2]} || $data[2] eq 'all')
350 && $opts->{h_sections}{$data[3]}) {
351 debug( "Using entry ".join( ':', @data), 2);
352 push @$results, [ $key, @data ];
354 push @$non_results, [ $key, @data ];
359 my ($hash, $key, $results, $opts) = @_;
361 read_entry_all( $hash, $key, $results, \@non_results, $opts );
363 sub read_entry_simple {
364 my ($hash, $key, $archives, $suite) = @_;
365 my $result = $hash->{$key} || '';
367 foreach (split /\000/o, $result) {
368 my @data = split ( /\s/o, $_, 8 );
369 debug( "Considering entry ".join( ':', @data), 2);
370 if ($data[1] eq $suite) {
371 if ($archives->{$data[0]}) {
372 debug( "Using entry ".join( ':', @data), 2);
374 } elsif ($data[0] eq 'us') {
375 debug( "Fuzzy entry ".join( ':', @data), 2);
382 sub read_src_entry_all {
383 my ($hash, $key, $results, $non_results, $opts) = @_;
384 my $result = $hash->{$key} || '';
385 foreach (split /\000/o, $result) {
386 my @data = split ( /\s/o, $_, 6 );
387 debug( "Considering entry ".join( ':', @data), 2);
388 if ($opts->{h_archives}{$data[0]}
389 && $opts->{h_suites}{$data[1]}
390 && $opts->{h_sections}{$data[2]}) {
391 debug( "Using entry ".join( ':', @data), 2);
392 push @$results, [ $key, @data ];
394 push @$non_results, [ $key, @data ];
399 my ($hash, $key, $results, $opts) = @_;
401 read_src_entry_all( $hash, $key, $results, \@non_results, $opts );
403 sub do_names_search {
404 my ($keyword, $packages, $postfixes, $read_entry, $opts) = @_;
407 $keyword = lc $keyword unless $opts->{case_bool};
409 if ($opts->{exact}) {
410 &$read_entry( $packages, $keyword, \@results, $opts );
412 my ($key, $prefixes) = ($keyword, '');
414 $postfixes->seq( $key, $prefixes, R_CURSOR );
415 while (index($key, $keyword) >= 0) {
416 if ($prefixes =~ /^\001(\d+)/o) {
417 $too_many_hits += $1;
419 foreach (split /\000/o, $prefixes) {
420 $_ = '' if $_ eq '^';
421 debug( "add word $_$key", 2);
425 last if $postfixes->seq( $key, $prefixes, R_NEXT ) != 0;
426 last if $too_many_hits or keys %pkgs >= 100;
429 my $no_results = keys %pkgs;
430 if ($too_many_hits || ($no_results >= 100)) {
431 $too_many_hits += $no_results;
432 %pkgs = ( $keyword => 1 );
434 foreach my $pkg (sort keys %pkgs) {
435 &$read_entry( $packages, $pkg, \@results, $opts );
440 sub do_fulltext_search {
441 my ($keyword, $file, $did2pkg, $packages, $read_entry, $opts) = @_;
446 if ($opts->{case_bool}) {
447 if ($opts->{exact}) {
448 $regex = qr/\b\Q$keyword\E\b/o;
450 $regex = qr/\Q$keyword\E/o;
453 if ($opts->{exact}) {
454 $regex = qr/\b\Q$keyword\E\b/io;
456 $regex = qr/\Q$keyword\E/io;
460 open DESC, '<', "$file"
461 or die "couldn't open $file: $!";
463 $_ =~ $regex or next;
464 debug( "Matched line $.", 2);
470 foreach my $l (@lines) {
471 my $result = $did2pkg->{$l};
472 foreach (split /\000/o, $result) {
473 my @data = split /\s/, $_, 3;
474 next unless $opts->{h_archs}{$data[2]};
475 $tmp_results{$data[0]}++;
478 foreach my $pkg (keys %tmp_results) {
479 &$read_entry( $packages, $pkg, \@results, $opts );
485 my ($pkg, $archive, $suite, $src2bin) = @_;
487 my $bins = $src2bin->{$pkg} || '';
489 foreach (split /\000/o, $bins) {
490 my @data = split /\s/, $_, 5;
492 if (($data[0] eq $archive)
493 && ($data[1] eq $suite)) {
498 return [ keys %bins ];