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