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
16 use CGI qw( -oldstyle_urls );
17 use CGI::Carp qw( fatalsToBrowser );
25 use Packages::Config qw( $DBDIR $ROOT $SEARCH_CGI $SEARCH_PAGE
26 @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES );
29 use Packages::Search qw( :all );
30 use Packages::HTML ();
32 &Packages::CGI::reset;
34 $ENV{PATH} = "/bin:/usr/bin";
36 # Read in all the variables set by the form
38 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
39 $input = new CGI(\*STDIN);
44 my $pet0 = new Benchmark;
45 my $tet0 = new Benchmark;
46 # use this to disable debugging in production mode completly
47 my $debug_allowed = 1;
48 my $debug = $debug_allowed && $input->param("debug");
49 $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
50 $Packages::CGI::debug = $debug;
52 &Packages::Config::init( '../' );
53 &Packages::DB::init();
55 if (my $path = $input->param('path')) {
56 my @components = map { lc $_ } split /\//, $path;
58 my %SUITES = map { $_ => 1 } @SUITES;
59 my %SECTIONS = map { $_ => 1 } @SECTIONS;
60 my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
61 my %ARCHITECTURES = map { $_ => 1 } @ARCHITECTURES;
63 foreach (@components) {
65 $input->param('suite', $_);
66 } elsif ($SECTIONS{$_}) {
67 $input->param('section', $_);
68 } elsif ($ARCHIVES{$_}) {
69 $input->param('archive', $_);
70 } elsif ($ARCHITECTURES{$_}) {
71 $input->param('arch', $_);
72 } elsif ($_ eq 'source') {
73 $input->param('searchon','sourcenames');
78 my ( $format, $keyword, $case, $subword, $exact, $searchon,
79 @suites, @sections, @archives, @archs );
81 my %params_def = ( keywords => { default => undef,
82 match => '^\s*([-+\@\w\/.:]+)\s*$',
84 suite => { default => 'stable', match => '^([\w-]+)$',
85 alias => 'version', array => ',',
87 replace => { all => \@SUITES } },
88 archive => { default => 'all', match => '^([\w-]+)$',
89 array => ',', var => \@archives,
90 replace => { all => \@ARCHIVES } },
91 case => { default => 'insensitive', match => '^(\w+)$',
93 official => { default => 0, match => '^(\w+)$' },
94 subword => { default => 0, match => '^(\w+)$',
96 exact => { default => undef, match => '^(\w+)$',
98 searchon => { default => 'all', match => '^(\w+)$',
100 section => { default => 'all', match => '^([\w-]+)$',
101 alias => 'release', array => ',',
103 replace => { all => \@SECTIONS } },
104 arch => { default => 'any', match => '^(\w+)$',
105 array => ',', var => \@archs, replace =>
106 { any => \@ARCHITECTURES } },
107 format => { default => 'html', match => '^(\w+)$',
111 my %params = Packages::Search::parse_params( $input, \%params_def, \%opts );
113 #XXX: Don't use alternative output formats yet
115 if ($format eq 'html') {
116 print $input->header( -charset => 'utf-8' );
119 if ($params{errors}{keywords}) {
120 fatal_error( "keyword not valid or missing" );
121 } elsif (length($keyword) < 2) {
122 fatal_error( "keyword too short (keywords need to have at least two characters)" );
125 my $case_bool = ( $case !~ /insensitive/ );
126 $exact = !$subword unless defined $exact;
127 $opts{h_suites} = { map { $_ => 1 } @suites };
128 $opts{h_sections} = { map { $_ => 1 } @sections };
129 $opts{h_archives} = { map { $_ => 1 } @archives };
130 $opts{h_archs} = { map { $_ => 1 } @archs };
132 # for URL construction
133 my $suites_param = join ',', @{$params{values}{suite}{no_replace}};
134 my $sections_param = join ',', @{$params{values}{section}{no_replace}};
135 my $archs_param = join ',', @{$params{values}{arch}{no_replace}};
138 my $keyword_enc = encode_entities $keyword || '';
139 my $searchon_enc = encode_entities $searchon;
140 my $suites_enc = encode_entities join ', ', @{$params{values}{suite}{no_replace}};
141 my $sections_enc = encode_entities join ', ', @{$params{values}{section}{no_replace}};
142 my $archs_enc = encode_entities join ', ', @{$params{values}{arch}{no_replace}};
143 my $pet1 = new Benchmark;
144 my $petd = timediff($pet1, $pet0);
145 debug( "Parameter evaluation took ".timestr($petd) );
147 my $st0 = new Benchmark;
150 unless (@Packages::CGI::fatal_errors) {
152 if ($searchon eq 'names') {
153 push @results, @{ do_names_search( $keyword, \%packages,
155 \&read_entry, \%opts ) };
156 } elsif ($searchon eq 'sourcenames') {
157 push @results, @{ do_names_search( $keyword, \%sources,
159 \&read_src_entry, \%opts ) };
160 } elsif ($searchon eq 'contents') {
161 require "./search_contents.pl";
164 push @results, @{ do_names_search( $keyword, \%packages,
166 \&read_entry, \%opts ) };
167 push @results, @{ do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
170 \&read_entry, \%opts ) };
174 my $st1 = new Benchmark;
175 my $std = timediff($st1, $st0);
176 debug( "Search took ".timestr($std) );
178 if ($format eq 'html') {
179 my $suite_wording = $suites_enc eq "all" ? "all suites"
180 : "suite(s) <em>$suites_enc</em>";
181 my $section_wording = $sections_enc eq 'all' ? "all sections"
182 : "section(s) <em>$sections_enc</em>";
183 my $arch_wording = $archs_enc eq 'any' ? "all architectures"
184 : "architecture(s) <em>$archs_enc</em>";
185 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
186 my $source_wording = ( $searchon eq 'sourcenames' ) ? "source " : "";
187 my $exact_wording = $exact ? "named" : "that names contain";
188 msg( "You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
190 my $exact_wording = $exact ? "" : " (including subword matching)";
191 msg( "You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording." );
195 if ($Packages::Search::too_many_hits) {
196 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." );
199 if (!@Packages::CGI::fatal_errors && !@results) {
200 if ($format eq 'html') {
201 my $keyword_esc = uri_escape( $keyword );
203 if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
204 if (($suites_enc eq 'all')
205 && ($archs_enc eq 'any')
206 && ($sections_enc eq 'all')) {
207 error( "Can't find that package." );
209 error( "Can't find that package, at least not in that suite ".
210 ( ( $searchon eq 'sourcenames' ) ? "" : " and on that architecture" ) )
215 hint( "You have searched only for exact matches of the package name. You can try to search for <a href=\"$SEARCH_CGI?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>." );
218 if (($suites_enc eq 'all')
219 && ($archs_enc eq 'any')
220 && ($sections_enc eq 'all')) {
221 error( "Can't find that string." );
223 error( "Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc)." );
228 hint( "You have searched only for words exactly matching your keywords. You can try to search <a href=\"$SEARCH_CGI?subword=1&searchon=$searchon&suite=$suites_param&case=$case&section=$sections_param&keywords=$keyword_esc&arch=$archs_param\">allowing subword matching</a>." );
231 hint( ( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$SEARCH_PAGE#search_packages\">Packages search page</a>." );
236 print Packages::HTML::header( title => 'Package Search Results' ,
238 title_tag => 'Debian Package Search Results',
239 print_title_above => 1,
240 print_search_field => 'packages',
241 search_field_values => {
242 keywords => $keyword_enc,
243 searchon => $searchon,
245 suite => $suites_enc,
246 section => $sections_enc,
258 my (%pkgs, %subsect, %sect, %archives, %desc, %binaries, %provided_by);
260 unless ($opts{searchon} eq 'sourcenames') {
262 my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
263 $priority, $version, $desc) = @$_;
265 my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
266 if ($arch ne 'virtual') {
268 if ($archive =~ /^(security|non-US)$/) {
269 $real_archive = $archive;
273 $pkgs{$pkg}{$suite}{$archive}{$version}{$arch} = 1;
274 $subsect{$pkg}{$suite}{$archive}{$version} = $subsection;
275 $sect{$pkg}{$suite}{$archive}{$version} = $section
276 unless $section eq 'main';
277 $archives{$pkg}{$suite}{$archive}{$version} = $real_archive
280 $desc{$pkg}{$suite}{$archive}{$version} = $desc;
282 $provided_by{$pkg}{$suite}{$archive} = [ split /\s+/, $desc ];
286 my @pkgs = sort(keys %pkgs, keys %provided_by);
287 if ($opts{format} eq 'html') {
288 #my ($start, $end) = multipageheader( $input, scalar @pkgs, \%opts );
289 print "<p>Found <em>".(scalar @pkgs)."</em> matching packages,";
292 foreach my $pkg (@pkgs) {
294 #next if $count < $start or $count > $end;
295 printf "<h3>Package %s</h3>\n", $pkg;
297 foreach my $suite (@SUITES) {
298 foreach my $archive (@ARCHIVES) {
299 next if $archive eq 'security';
300 next if $archive eq 'non-US';
301 my $path = $suite.(($archive ne 'us')?"/$archive":'');
302 if (exists $pkgs{$pkg}{$suite}{$archive}) {
304 my @versions = version_sort keys %{$pkgs{$pkg}{$suite}{$archive}};
306 if ($sect{$pkg}{$suite}{$archive}{$versions[0]}) {
307 $origin_str .= " [<span style=\"color:red\">$sect{$pkg}{$suite}{$archive}{$versions[0]}</span>]";
309 printf "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s %s\n",
310 $path, $pkg, $path, $subsect{$pkg}{$suite}{$archive}{$versions[0]},
311 $desc{$pkg}{$suite}{$archive}{$versions[0]}, $origin_str;
313 foreach my $v (@versions) {
314 my $archive_str = "";
315 if ($archives{$pkg}{$suite}{$archive}{$v}) {
316 $archive_str .= " [<span style=\"color:red\">$archives{$pkg}{$suite}{$archive}{$v}</span>]";
319 my @archs_to_print = grep { !$archs_printed{$_} } sort keys %{$pkgs{$pkg}{$suite}{$archive}{$v}};
320 printf "<br>%s$archive_str: %s\n",
321 $v, join (" ", @archs_to_print )
323 $archs_printed{$_}++ foreach @archs_to_print;
325 if (my $provided_by = $provided_by{$pkg}{$suite}{$archive}) {
326 print '<br>also provided by: ',
327 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$provided_by);
330 } elsif (my $provided_by = $provided_by{$pkg}{$suite}{$archive}) {
331 printf "<li><a href=\"$ROOT/%s/%s\">%s</a>: Virtual package<br>",
333 print 'provided by: ',
334 join( ', ', map { "<a href=\"$ROOT/$path/$_\">$_</a>" } @$provided_by);
343 my ($pkg, $archive, $suite, $section, $subsection, $priority,
346 my $real_archive = '';
347 if ($archive =~ /^(security|non-US)$/) {
348 $real_archive = $archive;
351 if (($real_archive eq $archive) &&
352 $pkgs{$pkg}{$suite}{$archive} &&
353 (version_cmp( $pkgs{$pkg}{$suite}{$archive}, $version ) >= 0)) {
356 $pkgs{$pkg}{$suite}{$archive} = $version;
357 $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
358 $sect{$pkg}{$suite}{$archive}{source} = $section
359 unless $section eq 'main';
360 $archives{$pkg}{$suite}{$archive}{source} = $real_archive
363 $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
366 if ($opts{format} eq 'html') {
367 #my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
368 print "<p>Found <em>".(scalar keys %pkgs)."</em> matching packages,";
371 foreach my $pkg (sort keys %pkgs) {
373 #next if ($count < $start) or ($count > $end);
374 printf "<h3>Source package %s</h3>\n", $pkg;
376 foreach my $suite (@SUITES) {
377 foreach my $archive (@ARCHIVES) {
378 if (exists $pkgs{$pkg}{$suite}{$archive}) {
380 if ($sect{$pkg}{$suite}{$archive}{source}) {
381 $origin_str .= " [<span style=\"color:red\">$sect{$pkg}{$suite}{$archive}{source}</span>]";
383 if ($archives{$pkg}{$suite}{$archive}{source}) {
384 $origin_str .= " [<span style=\"color:red\">$archives{$pkg}{$suite}{$archive}{source}</span>]";
386 printf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s %s",
387 $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect{$pkg}{$suite}{$archive}{source},
388 $pkgs{$pkg}{$suite}{$archive}, $origin_str );
390 print "<br>Binary packages: ";
392 foreach my $bp (@{$binaries{$pkg}{$suite}{$archive}}) {
393 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
394 $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ), $bp );
395 push @bp_links, $bp_link;
397 print join( ", ", @bp_links );
406 #printindexline( $input, scalar keys %pkgs, \%opts );
408 #print_results(\@results, \%opts) if @results;;
409 my $tet1 = new Benchmark;
410 my $tetd = timediff($tet1, $tet0);
411 print "Total page evaluation took ".timestr($tetd)."<br>"
414 my $trailer = Packages::HTML::trailer( $ROOT );
415 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME