]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoIndex.pm
Add text version of allpackages
[deb/packages.git] / lib / Packages / DoIndex.pm
1 package Packages::DoIndex;
2
3 use strict;
4 use warnings;
5
6 use CGI qw( :cgi );
7 use Exporter;
8
9 use Deb::Versions;
10 use Packages::Config qw( $TOPDIR );
11 use Packages::I18N::Locale;
12 use Packages::CGI;
13
14 our @ISA = qw( Exporter );
15 our @EXPORT = qw( do_index do_allpackages );
16
17 sub do_index {
18     return send_file( 'index', @_ );
19 }
20 sub do_allpackages {
21     return send_file( 'allpackages', @_ );
22 }
23
24 # no real need for more flexibility here, I think...
25 my %mime_types = (
26                   txt => 'text/plain',
27                   'txt.gz' => 'text/plain',
28                   html => 'text/html',
29                   );
30 my %encoding = (
31                 'txt.gz' => 'x-gzip',
32                 );
33 sub send_file {
34     my ($file, $params, $opts, $html_header) = @_;
35
36     if ($params->{errors}{suite}) {
37         fatal_error( _g( "suite not valid or not specified" ) );
38     }
39     if (@{$opts->{suite}} > 1) {
40         fatal_error( sprintf( _g( "more than one suite specified for show_static (%s)" ), "@{$opts->{suite}}" ) );
41     }
42     if (@{$opts->{subsection}} > 1) {
43         fatal_error( sprintf( _g( "more than one suite specified for show_static (%s)" ), "@{$opts->{suite}}" ) );
44     }
45
46     my $wwwdir = "$TOPDIR/www";
47     my $path = "$opts->{suite}[0]/";
48     $path .= "$opts->{archive}[0]/" if @{$opts->{archive}} == 1;
49     $path .= "$opts->{subsection}[0]/" if @{$opts->{subsection}};
50     # we don't have translated index pages for subsections yet
51     $opts->{lang} = 'en' if @{$opts->{subsection}} or $file eq 'allpackages';
52     $path .= "$file.$opts->{lang}.$opts->{format}";
53
54     unless (@Packages::CGI::fatal_errors) {
55         my $buffer;
56         if (open( INDEX, '<', "$wwwdir/$path" )) {
57             my %headers;
58             $headers{'-charset'} = get_charset( $opts->{lang} );
59             $headers{'-type'} = $mime_types{$opts->{format}} || 'text/plain';
60             $headers{'-content-encoding'} = $encoding{$opts->{format}} if exists $encoding{$opts->{format}};
61             print header( %headers );
62
63             binmode INDEX;
64             while (read INDEX, $buffer, 4096) {
65                 print $buffer;
66             }
67             close INDEX;
68             exit;
69         } else {
70             fatal_error( sprintf( _g( "couldn't read index file %s: %s" ),
71                                   $path, $! ) );
72         }
73     }
74
75     %$html_header = ( title => _g('Error'),
76                       lang => $opts->{lang},
77                       print_title => 1,
78                       print_search_field => 'packages',
79                       search_field_values => { 
80                           keywords => _g('search for a package'),
81                           searchon => 'default',
82                           arch => 'any',
83                           suite => 'all',
84                           section => 'all',
85                           exact => 1,
86                           debug => $Packages::CGI::debug,
87                       },
88                       );
89 }
90
91 1;
92