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';
22 use Packages::Config qw( $DBDIR $ROOT $SEARCH_CGI $SEARCH_PAGE
23 @SUITES @SECTIONS @ARCHIVES @ARCHITECTURES );
26 use Packages::Search qw( :all );
27 use Packages::HTML ();
28 use Packages::Sections;
30 use Packages::DoSearch;
31 use Packages::DoSearchContents;
33 use Packages::DoDownload;
34 use Packages::DoFilelist;
36 &Packages::CGI::reset;
38 $ENV{PATH} = "/bin:/usr/bin";
40 # Read in all the variables set by the form
42 if ($ARGV[0] && ($ARGV[0] eq 'php')) {
43 $input = new CGI(\*STDIN);
48 my $pet0 = new Benchmark;
49 my $tet0 = new Benchmark;
50 # use this to disable debugging in production mode completly
51 my $debug_allowed = 1;
52 my $debug = $debug_allowed && $input->param("debug");
53 $debug = 0 if !defined($debug) || $debug !~ /^\d+$/o;
54 $Packages::CGI::debug = $debug;
56 &Packages::Config::init( '../' );
57 &Packages::DB::init();
59 my $what_to_do = 'show';
61 if (my $path = $input->path_info() || $input->param('PATH_INFO')) {
62 my @components = grep { $_ } map { lc $_ } split /\/+/, $path;
64 debug( "components[0]=$components[0]", 2 );
65 if ($components[0] eq 'search') {
67 $what_to_do = 'search';
69 if ($components[0] eq 'source') {
71 $input->param( 'source', 1 );
73 if (@components == 0) {
74 # we just hope we get the information through our parameters...
75 } elsif (@components == 1) {
76 $what_to_do = 'search';
79 for ($components[-1]) {
80 /^(changelog|copyright|download|filelist)$/ && do {
87 my %SUITES = map { $_ => 1 } @SUITES;
88 my %SUITES_ALIAS = ( woody => 'oldstable',
92 my %SECTIONS = map { $_ => 1 } @SECTIONS;
93 my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
94 my %ARCHITECTURES = map { $_ => 1 } (@ARCHITECTURES, 'all');
97 my ($cgi, $params_set, $key, $val) = @_;
98 if ($params_set->{$key}++) {
99 fatal_error( "$key set more than once in path" );
101 $cgi->param( $key, $val );
106 foreach (@components) {
108 set_param_once( $input, \%params_set, 'suite', $_);
109 #possible conflicts with package names
110 # } elsif (my $s = $SUITES_ALIAS{$_}) {
111 # set_param_once( $input, \%params_set, 'suite', $s);
112 } elsif ($SECTIONS{$_}) {
113 set_param_once( $input, \%params_set, 'section', $_);
114 } elsif ($ARCHIVES{$_}) {
115 set_param_once( $input, \%params_set, 'archive', $_);
116 } elsif ($ARCHITECTURES{$_}) {
117 set_param_once( $input, \%params_set, 'arch', $_);
118 } elsif ($sections_descs{$_}) {
119 set_param_once( $input, \%params_set, 'subsection', $_);
120 } elsif ($_ eq 'source') {
121 set_param_once( $input, \%params_set, 'source', 1);
128 if (@components > 1) {
129 fatal_error( "two or more packages specified (@components)" );
131 } # else if (@components == 1)
134 $input->param( 'keywords', $components[0] );
135 $input->param( 'package', $components[0] );
139 my ( $pkg, @suites, @sections, @subsections, @archives, @archs );
141 my %params_def = ( keywords => { default => undef,
142 match => '^\s*([-+\@\s\w\/.:]+)\s*$',
144 package => { default => undef,
145 match => '^([\w.+-]+)$',
147 suite => { default => 'all', match => '^([\w-]+)$',
148 array => ',', var => \@suites,
149 replace => { all => \@SUITES } },
150 archive => { default => ($what_to_do eq 'search') ?
152 match => '^([\w-]+)$',
153 array => ',', var => \@archives,
154 replace => { all => \@ARCHIVES,
155 default => [qw(us security non-US)]} },
156 exact => { default => 0, match => '^(\w+)$', },
157 source => { default => 0, match => '^(\d+)$', },
158 searchon => { default => 'names', match => '^(\w+)$', },
159 section => { default => 'all', match => '^([\w-]+)$',
160 alias => 'release', array => ',',
162 replace => { all => \@SECTIONS } },
163 subsection => { default => 'default', match => '^([\w-]+)$',
164 array => ',', var => \@subsections,
165 replace => { default => [] } },
166 arch => { default => 'any', match => '^([\w-]+)$',
167 array => ',', var => \@archs, replace =>
168 { any => \@ARCHITECTURES } },
171 my %params = Packages::Search::parse_params( $input, \%params_def, \%opts );
173 $opts{h_suites} = { map { $_ => 1 } @suites };
174 $opts{h_sections} = { map { $_ => 1 } @sections };
175 $opts{h_archives} = { map { $_ => 1 } @archives };
176 $opts{h_archs} = { map { $_ => 1 } @archs };
178 if ((($opts{searchon} eq 'names') && $opts{source}) ||
179 ($opts{searchon} eq 'sourcenames')) {
181 $opts{searchon} = 'names',
182 $opts{searchon_form} = 'sourcenames';
184 $opts{searchon_form} = $opts{searchon};
186 if ($opts{searchon} eq 'contents' or $opts{searchon} eq 'filenames') {
187 $what_to_do = 'search_contents';
190 my $pet1 = new Benchmark;
191 my $petd = timediff($pet1, $pet0);
192 debug( "Parameter evaluation took ".timestr($petd) );
194 print $input->header( -charset => 'utf-8' );
196 my (%html_header, $menu, $page_content);
197 unless (@Packages::CGI::fatal_errors) {
199 &{"do_$what_to_do"}( \%params, \%opts, \%html_header,
200 \$menu, \$page_content );
202 %html_header = ( title => 'Error',
205 print_search_field => 'packages',
206 search_field_values => {
207 keywords => 'search for a package',
208 searchon => 'default',
218 print Packages::HTML::header( %html_header );
227 unless (@Packages::CGI::fatal_errors) {
231 my $tet1 = new Benchmark;
232 my $tetd = timediff($tet1, $tet0);
233 print "Total page evaluation took ".timestr($tetd)."<br>"
236 my $trailer = Packages::HTML::trailer( $ROOT );
237 $trailer =~ s/LAST_MODIFIED_DATE/gmtime()/e; #FIXME