]> git.deb.at Git - deb/packages.git/blob - lib/Packages/Dispatcher.pm
Remove all uses of $localedir
[deb/packages.git] / lib / Packages / Dispatcher.pm
1 #!/usr/bin/perl -T
2 # Packages::Dispatcher -- CGI interface for packages.debian.org
3 #
4 # Copyright (C) 2004-2007 Frank Lichtenheld
5 #
6 #    This program is free software; you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation; either version 1 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program; if not, write to the Free Software
18 #    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 package Packages::Dispatcher;
21
22 use strict;
23 use warnings;
24
25 use CGI;
26 use POSIX;
27 use File::Basename;
28 use Template;
29 use DB_File;
30 use URI::Escape;
31 use Benchmark ':hireswallclock';
32 use I18N::AcceptLanguage;
33
34 use Deb::Versions;
35 use Packages::Config qw( $DBDIR $ROOT $TEMPLATEDIR $CACHEDIR
36                          @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES @PRIORITIES
37                          @LANGUAGES @DDTP_LANGUAGES );
38 use Packages::CGI qw( :DEFAULT error get_all_messages );
39 use Packages::DB;
40 use Packages::Search qw( :all );
41 use Packages::Template ();
42 use Packages::Sections;
43 use Packages::I18N::Locale;
44
45 use Packages::DoSearch;
46 use Packages::DoSearchContents;
47 use Packages::DoShow;
48 use Packages::DoIndex;
49 use Packages::DoNewPkg;
50 use Packages::DoDownload;
51 use Packages::DoFilelist;
52
53
54 sub do_dispatch {
55
56     &Packages::CGI::reset;
57     $Packages::Search::too_many_hits = 0;
58
59     # clean up env
60     $ENV{PATH} = "/bin:/usr/bin";
61     delete $ENV{'LANGUAGE'};
62     delete $ENV{'LANG'};
63     delete $ENV{'LC_ALL'};
64     delete $ENV{'LC_MESSAGES'};
65
66     my %SUITES_ALIAS = ( oldstable => 'sarge',
67                          stable => 'etch',
68                          testing => 'lenny',
69                          unstable => 'sid',
70                          '3.1' => 'sarge',
71                          '4.0' => 'etch' );
72
73     # Read in all the variables set by the form
74     my $input;
75     if ($ARGV[0] && ($ARGV[0] eq 'php')) {
76         $input = new CGI(\*STDIN);
77     } else {
78         $input = new CGI;
79     }
80     my $cgi_error = $input->cgi_error;
81     if ($cgi_error) {
82         fatal_error( "Error parsing the request", $cgi_error );
83     }
84
85     my $pet0 = new Benchmark;
86     my $tet0 = new Benchmark;
87     my $debug = DEBUG && $input->param("debug");
88     $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
89     $Packages::CGI::debug = $debug;
90
91     my $homedir = dirname($ENV{SCRIPT_FILENAME}).'/../';
92     &Packages::Config::init( $homedir );
93     &Packages::DB::init();
94
95     my $acc = I18N::AcceptLanguage->new();
96     my %all_langs = map { $_ => 1 } (@LANGUAGES, @DDTP_LANGUAGES);
97     my @all_langs = sort keys %all_langs;
98     my $http_lang = $acc->accepts( $input->http("Accept-Language"),
99                                    \@all_langs ) || 'en';
100     debug( "LANGUAGES=@all_langs header=".
101            ($input->http("Accept-Language")||'').
102            " http_lang=$http_lang", 1 ) if DEBUG;
103
104     # backwards compatibility stuff
105     debug( "SCRIPT_URL=$ENV{SCRIPT_URL} SCRIPT_URI=$ENV{SCRIPT_URI}" ) if DEBUG;
106
107     if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_|) {
108         error( "You reached this site over an old URL. ".
109                "Depending on the exact parameters your search might work or not." );
110         # contents search changed a lot
111         if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_contents|) {
112             $input->param('keywords',$input->param('word')) if $input->param('word');
113             $input->param('searchon','contents');
114             for ($input->param('searchmode')) {
115                 /^searchfiles/ && do {
116                     $input->param('mode','filename');
117                     last;
118                 };
119                 /^filelist/ && do {
120                     $ENV{PATH_INFO} = '/'.join('/',($input->param('version')||'stable',
121                                                     $input->param('keywords'),
122                                                     $input->param('arch')||'i386',
123                                                     'filelist' ));
124                     $input->delete('searchon','version','keywords','arch');
125                     last;
126                 };
127             }
128         }
129     }
130     if ($ENV{is_reportbug}) {
131         $input->param('exact', 1);
132         debug( "reportbug detected, set paramater exact to '1'" ) if DEBUG;
133     }
134
135     my $what_to_do = 'show';
136     my $source = 0;
137     if (my $path = $ENV{'PATH_INFO'} || $input->param('PATH_INFO')) {
138         my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
139
140         debug( "PATH_INFO=$path components=@components", 3) if DEBUG;
141
142         push @components, 'index' if @components && $path =~ m,/$,;
143
144         my %LANGUAGES = map { $_ => 1 } @all_langs;
145         if (@components > 1 and $LANGUAGES{$components[0]}
146             and !$input->param('lang')) {
147             $input->param( 'lang', shift(@components) );
148         }
149         if (@components > 1 and $components[0] eq 'source') {
150             shift @components;
151             $input->param( 'source', 1 );
152         }
153         if (@components > 0 and $components[0] eq 'search') {
154             shift @components;
155             $what_to_do = 'search';
156             # Done
157             fatal_error( "search doesn't take any more path elements" )
158                 if @components;
159         } elsif (@components == 0) {
160             fatal_error( "We're supposed to display the homepage here, instead of getting dispatch.pl" );
161         } elsif (@components == 1) {
162             $what_to_do = 'search';
163         } else {
164
165             for ($components[-1]) {
166                 /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
167                     pop @components;
168                     $what_to_do = $1;
169                     last;
170                 };
171             }
172
173             my %SUITES = map { $_ => 1 } @SUITES;
174             my %SECTIONS = map { $_ => 1 } @SECTIONS;
175             my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
176             my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all', 'any');
177             my %PRIORITIES = map { $_ => 1 } @PRIORITIES;
178             my %params_set;
179             sub set_param_once {
180                 my ($cgi, $params_set, $key, $val) = @_;
181                 debug("set_param_once key=$key val=$val",4) if DEBUG;
182                 if ($params_set->{$key}++) {
183                     fatal_error( "$key set more than once in path" );
184                 } else {
185                     $cgi->param( $key, $val );
186                 }
187             }
188
189             my (@pkg, $need_pkg);
190             foreach (reverse @components) {
191                 $need_pkg = !@pkg
192                     && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
193                 debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
194                 if (!$need_pkg && $SUITES{$_}) {
195                     set_param_once( $input, \%params_set, 'suite', $_);
196                 } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
197                     set_param_once( $input, \%params_set, 'suite', $s);
198                 } elsif (!$need_pkg && $SECTIONS{$_}) {
199                     set_param_once( $input, \%params_set, 'section', $_);
200                 } elsif (!$need_pkg && $ARCHIVES{$_}) {
201                     set_param_once( $input, \%params_set, 'archive', $_);
202                 } elsif (!$need_pkg && $sections_descs{$_}) {
203                     set_param_once( $input, \%params_set, 'subsection', $_);
204                 } elsif (!$need_pkg && ($_ eq 'source')) {
205                     set_param_once( $input, \%params_set, 'source', 1);
206                 } elsif ($ARCHITECTURES{$_}) {
207                     set_param_once( $input, \%params_set, 'arch', $_)
208                         unless $_ eq 'any';
209                 } elsif ($PRIORITIES{$_}) {
210                     set_param_once( $input, \%params_set, 'priority', $_);
211                 } else {
212                     push @pkg, $_;
213                 }
214             }
215             @components = @pkg;
216
217             if (@components > 1) {
218                 fatal_error( "two or more packages specified (@components)" );
219             }
220         } # else if (@components == 1)
221
222         if (@components) {
223             my $c = uri_unescape($components[0]);
224             $input->param( 'keywords', $c );
225             $input->param( 'package', $c );
226         }
227     }
228
229     my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
230
231     my %params_def = ( keywords => { default => undef,
232                                      array => '\s+',
233                                      match => '^([-+\@\w\/.:]+)$',
234                                  },
235                        'package' => { default => undef,
236                                       match => '^([\w.+-]+)$',
237                                       var => \$pkg },
238                        suite => { default => 'default', match => '^([\w-]+)$',
239                                   array => ',', var => \@suites,
240                                   replace => { all => \@SUITES,
241                                                default => \@SUITES,
242                                                %SUITES_ALIAS } },
243                        archive => { default => ($what_to_do eq 'search') ?
244                                         'all' : 'default',
245                                         match => '^([\w-]+)$',
246                                         array => ',', var => \@archives,
247                                         replace => { all => \@ARCHIVES,
248                                                      default => \@ARCHIVES} },
249                        exact => { default => 0, match => '^(\w+)$',  },
250                        lang => { default => $http_lang, match => '^(\w+)$',  },
251                        source => { default => 0, match => '^(\d+)$',  },
252                        debug => { default => 0, match => '^(\d+)$',  },
253                        searchon => { default => 'names', match => '^(\w+)$', },
254                        section => { default => 'all', match => '^([\w-]+)$',
255                                     alias => 'release', array => ',',
256                                     var => \@sections,
257                                     replace => { all => \@SECTIONS } },
258                        subsection => { default => 'default', match => '^([\w-]+)$',
259                                        array => ',', var => \@subsections,
260                                        replace => { default => [] } },
261                        priority => { default => 'default', match => '^([\w-]+)$',
262                                      array => ',',
263                                      replace => { default => [] } },
264                        arch => { default => 'any', match => '^([\w-]+)$',
265                                  array => ',', var => \@archs, replace =>
266                                  { any => \@ARCHITECTURES } },
267                        'format' => { default => 'html', match => '^([\w.]+)$',  },
268                        mode => { default => '', match => '^(\w+)$',  },
269                        sort_by => { default => 'file', match => '^(\w+)$', },
270                        );
271     my %opts;
272     my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
273     Packages::CGI::init_url( $input, \%params, \%opts );
274
275     my $charset = "UTF-8";
276     my $cat = Packages::I18N::Locale->get_handle( $opts{lang} )
277         || Packages::I18N::Locale->get_handle( 'en' );
278     die "get_handle failed for $opts{lang}" unless $cat;
279     $opts{cat} = $cat;
280
281     $opts{h_suites} = { map { $_ => 1 } @suites };
282     $opts{h_sections} = { map { $_ => 1 } @sections };
283     $opts{h_archives} = { map { $_ => 1 } @archives };
284     $opts{h_archs} = { map { $_ => 1 } @archs };
285
286     if ((($opts{searchon} eq 'names') && $opts{source}) ||
287         ($opts{searchon} eq 'sourcenames')) {
288         $opts{source} = 1;
289         $opts{searchon} = 'names',
290         $opts{searchon_form} = 'sourcenames';
291     } else {
292         $opts{searchon_form} = $opts{searchon};
293     }
294     if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
295         $what_to_do = 'search_contents';
296     }
297
298     my $pet1 = new Benchmark;
299     my $petd = timediff($pet1, $pet0);
300     debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
301
302     my $template = new Packages::Template( $TEMPLATEDIR, $opts{format},
303                                            { lang => $opts{lang}, charset => $charset,
304                                              cat => $cat,
305                                              debug => ( DEBUG ? $opts{debug} : 0 ) },
306                                            ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
307
308     #FIXME: ugly hack
309     unless (($what_to_do eq 'allpackages' and $opts{format} =~ /^(html|txt\.gz)/)
310             || -e "$TEMPLATEDIR/$opts{format}/${what_to_do}.tmpl") {
311         fatal_error( $cat->g("requested format not available for this document"),
312                      "406 requested format not available");
313     }
314
315     my (%page_content);
316     unless (@Packages::CGI::fatal_errors) {
317         no strict 'refs';
318         &{"do_$what_to_do"}( \%params, \%opts, \%page_content );
319     }
320
321     $page_content{opts} = \%opts;
322     $page_content{params} = \%params;
323
324     unless (@Packages::CGI::fatal_errors) {
325         print $input->header(-charset => $charset, -type => get_mime($opts{format}) );
326         #use Data::Dumper;
327         #print '<pre>'.Dumper(\%ENV, \%page_content, get_all_messages()).'</pre>';
328         print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
329     } elsif ($Packages::CGI::http_code && $Packages::CGI::http_code !~ /^2\d\d/) {
330         print $input->header( -charset => $charset, -status => $Packages::CGI::http_code );
331     } else {
332         # We currently have only an error page in html
333         # so no format support here
334         print $input->header( -charset => $charset );
335         print $template->error_page( get_all_messages() );
336     }
337 }
338
339 1;
340 # vim: ts=8 sw=4