53443bc08bb1d1f666064629590089cc2d9c3ebe
[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 "collecting package info ...\n";
52 my %allpkgs;
53 while (my ($pkg, $data) = each %packages) {
54     my (%pkg,%virt);
55     my ($virt, $p_data) = split /\000/o, $data, 2;
56     %virt = split /\01/o, $virt; 
57     foreach (split /\000/o, $p_data||'') {
58         my @data = split ( /\s/o, $_, 9 );
59         $pkg{$data[1]} ||= new Packages::Page( $pkg );
60         $pkg{$data[1]}->merge_package( { package => $pkg,
61                                          archive => $data[0],
62                                          suite => $data[1],
63                                          architecture => $data[2],
64                                          section => $data[3],
65                                          subsection => $data[4],
66                                          priority => $data[5],
67                                          version => $data[6],
68                                          'description-md5' => $data[7],
69                                          description => $data[8] } );
70     }
71     foreach (keys %virt) {
72         next if $_ eq '-';
73         $pkg{$_} ||= new Packages::Page( $pkg );
74         $pkg{$_}->add_provided_by([split /\s+/, $virt{$_}]);
75     }
76
77     while (my ($key, $entry) = each %pkg) {
78         $allpkgs{$key} ||= [];
79
80         my %p = ( name => $pkg, providers => [], versions => '' );
81         if (my $provided_by = $entry->{provided_by}) {
82             $p{providers} = $provided_by;
83         }
84         $p{subsection} = $p{section} = $p{archive} = $p{desc} = $p{priority} = '';
85         unless ($entry->is_virtual) {
86             (undef, $p{versions}) = $entry->get_version_string;
87             $p{subsection} = $entry->get_newest( 'subsection' );
88             $p{section} = $entry->get_newest( 'section' );
89             $p{archive} = $entry->get_newest( 'archive' );
90             $p{desc} = $entry->get_newest( 'description' );
91             my $desc_md5 = $entry->get_newest( 'description-md5' );
92             my $trans_desc = $desctrans{$desc_md5};
93             if ($trans_desc) {
94                 my %sdescs;
95                 my %trans_desc = split /\000|\001/, $trans_desc;
96                 while (my ($l, $d) = each %trans_desc) {
97                     $d =~ s/\n.*//os;
98
99                     $sdescs{$l} = $d;
100                 }
101                 $p{trans_desc} = \%sdescs;
102             }
103             $p{priority} = $entry->get_newest( 'priority' );
104         }
105         push @{$allpkgs{$key}}, \%p;
106     }
107 }
108
109 write_files(\%allpkgs);
110
111 print "collecting source package info ...\n";
112 my %allsrcpkgs;
113 while (my ($pkg, $data) = each %src_packages) {
114     my %pkg;    
115     foreach (split /\000/o, $data||'') {
116         my @data = split ( /\s/o, $_ );
117         $pkg{$data[1]} ||= new Packages::SrcPage( $pkg );
118         $pkg{$data[1]}->merge_package( { package => $pkg,
119                                          archive => $data[0],
120                                          suite => $data[1],
121                                          section => $data[2],
122                                          subsection => $data[3],
123                                          priority => $data[4],
124                                          version => $data[5],
125                                          } );
126     }
127
128     while (my ($key, $entry) = each %pkg) {
129         $allsrcpkgs{$key} ||= [];
130
131         my %p = ( name => $pkg, providers => [], versions => '' );
132         $p{versions} = $entry->{version};
133         $p{subsection} = $entry->get_newest( 'subsection' );
134         $p{section} = $entry->get_newest( 'section' );
135         $p{archive} = $entry->get_newest( 'archive' );
136         $p{priority} = $entry->get_newest( 'priority' );
137         
138         $p{desc} = '';
139         $p{binaries} = [];
140 #       my $binaries = find_binaries( $pkg, $p{archive}, $p{suite}, \%src2bin );
141 #       if ($binaries && @$binaries) {
142 #           pkg_list( \%packages, $opts, $binaries, 'en', $contents{binaries} );
143 #       }
144
145         push @{$allsrcpkgs{$key}}, \%p;
146     }
147 }
148
149 write_files(\%allsrcpkgs, 1);
150
151 sub write_files {
152     my ($pkgs, $source) = @_;
153
154     $source = $source ? 'source/' : '';
155     print "writing files ...\n";
156     foreach my $s (@SUITES) {
157         my $key = $s;
158         mkpath ( "$wwwdir/$source$key" );
159         foreach my $lang (@LANGUAGES) {
160             my $charset = 'UTF-8';
161             my $cat = Packages::I18N::Locale->get_handle($lang)
162                 or die "get_handle failed for $lang";
163
164             my %lang_vars = ( po_lang => $lang, ddtp_lang => $lang,
165                               charset => $charset,
166                               cat => $cat, used_langs => \@LANGUAGES );
167             print "writing $source$s/allpackages (lang=$lang)...\n";
168             $template->page( 'index', { %lang_vars, packages => $pkgs->{$key},
169                                         suite => $s, is_source => $source  },
170                              "$wwwdir/$source$key/allpackages.$lang.html.new" );
171             print "writing $source$s/allpackages (txt,lang=$lang)...\n";
172             my $gzfh = gzopen("$wwwdir/$source$key/allpackages.$lang.txt.gz.new",
173                               'wb9')
174                 or die "can't open text index file for output: $!";
175             my $gztxt;
176             $gztxt = $txt_template->page( 'index', { %lang_vars, packages => $pkgs->{$key},
177                                                      suite => $s, is_source => $source  },
178                                           );
179             $gzfh->gzwrite($gztxt);
180             ($gzfh->gzclose == Z_OK) or
181                 warn "can't close text index file $wwwdir/$source$key/allpackages.$lang.txt.gz.new: ".$gzfh->gzerror;
182
183             rename( "$wwwdir/$source$key/allpackages.$lang.html.new",
184                     "$wwwdir/$source$key/allpackages.$lang.html" );
185             rename( "$wwwdir/$source$key/allpackages.$lang.txt.gz.new",
186                     "$wwwdir/$source$key/allpackages.$lang.txt.gz" );
187
188             foreach my $sec (keys %{$sections->{$s}}) {
189                 mkpath ( "$wwwdir/$source$key/$sec" );
190
191                 print "writing $source$s/$sec/index (lang=$lang)...\n";
192                 $template->page( 'index', { packages => [ grep { $_->{section} eq $sec } @{$pkgs->{$key}} ],
193                                             %lang_vars, suite => $s, is_source => $source,
194                                             category => { id => $cat->g('Section'), name => $sec } },
195                                  "$wwwdir/$source$key/$sec/index.$lang.html.new" );
196                 rename( "$wwwdir/$source$key/$sec/index.$lang.html.new",
197                         "$wwwdir/$source$key/$sec/index.$lang.html" );
198             }
199             foreach my $ssec ((keys %{$subsections->{$s}}, 'virtual')) {
200                 next if $ssec eq '-';
201                 mkpath ( "$wwwdir/$source$key/$ssec" );
202
203                 print "writing $source$s/$ssec/index (lang=$lang)...\n";
204                 $template->page( 'index', { packages => [ grep { $_->{subsection} eq $ssec } @{$pkgs->{$key}} ],
205                                             %lang_vars, suite => $s, is_source => $source,
206                                             category => { id => $cat->g('Subsection'), name => $ssec } },
207                                  "$wwwdir/$source$key/$ssec/index.$lang.html.new" );
208                 rename( "$wwwdir/$source$key/$ssec/index.$lang.html.new",
209                         "$wwwdir/$source$key/$ssec/index.$lang.html" );
210             }
211             foreach my $prio (keys %{$priorities->{$s}}) {
212                 next if $prio eq '-';
213                 mkpath ( "$wwwdir/$source$key/$prio" );
214
215                 print "writing $source$s/$prio/index (lang=$lang)...\n";
216                 $template->page( 'index', { packages => [ grep { $_->{priority} eq $prio } @{$pkgs->{$key}} ],
217                                             %lang_vars, suite => $s, is_source => $source,
218                                             category => { id => $cat->g('Priority'), name => $prio } },
219                                  "$wwwdir/$source$key/$prio/index.$lang.html.new" );
220                 rename( "$wwwdir/$source$key/$prio/index.$lang.html.new",
221                         "$wwwdir/$source$key/$prio/index.$lang.html" );
222             }
223         }
224     }
225 }