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