create_index_pages: Fix charset
[deb/packages.git] / bin / create_index_pages
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use POSIX;
7 use File::Path;
8 use DB_File;
9 use Storable;
10 use HTML::Entities;
11 use URI::Escape;
12 use Locale::gettext;
13 use Compress::Zlib;
14
15 use lib './lib';
16
17 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @LANGUAGES $LOCALES);
18 use Packages::Template;
19 use Packages::I18N::Locale;
20 use Packages::Page;
21 use Packages::SrcPage;
22 use Packages::Sections;
23 &Packages::Config::init( './' );
24
25 delete $ENV{'LANGUAGE'};
26 delete $ENV{'LANG'};
27 delete $ENV{'LC_ALL'};
28 delete $ENV{'LC_MESSAGES'};
29 bindtextdomain ( 'pdo', $LOCALES );
30 bindtextdomain ( 'sections', $LOCALES );
31 textdomain( 'pdo' );
32
33 my $wwwdir = "$TOPDIR/www";
34
35 tie my %packages, 'DB_File', "$DBDIR/packages_small.db",
36     O_RDONLY, 0666, $DB_BTREE
37     or die "couldn't tie DB $DBDIR/packages_small.db: $!";
38 tie my %src_packages, 'DB_File', "$DBDIR/sources_small.db",
39     O_RDONLY, 0666, $DB_BTREE
40     or die "couldn't tie DB $DBDIR/sources_small.db: $!";
41 tie my %src2bin, 'DB_File', "$DBDIR/sources_packages.db",
42     O_RDONLY, 0666, $DB_BTREE
43     or die "couldn't open $DBDIR/sources_packages.db: $!";
44
45 my $sections = retrieve "$DBDIR/sections.info";
46 my $subsections = retrieve "$DBDIR/subsections.info";
47 my $priorities = retrieve "$DBDIR/priorities.info";
48
49 #use Data::Dumper;
50 #print STDERR Dumper($sections, $subsections, $priorities);
51
52 my (%pages, %tt_vars);
53
54 $tt_vars{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
55 $tt_vars{make_url} = sub { return &Packages::CGI::make_url(@_) };
56 # needed to work around the limitations of the the FILTER syntax
57 $tt_vars{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
58 $tt_vars{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
59 $tt_vars{quotemeta} = sub { return quotemeta($_[0]) };
60
61 my $template = new Packages::Template( "$TOPDIR/templates", 'html', \%tt_vars );
62
63 print "write suite index files ...\n";
64 foreach my $s (@SUITES) {
65     my $key = $s;
66     mkpath ( "$wwwdir/$key" );
67     mkpath ( "$wwwdir/source/$key" );
68     foreach my $lang (@LANGUAGES) {
69         my $locale = get_locale( $lang );
70         my $charset = get_charset( $lang );
71         setlocale ( LC_ALL, $locale ) or do {
72             warn "couldn't set locale ($lang/$locale)\n";
73             next;
74         };
75         print "writing $key/index (lang=$lang)...\n";
76
77         my %content = ( subsections => [], suite => $s,
78                         lang => $lang, charset => $charset,
79                         used_langs => \@LANGUAGES, suites => \@SUITES );
80         foreach my $ssec ((keys %{$subsections->{$s}}, 'virtual')) {
81             next if $ssec eq '-';
82             if ($sections_descs{$ssec}) {
83                 push @{$content{subsections}}, {
84                     id => $ssec,
85                     name => dgettext( 'sections', $sections_descs{$ssec}[0] ),
86                     desc => dgettext( 'sections', $sections_descs{$ssec}[1] ),
87                 };
88             }
89         }
90
91         open $pages{$key}{$lang}{index}{fh}, '>', "$wwwdir/$key/index.$lang.html.new"
92             or die "can't open index file for output: $!";
93         print {$pages{$key}{$lang}{index}{fh}} $template->page( 'suite_index', \%content );
94         close $pages{$key}{$lang}{index}{fh} or
95             warn "can't close index file $wwwdir/$key/index.$lang.html.new: $!";
96         rename( "$wwwdir/$key/index.$lang.html.new",
97                 "$wwwdir/$key/index.$lang.html" );
98
99         $content{source} = 'source';
100         open $pages{$key}{$lang}{source_index}{fh}, '>', "$wwwdir/source/$key/index.$lang.html.new"
101             or die "can't open index file for output: $!";
102         print {$pages{$key}{$lang}{source_index}{fh}} $template->page( 'suite_index', \%content );
103         close $pages{$key}{$lang}{source_index}{fh} or
104             warn "can't close index file $wwwdir/source/$key/index.$lang.html.new: $!";
105         rename( "$wwwdir/source/$key/index.$lang.html.new",
106                 "$wwwdir/source/$key/index.$lang.html" );
107
108     }
109 }
110 setlocale( LC_ALL, 'C' ) or die "couldn't reset locale";
111
112 print "collecting package info ...\n";
113 my %allpkgs;
114 while (my ($pkg, $data) = each %packages) {
115     my (%pkg,%virt);
116     my ($virt, $p_data) = split /\000/o, $data, 2;
117     %virt = split /\01/o, $virt; 
118     foreach (split /\000/o, $p_data||'') {
119         my @data = split ( /\s/o, $_, 8 );
120         $pkg{$data[1]} ||= new Packages::Page( $pkg );
121         $pkg{$data[1]}->merge_package( { package => $pkg,
122                                          archive => $data[0],
123                                          suite => $data[1],
124                                          architecture => $data[2],
125                                          section => $data[3],
126                                          subsection => $data[4],
127                                          priority => $data[5],
128                                          version => $data[6],
129                                          description => $data[7] } );
130     }
131     foreach (keys %virt) {
132         next if $_ eq '-';
133         $pkg{$_} ||= new Packages::Page( $pkg );
134         $pkg{$_}->add_provided_by([split /\s+/, $virt{$_}]);
135     }
136
137     while (my ($key, $entry) = each %pkg) {
138         $allpkgs{$key} ||= [];
139
140         my %p = ( name => $pkg, providers => [], versions => '' );
141         if (my $provided_by = $entry->{provided_by}) {
142             $p{providers} = $provided_by;
143         }
144         $p{subsection} = $p{section} = $p{archive} = $p{desc} = $p{priority} = '';
145         unless ($entry->is_virtual) {
146             (undef, $p{versions}) = $entry->get_version_string;
147             $p{subsection} = $entry->get_newest( 'subsection' );
148             $p{section} = $entry->get_newest( 'section' );
149             $p{archive} = $entry->get_newest( 'archive' );
150             $p{desc} = $entry->get_newest( 'description' );
151             $p{priority} = $entry->get_newest( 'priority' );
152         }
153         push @{$allpkgs{$key}}, \%p;
154     }
155 }
156
157 write_files(\%allpkgs);
158
159 print "collecting source package info ...\n";
160 my %allsrcpkgs;
161 while (my ($pkg, $data) = each %src_packages) {
162     my %pkg;    
163     foreach (split /\000/o, $data||'') {
164         my @data = split ( /\s/o, $_ );
165         $pkg{$data[1]} ||= new Packages::SrcPage( $pkg );
166         $pkg{$data[1]}->merge_package( { package => $pkg,
167                                          archive => $data[0],
168                                          suite => $data[1],
169                                          section => $data[2],
170                                          subsection => $data[3],
171                                          priority => $data[4],
172                                          version => $data[5],
173                                          } );
174     }
175
176     while (my ($key, $entry) = each %pkg) {
177         $allsrcpkgs{$key} ||= [];
178
179         my %p = ( name => $pkg, providers => [], versions => '' );
180         $p{versions} = $entry->{version};
181         $p{subsection} = $entry->get_newest( 'subsection' );
182         $p{section} = $entry->get_newest( 'section' );
183         $p{archive} = $entry->get_newest( 'archive' );
184         $p{priority} = $entry->get_newest( 'priority' );
185         
186         $p{desc} = '';
187         $p{binaries} = [];
188 #       my $binaries = find_binaries( $pkg, $p{archive}, $p{suite}, \%src2bin );
189 #       if ($binaries && @$binaries) {
190 #           pkg_list( \%packages, $opts, $binaries, 'en', $contents{binaries} );
191 #       }
192
193         push @{$allsrcpkgs{$key}}, \%p;
194     }
195 }
196
197 write_files(\%allsrcpkgs, 1);
198
199 sub write_files {
200     my ($pkgs, $source) = @_;
201
202     $source = $source ? 'source/' : '';
203     print "writing files ...\n";
204     foreach my $s (@SUITES) {
205         my $key = $s;
206         mkpath ( "$wwwdir/$source$key" );
207         print "writing $source$s/allpackages...\n";
208         $template->process( 'html/index.tmpl', { packages => $pkgs->{$key}, suite => $s, lang => 'en', is_source => $source  },
209                             "$wwwdir/$source$key/allpackages.en.html.new" )
210             or die "error writing allpackages for $key: ".$template->error();
211         print "writing $source$s/allpackages (txt)...\n";
212         my $gzfh = gzopen("$wwwdir/$source$key/allpackages.en.txt.gz.new",
213                       'wb9')
214             or die "can't open text index file for output: $!";
215         my $gztxt;
216         $template->process( 'txt/index.tmpl', { packages => $pkgs->{$key}, suite => $s, lang => 'en', is_source => $source  },
217                             \$gztxt )
218             or die "error writing allpackages txt for $key: ".$template->error();    
219         $gzfh->gzwrite($gztxt);
220         ($gzfh->gzclose == Z_OK) or
221             warn "can't close text index file $wwwdir/$source$key/allpackages.en.txt.gz.new: ".$gzfh->gzerror;
222
223         rename( "$wwwdir/$source$key/allpackages.en.html.new",
224                 "$wwwdir/$source$key/allpackages.en.html" );
225         rename( "$wwwdir/$source$key/allpackages.en.txt.gz.new",
226                 "$wwwdir/$source$key/allpackages.en.txt.gz" );
227         
228         foreach my $sec (keys %{$sections->{$s}}) {
229             mkpath ( "$wwwdir/$source$key/$sec" );
230
231             print "writing $source$s/$sec/index...\n";
232             $template->process( 'html/index.tmpl', { packages => [ grep { $_->{section} eq $sec } @{$pkgs->{$key}} ],
233                                                      suite => $s, lang => 'en', is_source => $source,
234                                                      category => { id => 'section', name => $sec } },
235                                 "$wwwdir/$source$key/$sec/index.en.html.new" )
236                 or die "error writing section index for $key/$sec: ".$template->error();
237             rename( "$wwwdir/$source$key/$sec/index.en.html.new",
238                     "$wwwdir/$source$key/$sec/index.en.html" );
239     }
240         foreach my $ssec ((keys %{$subsections->{$s}}, 'virtual')) {
241             next if $ssec eq '-';
242             mkpath ( "$wwwdir/$source$key/$ssec" );
243
244             print "writing $source$s/$ssec/index...\n";
245             $template->process( 'html/index.tmpl', { packages => [ grep { $_->{subsection} eq $ssec } @{$pkgs->{$key}} ],
246                                                      suite => $s, lang => 'en', is_source => $source,
247                                                      category => { id => 'subsection', name => $ssec } },
248                                 "$wwwdir/$source$key/$ssec/index.en.html.new" )
249             or die "error writing subsection index for $key/$ssec: ".$template->error();
250         rename( "$wwwdir/$source$key/$ssec/index.en.html.new",
251                 "$wwwdir/$source$key/$ssec/index.en.html" );
252         }
253         foreach my $prio (keys %{$priorities->{$s}}) {
254             next if $prio eq '-';
255             mkpath ( "$wwwdir/$source$key/$prio" );
256             
257             print "writing $source$s/$prio/index...\n";
258             $template->process( 'html/index.tmpl', { packages => [ grep { $_->{priority} eq $prio } @{$pkgs->{$key}} ],
259                                                      suite => $s, lang => 'en', is_source => $source,
260                                                      category => { id => 'priority', name => $prio } },
261                                 "$wwwdir/$source$key/$prio/index.en.html.new" )
262                 or die "error writing priority index for $key/$prio: ".$template->error();
263             rename( "$wwwdir/$source$key/$prio/index.en.html.new",
264                     "$wwwdir/$source$key/$prio/index.en.html" );
265         }
266     }
267 }