]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoShow.pm
remove the html_header argument from do_* calls
[deb/packages.git] / lib / Packages / DoShow.pm
1 package Packages::DoShow;
2
3 use strict;
4 use warnings;
5
6 use POSIX;
7 use URI::Escape;
8 use HTML::Entities;
9 use DB_File;
10 use Benchmark ':hireswallclock';
11 use Exporter;
12
13 use Deb::Versions;
14 use Packages::Config qw( $DBDIR @SUITES @ARCHIVES @SECTIONS
15                          @ARCHITECTURES %FTP_SITES @DDTP_LANGUAGES);
16 use Packages::I18N::Locale;
17 use Packages::CGI qw( :DEFAULT make_url make_search_url note );
18 use Packages::DB;
19 use Packages::Search qw( :all );
20 use Packages::Page ();
21 use Packages::SrcPage ();
22
23 our @ISA = qw( Exporter );
24 our @EXPORT = qw( do_show );
25
26 sub do_show {
27     my ($params, $opts, $page_contents) = @_;
28
29     if ($params->{errors}{package}) {
30         fatal_error( _g( "package not valid or not specified" ) );
31     }
32     if ($params->{errors}{suite}) {
33         fatal_error( _g( "suite not valid or not specified" ) );
34     }
35     if (@{$opts->{suite}} > 1) {
36         fatal_error( sprintf( _g( "more than one suite specified for show (%s)" ), "@{$opts->{suite}}" ) );
37     }
38
39     my %contents;
40     $contents{make_url} = sub { return &Packages::CGI::make_url(@_) };
41
42     my $pkg = $opts->{package};
43     $contents{pkg} = $pkg;
44     my $suite = $opts->{suite}[0];
45     $contents{suite} = $suite;
46     my $archive = $opts->{archive}[0] ||'';
47     
48     our (%packages_all, %sources_all);
49     my (@results, @non_results);
50     my $page = $opts->{source} ?
51         new Packages::SrcPage( $pkg ) :
52         new Packages::Page( $pkg );
53     my ($short_desc, $version, $section, $subsection) = ("")x5;
54     
55     my $st0 = new Benchmark;
56     unless (@Packages::CGI::fatal_errors) {
57         tie %packages_all, 'DB_File', "$DBDIR/packages_all_$suite.db",
58         O_RDONLY, 0666, $DB_BTREE
59             or die "couldn't tie DB $DBDIR/packages_all_$suite.db: $!";
60         tie %sources_all, 'DB_File', "$DBDIR/sources_all_$suite.db",
61         O_RDONLY, 0666, $DB_BTREE
62             or die "couldn't tie DB $DBDIR/sources_all_$suite.db: $!";
63
64         unless ($opts->{source}) {
65             read_entry_all( \%packages, $pkg, \@results, \@non_results, $opts );
66         } else {
67             read_src_entry_all( \%sources, $pkg, \@results, \@non_results, $opts );
68         }
69
70         unless (@results || @non_results ) {
71             fatal_error( _g( "No such package." )."<br>".
72                          sprintf( _g( '<a href="%s">Search for the package</a>' ), make_search_url('','keywords='.uri_escape($pkg)) ) );
73         } else {
74             my %all_suites;
75             foreach (@results, @non_results) {
76                 my $a = $_->[1];
77                 my $s = $_->[2];
78                 $all_suites{$s}++;
79             }
80             $contents{suites} = [ suites_sort(keys %all_suites) ];
81
82             unless (@results) {
83                 fatal_error( _g( "Package not available in this suite." ) );
84             } else {
85                 $contents{page} = $page;
86                 unless ($opts->{source}) {
87
88                     for my $entry (@results) {
89                         debug( join(":", @$entry), 1 ) if DEBUG;
90                         my (undef, $archive, undef, $arch, $section, $subsection,
91                             $priority, $version, $provided_by) = @$entry;
92                         
93                         if ($arch ne 'virtual') {
94                             my %data = split /\000/, $packages_all{"$pkg $arch $version"};
95                             $data{package} = $pkg;
96                             $data{architecture} = $arch;
97                             $data{version} = $version;
98                             $page->merge_package(\%data)
99                                 or debug( "Merging $pkg $arch $version FAILED", 2 ) if DEBUG;
100                         } else {
101                             $page->add_provided_by([split /\s+/, $provided_by]);
102                         }
103                     }
104
105                     unless ($page->is_virtual()) {
106                         $version = $page->{newest};
107                         $contents{version} = $version;
108                         my $source = $page->get_newest( 'source' );
109                         $archive = $page->get_newest( 'archive' );
110                         $contents{archive} = $archive;
111
112                         debug( "find source package: source=$source", 1) if DEBUG;
113                         my $src_data = $sources_all{"$archive $suite $source"};
114                         #FIXME: should be $main_archive or similar, not hardcoded "us"
115                         $src_data = $sources_all{"us $suite $source"} unless $src_data;
116                         $page->add_src_data( $source, $src_data )
117                             if $src_data;
118
119                         my $st1 = new Benchmark;
120                         my $std = timediff($st1, $st0);
121                         debug( "Data search and merging took ".timestr($std) ) if DEBUG;
122
123                         my $did = $page->get_newest( 'description' );
124                         my $desc_md5 = $page->get_newest( 'description-md5' );
125                         my @complete_tags = split(/, /, $page->get_newest( 'tag' ));
126                         my @tags;
127                         foreach (@complete_tags) {
128                             my ($facet, $tag) = split( /::/, $_, 2);
129                             next if $facet =~ /^special/;
130                             next if $tag =~ /^special:/;
131                             push @tags, [ $facet, $tag ];
132                         }
133
134                         $contents{tags} = \@tags;
135                         $contents{debtags_voc} = \%debtags;
136
137                         $section = $page->get_newest( 'section' );
138                         $contents{section} = $section;
139                         $subsection = $page->get_newest( 'subsection' );
140                         $contents{subsection} = $subsection;
141
142                         my $archives = $page->get_arch_field( 'archive' );
143                         my $versions = $page->get_arch_field( 'version' );
144                         my $sizes_inst = $page->get_arch_field( 'installed-size' );
145                         my $sizes_deb = $page->get_arch_field( 'size' );
146                         my @archs = sort $page->get_architectures;
147
148                         # process description
149                         #
150                         sub process_description {
151                             my ($desc) = @_;
152
153                             my $short_desc = encode_entities( $1, "<>&\"" )
154                                 if $desc =~ s/^(.*)$//m;
155                             my $long_desc = encode_entities( $desc, "<>&\"" );
156
157                             $long_desc =~ s,((ftp|http|https)://[\S~-]+?/?)((\&gt\;)?[)]?[']?[:.\,]?(\s|$)),<a href=\"$1\">$1</a>$3,go; # syntax highlighting -> '];
158                             $long_desc =~ s/\A //o;
159                             $long_desc =~ s/\n /\n/sgo;
160                             $long_desc =~ s/\n.\n/\n<p>\n/go;
161                             $long_desc =~ s/(((\n|\A) [^\n]*)+)/\n<pre>$1\n<\/pre>/sgo;
162
163                             return ($short_desc, $long_desc);
164                         }
165
166                         my $desc = $descriptions{$did};
167                         my $long_desc;
168                         ($short_desc, $long_desc) = process_description($desc);
169
170                         $contents{desc}{en} = { short => $short_desc,
171                                                 long => $long_desc, };
172
173                         debug( "desc_md5=$desc_md5", 2)
174                             if DEBUG;
175                         my $trans_desc = $desctrans{$desc_md5};
176                         if ($trans_desc) {
177                             my %trans_desc = split /\000|\001/, $trans_desc;
178                             debug( "TRANSLATIONS: ".join(" ",keys %trans_desc), 2)
179                                 if DEBUG;
180                             while (my ($l, $d) = each %trans_desc) {
181                                 my ($short_t, $long_t) = process_description($d);
182
183                                 $contents{desc}{$l} = { short => $short_t,
184                                                         long => $long_t, };
185                             }
186                         }
187
188                         my $v_str = $version;
189                         my $multiple_versions = grep { $_ ne $version } values %$versions;
190                         $v_str .= _g(" and others") if $multiple_versions;
191                         $contents{versions} = { short => $v_str,
192                                                 multiple => $multiple_versions };
193
194                         my $provided_by = $page->{provided_by};
195                         $contents{providers} = [];
196                         pkg_list( \%packages, $opts, $provided_by, 'en', $contents{providers} ) if $provided_by;
197
198                         #
199                         # display dependencies
200                         #
201                         build_deps( \%packages, $opts, $pkg,
202                                     $page->get_dep_field('pre-depends'),
203                                     'depends', \%contents );
204                         build_deps( \%packages, $opts, $pkg,
205                                     $page->get_dep_field('depends'),
206                                     'depends', \%contents );
207                         build_deps( \%packages, $opts, $pkg,
208                                     $page->get_dep_field('recommends'),
209                                     'recommends', \%contents );
210                         build_deps( \%packages, $opts, $pkg,
211                                     $page->get_dep_field('suggests'),
212                                     'suggests', \%contents );
213
214                         #
215                         # Download package
216                         #
217                         my @downloads;
218                         foreach my $a ( @archs ) {
219                             my %d = ( arch => $a,
220                                       pkgsize => sprintf( '%.1f', floor(($sizes_deb->{$a}/102.4)+0.5)/10 ),
221                                       instsize => $sizes_inst->{$a}, );
222
223                             $d{version} = $versions->{$a} if $multiple_versions;
224                             $d{archive} = $archives->{$a};
225                             if ( ($suite ne "experimental")
226                                  && ($subsection ne 'debian-installer')) {
227                                 $d{contents_avail} = 1;
228                             }
229                             push @downloads, \%d;
230                         }
231                         $contents{downloads} = \@downloads;
232
233                         #
234                         # more information
235                         #
236                         moreinfo( name => $pkg, data => $page, vars => \%contents,
237                                   opts => $opts,
238                                   env => \%FTP_SITES,
239                                   bugreports => 1, sourcedownload => 1,
240                                   changesandcopy => 1, maintainers => 1,
241                                   search => 1 );
242                     } else { # unless $page->is_virtual
243                         $contents{is_virtual} = 1;
244                         $contents{desc}{short} = _g( "virtual package" );
245                         $contents{subsection} = 'virtual';
246
247                         my $provided_by = $page->{provided_by};
248                         $contents{providers} = [];
249                         pkg_list( \%packages, $opts, $provided_by, 'en', $contents{providers} );
250
251                     } # else (unless $page->is_virtual)
252                 } else { # unless $opts->{source}
253                     $contents{is_source} = 1;
254
255                     for my $entry (@results) {
256                         debug( join(":", @$entry), 1 ) if DEBUG;
257                         my (undef, $archive, undef, $section, $subsection,
258                             $priority, $version) = @$entry;
259
260                         my $data = $sources_all{"$archive $suite $pkg"};
261                         $page->merge_data($pkg, $suite, $archive, $data)
262                             or debug( "Merging $pkg $version FAILED", 2 ) if DEBUG;
263                     }
264                     $version = $page->{version};
265                     $contents{version} = $version;
266
267                     my $st1 = new Benchmark;
268                     my $std = timediff($st1, $st0);
269                     debug( "Data search and merging took ".timestr($std) ) if DEBUG;
270
271                     $archive = $page->get_newest( 'archive' );
272                     $contents{archive} = $archive;
273                     $section = $page->get_newest( 'section' );
274                     $contents{section} = $section;
275                     $subsection = $page->get_newest( 'subsection' );
276                     $contents{subsection} = $subsection;
277
278                     my $binaries = find_binaries( $pkg, $archive, $suite, \%src2bin );
279                     if ($binaries && @$binaries) {
280                         $contents{binaries} = [];
281                         pkg_list( \%packages, $opts, $binaries, 'en', $contents{binaries} );
282                     }
283
284                     #
285                     # display dependencies
286                     #
287                     build_deps( \%packages, $opts, $pkg,
288                                 $page->get_dep_field('build-depends'),
289                                 'build-depends', \%contents );
290                     build_deps( \%packages, $opts, $pkg,
291                                 $page->get_dep_field('build-depends-indep'),
292                                 'build-depends-indep', \%contents );
293
294                     #
295                     # Source package download
296                     #
297                     my $source_files = $page->get_src( 'files' );
298                     my $source_dir = $page->get_src( 'directory' );
299
300                     $contents{srcfiles} = [];
301                     foreach( @$source_files ) {
302                         my ($src_file_md5, $src_file_size, $src_file_name)
303                             = split /\s+/, $_;
304                         (my $server = lc $archive) =~ s/-//go; # non-US hack
305                         $server = $FTP_SITES{$server}
306                             || $FTP_SITES{us};
307                         my $path = "/$source_dir/$src_file_name";
308
309                         push @{$contents{srcfiles}}, { server => $server, path => $path, filename => $src_file_name,
310                                                        size => sprintf("%.1f", (floor(($src_file_size/102.4)+0.5)/10)),
311                                                        md5sum => $src_file_md5 };
312                     }
313
314                     #
315                     # more information
316                     #
317                     moreinfo( name => $pkg, data => $page, vars => \%contents,
318                               opts => $opts,
319                               env => \%FTP_SITES,
320                               bugreports => 1,
321                               changesandcopy => 1, maintainers => 1,
322                               search => 1, is_source => 1 );
323
324                 } # else (unless $opts->{source})
325             } # else (unless @results)
326         } # else (unless (@results || @non_results ))
327     }
328
329 #    use Data::Dumper;
330 #    debug( "Final page object:\n".Dumper(\%contents), 3 ) if DEBUG;
331
332     %$page_contents = %contents;
333 }
334
335 sub moreinfo {
336     my %info = @_;
337     
338     my $name = $info{name} or return;
339     my $env = $info{env} or return;
340     my $opts = $info{opts} or return;
341     my $page = $info{data} or return;
342     my $contents = $info{vars} or return;
343     my $is_source = $info{is_source};
344     my $suite = $opts->{suite}[0];
345
346     my $source = $page->get_src( 'package' );
347     my $source_version = $page->get_src( 'version' );
348     my $src_dir = $page->get_src('directory');
349     if ($info{sourcedownload}) {
350         $contents->{src}{url} = make_url($source,'',{source=>'source'});
351         $contents->{src}{pkg} = $source;
352
353         my @downloads;
354         my $files = $page->get_src( 'files' );
355         if (defined($files) and @$files) {
356             foreach( @$files ) {
357                 my ($src_file_md5, $src_file_size, $src_file_name) = split /\s/o, $_;
358                 my ($server, $path);
359                 # non-US hack
360                 ($server = lc $page->get_newest('archive')) =~ s/-//go;
361                 $server = $env->{$server}||$env->{us};
362                 $path = "/$src_dir/$src_file_name";
363                 push @downloads, { name => $src_file_name, server => $server, path => $path };
364             }
365         }
366         $contents->{src}{downloads} = \@downloads;
367     }
368
369     if ($info{changesandcopy}) {
370         if ( $src_dir ) {
371             (my $src_basename = $source_version) =~ s,^\d+:,,; # strip epoche
372             $src_basename = "${source}_$src_basename";
373             $src_dir =~ s,pool/updates,pool,o;
374             $src_dir =~ s,pool/non-US,pool,o;
375
376             $contents->{files}{changelog}{path} = "$src_dir/$src_basename/changelog";
377             $contents->{files}{copyright}{path} = "$src_dir/$src_basename/".( $is_source ? 'copyright' : "$name.copyright" );
378         }
379    }
380
381     if ($info{maintainers}) {
382         my $uploaders = $page->get_src( 'uploaders' );
383         if ($uploaders && @$uploaders) {
384             my @maintainers = map { { name => $_->[0], mail => $_->[1] } } @$uploaders;
385             $contents->{maintainers} = \@maintainers;
386         }
387     }
388
389 }
390
391 sub providers {
392     my ($suite, $entry, $also) = @_;
393     my %tmp = map { $_ => 1 } split /\s/, $entry;
394     my @provided_by = keys %tmp; # weed out duplicates
395     my %out = ( also => $also,
396                 pkgs => \@provided_by );
397     return \%out;
398 }
399
400 sub build_deps {
401     my ( $packages, $opts, $pkg, $relations, $type, $contents) = @_;
402     my %dep_type = ('depends' => 'dep', 'recommends' => 'rec', 
403                     'suggests' => 'sug', 'build-depends' => 'adep',
404                     'build-depends-indep' => 'idep' );
405     my $suite = $opts->{suite}[0];
406
407     my %out = ( id => $dep_type{$type}, terms => [] );
408
409 #    use Data::Dumper;
410 #    debug( "print_deps called:\n".Dumper( $pkg, $relations, \$type ), 3 ) if DEBUG;
411
412     foreach my $rel (@$relations) {
413         my %rel_out;
414         $rel_out{is_old_pkgs} = $rel->[0];
415         $rel_out{alternatives} = [];
416
417         foreach my $rel_alt ( @$rel ) {
418             next unless ref($rel_alt);
419             my ( $p_name, $pkg_version, $arch_neg,
420                  $arch_str, $subsection, $available ) = @$rel_alt;
421
422             if ($arch_str ||= '') {
423                 if ($arch_neg) {
424                     $arch_str = _g("not")." $arch_str";
425                 } else {
426                     $arch_str = $arch_str;
427                 }
428             }
429
430             my %rel_alt_out = ( name => $p_name,
431                                 version => $pkg_version,
432                                 arch_str => $arch_str,
433                                 arch_neg => $arch_neg );
434                              
435             my @results;
436             my %entries;
437             my $entry = $entries{$p_name} ||
438                 read_entry_simple( $packages, $p_name, $opts->{h_archives}, $suite);
439             my $short_desc = $entry->[-1];
440             my $arch = $entry->[3];
441             my $archive = $entry->[1];
442             my $p_suite = $entry->[2];
443             if ( $short_desc ) {
444                 $rel_alt_out{desc} = $short_desc;
445                 $rel_alt_out{suite} = $p_suite;
446                 if ( $rel_out{is_old_pkgs} ) {
447                 } elsif (defined $entry->[1]) {
448                     $entries{$p_name} ||= $entry;
449                     $rel_alt_out{providers} = providers( $p_suite,
450                                                         $entry->[0],
451                                                         1 ) if defined $entry->[0];
452                 } elsif (defined $entry->[0]) {
453                     $rel_alt_out{desc} = undef;
454                     $rel_alt_out{providers} = providers( $p_suite,
455                                                         $entry->[0] );
456                     #FIXME: we don't handle virtual packages from
457                     # the fallback suite correctly here
458                     $rel_alt_out{suite} = $suite;
459                 }
460             } elsif ( $rel_out{is_old_pkgs} ) {
461             } else {
462                 $rel_alt_out{desc} = _g( "Package not available" );
463                 $rel_alt_out{suite} = '';
464             }
465             push @{$rel_out{alternatives}}, \%rel_alt_out;
466         }
467
468         push @{$out{terms}}, \%rel_out;
469     }
470
471     $contents->{relations} ||= [];
472     push @{$contents->{relations}}, \%out if @{$out{terms}};
473 } # end print_deps
474
475 sub pkg_list {
476     my ( $packages, $opts, $pkgs, $lang, $list ) = @_;
477     my $suite = $opts->{suite}[0];
478
479     foreach my $p ( sort @$pkgs ) {
480
481         # we don't deal with virtual packages here because for the
482         # current uses of this function this isn't needed
483         my $short_desc = (read_entry_simple( $packages, $p, $opts->{h_archives}, $suite))->[-1];
484
485         if ( $short_desc ) {
486             push @$list, { name => $p, desc => $short_desc, available => 1 };
487         } else {
488             push @$list, { name => $p, desc => _g("Not available") };
489         }
490     }
491 }
492
493
494 1;
495