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