]> git.deb.at Git - deb/packages.git/blob - cgi-bin/search_packages.pl
* Move coniguratio stuf to own module
[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::Config qw( $DBDIR $ROOT $SEARCH_CGI $SEARCH_PAGE
25                          @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES );
26 use Packages::CGI;
27 use Packages::Search qw( :all );
28 use Packages::HTML ();
29
30 &Packages::CGI::reset;
31
32 $ENV{PATH} = "/bin:/usr/bin";
33
34 # Read in all the variables set by the form
35 my $input;
36 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
37         $input = new CGI(\*STDIN);
38 } else {
39         $input = new CGI;
40 }
41
42 my $pet0 = new Benchmark;
43 my $tet0 = new Benchmark;
44 # use this to disable debugging in production mode completly
45 my $debug_allowed = 1;
46 my $debug = $debug_allowed && $input->param("debug");
47 $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
48 $Packages::CGI::debug = $debug;
49
50 # read the configuration
51 our $db_read_time ||= 0;
52
53 &Packages::Config::init( '../' );
54
55 if (my $path = $input->param('path')) {
56     my @components = map { lc $_ } split /\//, $path;
57
58     my %SUITES = map { $_ => 1 } @SUITES;
59     my %SECTIONS = map { $_ => 1 } @SECTIONS;
60     my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
61     my %ARCHITECTURES = map { $_ => 1 } @ARCHITECTURES;
62
63     foreach (@components) {
64         if ($SUITES{$_}) {
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');
74         }
75     }
76 }
77
78 my ( $format, $keyword, $case, $subword, $exact, $searchon,
79      @suites, @sections, @archives, @archs );
80
81 my %params_def = ( keywords => { default => undef,
82                                  match => '^\s*([-+\@\w\/.:]+)\s*$',
83                                  var => \$keyword },
84                    suite => { default => 'stable', match => '^([\w-]+)$',
85                               alias => 'version', array => ',',
86                               var => \@suites,
87                               replace => { all => \@SUITES } },
88                    archive => { default => 'all', match => '^([\w-]+)$',
89                                 array => ',', var => \@archives,
90                                 replace => { all => \@ARCHIVES } },
91                    case => { default => 'insensitive', match => '^(\w+)$',
92                              var => \$case },
93                    official => { default => 0, match => '^(\w+)$' },
94                    subword => { default => 0, match => '^(\w+)$',
95                                 var => \$subword },
96                    exact => { default => undef, match => '^(\w+)$',
97                               var => \$exact },
98                    searchon => { default => 'all', match => '^(\w+)$',
99                                  var => \$searchon },
100                    section => { default => 'all', match => '^([\w-]+)$',
101                                 alias => 'release', array => ',',
102                                 var => \@sections,
103                                 replace => { all => \@SECTIONS } },
104                    arch => { default => 'any', match => '^(\w+)$',
105                              array => ',', var => \@archs, replace =>
106                              { any => \@ARCHITECTURES } },
107                    format => { default => 'html', match => '^(\w+)$',
108                                var => \$format },
109                    );
110 my %opts;
111 my %params = Packages::Search::parse_params( $input, \%params_def, \%opts );
112
113 #XXX: Don't use alternative output formats yet
114 $format = 'html';
115 if ($format eq 'html') {
116     print $input->header;
117 }
118
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)" );
123 }
124
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 };
131
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}};
136
137 # for output
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) );
146
147 my $st0 = new Benchmark;
148 my @results;
149
150 our ($obj, $s_obj, $p_obj, $sp_obj,
151      %packages, %sources, %postf, %spostf, %src2bin, %did2pkg );
152
153 unless (@Packages::CGI::fatal_errors) {
154
155     my $dbmodtime = (stat("$DBDIR/packages_small.db"))[9];
156     if ($dbmodtime > $db_read_time) {
157         $obj = tie %packages, 'DB_File', "$DBDIR/packages_small.db",
158         O_RDONLY, 0666, $DB_BTREE
159             or die "couldn't tie DB $DBDIR/packages_small.db: $!";
160         $s_obj = tie %sources, 'DB_File', "$DBDIR/sources_small.db",
161         O_RDONLY, 0666, $DB_BTREE
162             or die "couldn't tie DB $DBDIR/sources_small.db: $!";
163         $p_obj = tie %postf, 'DB_File', "$DBDIR/package_postfixes.db",
164         O_RDONLY, 0666, $DB_BTREE
165             or die "couldn't tie postfix db $DBDIR/package_postfixes.db: $!";
166         $sp_obj = tie %spostf, 'DB_File', "$DBDIR/source_postfixes.db",
167         O_RDONLY, 0666, $DB_BTREE
168             or die "couldn't tie postfix db $DBDIR/source_postfixes.db: $!";
169         tie %src2bin, 'DB_File', "$DBDIR/sources_packages.db",
170         O_RDONLY, 0666, $DB_BTREE
171             or die "couldn't open $DBDIR/sources_packages.db: $!";
172         tie %did2pkg, 'DB_File', "$DBDIR/descriptions_packages.db",
173         O_RDONLY, 0666, $DB_BTREE
174             or die "couldn't tie DB $DBDIR/descriptions_packages.db: $!";
175         
176         debug( "tied databases ($dbmodtime > $db_read_time)" );
177         $db_read_time = $dbmodtime;
178     }
179
180     if ($searchon eq 'names') {
181         push @results, @{ do_names_search( $keyword, \%packages,
182                                            $p_obj,
183                                            \&read_entry, \%opts ) };
184     } elsif ($searchon eq 'sourcenames') {
185         push @results, @{ do_names_search( $keyword, \%sources,
186                                            $sp_obj,
187                                            \&read_src_entry, \%opts ) };
188     } else {
189         push @results, @{ do_names_search( $keyword, \%packages,
190                                            $p_obj,
191                                            \&read_entry, \%opts ) };
192         push @results, @{ do_fulltext_search( $keyword, "$DBDIR/descriptions.txt",
193                                               \%did2pkg,
194                                               \%packages,
195                                               \&read_entry, \%opts ) };
196     }
197 }
198
199 my $st1 = new Benchmark;
200 my $std = timediff($st1, $st0);
201 debug( "Search took ".timestr($std) );
202
203 if ($format eq 'html') {
204     my $suite_wording = $suites_enc eq "all" ? "all suites"
205         : "suite(s) <em>$suites_enc</em>";
206     my $section_wording = $sections_enc eq 'all' ? "all sections"
207         : "section(s) <em>$sections_enc</em>";
208     my $arch_wording = $archs_enc eq 'any' ? "all architectures"
209         : "architecture(s) <em>$archs_enc</em>";
210     if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
211         my $source_wording = ( $searchon eq 'sourcenames' ) ? "source " : "";
212         my $exact_wording = $exact ? "named" : "that names contain";
213         msg( "You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording." );
214     } else {
215         my $exact_wording = $exact ? "" : " (including subword matching)";
216         msg( "You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording." );
217     }
218 }
219
220 if ($Packages::Search::too_many_hits) {
221     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." );
222 }
223
224 if (!@Packages::CGI::fatal_errors && !@results) {
225     if ($format eq 'html') {
226         my $keyword_esc = uri_escape( $keyword );
227         my $printed = 0;
228         if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
229             if (($suites_enc eq 'all')
230                 && ($archs_enc eq 'any')
231                 && ($sections_enc eq 'all')) {
232                 error( "Can't find that package." );
233             } else {
234                 error( "Can't find that package, at least not in that suite ".
235                     ( ( $searchon eq 'sourcenames' ) ? "" : " and on that architecture" ) )
236             }
237             
238             if ($exact) {
239                 $printed++;
240                 hint( "You have searched only for exact matches of the package name. You can try to search for <a href=\"$SEARCH_CGI?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>." );
241             }
242         } else {
243             if (($suites_enc eq 'all')
244                 && ($archs_enc eq 'any')
245                 && ($sections_enc eq 'all')) {
246                 error( "Can't find that string." );
247             } else {
248                 error( "Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc)." );
249             }
250             
251             unless ($subword) {
252                 $printed++;
253                 hint( "You have searched only for words exactly matching your keywords. You can try to search <a href=\"$SEARCH_CGI?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>." );
254             }
255         }
256         hint( ( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$SEARCH_PAGE#search_packages\">Packages search page</a>." );
257             
258     }
259 }
260
261 print Packages::HTML::header( title => 'Package Search Results' ,
262                               lang => 'en',
263                               title_tag => 'Debian Package Search Results',
264                               print_title_above => 1,
265                               print_search_field => 'packages',
266                               search_field_values => { 
267                                   keywords => $keyword_enc,
268                                   searchon => $searchon,
269                                   arch => $archs_enc,
270                                   suite => $suites_enc,
271                                   section => $sections_enc,
272                                   subword => $subword,
273                                   exact => $exact,
274                                   case => $case,
275                                   debug => $debug,
276                               },
277                               );
278 print_msgs();
279 print_errors();
280 print_hints();
281 print_debug();
282 if (@results) {
283     my (%pkgs, %subsect, %sect, %desc, %binaries);
284
285     unless ($opts{searchon} eq 'sourcenames') {
286         foreach (@results) {
287             my ($pkg_t, $archive, $suite, $arch, $section, $subsection,
288                 $priority, $version, $desc) = @$_;
289         
290             my ($pkg) = $pkg_t =~ m/^(.+)/; # untaint
291             $pkgs{$pkg}{$suite}{$archive}{$version}{$arch} = 1;
292             $subsect{$pkg}{$suite}{$archive}{$version} = $subsection;
293             $sect{$pkg}{$suite}{$archive}{$version} = $section
294                 unless $section eq 'main';
295             
296             $desc{$pkg}{$suite}{$archive}{$version} = $desc;
297         }
298
299         if ($opts{format} eq 'html') {
300             my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
301             my $count = 0;
302         
303             foreach my $pkg (sort keys %pkgs) {
304                 $count++;
305                 next if $count < $start or $count > $end;
306                 printf "<h3>Package %s</h3>\n", $pkg;
307                 print "<ul>\n";
308                 foreach my $suite (@SUITES) {
309                     foreach my $archive (@ARCHIVES) {
310                         if (exists $pkgs{$pkg}{$suite}{$archive}) {
311                             my @versions = version_sort keys %{$pkgs{$pkg}{$suite}{$archive}};
312                             my $origin_str = "";
313                             if ($sect{$pkg}{$suite}{$archive}{$versions[0]}) {
314                                 $origin_str .= " [<span style=\"color:red\">$sect{$pkg}{$suite}{$versions[0]}</span>]";
315                             }
316                             printf "<li><a href=\"$ROOT/%s/%s\">%s</a> (%s): %s   %s\n",
317                             $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect{$pkg}{$suite}{$archive}{$versions[0]},
318                             $desc{$pkg}{$suite}{$archive}{$versions[0]}, $origin_str;
319                             
320                             foreach my $v (@versions) {
321                                 printf "<br>%s: %s\n",
322                                 $v, join (" ", (sort keys %{$pkgs{$pkg}{$suite}{$archive}{$v}}) );
323                             }
324                             print "</li>\n";
325                         }
326                     }
327                 }
328                 print "</ul>\n";
329             }
330         }
331     } else {
332         foreach (@results) {
333             my ($pkg, $archive, $suite, $section, $subsection, $priority,
334                 $version) = @$_;
335         
336             $pkgs{$pkg}{$suite}{$archive} = $version;
337             $subsect{$pkg}{$suite}{$archive}{source} = $subsection;
338             $sect{$pkg}{$suite}{$archive}{source} = $section
339                 unless $section eq 'main';
340
341             $binaries{$pkg}{$suite}{$archive} = find_binaries( $pkg, $archive, $suite, \%src2bin );
342         }
343
344         if ($opts{format} eq 'html') {
345             my ($start, $end) = multipageheader( $input, scalar keys %pkgs, \%opts );
346             my $count = 0;
347             
348             foreach my $pkg (sort keys %pkgs) {
349                 $count++;
350                 next if ($count < $start) or ($count > $end);
351                 printf "<h3>Source package %s</h3>\n", $pkg;
352                 print "<ul>\n";
353                 foreach my $suite (@SUITES) {
354                     foreach my $archive (@ARCHIVES) {
355                         if (exists $pkgs{$pkg}{$suite}{$archive}) {
356                             my $origin_str = "";
357                             if ($sect{$pkg}{$suite}{$archive}{source}) {
358                                 $origin_str .= " [<span style=\"color:red\">$sect{$pkg}{$suite}{$archive}{source}</span>]";
359                             }
360                             printf( "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s   %s",
361                                     $suite.(($archive ne 'us')?"/$archive":''), $pkg, $suite.(($archive ne 'us')?"/$archive":''), $subsect{$pkg}{$suite}{$archive}{source},
362                                     $pkgs{$pkg}{$suite}{$archive}, $origin_str );
363                             
364                             print "<br>Binary packages: ";
365                             my @bp_links;
366                             foreach my $bp (@{$binaries{$pkg}{$suite}{$archive}}) {
367                                 my $bp_link = sprintf( "<a href=\"$ROOT/%s/%s\">%s</a>",
368                                                        $suite.(($archive ne 'us')?"/$archive":''), uri_escape( $bp ),  $bp );
369                                 push @bp_links, $bp_link;
370                             }
371                             print join( ", ", @bp_links );
372                             print "</li>\n";
373                         }
374                     }
375                 }
376                 print "</ul>\n";
377             }
378         }
379     }
380     printindexline( $input, scalar keys %pkgs, \%opts );
381 }
382 #print_results(\@results, \%opts) if @results;;
383 my $tet1 = new Benchmark;
384 my $tetd = timediff($tet1, $tet0);
385 print "Total page evaluation took ".timestr($tetd)."<br>"
386     if $debug_allowed;
387
388 my $trailer = Packages::HTML::trailer( $ROOT );
389 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME
390 print $trailer;
391
392 # vim: ts=8 sw=4