]> git.deb.at Git - deb/packages.git/blob - bin/create_index_pages
The Big, the Fat and the Ugly commit ;)
[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         open $pages{$key}{$lang}{index}{fh}, '>', "$wwwdir/$key/index.$lang.html.new"
71             or die "can't open index file for output: $!";
72
73         my %content = ( subsections => [], suite => $s,
74                         lang => $lang, charset => $charset );
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         print {$pages{$key}{$lang}{index}{fh}} $template->page( 'suite_index', \%content );
94         print {$pages{$key}{$lang}{index}{fh}} $template->trailer( 'index', $lang, \@LANGUAGES );
95         close $pages{$key}{$lang}{index}{fh} or
96             warn "can't close index file $wwwdir/$key/index.$lang.html.new: $!";
97         rename( "$wwwdir/$key/index.$lang.html.new",
98                 "$wwwdir/$key/index.$lang.html" );
99         
100     }
101 }
102 setlocale( LC_ALL, 'C' ) or die "couldn't reset locale";
103
104 print "collecting package info ...\n";
105 my %allpkgs;
106 while (my ($pkg, $data) = each %packages) {
107     my (%pkg,%virt);
108     my ($virt, $p_data) = split /\000/o, $data, 2;
109     %virt = split /\01/o, $virt; 
110     foreach (split /\000/o, $p_data||'') {
111         my @data = split ( /\s/o, $_, 8 );
112         $pkg{$data[1]} ||= new Packages::Page( $pkg );
113         $pkg{$data[1]}->merge_package( { package => $pkg,
114                                          archive => $data[0],
115                                          suite => $data[1],
116                                          architecture => $data[2],
117                                          section => $data[3],
118                                          subsection => $data[4],
119                                          priority => $data[5],
120                                          version => $data[6],
121                                          description => $data[7] } );
122     }
123     foreach (keys %virt) {
124         next if $_ eq '-';
125         $pkg{$_} ||= new Packages::Page( $pkg );
126         $pkg{$_}->add_provided_by([split /\s+/, $virt{$_}]);
127     }
128
129     while (my ($key, $entry) = each %pkg) {
130         $allpkgs{$key} ||= [];
131
132         my %p = ( name => $pkg, providers => [], versions => '' );
133         if (my $provided_by = $entry->{provided_by}) {
134             $p{providers} = $provided_by;
135         }
136         $p{subsection} = $p{section} = $p{archive} = $p{desc} = $p{priority} = '';
137         unless ($entry->is_virtual) {
138             (undef, $p{versions}) = $entry->get_version_string;
139             $p{subsection} = $entry->get_newest( 'subsection' );
140             $p{section} = $entry->get_newest( 'section' );
141             $p{archive} = $entry->get_newest( 'archive' );
142             $p{desc} = $entry->get_newest( 'description' );
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         print "writing $source$s/allpackages...\n";
200         $template->process( 'html/index.tmpl', { packages => $pkgs->{$key}, suite => $s, lang => 'en', is_source => $source  },
201                             "$wwwdir/$source$key/allpackages.en.html.new" )
202             or die "error writing allpackages for $key: ".$template->error();
203         print "writing $source$s/allpackages (txt)...\n";
204         my $gzfh = gzopen("$wwwdir/$source$key/allpackages.en.txt.gz.new",
205                       'wb9')
206             or die "can't open text index file for output: $!";
207         my $gztxt;
208         $template->process( 'txt/index.tmpl', { packages => $pkgs->{$key}, suite => $s, lang => 'en', is_source => $source  },
209                             \$gztxt )
210             or die "error writing allpackages txt for $key: ".$template->error();    
211         $gzfh->gzwrite($gztxt);
212         ($gzfh->gzclose == Z_OK) or
213             warn "can't close text index file $wwwdir/$source$key/allpackages.en.txt.gz.new: ".$gzfh->gzerror;
214
215         rename( "$wwwdir/$source$key/allpackages.en.html.new",
216                 "$wwwdir/$source$key/allpackages.en.html" );
217         rename( "$wwwdir/$source$key/allpackages.en.txt.gz.new",
218                 "$wwwdir/$source$key/allpackages.en.txt.gz" );
219         
220         foreach my $sec (keys %{$sections->{$s}}) {
221             mkpath ( "$wwwdir/$source$key/$sec" );
222
223             print "writing $source$s/$sec/index...\n";
224             $template->process( 'html/index.tmpl', { packages => [ grep { $_->{section} eq $sec } @{$pkgs->{$key}} ],
225                                                      suite => $s, lang => 'en', is_source => $source,
226                                                      category => { id => 'section', name => $sec } },
227                                 "$wwwdir/$source$key/$sec/index.en.html.new" )
228                 or die "error writing section index for $key/$sec: ".$template->error();
229             rename( "$wwwdir/$source$key/$sec/index.en.html.new",
230                     "$wwwdir/$source$key/$sec/index.en.html" );
231     }
232         foreach my $ssec ((keys %{$subsections->{$s}}, 'virtual')) {
233             next if $ssec eq '-';
234             mkpath ( "$wwwdir/$source$key/$ssec" );
235
236             print "writing $source$s/$ssec/index...\n";
237             $template->process( 'html/index.tmpl', { packages => [ grep { $_->{subsection} eq $ssec } @{$pkgs->{$key}} ],
238                                                      suite => $s, lang => 'en', is_source => $source,
239                                                      category => { id => 'subsection', name => $ssec } },
240                                 "$wwwdir/$source$key/$ssec/index.en.html.new" )
241             or die "error writing subsection index for $key/$ssec: ".$template->error();
242         rename( "$wwwdir/$source$key/$ssec/index.en.html.new",
243                 "$wwwdir/$source$key/$ssec/index.en.html" );
244         }
245         foreach my $prio (keys %{$priorities->{$s}}) {
246             next if $prio eq '-';
247             mkpath ( "$wwwdir/$source$key/$prio" );
248             
249             print "writing $source$s/$prio/index...\n";
250             $template->process( 'html/index.tmpl', { packages => [ grep { $_->{priority} eq $prio } @{$pkgs->{$key}} ],
251                                                      suite => $s, lang => 'en', is_source => $source,
252                                                      category => { id => 'priority', name => $prio } },
253                                 "$wwwdir/$source$key/$prio/index.en.html.new" )
254                 or die "error writing priority index for $key/$prio: ".$template->error();
255             rename( "$wwwdir/$source$key/$prio/index.en.html.new",
256                     "$wwwdir/$source$key/$prio/index.en.html" );
257         }
258     }
259 }