]> git.deb.at Git - deb/packages.git/blob - cgi-bin/dispatcher.pl
Remove a line that only made sense in the context where I copied the
[deb/packages.git] / cgi-bin / dispatcher.pl
1 #!/usr/bin/perl -T
2 # $Id: search_packages.pl 91 2006-02-10 22:18:31Z jeroen $
3 # dispatcher.pl -- CGI interface for packages.debian.org
4 #
5 # Copyright (C) 2004-2006 Frank Lichtenheld
6 #
7 # use is allowed under the terms of the GNU Public License (GPL)                              
8 # see http://www.fsf.org/copyleft/gpl.html for a copy of the license
9
10 use strict;
11 use warnings;
12
13 use lib '../lib';
14 use CGI;
15 use POSIX;
16 use URI::Escape;
17 use HTML::Entities;
18 use DB_File;
19 use Benchmark ':hireswallclock';
20 use I18N::AcceptLanguage;
21 use Locale::gettext;
22
23 use Deb::Versions;
24 use Packages::Config qw( $DBDIR $ROOT @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES @LANGUAGES $LOCALES );
25 use Packages::CGI;
26 use Packages::DB;
27 use Packages::Search qw( :all );
28 use Packages::HTML ();
29 use Packages::Sections;
30 use Packages::I18N::Locale;
31
32 use Packages::DoSearch;
33 use Packages::DoSearchContents;
34 use Packages::DoShow;
35 use Packages::DoDownload;
36 use Packages::DoFilelist;
37
38 &Packages::CGI::reset;
39
40 # clean up env
41 $ENV{PATH} = "/bin:/usr/bin";
42 delete $ENV{'LANGUAGE'};
43 delete $ENV{'LANG'};
44 delete $ENV{'LC_ALL'};
45 delete $ENV{'LC_MESSAGES'};
46
47 # Read in all the variables set by the form
48 my $input;
49 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
50         $input = new CGI(\*STDIN);
51 } else {
52         $input = new CGI;
53 }
54
55 my $pet0 = new Benchmark;
56 my $tet0 = new Benchmark;
57 # use this to disable debugging in production mode completly
58 my $debug_allowed = 1;
59 my $debug = $debug_allowed && $input->param("debug");
60 $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
61 $Packages::CGI::debug = $debug;
62
63 &Packages::Config::init( '../' );
64 &Packages::DB::init();
65
66 my $acc = I18N::AcceptLanguage->new();
67 my $http_lang = $acc->accepts( $input->http("Accept-Language"),
68                                \@LANGUAGES );
69 debug( "LANGUAGES=@LANGUAGES header=".
70        $input->http("Accept-Language").
71        " http_lang=$http_lang", 2 );
72 bindtextdomain ( 'pdo', $LOCALES );
73 textdomain( 'pdo' );
74
75 my $what_to_do = 'show';
76 my $source = 0;
77 if (my $path = $input->path_info() || $input->param('PATH_INFO')) {
78     my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
79
80     debug( "components[0]=$components[0]", 2 ) if @components>0;
81     if (@components > 0 and $components[0] eq 'source') {
82         shift @components;
83         $input->param( 'source', 1 );
84     }
85     if (@components > 0 and $components[0] eq 'search') {
86         shift @components;
87         $what_to_do = 'search';
88         # Done
89         fatal_error( _( "search doesn't take any more path elements" ) )
90             if @components > 0;
91     } elsif (@components == 0) {
92         fatal_error( _( "We're supposed to display the homepage here, instead of getting dispatch.pl" ) );
93     } elsif (@components == 1) {
94         $what_to_do = 'search';
95     } else {
96
97         for ($components[-1]) {
98             /^(changelog|copyright|download|filelist)$/ && do {
99                 pop @components;
100                 $what_to_do = $1;
101                 last;
102             };
103         }
104
105         my %SUITES = map { $_ => 1 } @SUITES;
106         my %SUITES_ALIAS = ( woody => 'oldstable',
107                              sarge => 'stable',
108                              etch => 'testing',
109                              sid => 'unstable', );
110         my %SECTIONS = map { $_ => 1 } @SECTIONS;
111         my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
112         my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all');
113         my %params_set;
114         sub set_param_once {
115             my ($cgi, $params_set, $key, $val) = @_;
116             if ($params_set->{$key}++) {
117                 fatal_error( sprintf( _( "%s set more than once in path" ), $key ) );
118             } else {
119                 $cgi->param( $key, $val );
120             }
121         }
122
123         my @tmp;
124         foreach (@components) {
125             if ($SUITES{$_}) {
126                 set_param_once( $input, \%params_set, 'suite', $_);
127 #possible conflicts with package names
128 #           } elsif (my $s = $SUITES_ALIAS{$_}) {
129 #               set_param_once( $input, \%params_set, 'suite', $s);
130             } elsif ($SECTIONS{$_}) {
131                 set_param_once( $input, \%params_set, 'section', $_);
132             } elsif ($ARCHIVES{$_}) {
133                 set_param_once( $input, \%params_set, 'archive', $_);
134             } elsif ($ARCHITECTURES{$_}) {
135                 set_param_once( $input, \%params_set, 'arch', $_);
136             } elsif ($sections_descs{$_}) {
137                 set_param_once( $input, \%params_set, 'subsection', $_);
138             } elsif ($_ eq 'source') {
139                 set_param_once( $input, \%params_set, 'source', 1);
140             } else {
141                 push @tmp, $_;
142             }
143         }
144         @components = @tmp;
145
146         if (@components > 1) {
147             fatal_error( sprintf( _( "two or more packages specified (%s)" ), "@components" ) );
148         }
149     } # else if (@components == 1)
150     
151     if (@components) {
152         $input->param( 'keywords', $components[0] );
153         $input->param( 'package', $components[0] );
154     }
155 }
156
157 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
158
159 my %params_def = ( keywords => { default => undef,
160                                  match => '^\s*([-+\@\s\w\/.:]+)\s*$',
161                              },
162                    package => { default => undef,
163                                 match => '^([\w.+-]+)$',
164                                 var => \$pkg },
165                    suite => { default => 'all', match => '^([\w-]+)$',
166                               array => ',', var => \@suites,
167                               replace => { all => \@SUITES } },
168                    archive => { default => ($what_to_do eq 'search') ?
169                                     'all' : 'default',
170                                     match => '^([\w-]+)$',
171                                     array => ',', var => \@archives,
172                                     replace => { all => \@ARCHIVES,
173                                                  default => [qw(us security non-US)]} },
174                    exact => { default => 0, match => '^(\w+)$',  },
175                    lang => { default => $http_lang, match => '^(\w+)$',  },
176                    source => { default => 0, match => '^(\d+)$',  },
177                    searchon => { default => 'names', match => '^(\w+)$', },
178                    section => { default => 'all', match => '^([\w-]+)$',
179                                 alias => 'release', array => ',',
180                                 var => \@sections,
181                                 replace => { all => \@SECTIONS } },
182                    subsection => { default => 'default', match => '^([\w-]+)$',
183                                    array => ',', var => \@subsections,
184                                    replace => { default => [] } },
185                    arch => { default => 'any', match => '^([\w-]+)$',
186                              array => ',', var => \@archs, replace =>
187                              { any => \@ARCHITECTURES } },
188                    );
189 my %opts;
190 my %params = Packages::Search::parse_params( $input, \%params_def, \%opts );
191
192 my $locale = get_locale($opts{lang});
193 my $charset = get_charset($opts{lang});
194 setlocale ( LC_ALL, $locale )
195     or do { debug( "couldn't set locale $locale, using default" );
196             setlocale( LC_ALL, get_locale() )
197                 or do {
198                     debug( "couldn't set default locale either" );
199                     setlocale( LC_ALL, "C" );
200                 };
201         };
202 debug( "locale=$locale charset=$charset", 2 );
203
204 $opts{h_suites} = { map { $_ => 1 } @suites };
205 $opts{h_sections} = { map { $_ => 1 } @sections };
206 $opts{h_archives} = { map { $_ => 1 } @archives };
207 $opts{h_archs} = { map { $_ => 1 } @archs };
208
209 if ((($opts{searchon} eq 'names') && $opts{source}) ||
210     ($opts{searchon} eq 'sourcenames')) {
211     $opts{source} = 1;
212     $opts{searchon} = 'names',
213     $opts{searchon_form} = 'sourcenames';
214 } else {
215     $opts{searchon_form} = $opts{searchon};
216 }
217 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
218     $what_to_do = 'search_contents';
219 }
220
221 my $pet1 = new Benchmark;
222 my $petd = timediff($pet1, $pet0);
223 debug( "Parameter evaluation took ".timestr($petd) );
224
225 print $input->header( -charset => $charset );
226
227 my (%html_header, $menu, $page_content);
228 unless (@Packages::CGI::fatal_errors) {
229     no strict 'refs';
230     &{"do_$what_to_do"}( \%params, \%opts, \%html_header,
231                          \$menu, \$page_content );
232 } else {
233     %html_header = ( title => _('Error'),
234                      lang => $opts{lang},
235                      print_title => 1,
236                      print_search_field => 'packages',
237                      search_field_values => { 
238                          keywords => _('search for a package'),
239                          searchon => 'default',
240                          arch => 'any',
241                          suite => 'all',
242                          section => 'all',
243                          exact => 1,
244                          debug => $debug,
245                      },
246                      );
247 }
248
249 print Packages::HTML::header( %html_header );
250
251 print $menu||'';
252 print_errors();
253 print_hints();
254 print_msgs();
255 print_debug();
256 print_notes();
257
258 unless (@Packages::CGI::fatal_errors) {
259     print $page_content;
260 }
261
262 my $tet1 = new Benchmark;
263 my $tetd = timediff($tet1, $tet0);
264 print "Total page evaluation took ".timestr($tetd)."<br>"
265     if $debug_allowed;
266
267 my $trailer = Packages::HTML::trailer( $ROOT );
268 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME
269 print $trailer;
270
271 # vim: ts=8 sw=4
272