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 );
25 use Packages::Search qw( :all );
26 use Packages::HTML ();
28 &Packages::CGI::reset;
30 $ENV{PATH} = "/bin:/usr/bin";
32 # Read in all the variables set by the form
34 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
35 $input = new CGI(\*STDIN);
40 my $pet0 = new Benchmark;
41 my $tet0 = new Benchmark;
42 # use this to disable debugging in production mode completly
43 my $debug_allowed = 1;
44 my $debug = $debug_allowed && $input->param("debug");
45 $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
46 $Packages::CGI::debug = $debug;
48 # read the configuration
49 our $config_read_time ||= 0;
50 our $db_read_time ||= 0;
51 our ( $topdir, $ROOT, @SUITES, @SECTIONS, @ARCHIVES, @ARCHITECTURES );
53 # FIXME: move to own module
54 my $modtime = (stat( "../config.sh" ))[9];
55 if ($modtime > $config_read_time) {
56 if (!open (C, '<', "../config.sh")) {
57 error( "Internal: Cannot open configuration file." );
62 $topdir = $1 if /^\s*topdir="?([^\"]*)"?\s*$/o;
63 $ROOT = $1 if /^\s*root="?([^\"]*)"?\s*$/o;
64 $Packages::HTML::HOME = $1 if /^\s*home="?([^\"]*)"?\s*$/o;
65 $Packages::HTML::SEARCH_CGI = $1 if /^\s*searchcgi="?([^\"]*)"?\s*$/o;
66 $Packages::HTML::SEARCH_PAGE = $1 if /^\s*searchpage="?([^\"]*)"?\s*$/o;
67 $Packages::HTML::WEBMASTER_MAIL = $1 if /^\s*webmaster="?([^\"]*)"?\s*$/o;
68 $Packages::HTML::CONTACT_MAIL = $1 if /^\s*contact="?([^\"]*)"?\s*$/o;
69 @SUITES = split(/\s+/, $1) if /^\s*suites="?([^\"]*)"?\s*$/o;
70 @SECTIONS = split(/\s+/, $1) if /^\s*sections="?([^\"]*)"?\s*$/o;
71 @ARCHIVES = split(/\s+/, $1) if /^\s*archives="?([^\"]*)"?\s*$/o;
72 @ARCHITECTURES = split(/\s+/, $1) if /^\s*architectures="?([^\"]*)"?\s*$/o;
75 debug( "read config ($modtime > $config_read_time)" );
76 $config_read_time = $modtime;
78 my $DBDIR = $topdir . "/files/db";
79 my $thisscript = $Packages::HTML::SEARCH_CGI;
81 if (my $path = $input->param('path')) {
82 my @components = map { lc $_ } split /\//, $path;
84 my %SUITES = map { $_ => 1 } @SUITES;
85 my %SECTIONS = map { $_ => 1 } @SECTIONS;
86 my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
87 my %ARCHITECTURES = map { $_ => 1 } @ARCHITECTURES;
89 foreach (@components) {
91 $input->param('suite', $_);
92 } elsif ($SECTIONS{$_}) {
93 $input->param('section', $_);
94 } elsif ($ARCHIVES{$_}) {
95 $input->param('archive', $_);
96 } elsif ($ARCHITECTURES{$_}) {
97 $input->param('arch', $_);
102 my ( $format, $keyword, $case, $subword, $exact, $searchon,
103 @suites, @sections, @archives, @archs );
105 my %params_def = ( keywords => { default => undef,
106 match => '^\s*([-+\@\w\/.:]+)\s*$',
108 suite => { default => 'stable', match => '^([\w-]+)$',
109 alias => 'version', array => ',',
111 replace => { all => \@SUITES } },
112 archive => { default => 'all', match => '^([\w-]+)$',
113 array => ',', var => \@archives,
114 replace => { all => \@ARCHIVES } },
115 case => { default => 'insensitive', match => '^(\w+)$',
117 official => { default => 0, match => '^(\w+)$' },
118 subword => { default => 0, match => '^(\w+)$',
120 exact => { default => undef, match => '^(\w+)$',
122 searchon => { default => 'all', match => '^(\w+)$',
124 section => { default => 'all', match => '^([\w-]+)$',
125 alias => 'release', array => ',',
127 replace => { all => \@SECTIONS } },
128 arch => { default => 'any', match => '^(\w+)$',
129 array => ',', var => \@archs, replace =>
130 { any => \@ARCHITECTURES } },
131 format => { default => 'html', match => '^(\w+)$',
135 my %params = Packages::Search::parse_params( $input, \%params_def, \%opts );
137 #XXX: Don't use alternative output formats yet
139 if ($format eq 'html') {
140 print $input->header;
143 if ($params{errors}{keywords}) {
144 fatal_error( "keyword not valid or missing" );
145 } elsif (length($keyword) < 2) {
146 fatal_error( "keyword too short (keywords need to have at least two characters)" );
149 my $case_bool = ( $case !~ /insensitive/ );
150 $exact = !$subword unless defined $exact;
151 $opts{h_suites} = { map { $_ => 1 } @suites };
152 $opts{h_sections} = { map { $_ => 1 } @sections };
153 $opts{h_archives} = { map { $_ => 1 } @archives };
154 $opts{h_archs} = { map { $_ => 1 } @archs };
156 # for URL construction
157 my $suites_param = join ',', @{$params{values}{suite}{no_replace}};
158 my $sections_param = join ',', @{$params{values}{section}{no_replace}};
159 my $archs_param = join ',', @{$params{values}{arch}{no_replace}};
162 my $keyword_enc = encode_entities $keyword || '';
163 my $searchon_enc = encode_entities $searchon;
164 my $suites_enc = encode_entities join ', ', @{$params{values}{suite}{no_replace}};
165 my $sections_enc = encode_entities join ', ', @{$params{values}{section}{no_replace}};
166 my $archs_enc = encode_entities join ', ', @{$params{values}{arch}{no_replace}};
167 my $pet1 = new Benchmark;
168 my $petd = timediff($pet1, $pet0);
169 debug( "Parameter evaluation took ".timestr($petd) );
171 my $st0 = new Benchmark;
174 our ($obj, $s_obj, $p_obj, $sp_obj,
175 %packages, %sources, %postf, %spostf, %src2bin, %did2pkg );
177 unless (@Packages::CGI::fatal_errors) {
179 my $dbmodtime = (stat("$DBDIR/packages_small.db"))[9];
180 if ($dbmodtime > $db_read_time) {
181 $obj = tie %packages, 'DB_File', "$DBDIR/packages_small.db",
182 O_RDONLY, 0666, $DB_BTREE
183 or die "couldn't tie DB $DBDIR/packages_small.db: $!";
184 $s_obj = tie %sources, 'DB_File', "$DBDIR/sources_small.db",
185 O_RDONLY, 0666, $DB_BTREE
186 or die "couldn't tie DB $DBDIR/sources_small.db: $!";
187 $p_obj = tie %postf, 'DB_File', "$DBDIR/package_postfixes.db",
188 O_RDONLY, 0666, $DB_BTREE
189 or die "couldn't tie postfix db $DBDIR/package_postfixes.db: $!";
190 $sp_obj = tie %spostf, 'DB_File', "$DBDIR/source_postfixes.db",
191 O_RDONLY, 0666, $DB_BTREE
192 or die "couldn't tie postfix db $DBDIR/source_postfixes.db: $!";
193 tie %src2bin, 'DB_File', "$DBDIR/sources_packages.db",
194 O_RDONLY, 0666, $DB_BTREE
195 or die "couldn't open $DBDIR/sources_packages.db: $!";
196 tie %did2pkg, 'DB_File', "$DBDIR/descriptions_packages.db",
197 O_RDONLY, 0666, $DB_BTREE
198 or die "couldn't tie DB $DBDIR/descriptions_packages.db: $!";
200 debug( "tied databases ($dbmodtime > $db_read_time)" );
201 $db_read_time = $dbmodtime;
204 if ($searchon eq 'names') {
205 push @results, @{ do_names_search( $keyword, \%packages,
207 \&read_entry, \%opts ) };
208 } elsif ($searchon eq 'sourcenames') {
209 push @results, @{ do_names_search( $keyword, \%sources,
211 \&read_src_entry, \%opts ) };
213 push @results, @{ do_names_search( $keyword, \%packages,
215 \&read_entry, \%opts ) };
216 push @results, @{ do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
219 \&read_entry, \%opts ) };
223 my $st1 = new Benchmark;
224 my $std = timediff($st1, $st0);
225 debug( "Search took ".timestr($std) );
227 if ($format eq 'html') {
228 my $suite_wording = $suites_enc eq "all" ? "all suites"
229 : "suite(s) <em>$suites_enc</em>";
230 my $section_wording = $sections_enc eq 'all' ? "all sections"
231 : "section(s) <em>$sections_enc</em>";
232 my $arch_wording = $archs_enc eq 'any' ? "all architectures"
233 : "architecture(s) <em>$archs_enc</em>";
234 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
235 my $source_wording = ( $searchon eq 'sourcenames' ) ? "source " : "";
236 my $exact_wording = $exact ? "named" : "that names contain";
237 msg( "You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
239 my $exact_wording = $exact ? "" : " (including subword matching)";
240 msg( "You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording." );
244 if ($Packages::Search::too_many_hits) {
245 error( "Your search was too wide so we will only display exact matches. At least <em>$Packages::Search::too_many_hits</em> results have been omitted and will not be displayed. Please consider using a longer keyword or more keywords." );
248 if (!@Packages::CGI::fatal_errors && !@results) {
249 if ($format eq 'html') {
250 my $keyword_esc = uri_escape( $keyword );
252 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
253 if (($suites_enc eq 'all')
254 && ($archs_enc eq 'any')
255 && ($sections_enc eq 'all')) {
256 error( "Can't find that package." );
258 error( "Can't find that package, at least not in that suite ".
259 ( ( $searchon eq 'sourcenames' ) ? "" : " and on that architecture" ) )
264 hint( "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>." );
267 if (($suites_enc eq 'all')
268 && ($archs_enc eq 'any')
269 && ($sections_enc eq 'all')) {
270 error( "Can't find that string." );
272 error( "Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc)." );
277 hint( "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>." );
280 hint( ( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$Packages::HTML::SEARCH_PAGE#search_packages\">Packages search page</a>." );
285 print Packages::HTML::header( title => 'Package Search Results' ,
287 title_tag => 'Debian Package Search Results',
288 print_title_above => 1,
289 print_search_field => 'packages',
290 search_field_values => {
291 keywords => $keyword_enc,
292 searchon => $searchon,
294 suite => $suites_enc,
295 section => $sections_enc,
307 my (%pkgs, %subsect, %sect, %desc, %binaries);
309 unless ($opts{searchon} eq 'sourcenames') {
311 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
312 $priority, $version, $desc) = @$_;
314 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
315 $pkgs{$pkg}{$suite}{$archive}{$version}{$arch} = 1;
316 $subsect{$pkg}{$suite}{$archive}{$version} = $subsection;
317 $sect{$pkg}{$suite}{$archive}{$version} = $section
318 unless $section eq 'main';
320 $desc{$pkg}{$suite}{$archive}{$version} = $desc;
323 if ($opts{format} eq 'html') {
324 my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
327 foreach my $pkg (sort keys %pkgs) {
329 next if $count < $start or $count > $end;
330 printf "<h3>Package %s</h3>\n", $pkg;
332 foreach my $suite (@SUITES) {
333 foreach my $archive (@ARCHIVES) {
334 if (exists $pkgs{$pkg}{$suite}{$archive}) {
335 my @versions = version_sort keys %{$pkgs{$pkg}{$suite}{$archive}};
337 if ($sect{$pkg}{$suite}{$archive}{$versions[0]}) {
338 $origin_str .= " [<span style=\"color:red\">$sect{$pkg}{$suite}{$versions[0]}</span>]";
340 printf "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s %s\n",
341 $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect{$pkg}{$suite}{$archive}{$versions[0]},
342 $desc{$pkg}{$suite}{$archive}{$versions[0]}, $origin_str;
344 foreach my $v (@versions) {
345 printf "<br>%s: %s\n",
346 $v, join (" ", (sort keys %{$pkgs{$pkg}{$suite}{$archive}{$v}}) );
357 my ($pkg, $archive, $suite, $section, $subsection, $priority,
360 $pkgs{$pkg}{$suite}{$archive} = $version;
361 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
362 $sect{$pkg}{$suite}{$archive}{source} = $section
363 unless $section eq 'main';
365 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
368 if ($opts{format} eq 'html') {
369 my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
372 foreach my $pkg (sort keys %pkgs) {
374 next if ($count < $start) or ($count > $end);
375 printf "<h3>Source package %s</h3>\n", $pkg;
377 foreach my $suite (@SUITES) {
378 foreach my $archive (@ARCHIVES) {
379 if (exists $pkgs{$pkg}{$suite}{$archive}) {
381 if ($sect{$pkg}{$suite}{$archive}{source}) {
382 $origin_str .= " [<span style=\"color:red\">$sect{$pkg}{$suite}{$archive}{source}</span>]";
384 printf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s",
385 $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect{$pkg}{$suite}{$archive}{source},
386 $pkgs{$pkg}{$suite}{$archive}, $origin_str );
388 print "<br>Binary packages: ";
390 foreach my $bp (@{$binaries{$pkg}{$suite}{$archive}}) {
391 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
392 $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ), $bp );
393 push @bp_links, $bp_link;
395 print join( ", ", @bp_links );
404 printindexline( $input, scalar keys %pkgs, \%opts );
406 #print_results(\@results, \%opts) if @results;;
407 my $tet1 = new Benchmark;
408 my $tetd = timediff($tet1, $tet0);
409 print "Total page evaluation took ".timestr($petd)."<br>"
412 my $trailer = Packages::HTML::trailer( $ROOT );
413 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME