Support translated short descriptions in index pages
[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 ( 'templates', $LOCALES );
31 bindtextdomain ( 'sections', $LOCALES );
32 bindtextdomain ( 'langs', $LOCALES );
33 textdomain( 'pdo' );
34
35 my $wwwdir = "$TOPDIR/www";
36
37 tie my %packages, 'DB_File', "$DBDIR/packages_small.db",
38     O_RDONLY, 0666, $DB_BTREE
39     or die "couldn't tie DB $DBDIR/packages_small.db: $!";
40 tie my %src_packages, 'DB_File', "$DBDIR/sources_small.db",
41     O_RDONLY, 0666, $DB_BTREE
42     or die "couldn't tie DB $DBDIR/sources_small.db: $!";
43 tie my %src2bin, 'DB_File', "$DBDIR/sources_packages.db",
44     O_RDONLY, 0666, $DB_BTREE
45     or die "couldn't open $DBDIR/sources_packages.db: $!";
46 tie my %desctrans, 'DB_File', "$DBDIR/descriptions_translated.db",
47     O_RDONLY, 0666, $DB_BTREE
48     or die "couldn't tie DB $DBDIR/descriptions_translated.db: $!";
49
50 my $sections = retrieve "$DBDIR/sections.info";
51 my $subsections = retrieve "$DBDIR/subsections.info";
52 my $priorities = retrieve "$DBDIR/priorities.info";
53
54 #use Data::Dumper;
55 #print STDERR Dumper($sections, $subsections, $priorities);
56
57 my (%pages);
58
59 my $template = new Packages::Template( "$TOPDIR/templates", 'html');
60 my $txt_template = new Packages::Template( "$TOPDIR/templates", 'txt');
61
62 print "write suite index files ...\n";
63 foreach my $s (@SUITES) {
64     my $key = $s;
65     mkpath ( "$wwwdir/$key" );
66     mkpath ( "$wwwdir/source/$key" );
67     foreach my $lang (@LANGUAGES) {
68         my $locale = get_locale( $lang );
69         my $charset = get_charset( $lang );
70         setlocale ( LC_ALL, $locale ) or do {
71             warn "couldn't set locale ($lang/$locale)\n";
72             next;
73         };
74         print "writing $key/index (lang=$lang)...\n";
75
76         my %content = ( subsections => [], suite => $s,
77                         lang => $lang, charset => $charset,
78                         used_langs => \@LANGUAGES, suites => \@SUITES );
79         foreach my $ssec ((keys %{$subsections->{$s}}, 'virtual')) {
80             next if $ssec eq '-';
81             if ($sections_descs{$ssec}) {
82                 push @{$content{subsections}}, {
83                     id => $ssec,
84                     name => dgettext( 'sections', $sections_descs{$ssec}[0] ),
85                     desc => dgettext( 'sections', $sections_descs{$ssec}[1] ),
86                 };
87             }
88         }
89
90         $template->page( 'suite_index', \%content,
91                          "$wwwdir/$key/index.$lang.html.new");
92         rename( "$wwwdir/$key/index.$lang.html.new",
93                 "$wwwdir/$key/index.$lang.html" );
94
95         $content{source} = 'source';
96         $template->page( 'suite_index', \%content,
97                          "$wwwdir/source/$key/index.$lang.html.new");
98         rename( "$wwwdir/source/$key/index.$lang.html.new",
99                 "$wwwdir/source/$key/index.$lang.html" );
100
101     }
102 }
103 setlocale( LC_ALL, 'C' ) or die "couldn't reset locale";
104
105 print "collecting package info ...\n";
106 my %allpkgs;
107 while (my ($pkg, $data) = each %packages) {
108     my (%pkg,%virt);
109     my ($virt, $p_data) = split /\000/o, $data, 2;
110     %virt = split /\01/o, $virt; 
111     foreach (split /\000/o, $p_data||'') {
112         my @data = split ( /\s/o, $_, 9 );
113         $pkg{$data[1]} ||= new Packages::Page( $pkg );
114         $pkg{$data[1]}->merge_package( { package => $pkg,
115                                          archive => $data[0],
116                                          suite => $data[1],
117                                          architecture => $data[2],
118                                          section => $data[3],
119                                          subsection => $data[4],
120                                          priority => $data[5],
121                                          version => $data[6],
122                                          'description-md5' => $data[7],
123                                          description => $data[8] } );
124     }
125     foreach (keys %virt) {
126         next if $_ eq '-';
127         $pkg{$_} ||= new Packages::Page( $pkg );
128         $pkg{$_}->add_provided_by([split /\s+/, $virt{$_}]);
129     }
130
131     while (my ($key, $entry) = each %pkg) {
132         $allpkgs{$key} ||= [];
133
134         my %p = ( name => $pkg, providers => [], versions => '' );
135         if (my $provided_by = $entry->{provided_by}) {
136             $p{providers} = $provided_by;
137         }
138         $p{subsection} = $p{section} = $p{archive} = $p{desc} = $p{priority} = '';
139         unless ($entry->is_virtual) {
140             (undef, $p{versions}) = $entry->get_version_string;
141             $p{subsection} = $entry->get_newest( 'subsection' );
142             $p{section} = $entry->get_newest( 'section' );
143             $p{archive} = $entry->get_newest( 'archive' );
144             $p{desc} = $entry->get_newest( 'description' );
145             my $desc_md5 = $entry->get_newest( 'description-md5' );
146             my $trans_desc = $desctrans{$desc_md5};
147             if ($trans_desc) {
148                 my %sdescs;
149                 my %trans_desc = split /\000|\001/, $trans_desc;
150                 while (my ($l, $d) = each %trans_desc) {
151                     $d =~ s/\n.*//os;
152
153                     $sdescs{$l} = $d;
154                 }
155                 $p{trans_desc} = \%sdescs;
156             }
157             $p{priority} = $entry->get_newest( 'priority' );
158         }
159         push @{$allpkgs{$key}}, \%p;
160     }
161 }
162
163 write_files(\%allpkgs);
164
165 print "collecting source package info ...\n";
166 my %allsrcpkgs;
167 while (my ($pkg, $data) = each %src_packages) {
168     my %pkg;    
169     foreach (split /\000/o, $data||'') {
170         my @data = split ( /\s/o, $_ );
171         $pkg{$data[1]} ||= new Packages::SrcPage( $pkg );
172         $pkg{$data[1]}->merge_package( { package => $pkg,
173                                          archive => $data[0],
174                                          suite => $data[1],
175                                          section => $data[2],
176                                          subsection => $data[3],
177                                          priority => $data[4],
178                                          version => $data[5],
179                                          } );
180     }
181
182     while (my ($key, $entry) = each %pkg) {
183         $allsrcpkgs{$key} ||= [];
184
185         my %p = ( name => $pkg, providers => [], versions => '' );
186         $p{versions} = $entry->{version};
187         $p{subsection} = $entry->get_newest( 'subsection' );
188         $p{section} = $entry->get_newest( 'section' );
189         $p{archive} = $entry->get_newest( 'archive' );
190         $p{priority} = $entry->get_newest( 'priority' );
191         
192         $p{desc} = '';
193         $p{binaries} = [];
194 #       my $binaries = find_binaries( $pkg, $p{archive}, $p{suite}, \%src2bin );
195 #       if ($binaries && @$binaries) {
196 #           pkg_list( \%packages, $opts, $binaries, 'en', $contents{binaries} );
197 #       }
198
199         push @{$allsrcpkgs{$key}}, \%p;
200     }
201 }
202
203 write_files(\%allsrcpkgs, 1);
204
205 sub write_files {
206     my ($pkgs, $source) = @_;
207
208     $source = $source ? 'source/' : '';
209     print "writing files ...\n";
210     foreach my $s (@SUITES) {
211         my $key = $s;
212         mkpath ( "$wwwdir/$source$key" );
213         foreach my $lang (@LANGUAGES) {
214             my $locale = get_locale( $lang );
215             my $charset = get_charset( $lang );
216             setlocale ( LC_ALL, $locale ) or do {
217                 warn "couldn't set locale ($lang/$locale)\n";
218                 next;
219             };
220
221             my %lang_vars = ( lang => $lang, charset => $charset,
222                               used_langs => \@LANGUAGES );
223             print "writing $source$s/allpackages (lang=$lang)...\n";
224             $template->page( 'index', { %lang_vars, packages => $pkgs->{$key},
225                                         suite => $s, is_source => $source  },
226                              "$wwwdir/$source$key/allpackages.$lang.html.new" );
227             print "writing $source$s/allpackages (txt,lang=$lang)...\n";
228             my $gzfh = gzopen("$wwwdir/$source$key/allpackages.$lang.txt.gz.new",
229                               'wb9')
230                 or die "can't open text index file for output: $!";
231             my $gztxt;
232             $gztxt = $txt_template->page( 'index', { %lang_vars, packages => $pkgs->{$key},
233                                                      suite => $s, is_source => $source  },
234                                           );
235             $gzfh->gzwrite($gztxt);
236             ($gzfh->gzclose == Z_OK) or
237                 warn "can't close text index file $wwwdir/$source$key/allpackages.$lang.txt.gz.new: ".$gzfh->gzerror;
238
239             rename( "$wwwdir/$source$key/allpackages.$lang.html.new",
240                     "$wwwdir/$source$key/allpackages.$lang.html" );
241             rename( "$wwwdir/$source$key/allpackages.$lang.txt.gz.new",
242                     "$wwwdir/$source$key/allpackages.$lang.txt.gz" );
243
244             foreach my $sec (keys %{$sections->{$s}}) {
245                 mkpath ( "$wwwdir/$source$key/$sec" );
246
247                 print "writing $source$s/$sec/index (lang=$lang)...\n";
248                 $template->page( 'index', { packages => [ grep { $_->{section} eq $sec } @{$pkgs->{$key}} ],
249                                             %lang_vars, suite => $s, is_source => $source,
250                                             category => { id => _g('Section'), name => $sec } },
251                                  "$wwwdir/$source$key/$sec/index.$lang.html.new" );
252                 rename( "$wwwdir/$source$key/$sec/index.$lang.html.new",
253                         "$wwwdir/$source$key/$sec/index.$lang.html" );
254             }
255             foreach my $ssec ((keys %{$subsections->{$s}}, 'virtual')) {
256                 next if $ssec eq '-';
257                 mkpath ( "$wwwdir/$source$key/$ssec" );
258
259                 print "writing $source$s/$ssec/index (lang=$lang)...\n";
260                 $template->page( 'index', { packages => [ grep { $_->{subsection} eq $ssec } @{$pkgs->{$key}} ],
261                                             %lang_vars, suite => $s, is_source => $source,
262                                             category => { id => _g('Subsection'), name => $ssec } },
263                                  "$wwwdir/$source$key/$ssec/index.$lang.html.new" );
264                 rename( "$wwwdir/$source$key/$ssec/index.$lang.html.new",
265                         "$wwwdir/$source$key/$ssec/index.$lang.html" );
266             }
267             foreach my $prio (keys %{$priorities->{$s}}) {
268                 next if $prio eq '-';
269                 mkpath ( "$wwwdir/$source$key/$prio" );
270
271                 print "writing $source$s/$prio/index (lang=$lang)...\n";
272                 $template->page( 'index', { packages => [ grep { $_->{priority} eq $prio } @{$pkgs->{$key}} ],
273                                             %lang_vars, suite => $s, is_source => $source,
274                                             category => { id => _g('Priority'), name => $prio } },
275                                  "$wwwdir/$source$key/$prio/index.$lang.html.new" );
276                 rename( "$wwwdir/$source$key/$prio/index.$lang.html.new",
277                         "$wwwdir/$source$key/$prio/index.$lang.html" );
278             }
279         }
280     }
281 }