]> git.deb.at Git - deb/packages.git/blob - cgi-bin/search_packages.pl
These changes make the search_packages.pl script mod_perl ready
[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;
52 our $ROOT;
53 our @SUITES;
54 our @SECTIONS;
55 our @ARCHITECTURES;
56
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." );
62     }
63     while (<C>) {
64         chomp;
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;
75     }
76     close (C);
77     debug( "read config ($modtime > $config_read_time)" );
78     $config_read_time = $modtime;
79 }
80 my $DBDIR = $topdir . "/files/db";
81 my $thisscript = $Packages::HTML::SEARCH_CGI;
82
83 if (my $path = $input->param('path')) {
84     my @components = map { lc $_ } split /\//, $path;
85
86     my %SUITES = map { $_ => 1 } @SUITES;
87     my %SECTIONS = map { $_ => 1 } @SECTIONS;
88     my %ARCHITECTURES = map { $_ => 1 } @ARCHITECTURES;
89
90     foreach (@components) {
91         if ($SUITES{$_}) {
92             $input->param('suite', $_);
93         } elsif ($SECTIONS{$_}) {
94             $input->param('section', $_);
95         }elsif ($ARCHITECTURES{$_}) {
96             $input->param('arch', $_);
97         }
98     }
99 }
100
101 my ( $format, $keyword, $case, $subword, $exact, $searchon,
102      @suites, @sections, @archs );
103
104 my %params_def = ( keywords => { default => undef,
105                                  match => '^\s*([-+\@\w\/.:]+)\s*$',
106                                  var => \$keyword },
107                    suite => { default => 'stable', match => '^(\w+)$',
108                               alias => 'version', array => ',',
109                               var => \@suites,
110                               replace => { all => \@SUITES } },
111                    case => { default => 'insensitive', match => '^(\w+)$',
112                              var => \$case },
113                    official => { default => 0, match => '^(\w+)$' },
114                    subword => { default => 0, match => '^(\w+)$',
115                                 var => \$subword },
116                    exact => { default => undef, match => '^(\w+)$',
117                               var => \$exact },
118                    searchon => { default => 'all', match => '^(\w+)$',
119                                  var => \$searchon },
120                    section => { default => 'all', match => '^([\w-]+)$',
121                                 alias => 'release', array => ',',
122                                 var => \@sections,
123                                 replace => { all => \@SECTIONS } },
124                    arch => { default => 'any', match => '^(\w+)$',
125                              array => ',', var => \@archs, replace =>
126                              { any => \@ARCHITECTURES } },
127                    format => { default => 'html', match => '^(\w+)$',
128                                var => \$format },
129                    );
130 my %opts;
131 my %params = Packages::Search::parse_params( $input, \%params_def, \%opts );
132
133 #XXX: Don't use alternative output formats yet
134 $format = 'html';
135 if ($format eq 'html') {
136     print $input->header;
137 }
138
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)" );
143 }
144
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 };
150
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}};
155
156 # for output
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) );
165
166 my $st0 = new Benchmark;
167 my @results;
168
169 our ($obj, $s_obj, $p_obj, $sp_obj,
170      %packages, %sources, %postf, %spostf, %src2bin, %did2pkg );
171
172 unless (@Packages::CGI::fatal_errors) {
173
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: $!";
194         
195         debug( "tied databases ($dbmodtime > $db_read_time)" );
196         $db_read_time = $dbmodtime;
197     }
198
199     if ($searchon eq 'names') {
200         push @results, @{ do_names_search( $keyword, \%packages,
201                                            $p_obj,
202                                            \&read_entry, \%opts ) };
203     } elsif ($searchon eq 'sourcenames') {
204         push @results, @{ do_names_search( $keyword, \%sources,
205                                            $sp_obj,
206                                            \&read_src_entry, \%opts ) };
207     } else {
208         push @results, @{ do_names_search( $keyword, \%packages,
209                                            $p_obj,
210                                            \&read_entry, \%opts ) };
211         push @results, @{ do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
212                                               \%did2pkg,
213                                               \%packages,
214                                               \&read_entry, \%opts ) };
215     }
216 }
217
218 my $st1 = new Benchmark;
219 my $std = timediff($st1, $st0);
220 debug( "Search took ".timestr($std) );
221
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." );
233     } else {
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." );
236     }
237 }
238
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." );
241 }
242
243 if (!@Packages::CGI::fatal_errors && !@results) {
244     if ($format eq 'html') {
245         my $keyword_esc = uri_escape( $keyword );
246         my $printed = 0;
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." );
252             } else {
253                 error( "Can't find that package, at least not in that suite ".
254                     ( ( $searchon eq 'sourcenames' ) ? "" : " and on that architecture" ) )
255             }
256             
257             if ($exact) {
258                 $printed++;
259                 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>." );
260             }
261         } else {
262             if (($suites_enc eq 'all')
263                 && ($archs_enc eq 'any')
264                 && ($sections_enc eq 'all')) {
265                 error( "Can't find that string." );
266             } else {
267                 error( "Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc)." );
268             }
269             
270             unless ($subword) {
271                 $printed++;
272                 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>." );
273             }
274         }
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>." );
276             
277     }
278 }
279
280 print Packages::HTML::header( title => 'Package Search Results' ,
281                               lang => 'en',
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,
288                                   arch => $archs_enc,
289                                   suite => $suites_enc,
290                                   section => $sections_enc,
291                                   subword => $subword,
292                                   exact => $exact,
293                                   case => $case,
294                                   debug => $debug,
295                               },
296                               );
297 print_msgs();
298 print_errors();
299 print_hints();
300 print_debug();
301 if (@results) {
302     my (%pkgs, %sect, %part, %desc, %binaries);
303
304     unless ($opts{searchon} eq 'sourcenames') {
305         foreach (@results) {
306             my ($pkg_t, $suite, $arch, $section, $subsection,
307                 $priority, $version, $desc) = @$_;
308         
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';
314             
315             $desc{$pkg}{$suite}{$version} = $desc;
316         }
317
318         if ($opts{format} eq 'html') {
319             my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
320             my $count = 0;
321         
322             foreach my $pkg (sort keys %pkgs) {
323                 $count++;
324                 next if $count < $start or $count > $end;
325                 printf "<h3>Package %s</h3>\n", $pkg;
326                 print "<ul>\n";
327                 foreach my $suite (@SUITES) {
328                     if (exists $pkgs{$pkg}{$suite}) {
329                         my @versions = version_sort keys %{$pkgs{$pkg}{$suite}};
330                         my $part_str = "";
331                         if ($part{$pkg}{$suite}{$versions[0]}) {
332                             $part_str = "[<span style=\"color:red\">$part{$pkg}{$suite}{$versions[0]}</span>]";
333                         }
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;
337                         
338                         foreach my $v (@versions) {
339                             printf "<br>%s: %s\n",
340                             $v, join (" ", (sort keys %{$pkgs{$pkg}{$suite}{$v}}) );
341                         }
342                         print "</li>\n";
343                     }
344                 }
345                 print "</ul>\n";
346             }
347         }
348     } else {
349         foreach (@results) {
350             my ($pkg, $suite, $section, $subsection, $priority,
351                 $version) = @$_;
352         
353             $pkgs{$pkg}{$suite} = $version;
354             $sect{$pkg}{$suite}{source} = $subsection;
355             $part{$pkg}{$suite}{source} = $section
356                 unless $section eq 'main';
357
358             $binaries{$pkg}{$suite} = find_binaries( $pkg, $suite, \%src2bin );
359         }
360
361         if ($opts{format} eq 'html') {
362             my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
363             my $count = 0;
364             
365             foreach my $pkg (sort keys %pkgs) {
366                 $count++;
367                 next if ($count < $start) or ($count > $end);
368                 printf "<h3>Source package %s</h3>\n", $pkg;
369                 print "<ul>\n";
370                 foreach my $suite (@SUITES) {
371                     if (exists $pkgs{$pkg}{$suite}) {
372                         my $part_str = "";
373                         if ($part{$pkg}{$suite}{source}) {
374                             $part_str = "[<span style=\"color:red\">$part{$pkg}{$suite}{source}</span>]";
375                         }
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 );
379                         
380                         print "<br>Binary packages: ";
381                         my @bp_links;
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;
386                         }
387                         print join( ", ", @bp_links );
388                         print "</li>\n";
389                     }
390                 }
391                 print "</ul>\n";
392             }
393         }
394     }
395     printindexline( $input, scalar keys %pkgs, \%opts );
396 }
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>"
401     if $debug_allowed;
402
403 my $trailer = Packages::HTML::trailer( $ROOT );
404 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME
405 print $trailer;
406
407 # vim: ts=8 sw=4