]> git.deb.at Git - deb/packages.git/blob - cgi-bin/dispatcher.pl
Finally fix the new packages rss feed
[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 File::Basename;
17 use URI::Escape;
18 use HTML::Entities;
19 use Template;
20 use DB_File;
21 use Benchmark ':hireswallclock';
22 use I18N::AcceptLanguage;
23 use Locale::gettext;
24
25 use Deb::Versions;
26 use Packages::Config qw( $DBDIR $ROOT $TEMPLATEDIR $CACHEDIR
27                          @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES @PRIORITIES
28                          @LANGUAGES $LOCALES );
29 use Packages::CGI qw( :DEFAULT get_all_messages );
30 use Packages::DB;
31 use Packages::Search qw( :all );
32 use Packages::Template ();
33 use Packages::Sections;
34 use Packages::I18N::Locale;
35
36 use Packages::DoSearch;
37 use Packages::DoSearchContents;
38 use Packages::DoShow;
39 use Packages::DoIndex;
40 use Packages::DoNewPkg;
41 use Packages::DoDownload;
42 use Packages::DoFilelist;
43
44 &Packages::CGI::reset;
45 $Packages::Search::too_many_hits = 0;
46
47 # clean up env
48 $ENV{PATH} = "/bin:/usr/bin";
49 delete $ENV{'LANGUAGE'};
50 delete $ENV{'LANG'};
51 delete $ENV{'LC_ALL'};
52 delete $ENV{'LC_MESSAGES'};
53
54 # Read in all the variables set by the form
55 my $input;
56 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
57         $input = new CGI(\*STDIN);
58 } else {
59         $input = new CGI;
60 }
61
62 my $pet0 = new Benchmark;
63 my $tet0 = new Benchmark;
64 my $debug = DEBUG && $input->param("debug");
65 $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
66 $Packages::CGI::debug = $debug;
67
68 my $homedir = dirname($ENV{SCRIPT_FILENAME}).'/../';
69 &Packages::Config::init( $homedir );
70 &Packages::DB::init();
71
72 my $acc = I18N::AcceptLanguage->new();
73 my $http_lang = $acc->accepts( $input->http("Accept-Language"),
74                                \@LANGUAGES ) || 'en';
75 debug( "LANGUAGES=@LANGUAGES header=".
76        ($input->http("Accept-Language")||'').
77        " http_lang=$http_lang", 2 ) if DEBUG;
78 bindtextdomain ( 'pdo', $LOCALES );
79 textdomain( 'pdo' );
80
81 my $what_to_do = 'show';
82 my $source = 0;
83 if (my $path = $input->path_info() || $input->param('PATH_INFO')) {
84     my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
85
86     push @components, 'index' if @components && $path =~ m,/$,;
87
88     my %LANGUAGES = map { $_ => 1 } @LANGUAGES;
89     if (@components > 0 and $LANGUAGES{$components[0]}) {
90         $input->param( 'lang', shift(@components) );
91     }
92     if (@components > 0 and $components[0] eq 'source') {
93         shift @components;
94         $input->param( 'source', 1 );
95     }
96     if (@components > 0 and $components[0] eq 'search') {
97         shift @components;
98         $what_to_do = 'search';
99         # Done
100         fatal_error( _g( "search doesn't take any more path elements" ) )
101             if @components;
102     } elsif (@components == 0) {
103         fatal_error( _g( "We're supposed to display the homepage here, instead of getting dispatch.pl" ) );
104     } elsif (@components == 1) {
105         $what_to_do = 'search';
106     } else {
107
108         for ($components[-1]) {
109             /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
110                 pop @components;
111                 $what_to_do = $1;
112                 last;
113             };
114         }
115
116         my %SUITES = map { $_ => 1 } @SUITES;
117         my %SUITES_ALIAS = ( woody => 'oldstable',
118                              sarge => 'stable',
119                              etch => 'testing',
120                              sid => 'unstable', );
121         my %SECTIONS = map { $_ => 1 } @SECTIONS;
122         my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
123         my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all');
124         my %PRIORITIES = map { $_ => 1 } @PRIORITIES;
125         my %params_set;
126         sub set_param_once {
127             my ($cgi, $params_set, $key, $val) = @_;
128             debug("set_param_once key=$key val=$val",4) if DEBUG;
129             if ($params_set->{$key}++) {
130                 fatal_error( sprintf( _g( "%s set more than once in path" ), $key ) );
131             } else {
132                 $cgi->param( $key, $val );
133             }
134         }
135
136         my (@pkg, $need_pkg);
137         foreach (reverse @components) {
138             $need_pkg = !@pkg
139                 && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
140             debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
141             if (!$need_pkg && $SUITES{$_}) {
142                 set_param_once( $input, \%params_set, 'suite', $_);
143             } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
144                 set_param_once( $input, \%params_set, 'suite', $s);
145             } elsif (!$need_pkg && $SECTIONS{$_}) {
146                 set_param_once( $input, \%params_set, 'section', $_);
147             } elsif (!$need_pkg && $ARCHIVES{$_}) {
148                 set_param_once( $input, \%params_set, 'archive', $_);
149             } elsif (!$need_pkg && $sections_descs{$_}) {
150                 set_param_once( $input, \%params_set, 'subsection', $_);
151             } elsif (!$need_pkg && ($_ eq 'non-us')) { # non-US hack
152                 set_param_once( $input, \%params_set, 'subsection', 'non-US');
153             } elsif (!$need_pkg && ($_ eq 'source')) {
154                 set_param_once( $input, \%params_set, 'source', 1);
155             } elsif ($ARCHITECTURES{$_}) {
156                 set_param_once( $input, \%params_set, 'arch', $_);
157             } elsif ($PRIORITIES{$_}) {
158                 set_param_once( $input, \%params_set, 'priority', $_);
159             } else {
160                 push @pkg, $_;
161             }
162         }
163         @components = @pkg;
164
165         if (@components > 1) {
166             fatal_error( sprintf( _g( "two or more packages specified (%s)" ), "@components" ) );
167         }
168     } # else if (@components == 1)
169     
170     if (@components) {
171         $input->param( 'keywords', $components[0] );
172         $input->param( 'package', $components[0] );
173     }
174 }
175
176 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
177
178 my %params_def = ( keywords => { default => undef,
179                                  array => '\s+',
180                                  match => '^([-+\@\w\/.:]+)$',
181                              },
182                    package => { default => undef,
183                                 match => '^([\w.+-]+)$',
184                                 var => \$pkg },
185                    suite => { default => 'default', match => '^([\w-]+)$',
186                               array => ',', var => \@suites,
187                               replace => { all => \@SUITES,
188                                            default => \@SUITES } },
189                    archive => { default => ($what_to_do eq 'search') ?
190                                     'all' : 'default',
191                                     match => '^([\w-]+)$',
192                                     array => ',', var => \@archives,
193                                     replace => { all => \@ARCHIVES,
194                                                  default => \@ARCHIVES} },
195                    exact => { default => 0, match => '^(\w+)$',  },
196                    lang => { default => $http_lang, match => '^(\w+)$',  },
197                    source => { default => 0, match => '^(\d+)$',  },
198                    debug => { default => 0, match => '^(\d+)$',  },
199                    searchon => { default => 'names', match => '^(\w+)$', },
200                    section => { default => 'all', match => '^([\w-]+)$',
201                                 alias => 'release', array => ',',
202                                 var => \@sections,
203                                 replace => { all => \@SECTIONS } },
204                    subsection => { default => 'default', match => '^([\w-]+)$',
205                                    array => ',', var => \@subsections,
206                                    replace => { default => [] } },
207                    priority => { default => 'default', match => '^([\w-]+)$',
208                                  array => ',',
209                                  replace => { default => [] } },
210                    arch => { default => 'any', match => '^([\w-]+)$',
211                              array => ',', var => \@archs, replace =>
212                              { any => \@ARCHITECTURES } },
213                    format => { default => 'html', match => '^([\w.]+)$',  },
214                    mode => { default => undef, match => '^(\w+)$',  },
215                    sort_by => { default => 'file', match => '^(\w+)$', },
216                    );
217 my %opts;
218 my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
219 Packages::CGI::init_url( $input, \%params, \%opts );
220
221 my $locale = get_locale($opts{lang});
222 my $charset = get_charset($opts{lang});
223 setlocale ( LC_ALL, $locale )
224     or do { debug( "couldn't set locale $locale, using default" ) if DEBUG;
225             setlocale( LC_ALL, get_locale() )
226                 or do {
227                     debug( "couldn't set default locale either" ) if DEBUG;
228                     setlocale( LC_ALL, "C" );
229                 };
230         };
231 debug( "locale=$locale charset=$charset", 2 ) if DEBUG;
232
233 $opts{h_suites} = { map { $_ => 1 } @suites };
234 $opts{h_sections} = { map { $_ => 1 } @sections };
235 $opts{h_archives} = { map { $_ => 1 } @archives };
236 $opts{h_archs} = { map { $_ => 1 } @archs };
237
238 if ((($opts{searchon} eq 'names') && $opts{source}) ||
239     ($opts{searchon} eq 'sourcenames')) {
240     $opts{source} = 1;
241     $opts{searchon} = 'names',
242     $opts{searchon_form} = 'sourcenames';
243 } else {
244     $opts{searchon_form} = $opts{searchon};
245 }
246 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
247     $what_to_do = 'search_contents';
248 }
249
250 my $pet1 = new Benchmark;
251 my $petd = timediff($pet1, $pet0);
252 debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
253
254 my $template = new Packages::Template( $TEMPLATEDIR, $opts{format}, { lang => $opts{lang}, charset => $charset, debug => ( DEBUG ? $opts{debug} : 0 ) }, ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
255
256 my (%html_header, %page_content);
257 unless (@Packages::CGI::fatal_errors) {
258     no strict 'refs';
259     &{"do_$what_to_do"}( \%params, \%opts, \%html_header,
260                          \%page_content );
261 }
262
263 $page_content{opts} = \%opts;
264 $page_content{params} = \%params;
265
266 $page_content{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
267 $page_content{make_url} = sub { return &Packages::CGI::make_url(@_) };
268 # needed to work around the limitations of the the FILTER syntax
269 $page_content{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
270 $page_content{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
271 $page_content{quotemeta} = sub { return quotemeta($_[0]) };
272 $page_content{string2id} = sub { return &Packages::CGI::string2id(@_) };
273
274 print $input->header(-charset => $charset, -type => get_mime($opts{format}) );
275
276 #use Data::Dumper;
277 #print '<pre>'.Dumper(\%ENV, \%html_header, \%page_content, get_all_messages()).'</pre>';
278
279 unless (@Packages::CGI::fatal_errors) {
280 print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
281 } else {
282 print $template->error_page( get_all_messages() );
283 }
284
285 my $tet1 = new Benchmark;
286 my $tetd = timediff($tet1, $tet0);
287
288 my $trailer = $template->trailer( undef, undef, undef, $tetd );
289 print $trailer;
290
291 # vim: ts=8 sw=4