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 @DDTP_LANGUAGES $LOCALES );
29 use Packages::CGI qw( :DEFAULT error 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);
61 my $cgi_error = $input->cgi_error;
63 fatal_error( "Error parsing the request", $cgi_error );
67 my $pet0 = new Benchmark;
68 my $tet0 = new Benchmark;
69 my $debug = DEBUG && $input->param("debug");
70 $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
71 $Packages::CGI::debug = $debug;
73 my $homedir = dirname($ENV{SCRIPT_FILENAME}).'/../';
74 &Packages::Config::init( $homedir );
75 &Packages::DB::init();
77 my $acc = I18N::AcceptLanguage->new();
78 my %all_langs = map { $_ => 1 } (@LANGUAGES, @DDTP_LANGUAGES);
79 my @all_langs = sort keys %all_langs;
80 my $http_lang = $acc->accepts( $input->http("Accept-Language"),
81 \@all_langs ) || 'en';
82 debug( "LANGUAGES=@all_langs header=".
83 ($input->http("Accept-Language")||'').
84 " http_lang=$http_lang", 2 ) if DEBUG;
85 bindtextdomain ( 'pdo', $LOCALES );
88 # backwards compatibility stuff
89 debug( "SCRIPT_URL=$ENV{SCRIPT_URL} SCRIPT_URI=$ENV{SCRIPT_URI}" ) if DEBUG;
91 if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_|) {
92 error( "You reached this site over an old URL. ".
93 "Depending on the exact parameters your search might work or not." );
94 # contents search changed a lot
95 if ($ENV{SCRIPT_URL} =~ m|^/cgi-bin/search_contents|) {
96 $input->param('keywords',$input->param('word')) if $input->param('word');
97 $input->param('searchon','contents');
98 for ($input->param('searchmode')) {
99 /^searchfiles/ && do {
100 $input->param('mode','filename');
104 $ENV{PATH_INFO} = '/'.join('/',($input->param('version')||'stable',
105 $input->param('keywords'),
106 $input->param('arch')||'i386',
108 $input->delete('searchon','version','keywords','arch');
114 if ($ENV{is_reportbug}) {
115 $input->param('exact', 1);
116 debug( "reportbug detected, set paramater exact to '1'" ) if DEBUG;
119 my $what_to_do = 'show';
121 if (my $path = $input->path_info() || $input->param('PATH_INFO')) {
122 my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
124 debug( "PATH_INFO=$path components=@components", 3) if DEBUG;
126 push @components, 'index' if @components && $path =~ m,/$,;
128 my %LANGUAGES = map { $_ => 1 } @LANGUAGES;
129 if (@components > 0 and $LANGUAGES{$components[0]}) {
130 $input->param( 'lang', shift(@components) );
132 if (@components > 0 and $components[0] eq 'source') {
134 $input->param( 'source', 1 );
136 if (@components > 0 and $components[0] eq 'search') {
138 $what_to_do = 'search';
140 fatal_error( _g( "search doesn't take any more path elements" ) )
142 } elsif (@components == 0) {
143 fatal_error( _g( "We're supposed to display the homepage here, instead of getting dispatch.pl" ) );
144 } elsif (@components == 1) {
145 $what_to_do = 'search';
148 for ($components[-1]) {
149 /^(index|allpackages|newpkg|changelog|copyright|download|filelist)$/ && do {
156 my %SUITES = map { $_ => 1 } @SUITES;
157 my %SUITES_ALIAS = ( sarge => 'oldstable',
160 sid => 'unstable', );
161 my %SECTIONS = map { $_ => 1 } @SECTIONS;
162 my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
163 my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all', 'any');
164 my %PRIORITIES = map { $_ => 1 } @PRIORITIES;
167 my ($cgi, $params_set, $key, $val) = @_;
168 debug("set_param_once key=$key val=$val",4) if DEBUG;
169 if ($params_set->{$key}++) {
170 fatal_error( sprintf( _g( "%s set more than once in path" ), $key ) );
172 $cgi->param( $key, $val );
176 my (@pkg, $need_pkg);
177 foreach (reverse @components) {
179 && ($what_to_do !~ /^(index|allpackages|newpkg)$/);
180 debug("need_pkg=$need_pkg component=$_",4) if DEBUG;
181 if (!$need_pkg && $SUITES{$_}) {
182 set_param_once( $input, \%params_set, 'suite', $_);
183 } elsif (!$need_pkg && (my $s = $SUITES_ALIAS{$_})) {
184 set_param_once( $input, \%params_set, 'suite', $s);
185 } elsif (!$need_pkg && $SECTIONS{$_}) {
186 set_param_once( $input, \%params_set, 'section', $_);
187 } elsif (!$need_pkg && $ARCHIVES{$_}) {
188 set_param_once( $input, \%params_set, 'archive', $_);
189 } elsif (!$need_pkg && $sections_descs{$_}) {
190 set_param_once( $input, \%params_set, 'subsection', $_);
191 } elsif (!$need_pkg && ($_ eq 'non-us')) { # non-US hack
192 set_param_once( $input, \%params_set, 'subsection', 'non-US');
193 } elsif (!$need_pkg && ($_ eq 'source')) {
194 set_param_once( $input, \%params_set, 'source', 1);
195 } elsif ($ARCHITECTURES{$_}) {
196 set_param_once( $input, \%params_set, 'arch', $_)
198 } elsif ($PRIORITIES{$_}) {
199 set_param_once( $input, \%params_set, 'priority', $_);
206 if (@components > 1) {
207 fatal_error( sprintf( _g( "two or more packages specified (%s)" ), "@components" ) );
209 } # else if (@components == 1)
212 $input->param( 'keywords', $components[0] );
213 $input->param( 'package', $components[0] );
217 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
219 my %params_def = ( keywords => { default => undef,
221 match => '^([-+\@\w\/.:]+)$',
223 package => { default => undef,
224 match => '^([\w.+-]+)$',
226 suite => { default => 'default', match => '^([\w-]+)$',
227 array => ',', var => \@suites,
228 replace => { all => \@SUITES,
229 default => \@SUITES } },
230 archive => { default => ($what_to_do eq 'search') ?
232 match => '^([\w-]+)$',
233 array => ',', var => \@archives,
234 replace => { all => \@ARCHIVES,
235 default => \@ARCHIVES} },
236 exact => { default => 0, match => '^(\w+)$', },
237 lang => { default => $http_lang, match => '^(\w+)$', },
238 source => { default => 0, match => '^(\d+)$', },
239 debug => { default => 0, match => '^(\d+)$', },
240 searchon => { default => 'names', match => '^(\w+)$', },
241 section => { default => 'all', match => '^([\w-]+)$',
242 alias => 'release', array => ',',
244 replace => { all => \@SECTIONS } },
245 subsection => { default => 'default', match => '^([\w-]+)$',
246 array => ',', var => \@subsections,
247 replace => { default => [] } },
248 priority => { default => 'default', match => '^([\w-]+)$',
250 replace => { default => [] } },
251 arch => { default => 'any', match => '^([\w-]+)$',
252 array => ',', var => \@archs, replace =>
253 { any => \@ARCHITECTURES } },
254 format => { default => 'html', match => '^([\w.]+)$', },
255 mode => { default => undef, match => '^(\w+)$', },
256 sort_by => { default => 'file', match => '^(\w+)$', },
259 my %params = Packages::CGI::parse_params( $input, \%params_def, \%opts );
260 Packages::CGI::init_url( $input, \%params, \%opts );
262 my $locale = get_locale($opts{lang});
263 my $charset = get_charset($opts{lang});
264 setlocale ( LC_ALL, $locale )
265 or do { debug( "couldn't set locale $locale, using default" ) if DEBUG;
266 setlocale( LC_ALL, get_locale() )
268 debug( "couldn't set default locale either" ) if DEBUG;
269 setlocale( LC_ALL, "C" );
272 debug( "locale=$locale charset=$charset", 2 ) if DEBUG;
274 $opts{h_suites} = { map { $_ => 1 } @suites };
275 $opts{h_sections} = { map { $_ => 1 } @sections };
276 $opts{h_archives} = { map { $_ => 1 } @archives };
277 $opts{h_archs} = { map { $_ => 1 } @archs };
279 if ((($opts{searchon} eq 'names') && $opts{source}) ||
280 ($opts{searchon} eq 'sourcenames')) {
282 $opts{searchon} = 'names',
283 $opts{searchon_form} = 'sourcenames';
285 $opts{searchon_form} = $opts{searchon};
287 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
288 $what_to_do = 'search_contents';
291 my $pet1 = new Benchmark;
292 my $petd = timediff($pet1, $pet0);
293 debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
295 my $template = new Packages::Template( $TEMPLATEDIR, $opts{format}, { lang => $opts{lang}, charset => $charset, debug => ( DEBUG ? $opts{debug} : 0 ) }, ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
297 unless (-e "$TEMPLATEDIR/$opts{format}/${what_to_do}.tmpl") {
298 fatal_error( "requested format not available for this document",
299 "406 requested format not available");
303 unless (@Packages::CGI::fatal_errors) {
305 &{"do_$what_to_do"}( \%params, \%opts, \%page_content );
308 $page_content{opts} = \%opts;
309 $page_content{params} = \%params;
311 $page_content{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
312 $page_content{make_url} = sub { return &Packages::CGI::make_url(@_) };
313 # needed to work around the limitations of the the FILTER syntax
314 $page_content{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
315 $page_content{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
316 $page_content{quotemeta} = sub { return quotemeta($_[0]) };
317 $page_content{string2id} = sub { return &Packages::CGI::string2id(@_) };
319 unless (@Packages::CGI::fatal_errors) {
320 print $input->header(-charset => $charset, -type => get_mime($opts{format}) );
322 #print '<pre>'.Dumper(\%ENV, \%page_content, get_all_messages()).'</pre>';
323 print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
324 my $tet1 = new Benchmark;
325 my $tetd = timediff($tet1, $tet0);
326 print $template->trailer( undef, undef, undef, $tetd );
327 } elsif ($Packages::CGI::http_code && $Packages::CGI::http_code !~ /^2\d\d/) {
328 print $input->header( -charset => $charset, -status => $Packages::CGI::http_code );
330 # We currently have only an error page in html
331 # so no format support here
332 print $input->header( -charset => $charset );
333 print $template->error_page( get_all_messages() );
334 print $template->trailer();;