]> git.deb.at Git - deb/packages.git/blob - cgi-bin/dispatcher.pl
- Fix some issues with non-US handling
[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::DoIndex;
36 use Packages::DoNewPkg;
37 use Packages::DoDownload;
38 use Packages::DoFilelist;
39
40 &Packages::CGI::reset;
41
42 # clean up env
43 $ENV{PATH} = "/bin:/usr/bin";
44 delete $ENV{'LANGUAGE'};
45 delete $ENV{'LANG'};
46 delete $ENV{'LC_ALL'};
47 delete $ENV{'LC_MESSAGES'};
48
49 # Read in all the variables set by the form
50 my $input;
51 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
52         $input = new CGI(\*STDIN);
53 } else {
54         $input = new CGI;
55 }
56
57 my $pet0 = new Benchmark;
58 my $tet0 = new Benchmark;
59 my $debug = DEBUG && $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 ) || 'en';
69 debug( "LANGUAGES=@LANGUAGES header=".
70        ($input->http("Accept-Language")||'').
71        " http_lang=$http_lang", 2 ) if DEBUG;
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     push @components, 'index' if $path =~ m,/$,;
81
82     my %LANGUAGES = map { $_ => 1 } @LANGUAGES;
83     if (@components > 0 and $LANGUAGES{$components[0]}) {
84         $input->param( 'lang', shift(@components) );
85     }
86     if (@components > 0 and $components[0] eq 'source') {
87         shift @components;
88         $input->param( 'source', 1 );
89     }
90     if (@components > 0 and $components[0] eq 'search') {
91         shift @components;
92         $what_to_do = 'search';
93         # Done
94         fatal_error( _g( "search doesn't take any more path elements" ) )
95             if @components;
96     } elsif (@components == 0) {
97         fatal_error( _g( "We're supposed to display the homepage here, instead of getting dispatch.pl" ) );
98     } elsif (@components == 1) {
99         $what_to_do = 'search';
100     } else {
101
102         for ($components[-1]) {
103             /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
104                 pop @components;
105                 $what_to_do = $1;
106                 last;
107             };
108         }
109
110         my %SUITES = map { $_ => 1 } @SUITES;
111         my %SUITES_ALIAS = ( woody => 'oldstable',
112                              sarge => 'stable',
113                              etch => 'testing',
114                              sid => 'unstable', );
115         my %SECTIONS = map { $_ => 1 } @SECTIONS;
116         my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
117         my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all');
118         my %params_set;
119         sub set_param_once {
120             my ($cgi, $params_set, $key, $val) = @_;
121             debug("set_param_once key=$key val=$val",4) if DEBUG;
122             if ($params_set->{$key}++) {
123                 fatal_error( sprintf( _g( "%s set more than once in path" ), $key ) );
124             } else {
125                 $cgi->param( $key, $val );
126             }
127         }
128
129         my (@pkg, $need_pkg);
130         foreach (reverse @components) {
131             $need_pkg = !@pkg
132                 && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
133             debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
134             if (!$need_pkg && $SUITES{$_}) {
135                 set_param_once( $input, \%params_set, 'suite', $_);
136             } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
137                 set_param_once( $input, \%params_set, 'suite', $s);
138             } elsif (!$need_pkg && $SECTIONS{$_}) {
139                 set_param_once( $input, \%params_set, 'section', $_);
140             } elsif (!$need_pkg && $ARCHIVES{$_}) {
141                 set_param_once( $input, \%params_set, 'archive', $_);
142             } elsif (!$need_pkg && $sections_descs{$_}) {
143                 set_param_once( $input, \%params_set, 'subsection', $_);
144             } elsif (!$need_pkg && ($_ eq 'non-us')) { # non-US hack
145                 set_param_once( $input, \%params_set, 'subsection', 'non-US');
146             } elsif (!$need_pkg && ($_ eq 'source')) {
147                 set_param_once( $input, \%params_set, 'source', 1);
148             } elsif ($ARCHITECTURES{$_}) {
149                 set_param_once( $input, \%params_set, 'arch', $_);
150             } else {
151                 push @pkg, $_;
152             }
153         }
154         @components = @pkg;
155
156         if (@components > 1) {
157             fatal_error( sprintf( _g( "two or more packages specified (%s)" ), "@components" ) );
158         }
159     } # else if (@components == 1)
160     
161     if (@components) {
162         $input->param( 'keywords', $components[0] );
163         $input->param( 'package', $components[0] );
164     }
165 }
166
167 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
168
169 my %params_def = ( keywords => { default => undef,
170                                  match => '^\s*([-+\@\s\w\/.:]+)\s*$',
171                              },
172                    package => { default => undef,
173                                 match => '^([\w.+-]+)$',
174                                 var => \$pkg },
175                    suite => { default => 'default', match => '^([\w-]+)$',
176                               array => ',', var => \@suites,
177                               replace => { all => \@SUITES,
178                                            default => \@SUITES } },
179                    archive => { default => ($what_to_do eq 'search') ?
180                                     'all' : 'default',
181                                     match => '^([\w-]+)$',
182                                     array => ',', var => \@archives,
183                                     replace => { all => \@ARCHIVES,
184                                                  default => \@ARCHIVES} },
185                    exact => { default => 0, match => '^(\w+)$',  },
186                    lang => { default => $http_lang, match => '^(\w+)$',  },
187                    source => { default => 0, match => '^(\d+)$',  },
188                    debug => { default => 0, match => '^(\d+)$',  },
189                    searchon => { default => 'names', match => '^(\w+)$', },
190                    section => { default => 'all', match => '^([\w-]+)$',
191                                 alias => 'release', array => ',',
192                                 var => \@sections,
193                                 replace => { all => \@SECTIONS } },
194                    subsection => { default => 'default', match => '^([\w-]+)$',
195                                    array => ',', var => \@subsections,
196                                    replace => { default => [] } },
197                    arch => { default => 'any', match => '^([\w-]+)$',
198                              array => ',', var => \@archs, replace =>
199                              { any => \@ARCHITECTURES } },
200                    format => { default => 'html', match => '^([\w.]+)$',  },
201                    mode => { default => undef, match => '^(\w+)$',  },
202                    );
203 my %opts;
204 my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
205 Packages::CGI::init_url( $input, \%params, \%opts );
206
207 my $locale = get_locale($opts{lang});
208 my $charset = get_charset($opts{lang});
209 setlocale ( LC_ALL, $locale )
210     or do { debug( "couldn't set locale $locale, using default" ) if DEBUG;
211             setlocale( LC_ALL, get_locale() )
212                 or do {
213                     debug( "couldn't set default locale either" ) if DEBUG;
214                     setlocale( LC_ALL, "C" );
215                 };
216         };
217 debug( "locale=$locale charset=$charset", 2 ) if DEBUG;
218
219 $opts{h_suites} = { map { $_ => 1 } @suites };
220 $opts{h_sections} = { map { $_ => 1 } @sections };
221 $opts{h_archives} = { map { $_ => 1 } @archives };
222 $opts{h_archs} = { map { $_ => 1 } @archs };
223
224 if ((($opts{searchon} eq 'names') && $opts{source}) ||
225     ($opts{searchon} eq 'sourcenames')) {
226     $opts{source} = 1;
227     $opts{searchon} = 'names',
228     $opts{searchon_form} = 'sourcenames';
229 } else {
230     $opts{searchon_form} = $opts{searchon};
231 }
232 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
233     $what_to_do = 'search_contents';
234 }
235
236 my $pet1 = new Benchmark;
237 my $petd = timediff($pet1, $pet0);
238 debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
239
240 my (%html_header, $menu, $page_content);
241 unless (@Packages::CGI::fatal_errors) {
242     no strict 'refs';
243     &{"do_$what_to_do"}( \%params, \%opts, \%html_header,
244                          \$menu, \$page_content );
245 } else {
246     %html_header = ( title => _g('Error'),
247                      lang => $opts{lang},
248                      print_title => 1,
249                      print_search_field => 'packages',
250                      search_field_values => { 
251                          keywords => _g('search for a package'),
252                          searchon => 'default',
253                          arch => 'any',
254                          suite => 'all',
255                          section => 'all',
256                          exact => 1,
257                          debug => $debug,
258                      },
259                      );
260 }
261
262 print $input->header( -charset => $charset );
263
264 print Packages::HTML::header( %html_header );
265
266 print $menu||'';
267 print_errors();
268 print_hints();
269 print_msgs();
270 print_debug() if DEBUG;
271 print_notes();
272
273 unless (@Packages::CGI::fatal_errors) {
274     print $page_content;
275 }
276
277 my $tet1 = new Benchmark;
278 my $tetd = timediff($tet1, $tet0);
279 print "Total page evaluation took ".timestr($tetd)."<br>"
280     if DEBUG;
281
282 my $trailer = Packages::HTML::trailer( $ROOT );
283 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME
284 print $trailer;
285
286 # vim: ts=8 sw=4
287