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