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;
57 # FIXME: move to own module
58 my $modtime = (stat( "../config.sh" ))[9];
59 if ($modtime > $config_read_time) {
60 if (!open (C, '<', "../config.sh")) {
61 error( "Internal Error: Cannot open configuration file." );
65 $topdir = $1 if /^\s*topdir="?([^\"]*)"?\s*$/o;
66 $ROOT = $1 if /^\s*root="?([^\"]*)"?\s*$/o;
67 $Packages::HTML::HOME = $1 if /^\s*home="?([^\"]*)"?\s*$/o;
68 $Packages::HTML::SEARCH_CGI = $1 if /^\s*searchcgi="?([^\"]*)"?\s*$/o;
69 $Packages::HTML::SEARCH_PAGE = $1 if /^\s*searchpage="?([^\"]*)"?\s*$/o;
70 $Packages::HTML::WEBMASTER_MAIL = $1 if /^\s*webmaster="?([^\"]*)"?\s*$/o;
71 $Packages::HTML::CONTACT_MAIL = $1 if /^\s*contact="?([^\"]*)"?\s*$/o;
72 @SUITES = split(/\s+/, $1) if /^\s*suites="?([^\"]*)"?\s*$/o;
73 @SECTIONS = split(/\s+/, $1) if /^\s*sections="?([^\"]*)"?\s*$/o;
74 @ARCHITECTURES = split(/\s+/, $1) if /^\s*architectures="?([^\"]*)"?\s*$/o;
77 debug( "read config ($modtime > $config_read_time)" );
78 $config_read_time = $modtime;
80 my $DBDIR = $topdir . "/files/db";
81 my $thisscript = $Packages::HTML::SEARCH_CGI;
83 if (my $path = $input->param('path')) {
84 my @components = map { lc $_ } split /\//, $path;
86 my %SUITES = map { $_ => 1 } @SUITES;
87 my %SECTIONS = map { $_ => 1 } @SECTIONS;
88 my %ARCHITECTURES = map { $_ => 1 } @ARCHITECTURES;
90 foreach (@components) {
92 $input->param('suite', $_);
93 } elsif ($SECTIONS{$_}) {
94 $input->param('section', $_);
95 }elsif ($ARCHITECTURES{$_}) {
96 $input->param('arch', $_);
101 my ( $format, $keyword, $case, $subword, $exact, $searchon,
102 @suites, @sections, @archs );
104 my %params_def = ( keywords => { default => undef,
105 match => '^\s*([-+\@\w\/.:]+)\s*$',
107 suite => { default => 'stable', match => '^(\w+)$',
108 alias => 'version', array => ',',
110 replace => { all => \@SUITES } },
111 case => { default => 'insensitive', match => '^(\w+)$',
113 official => { default => 0, match => '^(\w+)$' },
114 subword => { default => 0, match => '^(\w+)$',
116 exact => { default => undef, match => '^(\w+)$',
118 searchon => { default => 'all', match => '^(\w+)$',
120 section => { default => 'all', match => '^([\w-]+)$',
121 alias => 'release', array => ',',
123 replace => { all => \@SECTIONS } },
124 arch => { default => 'any', match => '^(\w+)$',
125 array => ',', var => \@archs, replace =>
126 { any => \@ARCHITECTURES } },
127 format => { default => 'html', match => '^(\w+)$',
131 my %params = Packages::Search::parse_params( $input, \%params_def, \%opts );
133 #XXX: Don't use alternative output formats yet
135 if ($format eq 'html') {
136 print $input->header;
139 if ($params{errors}{keywords}) {
140 fatal_error( "keyword not valid or missing" );
141 } elsif (length($keyword) < 2) {
142 fatal_error( "keyword too short (keywords need to have at least two characters)" );
145 my $case_bool = ( $case !~ /insensitive/ );
146 $exact = !$subword unless defined $exact;
147 $opts{h_suites} = { map { $_ => 1 } @suites };
148 $opts{h_sections} = { map { $_ => 1 } @sections };
149 $opts{h_archs} = { map { $_ => 1 } @archs };
151 # for URL construction
152 my $suites_param = join ',', @{$params{values}{suite}{no_replace}};
153 my $sections_param = join ',', @{$params{values}{section}{no_replace}};
154 my $archs_param = join ',', @{$params{values}{arch}{no_replace}};
157 my $keyword_enc = encode_entities $keyword || '';
158 my $searchon_enc = encode_entities $searchon;
159 my $suites_enc = encode_entities join ', ', @{$params{values}{suite}{no_replace}};
160 my $sections_enc = encode_entities join ', ', @{$params{values}{section}{no_replace}};
161 my $archs_enc = encode_entities join ', ', @{$params{values}{arch}{no_replace}};
162 my $pet1 = new Benchmark;
163 my $petd = timediff($pet1, $pet0);
164 debug( "Parameter evaluation took ".timestr($petd) );
166 my $st0 = new Benchmark;
169 our ($obj, $s_obj, $p_obj, $sp_obj,
170 %packages, %sources, %postf, %spostf, %src2bin, %did2pkg );
172 unless (@Packages::CGI::fatal_errors) {
174 my $dbmodtime = (stat("$DBDIR/packages_small.db"))[9];
175 if ($dbmodtime > $db_read_time) {
176 $obj = tie %packages, 'DB_File', "$DBDIR/packages_small.db",
177 O_RDONLY, 0666, $DB_BTREE
178 or die "couldn't tie DB $DBDIR/packages_small.db: $!";
179 $s_obj = tie %sources, 'DB_File', "$DBDIR/sources_small.db",
180 O_RDONLY, 0666, $DB_BTREE
181 or die "couldn't tie DB $DBDIR/sources_small.db: $!";
182 $p_obj = tie %postf, 'DB_File', "$DBDIR/package_postfixes.db",
183 O_RDONLY, 0666, $DB_BTREE
184 or die "couldn't tie postfix db $DBDIR/package_postfixes.db: $!";
185 $sp_obj = tie %spostf, 'DB_File', "$DBDIR/source_postfixes.db",
186 O_RDONLY, 0666, $DB_BTREE
187 or die "couldn't tie postfix db $DBDIR/source_postfixes.db: $!";
188 tie %src2bin, 'DB_File', "$DBDIR/sources_packages.db",
189 O_RDONLY, 0666, $DB_BTREE
190 or die "couldn't open $DBDIR/sources_packages.db: $!";
191 tie %did2pkg, 'DB_File', "$DBDIR/descriptions_packages.db",
192 O_RDONLY, 0666, $DB_BTREE
193 or die "couldn't tie DB $DBDIR/descriptions_packages.db: $!";
195 debug( "tied databases ($dbmodtime > $db_read_time)" );
196 $db_read_time = $dbmodtime;
199 if ($searchon eq 'names') {
200 push @results, @{ do_names_search( $keyword, \%packages,
202 \&read_entry, \%opts ) };
203 } elsif ($searchon eq 'sourcenames') {
204 push @results, @{ do_names_search( $keyword, \%sources,
206 \&read_src_entry, \%opts ) };
208 push @results, @{ do_names_search( $keyword, \%packages,
210 \&read_entry, \%opts ) };
211 push @results, @{ do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
214 \&read_entry, \%opts ) };
218 my $st1 = new Benchmark;
219 my $std = timediff($st1, $st0);
220 debug( "Search took ".timestr($std) );
222 if ($format eq 'html') {
223 my $suite_wording = $suites_enc eq "all" ? "all suites"
224 : "suite(s) <em>$suites_enc</em>";
225 my $section_wording = $sections_enc eq 'all' ? "all sections"
226 : "section(s) <em>$sections_enc</em>";
227 my $arch_wording = $archs_enc eq 'any' ? "all architectures"
228 : "architecture(s) <em>$archs_enc</em>";
229 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
230 my $source_wording = ( $searchon eq 'sourcenames' ) ? "source " : "";
231 my $exact_wording = $exact ? "named" : "that names contain";
232 msg( "You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
234 my $exact_wording = $exact ? "" : " (including subword matching)";
235 msg( "You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording." );
239 if ($Packages::Search::too_many_hits) {
240 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." );
243 if (!@Packages::CGI::fatal_errors && !@results) {
244 if ($format eq 'html') {
245 my $keyword_esc = uri_escape( $keyword );
247 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
248 if (($suites_enc eq 'all')
249 && ($archs_enc eq 'any')
250 && ($sections_enc eq 'all')) {
251 error( "Can't find that package." );
253 error( "Can't find that package, at least not in that suite ".
254 ( ( $searchon eq 'sourcenames' ) ? "" : " and on that architecture" ) )
259 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>." );
262 if (($suites_enc eq 'all')
263 && ($archs_enc eq 'any')
264 && ($sections_enc eq 'all')) {
265 error( "Can't find that string." );
267 error( "Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc)." );
272 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>." );
275 hint( ( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$Packages::HTML::SEARCH_PAGE#search_packages\">Packages search page</a>." );
280 print Packages::HTML::header( title => 'Package Search Results' ,
282 title_tag => 'Debian Package Search Results',
283 print_title_above => 1,
284 print_search_field => 'packages',
285 search_field_values => {
286 keywords => $keyword_enc,
287 searchon => $searchon,
289 suite => $suites_enc,
290 section => $sections_enc,
302 my (%pkgs, %sect, %part, %desc, %binaries);
304 unless ($opts{searchon} eq 'sourcenames') {
306 my ($pkg_t, $suite, $arch, $section, $subsection,
307 $priority, $version, $desc) = @$_;
309 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
310 $pkgs{$pkg}{$suite}{$version}{$arch} = 1;
311 $sect{$pkg}{$suite}{$version} = $subsection;
312 $part{$pkg}{$suite}{$version} = $section
313 unless $section eq 'main';
315 $desc{$pkg}{$suite}{$version} = $desc;
318 if ($opts{format} eq 'html') {
319 my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
322 foreach my $pkg (sort keys %pkgs) {
324 next if $count < $start or $count > $end;
325 printf "<h3>Package %s</h3>\n", $pkg;
327 foreach my $suite (@SUITES) {
328 if (exists $pkgs{$pkg}{$suite}) {
329 my @versions = version_sort keys %{$pkgs{$pkg}{$suite}};
331 if ($part{$pkg}{$suite}{$versions[0]}) {
332 $part_str = "[<span style=\"color:red\">$part{$pkg}{$suite}{$versions[0]}</span>]";
334 printf "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s %s\n",
335 $suite, $pkg, $suite, $sect{$pkg}{$suite}{$versions[0]},
336 $desc{$pkg}{$suite}{$versions[0]}, $part_str;
338 foreach my $v (@versions) {
339 printf "<br>%s: %s\n",
340 $v, join (" ", (sort keys %{$pkgs{$pkg}{$suite}{$v}}) );
350 my ($pkg, $suite, $section, $subsection, $priority,
353 $pkgs{$pkg}{$suite} = $version;
354 $sect{$pkg}{$suite}{source} = $subsection;
355 $part{$pkg}{$suite}{source} = $section
356 unless $section eq 'main';
358 $binaries{$pkg}{$suite} = find_binaries( $pkg, $suite, \%src2bin );
361 if ($opts{format} eq 'html') {
362 my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
365 foreach my $pkg (sort keys %pkgs) {
367 next if ($count < $start) or ($count > $end);
368 printf "<h3>Source package %s</h3>\n", $pkg;
370 foreach my $suite (@SUITES) {
371 if (exists $pkgs{$pkg}{$suite}) {
373 if ($part{$pkg}{$suite}{source}) {
374 $part_str = "[<span style=\"color:red\">$part{$pkg}{$suite}{source}</span>]";
376 printf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s",
377 $suite, $pkg, $suite, $sect{$pkg}{$suite}{source},
378 $pkgs{$pkg}{$suite}, $part_str );
380 print "<br>Binary packages: ";
382 foreach my $bp (@{$binaries{$pkg}{$suite}}) {
383 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
384 $suite, uri_escape( $bp ), $bp );
385 push @bp_links, $bp_link;
387 print join( ", ", @bp_links );
395 printindexline( $input, scalar keys %pkgs, \%opts );
397 #print_results(\@results, \%opts) if @results;;
398 my $tet1 = new Benchmark;
399 my $tetd = timediff($tet1, $tet0);
400 print "Total page evaluation took ".timestr($petd)."<br>"
403 my $trailer = Packages::HTML::trailer( $ROOT );
404 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME