]> git.deb.at Git - deb/packages.git/blob - lib/Packages/Dispatcher.pm
debtags.ja.po: Make a minor improvement.
[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-2008 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 use Date::Parse;
34
35 use Deb::Versions;
36 use Packages::Config qw( $DBDIR $ROOT $TEMPLATEDIR $CACHEDIR
37                          @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES @PRIORITIES
38                          @LANGUAGES @DDTP_LANGUAGES );
39 use Packages::CGI qw( :DEFAULT error get_all_messages );
40 use Packages::DB;
41 use Packages::Search qw( :all );
42 use Packages::Template ();
43 use Packages::Sections;
44 use Packages::I18N::Locale;
45
46 use Packages::DoSearch;
47 use Packages::DoSearchContents;
48 use Packages::DoShow;
49 use Packages::DoIndex;
50 use Packages::DoNewPkg;
51 use Packages::DoDownload;
52 use Packages::DoFilelist;
53
54
55 sub do_dispatch {
56
57     &Packages::CGI::reset;
58     $Packages::Search::too_many_hits = 0;
59
60     # clean up env
61     $ENV{PATH} = "/bin:/usr/bin";
62     delete $ENV{'LANGUAGE'};
63     delete $ENV{'LANG'};
64     delete $ENV{'LC_ALL'};
65     delete $ENV{'LC_MESSAGES'};
66
67     my %SUITES_ALIAS = ( #oldstable => 'sarge',
68                          stable => 'etch',
69                          testing => 'lenny',
70                          unstable => 'sid',
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     my $last_modified = $Packages::DB::db_read_time;
95     my $now = time;
96     my $expires = $last_modified + (12*3600);
97     $expires = $now + 3600 if $expires < $now;
98     # allow some fudge, since the db mod time is not the end of
99     # the cron job
100     $last_modified = $now if $now - $last_modified < 3600;
101
102     if ($input->http('If-Modified-Since') and
103         (my $modtime = str2time($input->http('If-Modified-Since'), 'UTC'))) {
104         if ($modtime <= $last_modified) {
105             print $input->header(-status => 304);
106             exit;
107         }
108     }
109
110     my %PO_LANGUAGES = map { $_ => 1 } @LANGUAGES;
111     my %DDTP_LANGUAGES = map { $_ => 1 } @DDTP_LANGUAGES;
112     my $acc = I18N::AcceptLanguage->new(defaultLanguage => 'en');
113     my $ddtp_lang = $acc->accepts( $input->http("Accept-Language"),
114                                    \@DDTP_LANGUAGES );
115     my $po_lang = $acc->accepts( $input->http("Accept-Language"),
116                                  \@LANGUAGES );
117     debug( "LANGS=@LANGUAGES DDTP_LANGS=@DDTP_LANGUAGES header=".
118            ($input->http("Accept-Language")||'').
119            " po_lang=$po_lang ddtp_lang=$ddtp_lang", 1 ) if DEBUG;
120
121     # backwards compatibility stuff
122     debug( "SCRIPT_URL=$ENV{SCRIPT_URL} SCRIPT_URI=$ENV{SCRIPT_URI}" ) if DEBUG;
123
124     if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_|) {
125         error( "You reached this site over an old URL. ".
126                "Depending on the exact parameters your search might work or not." );
127         # contents search changed a lot
128         if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_contents|) {
129             $input->param('keywords',$input->param('word')) if $input->param('word');
130             $input->param('searchon','contents');
131             for ($input->param('searchmode')) {
132                 /^searchfiles/ && do {
133                     $input->param('mode','filename');
134                     last;
135                 };
136                 /^filelist/ && do {
137                     $ENV{PATH_INFO} = '/'.join('/',($input->param('version')||'stable',
138                                                     $input->param('keywords'),
139                                                     $input->param('arch')||'i386',
140                                                     'filelist' ));
141                     $input->delete('searchon','version','keywords','arch');
142                     last;
143                 };
144             }
145         }
146     }
147     if ($ENV{is_reportbug}) {
148         $input->param('exact', 1);
149         debug( "reportbug detected, set paramater exact to '1'" ) if DEBUG;
150     }
151
152     my $what_to_do = 'show';
153     my $source = 0;
154     if (my $path = $ENV{'PATH_INFO'} || $input->param('PATH_INFO')) {
155         my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
156
157         debug( "PATH_INFO=$path components=@components", 3) if DEBUG;
158
159         push @components, 'index' if @components && $path =~ m,/$,;
160
161         my %LANGUAGES = map { $_ => 1 } (@LANGUAGES, @DDTP_LANGUAGES);
162         if (@components > 1 and $LANGUAGES{$components[0]}
163             and !$input->param('lang')) {
164             $input->param( 'lang', shift(@components) );
165         }
166         if (@components > 1 and $components[0] eq 'source') {
167             shift @components;
168             $input->param( 'source', 1 );
169         }
170         if (@components > 0 and $components[0] eq 'search') {
171             shift @components;
172             $what_to_do = 'search';
173             # Done
174             fatal_error( "search doesn't take any more path elements" )
175                 if @components;
176         } elsif (@components == 0) {
177             fatal_error( "We're supposed to display the homepage here, instead of getting dispatch.pl" );
178         } elsif (@components == 1) {
179             $what_to_do = 'search';
180         } else {
181
182             for ($components[-1]) {
183                 /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
184                     pop @components;
185                     $what_to_do = $1;
186                     last;
187                 };
188             }
189
190             my %SUITES = map { $_ => 1 } @SUITES;
191             my %SECTIONS = map { $_ => 1 } @SECTIONS;
192             my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
193             my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all', 'any');
194             my %PRIORITIES = map { $_ => 1 } @PRIORITIES;
195             my %params_set;
196             sub set_param_once {
197                 my ($cgi, $params_set, $key, $val) = @_;
198                 debug("set_param_once key=$key val=$val",4) if DEBUG;
199                 if ($params_set->{$key}++) {
200                     fatal_error( "$key set more than once in path" );
201                 } else {
202                     $cgi->param( $key, $val );
203                 }
204             }
205
206             my (@pkg, $need_pkg);
207             foreach (reverse @components) {
208                 $need_pkg = !@pkg
209                     && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
210                 debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
211                 if (!$need_pkg && $SUITES{$_}) {
212                     set_param_once( $input, \%params_set, 'suite', $_);
213                 } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
214                     set_param_once( $input, \%params_set, 'suite', $s);
215                 } elsif (!$need_pkg && $SECTIONS{$_}) {
216                     set_param_once( $input, \%params_set, 'section', $_);
217                 } elsif (!$need_pkg && $sections_descs{$_}) {
218                     set_param_once( $input, \%params_set, 'subsection', $_);
219                 } elsif (!$need_pkg && ($_ eq 'source')) {
220                     set_param_once( $input, \%params_set, 'source', 1);
221                 } elsif ($ARCHITECTURES{$_}) {
222                     set_param_once( $input, \%params_set, 'arch', $_)
223                         unless $_ eq 'any';
224                 } elsif (!$need_pkg && $ARCHIVES{$_}) {
225                     set_param_once( $input, \%params_set, 'archive', $_);
226                 } elsif ($PRIORITIES{$_}) {
227                     set_param_once( $input, \%params_set, 'priority', $_);
228                 } else {
229                     push @pkg, $_;
230                 }
231             }
232             @components = @pkg;
233
234             if (@components > 1) {
235                 fatal_error( "two or more packages specified (@components)" );
236             }
237         } # else if (@components == 1)
238
239         if (@components) {
240             my $c = uri_unescape($components[0]);
241             $input->param( 'keywords', $c );
242             $input->param( 'package', $c );
243         }
244     }
245
246     my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
247
248     my %params_def = ( keywords => { default => undef,
249                                      array => '\s+',
250                                      match => '^([-+\@\w\/.:]+)$',
251                                  },
252                        'package' => { default => undef,
253                                       match => '^([\w.+-]+)$',
254                                       var => \$pkg },
255                        suite => { default => 'default', match => '^([\w-]+)$',
256                                   array => ',', var => \@suites,
257                                   replace => { all => \@SUITES,
258                                                default => \@SUITES,
259                                                %SUITES_ALIAS } },
260                        archive => { default => ($what_to_do eq 'search') ?
261                                         'all' : 'default',
262                                         match => '^([\w-]+)$',
263                                         array => ',', var => \@archives,
264                                         replace => { all => \@ARCHIVES,
265                                                      default => \@ARCHIVES} },
266                        exact => { default => 0, match => '^(\w+)$',  },
267                        lang => { default => undef, match => '^([\w-]+)$',  },
268                        source => { default => 0, match => '^(\d+)$',  },
269                        debug => { default => 0, match => '^(\d+)$',  },
270                        searchon => { default => 'names', match => '^(\w+)$', },
271                        section => { default => 'all', match => '^([\w-]+)$',
272                                     alias => 'release', array => ',',
273                                     var => \@sections,
274                                     replace => { all => \@SECTIONS } },
275                        subsection => { default => 'default', match => '^([\w-]+)$',
276                                        array => ',', var => \@subsections,
277                                        replace => { default => [] } },
278                        priority => { default => 'default', match => '^([\w-]+)$',
279                                      array => ',',
280                                      replace => { default => [] } },
281                        arch => { default => 'any', match => '^([\w-]+)$',
282                                  array => ',', var => \@archs, replace =>
283                                  { any => \@ARCHITECTURES } },
284                        'format' => { default => 'html', match => '^([\w.]+)$',  },
285                        mode => { default => '', match => '^(\w+)$',  },
286                        sort_by => { default => 'file', match => '^(\w+)$', },
287                        );
288     my %opts;
289     my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
290     Packages::CGI::init_url( $input, \%params, \%opts );
291
292     if (defined($opts{lang})) {
293         $opts{po_lang} = $PO_LANGUAGES{$opts{lang}} ? $opts{lang} : 'en';
294         $opts{ddtp_lang} = $DDTP_LANGUAGES{$opts{lang}} ? $opts{lang} : 'en';
295     } else {
296         $opts{po_lang} = $po_lang;
297         $opts{ddtp_lang} = $ddtp_lang;
298     }
299     my $charset = "UTF-8";
300     my $cat = Packages::I18N::Locale->get_handle( $opts{po_lang}, 'en' )
301         or die "get_handle failed for $opts{po_lang}";
302     $opts{cat} = $cat;
303
304     $opts{h_suites} = { map { $_ => 1 } @suites };
305     $opts{h_sections} = { map { $_ => 1 } @sections };
306     $opts{h_archives} = { map { $_ => 1 } @archives };
307     $opts{h_archs} = { map { $_ => 1 } @archs };
308
309     if ((($opts{searchon} eq 'names') && $opts{source}) ||
310         ($opts{searchon} eq 'sourcenames')) {
311         $opts{source} = 1;
312         $opts{searchon} = 'names',
313         $opts{searchon_form} = 'sourcenames';
314     } else {
315         $opts{searchon_form} = $opts{searchon};
316     }
317     if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
318         $what_to_do = 'search_contents';
319     }
320
321     my $pet1 = new Benchmark;
322     my $petd = timediff($pet1, $pet0);
323     debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
324
325     my $template = new Packages::Template( $TEMPLATEDIR, $opts{format},
326                                            { po_lang => $opts{po_lang}, ddtp_lang => $opts{ddtp_lang},
327                                              charset => $charset, cat => $cat,
328                                              debug => ( DEBUG ? $opts{debug} : 0 ) },
329                                            ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
330
331     #FIXME: ugly hack
332     unless (($what_to_do eq 'allpackages' and $opts{format} =~ /^(html|txt\.gz)/)
333             || ($what_to_do eq 'index' and $opts{format} eq 'html')
334             || -e "$TEMPLATEDIR/$opts{format}/${what_to_do}.tmpl") {
335         fatal_error( $cat->g("requested format not available for this document"),
336                      "406 requested format not available");
337     }
338
339     my (%page_content);
340     unless (@Packages::CGI::fatal_errors) {
341         no strict 'refs';
342         &{"do_$what_to_do"}( \%params, \%opts, \%page_content );
343     }
344
345     $page_content{opts} = \%opts;
346     $page_content{params} = \%params;
347
348     unless (@Packages::CGI::fatal_errors) {
349         print $input->header(-charset => $charset,
350                              -type => get_mime($opts{format}),
351                              -vary => 'negotiate,accept-language',
352                              -last_modified => strftime("%a, %d %b %Y %T %z",
353                                                         localtime($last_modified)),
354                              -expires => strftime("%a, %d %b %Y %T %z",
355                                                   localtime($expires)),
356                              );
357         #use Data::Dumper;
358         #print '<pre>'.Dumper(\%ENV, \%page_content, get_all_messages()).'</pre>';
359         print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
360     } elsif ($Packages::CGI::http_code && $Packages::CGI::http_code !~ /^2\d\d/) {
361         print $input->header( -charset => $charset, -status => $Packages::CGI::http_code );
362     } else {
363         # We currently have only an error page in html
364         # so no format support here
365         print $input->header( -charset => $charset );
366         print $template->error_page( get_all_messages() );
367     }
368 }
369
370 1;
371 # vim: ts=8 sw=4