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