]> git.deb.at Git - deb/packages.git/blob - cgi-bin/search_packages.pl
e5cf44d15d4b2bd44a517fd0bc7a29a09a22dc2f
[deb/packages.git] / cgi-bin / search_packages.pl
1 #!/usr/bin/perl -wT
2 # $Id$
3 # search_packages.pl -- CGI interface to the Packages files on packages.debian.org
4 #
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
10 #
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
13
14 use strict;
15 use CGI qw( -oldstyle_urls );
16 use CGI::Carp qw( fatalsToBrowser );
17 use POSIX;
18 use URI::Escape;
19 use HTML::Entities;
20 use DB_File;
21 use Benchmark;
22
23 use Deb::Versions;
24 use Packages::CGI;
25 use Packages::Search qw( :all );
26 use Packages::HTML ();
27
28 &Packages::CGI::reset;
29
30 $ENV{PATH} = "/bin:/usr/bin";
31
32 # Read in all the variables set by the form
33 my $input;
34 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
35         $input = new CGI(\*STDIN);
36 } else {
37         $input = new CGI;
38 }
39
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;
47
48 # read the configuration
49 our $config_read_time ||= 0;
50 our $db_read_time ||= 0;
51 our ( $topdir, $ROOT, @SUITES, @SECTIONS, @ARCHIVES, @ARCHITECTURES );
52
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." );
58     }
59     while (<C>) {
60         next if /^\s*\#/o;
61         chomp;
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;
73     }
74     close (C);
75     debug( "read config ($modtime > $config_read_time)" );
76     $config_read_time = $modtime;
77 }
78 my $DBDIR = $topdir . "/files/db";
79 my $thisscript = $Packages::HTML::SEARCH_CGI;
80
81 if (my $path = $input->param('path')) {
82     my @components = map { lc $_ } split /\//, $path;
83
84     my %SUITES = map { $_ => 1 } @SUITES;
85     my %SECTIONS = map { $_ => 1 } @SECTIONS;
86     my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
87     my %ARCHITECTURES = map { $_ => 1 } @ARCHITECTURES;
88
89     foreach (@components) {
90         if ($SUITES{$_}) {
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', $_);
98         }
99     }
100 }
101
102 my ( $format, $keyword, $case, $subword, $exact, $searchon,
103      @suites, @sections, @archives, @archs );
104
105 my %params_def = ( keywords => { default => undef,
106                                  match => '^\s*([-+\@\w\/.:]+)\s*$',
107                                  var => \$keyword },
108                    suite => { default => 'stable', match => '^([\w-]+)$',
109                               alias => 'version', array => ',',
110                               var => \@suites,
111                               replace => { all => \@SUITES } },
112                    archive => { default => 'all', match => '^([\w-]+)$',
113                                 array => ',', var => \@archives,
114                                 replace => { all => \@ARCHIVES } },
115                    case => { default => 'insensitive', match => '^(\w+)$',
116                              var => \$case },
117                    official => { default => 0, match => '^(\w+)$' },
118                    subword => { default => 0, match => '^(\w+)$',
119                                 var => \$subword },
120                    exact => { default => undef, match => '^(\w+)$',
121                               var => \$exact },
122                    searchon => { default => 'all', match => '^(\w+)$',
123                                  var => \$searchon },
124                    section => { default => 'all', match => '^([\w-]+)$',
125                                 alias => 'release', array => ',',
126                                 var => \@sections,
127                                 replace => { all => \@SECTIONS } },
128                    arch => { default => 'any', match => '^(\w+)$',
129                              array => ',', var => \@archs, replace =>
130                              { any => \@ARCHITECTURES } },
131                    format => { default => 'html', match => '^(\w+)$',
132                                var => \$format },
133                    );
134 my %opts;
135 my %params = Packages::Search::parse_params( $input, \%params_def, \%opts );
136
137 #XXX: Don't use alternative output formats yet
138 $format = 'html';
139 if ($format eq 'html') {
140     print $input->header;
141 }
142
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)" );
147 }
148
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 };
155
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}};
160
161 # for output
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) );
170
171 my $st0 = new Benchmark;
172 my @results;
173
174 our ($obj, $s_obj, $p_obj, $sp_obj,
175      %packages, %sources, %postf, %spostf, %src2bin, %did2pkg );
176
177 unless (@Packages::CGI::fatal_errors) {
178
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: $!";
199         
200         debug( "tied databases ($dbmodtime > $db_read_time)" );
201         $db_read_time = $dbmodtime;
202     }
203
204     if ($searchon eq 'names') {
205         push @results, @{ do_names_search( $keyword, \%packages,
206                                            $p_obj,
207                                            \&read_entry, \%opts ) };
208     } elsif ($searchon eq 'sourcenames') {
209         push @results, @{ do_names_search( $keyword, \%sources,
210                                            $sp_obj,
211                                            \&read_src_entry, \%opts ) };
212     } else {
213         push @results, @{ do_names_search( $keyword, \%packages,
214                                            $p_obj,
215                                            \&read_entry, \%opts ) };
216         push @results, @{ do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
217                                               \%did2pkg,
218                                               \%packages,
219                                               \&read_entry, \%opts ) };
220     }
221 }
222
223 my $st1 = new Benchmark;
224 my $std = timediff($st1, $st0);
225 debug( "Search took ".timestr($std) );
226
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." );
238     } else {
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." );
241     }
242 }
243
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." );
246 }
247
248 if (!@Packages::CGI::fatal_errors && !@results) {
249     if ($format eq 'html') {
250         my $keyword_esc = uri_escape( $keyword );
251         my $printed = 0;
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." );
257             } else {
258                 error( "Can't find that package, at least not in that suite ".
259                     ( ( $searchon eq 'sourcenames' ) ? "" : " and on that architecture" ) )
260             }
261             
262             if ($exact) {
263                 $printed++;
264                 hint( "You have searched only for exact matches of the package name. You can try to search for <a href=\"$thisscript?exact=0&amp;searchon=$searchon&amp;suite=$suites_param&amp;case=$case&amp;section=$sections_param&amp;keywords=$keyword_esc&amp;arch=$archs_param\">package names that contain your search string</a>." );
265             }
266         } else {
267             if (($suites_enc eq 'all')
268                 && ($archs_enc eq 'any')
269                 && ($sections_enc eq 'all')) {
270                 error( "Can't find that string." );
271             } else {
272                 error( "Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc)." );
273             }
274             
275             unless ($subword) {
276                 $printed++;
277                 hint( "You have searched only for words exactly matching your keywords. You can try to search <a href=\"$thisscript?subword=1&amp;searchon=$searchon&amp;suite=$suites_param&amp;case=$case&amp;section=$sections_param&amp;keywords=$keyword_esc&amp;arch=$archs_param\">allowing subword matching</a>." );
278             }
279         }
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>." );
281             
282     }
283 }
284
285 print Packages::HTML::header( title => 'Package Search Results' ,
286                               lang => 'en',
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,
293                                   arch => $archs_enc,
294                                   suite => $suites_enc,
295                                   section => $sections_enc,
296                                   subword => $subword,
297                                   exact => $exact,
298                                   case => $case,
299                                   debug => $debug,
300                               },
301                               );
302 print_msgs();
303 print_errors();
304 print_hints();
305 print_debug();
306 if (@results) {
307     my (%pkgs, %subsect, %sect, %desc, %binaries);
308
309     unless ($opts{searchon} eq 'sourcenames') {
310         foreach (@results) {
311             my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
312                 $priority, $version, $desc) = @$_;
313         
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';
319             
320             $desc{$pkg}{$suite}{$archive}{$version} = $desc;
321         }
322
323         if ($opts{format} eq 'html') {
324             my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
325             my $count = 0;
326         
327             foreach my $pkg (sort keys %pkgs) {
328                 $count++;
329                 next if $count < $start or $count > $end;
330                 printf "<h3>Package %s</h3>\n", $pkg;
331                 print "<ul>\n";
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}};
336                             my $origin_str = "";
337                             if ($sect{$pkg}{$suite}{$archive}{$versions[0]}) {
338                                 $origin_str .= " [<span style=\"color:red\">$sect{$pkg}{$suite}{$versions[0]}</span>]";
339                             }
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;
343                             
344                             foreach my $v (@versions) {
345                                 printf "<br>%s: %s\n",
346                                 $v, join (" ", (sort keys %{$pkgs{$pkg}{$suite}{$archive}{$v}}) );
347                             }
348                             print "</li>\n";
349                         }
350                     }
351                 }
352                 print "</ul>\n";
353             }
354         }
355     } else {
356         foreach (@results) {
357             my ($pkg, $archive, $suite, $section, $subsection, $priority,
358                 $version) = @$_;
359         
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';
364
365             $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
366         }
367
368         if ($opts{format} eq 'html') {
369             my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
370             my $count = 0;
371             
372             foreach my $pkg (sort keys %pkgs) {
373                 $count++;
374                 next if ($count < $start) or ($count > $end);
375                 printf "<h3>Source package %s</h3>\n", $pkg;
376                 print "<ul>\n";
377                 foreach my $suite (@SUITES) {
378                     foreach my $archive (@ARCHIVES) {
379                         if (exists $pkgs{$pkg}{$suite}{$archive}) {
380                             my $origin_str = "";
381                             if ($sect{$pkg}{$suite}{$archive}{source}) {
382                                 $origin_str .= " [<span style=\"color:red\">$sect{$pkg}{$suite}{$archive}{source}</span>]";
383                             }
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 );
387                             
388                             print "<br>Binary packages: ";
389                             my @bp_links;
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;
394                             }
395                             print join( ", ", @bp_links );
396                             print "</li>\n";
397                         }
398                     }
399                 }
400                 print "</ul>\n";
401             }
402         }
403     }
404     printindexline( $input, scalar keys %pkgs, \%opts );
405 }
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($tetd)."<br>"
410     if $debug_allowed;
411
412 my $trailer = Packages::HTML::trailer( $ROOT );
413 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME
414 print $trailer;
415
416 # vim: ts=8 sw=4