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
21 use Benchmark ':hireswallclock';
22 use I18N::AcceptLanguage;
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 );
31 use Packages::Search qw( :all );
32 use Packages::Template ();
33 use Packages::Sections;
34 use Packages::I18N::Locale;
36 use Packages::DoSearch;
37 use Packages::DoSearchContents;
39 use Packages::DoIndex;
40 use Packages::DoNewPkg;
41 use Packages::DoDownload;
42 use Packages::DoFilelist;
44 &Packages::CGI::reset;
45 $Packages::Search::too_many_hits = 0;
48 $ENV{PATH} = "/bin:/usr/bin";
49 delete $ENV{'LANGUAGE'};
51 delete $ENV{'LC_ALL'};
52 delete $ENV{'LC_MESSAGES'};
54 # Read in all the variables set by the form
56 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
57 $input = new CGI(\*STDIN);
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;
68 my $homedir = dirname($ENV{SCRIPT_FILENAME}).'/../';
69 &Packages::Config::init( $homedir );
70 &Packages::DB::init();
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 );
81 my $what_to_do = 'show';
83 if (my $path = $input->path_info() || $input->param('PATH_INFO')) {
84 my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
86 push @components, 'index' if @components && $path =~ m,/$,;
88 my %LANGUAGES = map { $_ => 1 } @LANGUAGES;
89 if (@components > 0 and $LANGUAGES{$components[0]}) {
90 $input->param( 'lang', shift(@components) );
92 if (@components > 0 and $components[0] eq 'source') {
94 $input->param( 'source', 1 );
96 if (@components > 0 and $components[0] eq 'search') {
98 $what_to_do = 'search';
100 fatal_error( _g( "search doesn't take any more path elements" ) )
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';
108 for ($components[-1]) {
109 /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
116 my %SUITES = map { $_ => 1 } @SUITES;
117 my %SUITES_ALIAS = ( woody => 'oldstable',
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;
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 ) );
132 $cgi->param( $key, $val );
136 my (@pkg, $need_pkg);
137 foreach (reverse @components) {
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', $_);
165 if (@components > 1) {
166 fatal_error( sprintf( _g( "two or more packages specified (%s)" ), "@components" ) );
168 } # else if (@components == 1)
171 $input->param( 'keywords', $components[0] );
172 $input->param( 'package', $components[0] );
176 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
178 my %params_def = ( keywords => { default => undef,
180 match => '^([-+\@\w\/.:]+)$',
182 package => { default => undef,
183 match => '^([\w.+-]+)$',
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') ?
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 => ',',
203 replace => { all => \@SECTIONS } },
204 subsection => { default => 'default', match => '^([\w-]+)$',
205 array => ',', var => \@subsections,
206 replace => { default => [] } },
207 priority => { default => 'default', match => '^([\w-]+)$',
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+)$', },
218 my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
219 Packages::CGI::init_url( $input, \%params, \%opts );
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() )
227 debug( "couldn't set default locale either" ) if DEBUG;
228 setlocale( LC_ALL, "C" );
231 debug( "locale=$locale charset=$charset", 2 ) if DEBUG;
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 };
238 if ((($opts{searchon} eq 'names') && $opts{source}) ||
239 ($opts{searchon} eq 'sourcenames')) {
241 $opts{searchon} = 'names',
242 $opts{searchon_form} = 'sourcenames';
244 $opts{searchon_form} = $opts{searchon};
246 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
247 $what_to_do = 'search_contents';
250 my $pet1 = new Benchmark;
251 my $petd = timediff($pet1, $pet0);
252 debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
254 my $template = new Packages::Template( $TEMPLATEDIR, $opts{format}, { lang => $opts{lang}, charset => $charset, debug => ( DEBUG ? $opts{debug} : 0 ) }, ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
256 my (%html_header, %page_content);
257 unless (@Packages::CGI::fatal_errors) {
259 &{"do_$what_to_do"}( \%params, \%opts, \%html_header,
263 $page_content{opts} = \%opts;
264 $page_content{params} = \%params;
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(@_) };
274 print $input->header( -charset => $charset );
277 #print '<pre>'.Dumper(\%ENV, \%html_header, \%page_content, get_all_messages()).'</pre>';
279 unless (@Packages::CGI::fatal_errors) {
280 print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
282 print $template->error_page( get_all_messages() );
285 my $tet1 = new Benchmark;
286 my $tetd = timediff($tet1, $tet0);
288 my $trailer = $template->trailer( undef, undef, undef, $tetd );