]> git.deb.at Git - deb/packages.git/blob - bin/create_index_pages
Clean up footer handling
[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 # work around problems with non-US security updates
48 $subsections->{oldstable}{us}{'non-US'}++;
49 my $priorities = retrieve "$DBDIR/priorities.info";
50
51 #use Data::Dumper;
52 #print STDERR Dumper($sections, $subsections, $priorities);
53
54 my (%pages);
55
56 my $template = new Packages::Template( "$TOPDIR/templates", 'html', {} );
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_locale( $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         $content{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) };
76         $content{make_url} = sub { return &Packages::CGI::make_url(@_) };
77         # needed to work around the limitations of the the FILTER syntax
78         $content{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
79         $content{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
80         $content{quotemeta} = sub { return quotemeta($_[0]) };
81
82         foreach my $ssec ((keys %{$subsections->{$s}}, 'virtual')) {
83             next if $ssec eq '-';
84             if ($sections_descs{$ssec}) {
85                 push @{$content{subsections}}, {
86                     id => $ssec,
87                     name => dgettext( 'sections', $sections_descs{$ssec}[0] ),
88                     desc => dgettext( 'sections', $sections_descs{$ssec}[1] ),
89                 };
90             }
91         }
92
93         open $pages{$key}{$lang}{index}{fh}, '>', "$wwwdir/$key/index.$lang.html.new"
94             or die "can't open index file for output: $!";
95         print {$pages{$key}{$lang}{index}{fh}} $template->page( 'suite_index', \%content );
96         close $pages{$key}{$lang}{index}{fh} or
97             warn "can't close index file $wwwdir/$key/index.$lang.html.new: $!";
98         rename( "$wwwdir/$key/index.$lang.html.new",
99                 "$wwwdir/$key/index.$lang.html" );
100
101         $content{source} = 'source';
102         open $pages{$key}{$lang}{source_index}{fh}, '>', "$wwwdir/source/$key/index.$lang.html.new"
103             or die "can't open index file for output: $!";
104         print {$pages{$key}{$lang}{source_index}{fh}} $template->page( 'suite_index', \%content );
105         close $pages{$key}{$lang}{source_index}{fh} or
106             warn "can't close index file $wwwdir/source/$key/index.$lang.html.new: $!";
107         rename( "$wwwdir/source/$key/index.$lang.html.new",
108                 "$wwwdir/source/$key/index.$lang.html" );
109
110     }
111 }
112 setlocale( LC_ALL, 'C' ) or die "couldn't reset locale";
113
114 print "collecting package info ...\n";
115 my %allpkgs;
116 while (my ($pkg, $data) = each %packages) {
117     my (%pkg,%virt);
118     my ($virt, $p_data) = split /\000/o, $data, 2;
119     %virt = split /\01/o, $virt; 
120     foreach (split /\000/o, $p_data||'') {
121         my @data = split ( /\s/o, $_, 8 );
122         $pkg{$data[1]} ||= new Packages::Page( $pkg );
123         $pkg{$data[1]}->merge_package( { package => $pkg,
124                                          archive => $data[0],
125                                          suite => $data[1],
126                                          architecture => $data[2],
127                                          section => $data[3],
128                                          subsection => $data[4],
129                                          priority => $data[5],
130                                          version => $data[6],
131                                          description => $data[7] } );
132     }
133     foreach (keys %virt) {
134         next if $_ eq '-';
135         $pkg{$_} ||= new Packages::Page( $pkg );
136         $pkg{$_}->add_provided_by([split /\s+/, $virt{$_}]);
137     }
138
139     while (my ($key, $entry) = each %pkg) {
140         $allpkgs{$key} ||= [];
141
142         my %p = ( name => $pkg, providers => [], versions => '' );
143         if (my $provided_by = $entry->{provided_by}) {
144             $p{providers} = $provided_by;
145         }
146         $p{subsection} = $p{section} = $p{archive} = $p{desc} = $p{priority} = '';
147         unless ($entry->is_virtual) {
148             (undef, $p{versions}) = $entry->get_version_string;
149             $p{subsection} = $entry->get_newest( 'subsection' );
150             $p{section} = $entry->get_newest( 'section' );
151             $p{archive} = $entry->get_newest( 'archive' );
152             $p{desc} = $entry->get_newest( 'description' );
153             $p{priority} = $entry->get_newest( 'priority' );
154         }
155         push @{$allpkgs{$key}}, \%p;
156     }
157 }
158
159 write_files(\%allpkgs);
160
161 print "collecting source package info ...\n";
162 my %allsrcpkgs;
163 while (my ($pkg, $data) = each %src_packages) {
164     my %pkg;    
165     foreach (split /\000/o, $data||'') {
166         my @data = split ( /\s/o, $_ );
167         $pkg{$data[1]} ||= new Packages::SrcPage( $pkg );
168         $pkg{$data[1]}->merge_package( { package => $pkg,
169                                          archive => $data[0],
170                                          suite => $data[1],
171                                          section => $data[2],
172                                          subsection => $data[3],
173                                          priority => $data[4],
174                                          version => $data[5],
175                                          } );
176     }
177
178     while (my ($key, $entry) = each %pkg) {
179         $allsrcpkgs{$key} ||= [];
180
181         my %p = ( name => $pkg, providers => [], versions => '' );
182         $p{versions} = $entry->{version};
183         $p{subsection} = $entry->get_newest( 'subsection' );
184         $p{section} = $entry->get_newest( 'section' );
185         $p{archive} = $entry->get_newest( 'archive' );
186         $p{priority} = $entry->get_newest( 'priority' );
187         
188         $p{desc} = '';
189         $p{binaries} = [];
190 #       my $binaries = find_binaries( $pkg, $p{archive}, $p{suite}, \%src2bin );
191 #       if ($binaries && @$binaries) {
192 #           pkg_list( \%packages, $opts, $binaries, 'en', $contents{binaries} );
193 #       }
194
195         push @{$allsrcpkgs{$key}}, \%p;
196     }
197 }
198
199 write_files(\%allsrcpkgs, 1);
200
201 sub write_files {
202     my ($pkgs, $source) = @_;
203
204     $source = $source ? 'source/' : '';
205     print "writing files ...\n";
206     foreach my $s (@SUITES) {
207         my $key = $s;
208         mkpath ( "$wwwdir/$source$key" );
209         print "writing $source$s/allpackages...\n";
210         $template->process( 'html/index.tmpl', { packages => $pkgs->{$key}, suite => $s, lang => 'en', is_source => $source  },
211                             "$wwwdir/$source$key/allpackages.en.html.new" )
212             or die "error writing allpackages for $key: ".$template->error();
213         print "writing $source$s/allpackages (txt)...\n";
214         my $gzfh = gzopen("$wwwdir/$source$key/allpackages.en.txt.gz.new",
215                       'wb9')
216             or die "can't open text index file for output: $!";
217         my $gztxt;
218         $template->process( 'txt/index.tmpl', { packages => $pkgs->{$key}, suite => $s, lang => 'en', is_source => $source  },
219                             \$gztxt )
220             or die "error writing allpackages txt for $key: ".$template->error();    
221         $gzfh->gzwrite($gztxt);
222         ($gzfh->gzclose == Z_OK) or
223             warn "can't close text index file $wwwdir/$source$key/allpackages.en.txt.gz.new: ".$gzfh->gzerror;
224
225         rename( "$wwwdir/$source$key/allpackages.en.html.new",
226                 "$wwwdir/$source$key/allpackages.en.html" );
227         rename( "$wwwdir/$source$key/allpackages.en.txt.gz.new",
228                 "$wwwdir/$source$key/allpackages.en.txt.gz" );
229         
230         foreach my $sec (keys %{$sections->{$s}}) {
231             mkpath ( "$wwwdir/$source$key/$sec" );
232
233             print "writing $source$s/$sec/index...\n";
234             $template->process( 'html/index.tmpl', { packages => [ grep { $_->{section} eq $sec } @{$pkgs->{$key}} ],
235                                                      suite => $s, lang => 'en', is_source => $source,
236                                                      category => { id => 'section', name => $sec } },
237                                 "$wwwdir/$source$key/$sec/index.en.html.new" )
238                 or die "error writing section index for $key/$sec: ".$template->error();
239             rename( "$wwwdir/$source$key/$sec/index.en.html.new",
240                     "$wwwdir/$source$key/$sec/index.en.html" );
241     }
242         foreach my $ssec ((keys %{$subsections->{$s}}, 'virtual')) {
243             next if $ssec eq '-';
244             mkpath ( "$wwwdir/$source$key/$ssec" );
245
246             print "writing $source$s/$ssec/index...\n";
247             $template->process( 'html/index.tmpl', { packages => [ grep { $_->{subsection} eq $ssec } @{$pkgs->{$key}} ],
248                                                      suite => $s, lang => 'en', is_source => $source,
249                                                      category => { id => 'subsection', name => $ssec } },
250                                 "$wwwdir/$source$key/$ssec/index.en.html.new" )
251             or die "error writing subsection index for $key/$ssec: ".$template->error();
252         rename( "$wwwdir/$source$key/$ssec/index.en.html.new",
253                 "$wwwdir/$source$key/$ssec/index.en.html" );
254         }
255         foreach my $prio (keys %{$priorities->{$s}}) {
256             next if $prio eq '-';
257             mkpath ( "$wwwdir/$source$key/$prio" );
258             
259             print "writing $source$s/$prio/index...\n";
260             $template->process( 'html/index.tmpl', { packages => [ grep { $_->{priority} eq $prio } @{$pkgs->{$key}} ],
261                                                      suite => $s, lang => 'en', is_source => $source,
262                                                      category => { id => 'priority', name => $prio } },
263                                 "$wwwdir/$source$key/$prio/index.en.html.new" )
264                 or die "error writing priority index for $key/$prio: ".$template->error();
265             rename( "$wwwdir/$source$key/$prio/index.en.html.new",
266                     "$wwwdir/$source$key/$prio/index.en.html" );
267         }
268     }
269 }