3 # search_packages.pl -- CGI interface to the Packages files on packages.debian.org
5 # Copyright (C) 1998 James Treacy
6 # Copyright (C) 2000, 2001 Josip Rodin
7 # Copyright (C) 2001 Adam Heath
8 # Copyright (C) 2004 Martin Schulze
9 # Copyright (C) 2004-2006 Frank Lichtenheld
11 # use is allowed under the terms of the GNU Public License (GPL)
12 # see http://www.fsf.org/copyleft/gpl.html for a copy of the license
15 use CGI qw( -oldstyle_urls );
16 use CGI::Carp qw( fatalsToBrowser );
26 use Packages::Search qw( :all );
27 use Packages::HTML ();
29 my $thisscript = $Packages::HTML::SEARCH_CGI;
30 my $HOME = "http://www.debian.org";
32 my $SEARCHPAGE = "http://packages.debian.org/";
33 my @SUITES = qw( oldstable stable testing unstable experimental );
34 my @SECTIONS = qw( main contrib non-free );
35 my @ARCHIVES = qw( us security installer );
36 my @ARCHITECTURES = qw( alpha amd64 arm hppa hurd-i386 i386 ia64
37 kfreebsd-i386 mips mipsel powerpc s390 sparc );
38 my %SUITES = map { $_ => 1 } @SUITES;
39 my %SECTIONS = map { $_ => 1 } @SECTIONS;
40 my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
41 my %ARCHITECTURES = map { $_ => 1 } @ARCHITECTURES;
43 $ENV{PATH} = "/bin:/usr/bin";
45 # Read in all the variables set by the form
47 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
48 $input = new CGI(\*STDIN);
53 my $pet0 = new Benchmark;
54 # use this to disable debugging in production mode completly
55 my $debug_allowed = 1;
56 my $debug = $debug_allowed && $input->param("debug");
57 $debug = 0 if not defined($debug);
58 $Packages::Search::debug = 1 if $debug > 1;
60 # If you want, just print out a list of all of the variables and exit.
61 print $input->header if $debug;
65 if (my $path = $input->param('path')) {
66 my @components = map { lc $_ } split /\//, $path;
68 foreach (@components) {
70 $input->param('suite', $_);
71 } elsif ($SECTIONS{$_}) {
72 $input->param('section', $_);
73 } elsif ($ARCHIVES{$_}) {
74 $input->param('archive', $_);
75 }elsif ($ARCHITECTURES{$_}) {
76 $input->param('arch', $_);
81 my ( $format, $keyword, $case, $subword, $exact, $searchon,
82 @suites, @sections, @archs );
84 my %params_def = ( keywords => { default => undef,
85 match => '^\s*([-+\@\w\/.:]+)\s*$',
87 suite => { default => 'stable', match => '^(\w+)$',
88 alias => 'version', array => ',',
90 replace => { all => \@SUITES } },
91 case => { default => 'insensitive', match => '^(\w+)$',
93 # official => { default => 0, match => '^(\w+)$' },
94 # use_cache => { default => 1, match => '^(\w+)$' },
95 subword => { default => 0, match => '^(\w+)$',
97 exact => { default => undef, match => '^(\w+)$',
99 searchon => { default => 'all', match => '^(\w+)$',
101 section => { default => 'all', match => '^([\w-]+)$',
102 alias => 'release', array => ',',
104 replace => { all => \@SECTIONS } },
105 arch => { default => 'any', match => '^(\w+)$',
106 array => ',', var => \@archs, replace =>
107 { any => \@ARCHITECTURES } },
108 archive => { default => 'all', match => '^(\w+)$',
109 array => ',', replace =>
110 { all => \@ARCHIVES } },
111 format => { default => 'html', match => '^(\w+)$',
115 my %params = Packages::Search::parse_params( $input, \%params_def, \%opts );
117 #XXX: Don't use alternative output formats yet
120 if ($format eq 'html') {
121 print $input->header;
122 } elsif ($format eq 'xml') {
123 # print $input->header( -type=>'application/rdf+xml' );
124 print $input->header( -type=>'text/plain' );
127 if ($params{errors}{keywords}) {
128 print "Error: keyword not valid or missing" if $format eq 'html';
132 my $case_bool = ( $case !~ /insensitive/ );
133 $exact = !$subword unless defined $exact;
134 $opts{h_suites} = { map { $_ => 1 } @suites };
135 $opts{h_sections} = { map { $_ => 1 } @sections };
136 $opts{h_archs} = { map { $_ => 1 } @archs };
138 # for URL construction
139 my $suites_param = join ',', @{$params{values}{suite}{no_replace}};
140 my $sections_param = join ',', @{$params{values}{section}{no_replace}};
141 my $archs_param = join ',', @{$params{values}{arch}{no_replace}};
144 my $keyword_enc = encode_entities $keyword;
145 my $searchon_enc = encode_entities $searchon;
146 my $suites_enc = encode_entities join ', ', @{$params{values}{suite}{no_replace}};
147 my $sections_enc = encode_entities join ', ', @{$params{values}{section}{no_replace}};
148 my $archs_enc = encode_entities join ', ', @{$params{values}{arch}{no_replace}};
149 my $pet1 = new Benchmark;
150 my $petd = timediff($pet1, $pet0);
151 print "DEBUG: Parameter evaluation took ".timestr($petd)."<br>" if $debug;
153 if ($format eq 'html') {
154 print Packages::HTML::header( title => 'Package Search Results' ,
156 title_tag => 'Debian Package Search Results',
157 print_title_above => 1,
158 print_search_field => 'packages',
159 search_field_values => {
160 keywords => $keyword_enc,
161 searchon => $searchon,
163 suite => $suites_enc,
164 section => $sections_enc,
172 # read the configuration
174 if (!open (C, "../config.sh")) {
175 print "\nInternal Error: Cannot open configuration file.\n\n"
176 if $format eq 'html';
180 $topdir = $1 if (/^\s*topdir="?(.*)"?\s*$/);
184 my $DBDIR = $topdir . "/files/db";
185 my $search_on_sources = 0;
187 my $st0 = new Benchmark;
190 if ($searchon eq 'sourcenames') {
191 $search_on_sources = 1;
195 my ($hash, $key, $results, $opts) = @_;
196 my $result = $hash->{$key} || '';
197 foreach (split /\000/, $result) {
198 my @data = split ( /\s/, $_, 7 );
199 print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
200 if ($opts->{h_suites}{$data[0]}
201 && ($opts->{h_archs}{$data[1]} || $data[1] eq 'all')
202 && $opts->{h_sections}{$data[2]}) {
203 print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
204 push @$results, [ $key, @data ];
209 my ($hash, $key, $results, $opts) = @_;
210 my $result = $hash->{$key} || '';
211 foreach (split /\000/, $result) {
212 my @data = split ( /\s/, $_, 5 );
213 print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
214 if ($opts->{h_suites}{$data[0]} && $opts->{h_sections}{$data[1]}) {
215 print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
216 push @$results, [ $key, @data ];
220 sub do_names_search {
221 my ($keyword, $file, $postfix_file, $read_entry, $opts) = @_;
224 $keyword = lc $keyword unless $opts->{case_bool};
226 my $obj = tie my %packages, 'DB_File', "$DBDIR/$file", O_RDONLY, 0666, $DB_BTREE
227 or die "couldn't tie DB $DBDIR/$file: $!";
229 if ($opts->{exact}) {
230 &$read_entry( \%packages, $keyword, \@results, $opts );
232 my ($key, $prefixes) = ($keyword, '');
234 my $p_obj = tie my %pref, 'DB_File', "$DBDIR/$postfix_file", O_RDONLY, 0666, $DB_BTREE
235 or die "couldn't tie postfix db $DBDIR/$postfix_file: $!";
236 $p_obj->seq( $key, $prefixes, R_CURSOR );
237 while (index($key, $keyword) >= 0) {
238 if ($prefixes =~ /^\001(\d+)/o) {
239 $too_many_hits += $1;
241 foreach (split /\000/o, $prefixes) {
242 $_ = '' if $_ eq '^';
243 print "DEBUG: add word $_$key<br>" if $debug > 2;
247 last if $p_obj->seq( $key, $prefixes, R_NEXT ) != 0;
248 last if $too_many_hits or keys %pkgs >= 100;
251 my $no_results = keys %pkgs;
252 if ($too_many_hits || ($no_results >= 100)) {
253 $too_many_hits += $no_results;
254 %pkgs = ( $keyword => 1 );
256 foreach my $pkg (sort keys %pkgs) {
257 &$read_entry( \%packages, $pkg, \@results, $opts );
262 sub do_fulltext_search {
263 my ($keword, $file, $mapping, $lookup, $read_entry, $opts) = @_;
268 if ($opts->{case_bool}) {
269 if ($opts->{exact}) {
270 $regex = qr/\b\Q$keyword\E\b/o;
272 $regex = qr/\Q$keyword\E/o;
276 $regex = qr/\b\Q$keyword\E\b/io;
278 $regex = qr/\Q$keyword\E/io;
282 open DESC, '<', "$DBDIR/$file"
283 or die "couldn't open $DBDIR/$file: $!";
285 $_ =~ $regex or next;
286 print "DEBUG: Matched line $.<br>" if $debug > 2;
291 tie my %packages, 'DB_File', "$DBDIR/$lookup", O_RDONLY, 0666, $DB_BTREE
292 or die "couldn't tie DB $DBDIR/$lookup: $!";
293 tie my %did2pkg, 'DB_File', "$DBDIR/$mapping", O_RDONLY, 0666, $DB_BTREE
294 or die "couldn't tie DB $DBDIR/$mapping: $!";
297 foreach my $l (@lines) {
298 my $result = $did2pkg{$l};
299 foreach (split /\000/o, $result) {
300 my @data = split /\s/, $_, 3;
301 next unless $opts->{h_archs}{$data[2]};
302 $tmp_results{$data[0]}++;
305 foreach my $pkg (keys %tmp_results) {
306 &$read_entry( \%packages, $pkg, \@results, $opts );
312 my ($pkg, $suite) = @_;
314 tie my %src2bin, 'DB_File', "$DBDIR/sources_packages.db", O_RDONLY, 0666, $DB_BTREE
315 or die "couldn't open $DBDIR/sources_packages.db: $!";
317 my $bins = $src2bin{$pkg} || '';
319 foreach (split /\000/o, $bins) {
320 my @data = split /\s/, $_, 4;
322 if ($data[0] eq $suite) {
327 return [ keys %bins ];
330 if ($searchon eq 'names') {
331 push @results, @{ do_names_search( $keyword, 'packages_small.db',
332 'package_postfixes.db',
333 \&read_entry, \%opts ) };
334 } elsif ($searchon eq 'sourcenames') {
335 push @results, @{ do_names_search( $keyword, 'sources_small.db',
336 'source_postfixes.db',
337 \&read_src_entry, \%opts ) };
339 push @results, @{ do_names_search( $keyword, 'packages_small.db',
340 'package_postfixes.db',
341 \&read_entry, \%opts ) };
342 push @results, @{ do_fulltext_search( $keyword, 'descriptions.txt',
343 'descriptions_packages.db',
345 \&read_entry, \%opts ) };
348 my $st1 = new Benchmark;
349 my $std = timediff($st1, $st0);
350 print "DEBUG: Search took ".timestr($std)."<br>" if $debug;
352 if ($format eq 'html') {
353 my $suite_wording = $suites_enc eq "all" ? "all suites"
354 : "suite(s) <em>$suites_enc</em>";
355 my $section_wording = $sections_enc eq 'all' ? "all sections"
356 : "section(s) <em>$sections_enc</em>";
357 my $arch_wording = $archs_enc eq 'any' ? "all architectures"
358 : "architecture(s) <em>$archs_enc</em>";
359 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
360 my $source_wording = $search_on_sources ? "source " : "";
361 my $exact_wording = $exact ? "named" : "that names contain";
362 print "<p>You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording.</p>";
364 my $exact_wording = $exact ? "" : " (including subword matching)";
365 print "<p>You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording.</p>";
369 if ($too_many_hits) {
370 print "<p><strong>Your search was too wide so we will only display exact matches. At least <em>$too_many_hits</em> results have been omitted and will not be displayed. Please consider using a longer keyword or more keywords.</strong></p>";
374 if ($format eq 'html') {
375 my $keyword_esc = uri_escape( $keyword );
377 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
378 if (($suites_enc eq 'all')
379 && ($archs_enc eq 'any')
380 && ($sections_enc eq 'all')) {
381 print "<p><strong>Can't find that package.</strong></p>\n";
383 print "<p><strong>Can't find that package, at least not in that suite ".
384 ( $search_on_sources ? "" : " and on that architecture" ).
390 print "<p>You have searched only for exact matches of the package name. You can try to search for <a href=\"$thisscript?exact=0&searchon=$searchon&suite=$suites_param&case=$case&section=$sections_param&keywords=$keyword_esc&arch=$archs_param\">package names that contain your search string</a>.</p>";
393 if (($suites_enc eq 'all')
394 && ($archs_enc eq 'any')
395 && ($sections_enc eq 'all')) {
396 print "<p><strong>Can't find that string.</strong></p>\n";
398 print "<p><strong>Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc).</strong></p>\n";
403 print "<p>You have searched only for words exactly matching your keywords. You can try to search <a href=\"$thisscript?subword=1&searchon=$searchon&suite=$suites_param&case=$case&section=$sections_param&keywords=$keyword_esc&arch=$archs_param\">allowing subword matching</a>.</p>";
406 print "<p>".( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$SEARCHPAGE#search_packages\">Packages search page</a>.</p>";
413 my (%pkgs, %sect, %part, %desc, %binaries);
415 unless ($search_on_sources) {
417 my ($pkg_t, $suite, $arch, $section, $subsection,
418 $priority, $version, $desc) = @$_;
420 my ($package) = $pkg_t =~ m/^(.+)/; # untaint
421 $pkgs{$package}{$suite}{$version}{$arch} = 1;
422 $sect{$package}{$suite}{$version} = $subsection;
423 $part{$package}{$suite}{$version} = $section unless $section eq 'main';
425 $desc{$package}{$suite}{$version} = $desc;
428 if ($format eq 'html') {
429 my ($start, $end) = multipageheader( scalar keys %pkgs );
432 foreach my $pkg (sort keys %pkgs) {
434 next if $count < $start or $count > $end;
435 printf "<h3>Package %s</h3>\n", $pkg;
437 foreach my $ver (@SUITES) {
438 if (exists $pkgs{$pkg}{$ver}) {
439 my @versions = version_sort keys %{$pkgs{$pkg}{$ver}};
441 if ($part{$pkg}{$ver}{$versions[0]}) {
442 $part_str = "[<span style=\"color:red\">$part{$pkg}{$ver}{$versions[0]}</span>]";
444 printf "<li><a href=\"$ROOT/%s/%s/%s\">%s</a> (%s): %s %s\n",
445 $ver, $sect{$pkg}{$ver}{$versions[0]}, $pkg, $ver, $sect{$pkg}{$ver}{$versions[0]}, $desc{$pkg}{$ver}{$versions[0]}, $part_str;
447 foreach my $v (@versions) {
448 printf "<br>%s: %s\n",
449 $v, join (" ", (sort keys %{$pkgs{$pkg}{$ver}{$v}}) );
459 my ($package, $suite, $section, $subsection, $priority,
462 $pkgs{$package}{$suite} = $version;
463 $sect{$package}{$suite}{source} = $subsection;
464 $part{$package}{$suite}{source} = $section unless $section eq 'main';
466 $binaries{$package}{$suite} = find_binaries( $package, $suite );
469 if ($format eq 'html') {
470 my ($start, $end) = multipageheader( scalar keys %pkgs );
473 foreach my $pkg (sort keys %pkgs) {
475 next if ($count < $start) or ($count > $end);
476 printf "<h3>Source package %s</h3>\n", $pkg;
478 foreach my $ver (@SUITES) {
479 if (exists $pkgs{$pkg}{$ver}) {
481 if ($part{$pkg}{$ver}{source}) {
482 $part_str = "[<span style=\"color:red\">$part{$pkg}{$ver}{source}</span>]";
484 printf "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s", $ver, $pkg, $ver, $sect{$pkg}{$ver}{source}, $pkgs{$pkg}{$ver}, $part_str;
486 print "<br>Binary packages: ";
488 foreach my $bp (@{$binaries{$pkg}{$ver}}) {
489 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
490 $ver, uri_escape( $bp ), $bp );
491 push @bp_links, $bp_link;
493 print join( ", ", @bp_links );
502 if ($format eq 'html') {
503 &printindexline( scalar keys %pkgs );
510 my $no_results = shift;
513 if ($no_results > $opts{number}) {
515 $index_line = prevlink($input,\%params)." | ".
516 indexline( $input, \%params, $no_results)." | ".
517 nextlink($input,\%params, $no_results);
519 print "<p style=\"text-align:center\">$index_line</p>";
523 sub multipageheader {
524 my $no_results = shift;
527 if ($opts{number} =~ /^all$/i) {
530 $opts{number} = $no_results;
532 $start = Packages::Search::start( \%params );
533 $end = Packages::Search::end( \%params );
534 if ($end > $no_results) { $end = $no_results; }
537 print "<p>Found <em>$no_results</em> matching packages,";
538 if ($end == $start) {
539 print " displaying package $end.</p>";
541 print " displaying packages $start to $end.</p>";
544 printindexline( $no_results );
546 if ($no_results > 100) {
547 print "<p>Results per page: ";
550 if ($opts{number} == $_) {
551 push @resperpagelinks, $_;
553 push @resperpagelinks, resperpagelink($input,\%params,$_);
556 if ($params{values}{number}{final} =~ /^all$/i) {
557 push @resperpagelinks, "all";
559 push @resperpagelinks, resperpagelink($input, \%params,"all");
561 print join( " | ", @resperpagelinks )."</p>";
563 return ( $start, $end );
571 <p style="text-align:right;font-size:small;font-stlye:italic"><a href="$SEARCHPAGE">Packages search page</a></p>
576 my $pete = new Benchmark;
577 my $petd = timediff($pete, $pet0);
578 print "Total page evaluation took ".timestr($petd)."<br>"
580 print $input->end_html;