2 # $Id: search_packages.pl 91 2006-02-10 22:18:31Z jeroen $
3 # dispatcher.pl -- CGI interface for packages.debian.org
5 # Copyright (C) 2004-2006 Frank Lichtenheld
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
19 use Benchmark ':hireswallclock';
20 use I18N::AcceptLanguage;
24 use Packages::Config qw( $DBDIR $ROOT @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES @LANGUAGES $LOCALES );
27 use Packages::Search qw( :all );
28 use Packages::HTML ();
29 use Packages::Sections;
30 use Packages::I18N::Locale;
32 use Packages::DoSearch;
33 use Packages::DoSearchContents;
35 use Packages::DoIndex;
36 use Packages::DoNewPkg;
37 use Packages::DoDownload;
38 use Packages::DoFilelist;
40 &Packages::CGI::reset;
43 $ENV{PATH} = "/bin:/usr/bin";
44 delete $ENV{'LANGUAGE'};
46 delete $ENV{'LC_ALL'};
47 delete $ENV{'LC_MESSAGES'};
49 # Read in all the variables set by the form
51 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
52 $input = new CGI(\*STDIN);
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;
63 &Packages::Config::init( '../' );
64 &Packages::DB::init();
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 );
75 my $what_to_do = 'show';
77 if (my $path = $input->path_info() || $input->param('PATH_INFO')) {
78 my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
80 push @components, 'index' if $path =~ m,/$,;
82 my %LANGUAGES = map { $_ => 1 } @LANGUAGES;
83 if (@components > 0 and $LANGUAGES{$components[0]}) {
84 $input->param( 'lang', shift(@components) );
86 if (@components > 0 and $components[0] eq 'source') {
88 $input->param( 'source', 1 );
90 if (@components > 0 and $components[0] eq 'search') {
92 $what_to_do = 'search';
94 fatal_error( _g( "search doesn't take any more path elements" ) )
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';
102 for ($components[-1]) {
103 /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
110 my %SUITES = map { $_ => 1 } @SUITES;
111 my %SUITES_ALIAS = ( woody => 'oldstable',
114 sid => 'unstable', );
115 my %SECTIONS = map { $_ => 1 } @SECTIONS;
116 my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
117 my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all');
120 my ($cgi, $params_set, $key, $val) = @_;
121 if ($params_set->{$key}++) {
122 fatal_error( sprintf( _g( "%s set more than once in path" ), $key ) );
124 $cgi->param( $key, $val );
128 my (@pkg, $need_pkg);
129 foreach (reverse @components) {
131 && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
132 if (!$need_pkg && $SUITES{$_}) {
133 set_param_once( $input, \%params_set, 'suite', $_);
134 } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
135 set_param_once( $input, \%params_set, 'suite', $s);
136 } elsif (!$need_pkg && $SECTIONS{$_}) {
137 set_param_once( $input, \%params_set, 'section', $_);
138 } elsif (!$need_pkg && $ARCHIVES{$_}) {
139 set_param_once( $input, \%params_set, 'archive', $_);
140 } elsif (!$need_pkg && $sections_descs{$_}) {
141 set_param_once( $input, \%params_set, 'subsection', $_);
142 } elsif (!$need_pkg && ($_ eq 'source')) {
143 set_param_once( $input, \%params_set, 'source', 1);
144 } elsif ($ARCHITECTURES{$_}) {
145 set_param_once( $input, \%params_set, 'arch', $_);
152 if (@components > 1) {
153 fatal_error( sprintf( _g( "two or more packages specified (%s)" ), "@components" ) );
155 } # else if (@components == 1)
158 $input->param( 'keywords', $components[0] );
159 $input->param( 'package', $components[0] );
163 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
165 my %params_def = ( keywords => { default => undef,
166 match => '^\s*([-+\@\s\w\/.:]+)\s*$',
168 package => { default => undef,
169 match => '^([\w.+-]+)$',
171 suite => { default => 'all', match => '^([\w-]+)$',
172 array => ',', var => \@suites,
173 replace => { all => \@SUITES } },
174 archive => { default => ($what_to_do eq 'search') ?
176 match => '^([\w-]+)$',
177 array => ',', var => \@archives,
178 replace => { all => \@ARCHIVES,
179 default => \@ARCHIVES} },
180 exact => { default => 0, match => '^(\w+)$', },
181 lang => { default => $http_lang, match => '^(\w+)$', },
182 source => { default => 0, match => '^(\d+)$', },
183 debug => { default => 0, match => '^(\d+)$', },
184 searchon => { default => 'names', match => '^(\w+)$', },
185 section => { default => 'all', match => '^([\w-]+)$',
186 alias => 'release', array => ',',
188 replace => { all => \@SECTIONS } },
189 subsection => { default => 'default', match => '^([\w-]+)$',
190 array => ',', var => \@subsections,
191 replace => { default => [] } },
192 arch => { default => 'any', match => '^([\w-]+)$',
193 array => ',', var => \@archs, replace =>
194 { any => \@ARCHITECTURES } },
195 format => { default => 'html', match => '^(\w+)$', },
196 mode => { default => undef, match => '^(\w+)$', },
199 my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
200 Packages::CGI::init_url( $input, \%params, \%opts );
202 my $locale = get_locale($opts{lang});
203 my $charset = get_charset($opts{lang});
204 setlocale ( LC_ALL, $locale )
205 or do { debug( "couldn't set locale $locale, using default" ) if DEBUG;
206 setlocale( LC_ALL, get_locale() )
208 debug( "couldn't set default locale either" ) if DEBUG;
209 setlocale( LC_ALL, "C" );
212 debug( "locale=$locale charset=$charset", 2 ) if DEBUG;
214 $opts{h_suites} = { map { $_ => 1 } @suites };
215 $opts{h_sections} = { map { $_ => 1 } @sections };
216 $opts{h_archives} = { map { $_ => 1 } @archives };
217 $opts{h_archs} = { map { $_ => 1 } @archs };
219 if ((($opts{searchon} eq 'names') && $opts{source}) ||
220 ($opts{searchon} eq 'sourcenames')) {
222 $opts{searchon} = 'names',
223 $opts{searchon_form} = 'sourcenames';
225 $opts{searchon_form} = $opts{searchon};
227 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
228 $what_to_do = 'search_contents';
231 my $pet1 = new Benchmark;
232 my $petd = timediff($pet1, $pet0);
233 debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
235 my (%html_header, $menu, $page_content);
236 unless (@Packages::CGI::fatal_errors) {
238 &{"do_$what_to_do"}( \%params, \%opts, \%html_header,
239 \$menu, \$page_content );
241 %html_header = ( title => _g('Error'),
244 print_search_field => 'packages',
245 search_field_values => {
246 keywords => _g('search for a package'),
247 searchon => 'default',
257 print $input->header( -charset => $charset );
259 print Packages::HTML::header( %html_header );
265 print_debug() if DEBUG;
268 unless (@Packages::CGI::fatal_errors) {
272 my $tet1 = new Benchmark;
273 my $tetd = timediff($tet1, $tet0);
274 print "Total page evaluation took ".timestr($tetd)."<br>"
277 my $trailer = Packages::HTML::trailer( $ROOT );
278 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME