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 = "search_packages.pl";
30 my $HOME = "http://www.debian.org";
32 my $SEARCHPAGE = "http://packages.debian.org/";
33 my @SUITES = qw( oldstable stable testing unstable experimental );
35 my @SECTIONS = qw( main contrib non-free );
36 my @ARCHIVES = qw( us security installer );
37 my @ARCHITECTURES = qw( alpha amd64 arm hppa hurd-i386 i386 ia64
38 kfreebsd-i386 mips mipsel powerpc s390 sparc );
39 my %SUITES = map { $_ => 1 } @SUITES;
40 my %SECTIONS = map { $_ => 1 } @SECTIONS;
41 my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
42 my %ARCHITECTURES = map { $_ => 1 } @ARCHITECTURES;
44 $ENV{PATH} = "/bin:/usr/bin";
46 # Read in all the variables set by the form
48 if ($ARGV[0] eq 'php') {
49 $input = new CGI(\*STDIN);
54 my $pet0 = new Benchmark;
55 # use this to disable debugging in production mode completly
56 my $debug_allowed = 1;
57 my $debug = $debug_allowed && $input->param("debug");
58 $debug = 0 if not defined($debug);
59 $Search::Param::debug = 1 if $debug > 1;
61 # If you want, just print out a list of all of the variables and exit.
62 print $input->header if $debug;
66 if (my $path = $input->param('path')) {
67 my @components = map { lc $_ } split /\//, $path;
69 foreach (@components) {
71 $input->param('suite', $_);
72 } elsif ($SECTIONS{$_}) {
73 $input->param('section', $_);
74 } elsif ($ARCHIVES{$_}) {
75 $input->param('archive', $_);
76 }elsif ($ARCHITECTURES{$_}) {
77 $input->param('arch', $_);
82 my %params_def = ( keywords => { default => undef, match => '^\s*([-+\@\w\/.:]+)\s*$' },
83 suite => { default => 'stable', match => '^(\w+)$',
84 alias => 'version', array => ',',
85 replace => { all => \@SUITES } },
86 case => { default => 'insensitive', match => '^(\w+)$' },
87 official => { default => 0, match => '^(\w+)$' },
88 use_cache => { default => 1, match => '^(\w+)$' },
89 subword => { default => 0, match => '^(\w+)$' },
90 exact => { default => undef, match => '^(\w+)$' },
91 searchon => { default => 'all', match => '^(\w+)$' },
92 section => { default => 'all', match => '^([\w-]+)$',
93 alias => 'release', array => ',',
94 replace => { all => \@SECTIONS } },
95 arch => { default => 'any', match => '^(\w+)$',
96 array => ',', replace =>
97 { any => \@ARCHITECTURES } },
98 archive => { default => 'all', match => '^(\w+)$',
99 array => ',', replace =>
100 { all => \@ARCHIVES } },
101 format => { default => 'html', match => '^(\w+)$' },
103 my %params = Packages::Search::parse_params( $input, \%params_def );
105 my $format = $params{values}{format}{final};
106 #XXX: Don't use alternative output formats yet
109 if ($format eq 'html') {
110 print $input->header;
111 } elsif ($format eq 'xml') {
112 # print $input->header( -type=>'application/rdf+xml' );
113 print $input->header( -type=>'text/plain' );
116 if ($params{errors}{keywords}) {
117 print "Error: keyword not valid or missing" if $format eq 'html';
120 my $keyword = $params{values}{keywords}{final};
121 my @suites = @{$params{values}{suite}{final}};
122 my $official = $params{values}{official}{final};
123 my $use_cache = $params{values}{use_cache}{final};
124 my $case = $params{values}{case}{final};
125 my $case_bool = ( $case !~ /insensitive/ );
126 my $subword = $params{values}{subword}{final};
127 my $exact = $params{values}{exact}{final};
128 $exact = !$subword unless defined $exact;
129 my $searchon = $params{values}{searchon}{final};
130 my @sections = @{$params{values}{section}{final}};
131 my @archs = @{$params{values}{arch}{final}};
132 my $page = $params{values}{page}{final};
133 my $results_per_page = $params{values}{number}{final};
135 # for URL construction
136 my $suites_param = join ',', @{$params{values}{suite}{no_replace}};
137 my $sections_param = join ',', @{$params{values}{section}{no_replace}};
138 my $archs_param = join ',', @{$params{values}{arch}{no_replace}};
141 my $keyword_enc = encode_entities $keyword;
142 my $searchon_enc = encode_entities $searchon;
143 my $suites_enc = encode_entities join ', ', @{$params{values}{suite}{no_replace}};
144 my $sections_enc = encode_entities join ', ', @{$params{values}{section}{no_replace}};
145 my $archs_enc = encode_entities join ', ', @{$params{values}{arch}{no_replace}};
146 my $pet1 = new Benchmark;
147 my $petd = timediff($pet1, $pet0);
148 print "DEBUG: Parameter evaluation took ".timestr($petd)."<br>" if $debug;
150 if ($format eq 'html') {
151 print Packages::HTML::header( title => 'Package Search Results' ,
153 title_tag => 'Debian Package Search Results',
154 print_title_above => 1,
155 print_search_field => 'packages',
156 search_field_values => {
157 keywords => $keyword_enc,
158 searchon => $searchon,
160 suite => $suites_enc,
161 section => $sections_enc,
169 # read the configuration
171 if (!open (C, "../config.sh")) {
172 print "\nInternal Error: Cannot open configuration file.\n\n"
173 if $format eq 'html';
177 $topdir = $1 if (/^\s*topdir="?(.*)"?\s*$/);
181 my $DBDIR = $topdir . "/files/db";
182 my $search_on_sources = 0;
184 my $st0 = new Benchmark;
187 if ($searchon eq 'sourcenames') {
188 $search_on_sources = 1;
191 my %suites = map { $_ => 1 } @suites;
192 my %sections = map { $_ => 1 } @sections;
193 my %archs = map { $_ => 1 } @archs;
195 print "DEBUG: suites=@suites, sections=@sections, archs=@archs<br>" if $debug > 2;
198 my ($hash, $key, $results) = @_;
199 my $result = $hash->{$key};
200 foreach (split /\000/, $result) {
201 my @data = split ( /\s/, $_, 7 );
202 print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
203 if ($suites{$data[0]} && ($archs{$data[1]} || $data[1] eq 'all')
204 && $sections{$data[2]}) {
205 print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
206 push @$results, [ $key, @data ];
211 my ($hash, $key, $results) = @_;
212 my $result = $hash->{$key};
214 foreach (split /\000/, $result) {
215 my @data = split ( /\s/, $_, 5 );
216 print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
217 if ($suites{$data[0]} && $sections{$data[1]}) {
218 print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
219 push @$results, [ $key, @data ];
223 sub do_names_search {
224 my ($keyword, $file, $postfix_file, $read_entry) = @_;
226 $keyword = lc $keyword unless $case_bool;
228 my $obj = tie my %packages, 'DB_File', "$DBDIR/$file", O_RDONLY, 0666, $DB_BTREE
229 or die "couldn't tie DB $DBDIR/$file: $!";
232 &$read_entry( \%packages, $keyword, \@results );
234 my ($key, $prefixes) = ($keyword, '');
236 my $p_obj = tie my %pref, 'DB_File', "$DBDIR/$postfix_file", O_RDONLY, 0666, $DB_BTREE
237 or die "couldn't tie postfix db $DBDIR/$postfix_file: $!";
238 $p_obj->seq( $key, $prefixes, R_CURSOR );
240 if ($prefixes =~ /^\001(\d+)/o) {
241 $too_much_hits += $1;
243 print "DEBUG: add word $key<br>" if $debug > 2;
245 foreach (split /\000/o, $prefixes) {
246 print "DEBUG: add word $_$key<br>" if $debug > 2;
250 } while (($p_obj->seq( $key, $prefixes, R_NEXT ) == 0)
251 && (index($key, $keyword) >= 0)
253 && (keys %pkgs < 100));
255 my $no_results = keys %pkgs;
256 if ($too_much_hits || ($no_results >= 100)) {
257 $too_much_hits += $no_results;
258 %pkgs = ( $keyword => 1 );
260 foreach my $pkg (sort keys %pkgs) {
261 &$read_entry( \%packages, $pkg, \@results );
266 if ($searchon eq 'names') {
267 do_names_search( $keyword, 'packages_small.db',
268 'package_postfixes.db', \&read_entry );
269 } elsif ($searchon eq 'sourcenames') {
270 do_names_search( $keyword, 'sources_small.db',
271 'source_postfixes.db', \&read_src_entry );
278 $regex = qr/\b\Q$keyword\E\b/o;
280 $regex = qr/\Q$keyword\E/o;
284 $regex = qr/\b\Q$keyword\E\b/io;
286 $regex = qr/\Q$keyword\E/io;
290 open DESC, '<', "$DBDIR/descriptions.txt" or die "couldn't open $DBDIR/descriptions.txt: $!";
292 $_ =~ $regex or next;
293 print "DEBUG: Matched line $.<br>" if $debug > 2;
298 my $obj = tie my %packages, 'DB_File', "$DBDIR/packages_small.db", O_RDONLY, 0666, $DB_BTREE
299 or die "couldn't tie DB $DBDIR/packages_small.db: $!";
300 my $obj = tie my %did2pkg, 'DB_File', "$DBDIR/descriptions_packages.db", O_RDONLY, 0666, $DB_BTREE
301 or die "couldn't tie DB $DBDIR/descriptions_packages.db: $!";
304 foreach my $l (@lines) {
305 my $result = $did2pkg{$l};
306 foreach (split /\000/o, $result) {
307 my @data = split /\s/, $_, 3;
308 next unless $archs{$data[2]};
309 $tmp_results{$data[0]}++;
312 foreach my $pkg (keys %tmp_results) {
313 read_entry( \%packages, $pkg, \@results );
317 my $st1 = new Benchmark;
318 my $std = timediff($st1, $st0);
319 print "DEBUG: Search took ".timestr($std)."<br>" if $debug;
321 if ($format eq 'html') {
322 my $suite_wording = $suites_enc eq "all" ? "all suites"
323 : "suite(s) <em>$suites_enc</em>";
324 my $section_wording = $sections_enc eq 'all' ? "all sections"
325 : "section(s) <em>$sections_enc</em>";
326 my $arch_wording = $archs_enc eq 'any' ? "all architectures"
327 : "architecture(s) <em>$archs_enc</em>";
328 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
329 my $source_wording = $search_on_sources ? "source " : "";
330 my $exact_wording = $exact ? "named" : "that names contain";
331 print "<p>You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording.</p>";
333 my $exact_wording = $exact ? "" : " (including subword matching)";
334 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>";
338 if ($too_much_hits) {
339 print "<p><strong>Your search was too wide so we will only display exact matches. At least <em>$too_much_hits</em> results have been omitted and will not be displayed. Please consider using a longer keyword or more keywords.</strong></p>";
343 if ($format eq 'html') {
344 my $keyword_esc = uri_escape( $keyword );
346 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
347 if (($suites_enc eq 'all')
348 && ($archs_enc eq 'any')
349 && ($sections_enc eq 'all')) {
350 print "<p><strong>Can't find that package.</strong></p>\n";
352 print "<p><strong>Can't find that package, at least not in that suite ".
353 ( $search_on_sources ? "" : " and on that architecture" ).
359 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>";
362 if (($suites_enc eq 'all')
363 && ($archs_enc eq 'any')
364 && ($sections_enc eq 'all')) {
365 print "<p><strong>Can't find that string.</strong></p>\n";
367 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";
372 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>";
375 print "<p>".( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$SEARCHPAGE#search_packages\">Packages search page</a>.</p>";
382 my (%pkgs, %sect, %part, %desc, %binaries);
384 unless ($search_on_sources) {
386 my ($pkg_t, $suite, $arch, $section, $subsection,
387 $priority, $version, $desc) = @$_;
389 my ($package) = $pkg_t =~ m/^(.+)/; # untaint
390 $pkgs{$package}{$suite}{$version}{$arch} = 1;
391 $sect{$package}{$suite}{$version} = $subsection;
392 $part{$package}{$suite}{$version} = $section unless $section eq 'main';
394 $desc{$package}{$suite}{$version} = $desc;
398 if ($format eq 'html') {
399 my ($start, $end) = multipageheader( scalar keys %pkgs );
402 foreach my $pkg (sort keys %pkgs) {
404 next if $count < $start or $count > $end;
405 printf "<h3>Package %s</h3>\n", $pkg;
407 foreach my $ver (@SUITES) {
408 if (exists $pkgs{$pkg}{$ver}) {
409 my @versions = version_sort keys %{$pkgs{$pkg}{$ver}};
411 if ($part{$pkg}{$ver}{$versions[0]}) {
412 $part_str = "[<span style=\"color:red\">$part{$pkg}{$ver}{$versions[0]}</span>]";
414 printf "<li><a href=\"$ROOT/%s/%s/%s\">%s</a> (%s): %s %s\n",
415 $ver, $sect{$pkg}{$ver}{$versions[0]}, $pkg, $ver, $sect{$pkg}{$ver}{$versions[0]}, $desc{$pkg}{$ver}{$versions[0]}, $part_str;
417 foreach my $v (@versions) {
418 printf "<br>%s: %s\n",
419 $v, join (" ", (sort keys %{$pkgs{$pkg}{$ver}{$v}}) );
426 } elsif ($format eq 'xml') {
427 require RDF::Simple::Serialiser;
428 my $rdf = new RDF::Simple::Serialiser;
429 $rdf->addns( debpkg => 'http://packages.debian.org/xml/01-debian-packages-rdf' );
431 foreach my $pkg (sort keys %pkgs) {
432 foreach my $ver (@DISTS) {
433 if (exists $pkgs{$pkg}{$ver}) {
434 my @versions = version_sort keys %{$pkgs{$pkg}{$ver}};
435 foreach my $version (@versions) {
436 my $id = "$ROOT/$ver/$sect{$pkg}{$ver}{$version}/$pkg/$version";
437 push @triples, [ $id, 'debpkg:package', $pkg ];
438 push @triples, [ $id, 'debpkg:version', $version ];
439 push @triples, [ $id, 'debpkg:section', $sect{$pkg}{$ver}{$version}, ];
440 push @triples, [ $id, 'debpkg:suite', $ver ];
441 push @triples, [ $id, 'debpkg:shortdesc', $desc{$pkg}{$ver}{$version} ];
442 push @triples, [ $id, 'debpkg:part', $part{$pkg}{$ver}{$version} || 'main' ];
443 foreach my $arch (sort keys %{$pkgs{$pkg}{$ver}{$version}}) {
444 push @triples, [ $id, 'debpkg:architecture', $arch ];
451 print $rdf->serialise(@triples);
455 my ($package, $suite, $section, $subsection, $priority,
456 $version, $binaries) = @$_;
458 $pkgs{$package}{$suite} = $version;
459 $sect{$package}{$suite}{source} = $subsection;
460 $part{$package}{$suite}{source} = $section unless $section eq 'main';
462 $binaries{$package}{$suite} = [ sort split( /\s*,\s*/, $binaries ) ];
465 if ($format eq 'html') {
466 my ($start, $end) = multipageheader( scalar keys %pkgs );
469 foreach my $pkg (sort keys %pkgs) {
471 next if ($count < $start) or ($count > $end);
472 printf "<h3>Source package %s</h3>\n", $pkg;
474 foreach my $ver (@SUITES) {
475 if (exists $pkgs{$pkg}{$ver}) {
477 if ($part{$pkg}{$ver}{source}) {
478 $part_str = "[<span style=\"color:red\">$part{$pkg}{$ver}{source}</span>]";
480 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;
482 print "<br>Binary packages: ";
484 foreach my $bp (@{$binaries{$pkg}{$ver}}) {
485 my $sect = 'section';
489 $bp_link = sprintf( "<a href=\"$ROOT/%s/%s/%s\">%s</a>",
490 $ver, $sect, uri_escape( $bp ), $bp );
494 push @bp_links, $bp_link;
496 print join( ", ", @bp_links );
502 } elsif ($format eq 'xml') {
503 require RDF::Simple::Serialiser;
504 my $rdf = new RDF::Simple::Serialiser;
505 $rdf->addns( debpkg => 'http://packages.debian.org/xml/01-debian-packages-rdf' );
507 foreach my $pkg (sort keys %pkgs) {
508 foreach my $ver (@SUITES) {
509 if (exists $pkgs{$pkg}{$ver}) {
510 my $id = "$ROOT/$ver/source/$pkg";
512 push @triples, [ $id, 'debpkg:package', $pkg ];
513 push @triples, [ $id, 'debpkg:type', 'source' ];
514 push @triples, [ $id, 'debpkg:section', $sect{$pkg}{$ver}{source} ];
515 push @triples, [ $id, 'debpkg:version', $pkgs{$pkg}{$ver} ];
516 push @triples, [ $id, 'debpkg:part', $part{$pkg}{$ver}{source} || 'main' ];
518 foreach my $bp (@{$binaries{$pkg}{$ver}}) {
519 push @triples, [ $id, 'debpkg:binary', $bp ];
524 print $rdf->serialise(@triples);
528 if ($format eq 'html') {
529 &printindexline( scalar keys %pkgs );
536 my $no_results = shift;
539 if ($no_results > $results_per_page) {
541 $index_line = prevlink($input,\%params)." | ".
542 indexline( $input, \%params, $no_results)." | ".
543 nextlink($input,\%params, $no_results);
545 print "<p style=\"text-align:center\">$index_line</p>";
549 sub multipageheader {
550 my $no_results = shift;
553 if ($results_per_page =~ /^all$/i) {
556 $results_per_page = $no_results;
558 $start = Packages::Search::start( \%params );
559 $end = Packages::Search::end( \%params );
560 if ($end > $no_results) { $end = $no_results; }
563 print "<p>Found <em>$no_results</em> matching packages,";
564 if ($end == $start) {
565 print " displaying package $end.</p>";
567 print " displaying packages $start to $end.</p>";
570 printindexline( $no_results );
572 if ($no_results > 100) {
573 print "<p>Results per page: ";
576 if ($results_per_page == $_) {
577 push @resperpagelinks, $_;
579 push @resperpagelinks, resperpagelink($input,\%params,$_);
582 if ($params{values}{number}{final} =~ /^all$/i) {
583 push @resperpagelinks, "all";
585 push @resperpagelinks, resperpagelink($input, \%params,"all");
587 print join( " | ", @resperpagelinks )."</p>";
589 return ( $start, $end );
597 <p style="text-align:right;font-size:small;font-stlye:italic"><a href="$SEARCHPAGE">Packages search page</a></p>
602 print $input->end_html;