]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoShow.pm
bd0dc216f82f3f40cd1b6085b8ca5620c553f82e
[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 );
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, $html_header, $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 @complete_tags = split(/, /, $page->get_newest( 'tag' ));
125                         my @tags;
126                         foreach (@complete_tags) {
127                             my ($facet, $tag) = split( /::/, $_, 2);
128                             # handle tags like devel::{lang:c,lang:c++}
129                             if ($tag =~ s/^\{(.+)\}$/$1/) {
130                                 foreach (split( /,/, $tag )) {
131                                     next if $tag =~ /^special:/;
132                                     push @tags, [ $facet, $_ ];
133                                 }
134                             } else {
135                                 next if $tag =~ /^special:/;
136                                 push @tags, [ $facet, $tag ];
137                             }
138                         }
139
140                         $contents{tags} = \@tags;
141                         $contents{debtags_voc} = \%debtags;
142
143                         $section = $page->get_newest( 'section' );
144                         $contents{section} = $section;
145                         $subsection = $page->get_newest( 'subsection' );
146                         $contents{subsection} = $subsection;
147
148                         my $archives = $page->get_arch_field( 'archive' );
149                         my $versions = $page->get_arch_field( 'version' );
150                         my $sizes_inst = $page->get_arch_field( 'installed-size' );
151                         my $sizes_deb = $page->get_arch_field( 'size' );
152                         my @archs = sort $page->get_architectures;
153
154                         # process description
155                         #
156                         my $desc = $descriptions{$did};
157                         $short_desc = encode_entities( $1, "<>&\"" )
158                             if $desc =~ s/^(.*)$//m;
159                         my $long_desc = encode_entities( $desc, "<>&\"" );
160
161                         $long_desc =~ s,((ftp|http|https)://[\S~-]+?/?)((\&gt\;)?[)]?[']?[:.\,]?(\s|$)),<a href=\"$1\">$1</a>$3,go; # syntax highlighting -> '];
162                         $long_desc =~ s/\A //o;
163                         $long_desc =~ s/\n /\n/sgo;
164                         $long_desc =~ s/\n.\n/\n<p>\n/go;
165                         $long_desc =~ s/(((\n|\A) [^\n]*)+)/\n<pre>$1\n<\/pre>/sgo;
166
167                         $contents{desc} = { short => $short_desc,
168                                             long => $long_desc, };
169
170                         my $v_str = $version;
171                         my $multiple_versions = grep { $_ ne $version } values %$versions;
172                         $v_str .= _g(" and others") if $multiple_versions;
173                         $contents{versions} = { short => $v_str,
174                                                 multiple => $multiple_versions };
175
176                         my $provided_by = $page->{provided_by};
177                         $contents{providers} = [];
178                         pkg_list( \%packages, $opts, $provided_by, 'en', $contents{providers} ) if $provided_by;
179
180                         #
181                         # display dependencies
182                         #
183                         build_deps( \%packages, $opts, $pkg,
184                                     $page->get_dep_field('pre-depends'),
185                                     'depends', \%contents );
186                         build_deps( \%packages, $opts, $pkg,
187                                     $page->get_dep_field('depends'),
188                                     'depends', \%contents );
189                         build_deps( \%packages, $opts, $pkg,
190                                     $page->get_dep_field('recommends'),
191                                     'recommends', \%contents );
192                         build_deps( \%packages, $opts, $pkg,
193                                     $page->get_dep_field('suggests'),
194                                     'suggests', \%contents );
195
196                         #
197                         # Download package
198                         #
199                         my @downloads;
200                         foreach my $a ( @archs ) {
201                             my %d = ( arch => $a,
202                                       pkgsize => sprintf( '%.1f', floor(($sizes_deb->{$a}/102.4)+0.5)/10 ),
203                                       instsize => $sizes_inst->{$a}, );
204
205                             $d{version} = $versions->{$a} if $multiple_versions;
206                             $d{archive} = $archives->{$a};
207                             if ( ($suite ne "experimental")
208                                  && ($subsection ne 'debian-installer')) {
209                                 $d{contents_avail} = 1;
210                             }
211                             push @downloads, \%d;
212                         }
213                         $contents{downloads} = \@downloads;
214
215                         #
216                         # more information
217                         #
218                         moreinfo( name => $pkg, data => $page, vars => \%contents,
219                                   opts => $opts,
220                                   env => \%FTP_SITES,
221                                   bugreports => 1, sourcedownload => 1,
222                                   changesandcopy => 1, maintainers => 1,
223                                   search => 1 );
224                     } else { # unless $page->is_virtual
225                         $contents{is_virtual} = 1;
226                         $contents{desc}{short} = _g( "virtual package" );
227                         $contents{subsection} = 'virtual';
228
229                         my $provided_by = $page->{provided_by};
230                         $contents{providers} = [];
231                         pkg_list( \%packages, $opts, $provided_by, 'en', $contents{providers} );
232
233                     } # else (unless $page->is_virtual)
234                 } else { # unless $opts->{source}
235                     $contents{is_source} = 1;
236
237                     for my $entry (@results) {
238                         debug( join(":", @$entry), 1 ) if DEBUG;
239                         my (undef, $archive, undef, $section, $subsection,
240                             $priority, $version) = @$entry;
241
242                         my $data = $sources_all{"$archive $suite $pkg"};
243                         $page->merge_data($pkg, $suite, $archive, $data)
244                             or debug( "Merging $pkg $version FAILED", 2 ) if DEBUG;
245                     }
246                     $version = $page->{version};
247                     $contents{version} = $version;
248
249                     my $st1 = new Benchmark;
250                     my $std = timediff($st1, $st0);
251                     debug( "Data search and merging took ".timestr($std) ) if DEBUG;
252
253                     $archive = $page->get_newest( 'archive' );
254                     $contents{archive} = $archive;
255                     $section = $page->get_newest( 'section' );
256                     $contents{section} = $section;
257                     $subsection = $page->get_newest( 'subsection' );
258                     $contents{subsection} = $subsection;
259
260                     my $binaries = find_binaries( $pkg, $archive, $suite, \%src2bin );
261                     if ($binaries && @$binaries) {
262                         $contents{binaries} = [];
263                         pkg_list( \%packages, $opts, $binaries, 'en', $contents{binaries} );
264                     }
265
266                     #
267                     # display dependencies
268                     #
269                     build_deps( \%packages, $opts, $pkg,
270                                 $page->get_dep_field('build-depends'),
271                                 'build-depends', \%contents );
272                     build_deps( \%packages, $opts, $pkg,
273                                 $page->get_dep_field('build-depends-indep'),
274                                 'build-depends-indep', \%contents );
275
276                     #
277                     # Source package download
278                     #
279                     my $source_files = $page->get_src( 'files' );
280                     my $source_dir = $page->get_src( 'directory' );
281
282                     $contents{srcfiles} = [];
283                     foreach( @$source_files ) {
284                         my ($src_file_md5, $src_file_size, $src_file_name)
285                             = split /\s+/, $_;
286                         (my $server = lc $archive) =~ s/-//go; # non-US hack
287                         $server = $FTP_SITES{$server}
288                             || $FTP_SITES{us};
289                         my $path = "/$source_dir/$src_file_name";
290
291                         push @{$contents{srcfiles}}, { server => $server, path => $path, filename => $src_file_name,
292                                                        size => sprintf("%.1f", (floor(($src_file_size/102.4)+0.5)/10)),
293                                                        md5sum => $src_file_md5 };
294                     }
295
296                     #
297                     # more information
298                     #
299                     moreinfo( name => $pkg, data => $page, vars => \%contents,
300                               opts => $opts,
301                               env => \%FTP_SITES,
302                               bugreports => 1,
303                               changesandcopy => 1, maintainers => 1,
304                               search => 1, is_source => 1 );
305
306                 } # else (unless $opts->{source})
307             } # else (unless @results)
308         } # else (unless (@results || @non_results ))
309     }
310
311 #    use Data::Dumper;
312 #    debug( "Final page object:\n".Dumper(\%contents), 3 ) if DEBUG;
313
314     %$page_contents = %contents;
315 }
316
317 sub moreinfo {
318     my %info = @_;
319     
320     my $name = $info{name} or return;
321     my $env = $info{env} or return;
322     my $opts = $info{opts} or return;
323     my $page = $info{data} or return;
324     my $contents = $info{vars} or return;
325     my $is_source = $info{is_source};
326     my $suite = $opts->{suite}[0];
327
328     my $source = $page->get_src( 'package' );
329     my $source_version = $page->get_src( 'version' );
330     my $src_dir = $page->get_src('directory');
331     if ($info{sourcedownload}) {
332         $contents->{src}{url} = make_url($source,'',{source=>'source'});
333         $contents->{src}{pkg} = $source;
334
335         my @downloads;
336         my $files = $page->get_src( 'files' );
337         if (defined($files) and @$files) {
338             foreach( @$files ) {
339                 my ($src_file_md5, $src_file_size, $src_file_name) = split /\s/o, $_;
340                 my ($name, $server, $path);
341                 # non-US hack
342                 ($server = lc $page->get_newest('archive')) =~ s/-//go;
343                 $server = $env->{$server}||$env->{us};
344                 $path = "/$src_dir/$src_file_name";
345                 if ($src_file_name =~ /dsc$/) {
346                     $name = 'dsc'
347                 } else {
348                     $name = $src_file_name;
349                 }
350                 push @downloads, { name => $name, server => $server, path => $path };
351             }
352         }
353         $contents->{src}{downloads} = \@downloads;
354     }
355
356     if ($info{changesandcopy}) {
357         if ( $src_dir ) {
358             (my $src_basename = $source_version) =~ s,^\d+:,,; # strip epoche
359             $src_basename = "${source}_$src_basename";
360             $src_dir =~ s,pool/updates,pool,o;
361             $src_dir =~ s,pool/non-US,pool,o;
362
363             $contents->{files}{changelog}{path} = "$src_dir/$src_basename/changelog";
364             $contents->{files}{copyright}{path} = "$src_dir/$src_basename/".( $is_source ? 'copyright' : "$name.copyright" );
365         }
366    }
367
368     if ($info{maintainers}) {
369         my $uploaders = $page->get_src( 'uploaders' );
370         if ($uploaders && @$uploaders) {
371             my @maintainers = map { { name => $_->[0], mail => $_->[1] } } @$uploaders;
372             $contents->{maintainers} = \@maintainers;
373         }
374     }
375
376 }
377
378 sub providers {
379     my ($suite, $entry, $also) = @_;
380     my %tmp = map { $_ => 1 } split /\s/, $entry;
381     my @provided_by = keys %tmp; # weed out duplicates
382     my %out = ( also => $also,
383                 pkgs => \@provided_by );
384     return \%out;
385 }
386
387 sub build_deps {
388     my ( $packages, $opts, $pkg, $relations, $type, $contents) = @_;
389     my %dep_type = ('depends' => 'dep', 'recommends' => 'rec', 
390                     'suggests' => 'sug', 'build-depends' => 'adep',
391                     'build-depends-indep' => 'idep' );
392     my $suite = $opts->{suite}[0];
393
394     my %out = ( id => $dep_type{$type}, terms => [] );
395
396 #    use Data::Dumper;
397 #    debug( "print_deps called:\n".Dumper( $pkg, $relations, \$type ), 3 ) if DEBUG;
398
399     foreach my $rel (@$relations) {
400         my %rel_out;
401         $rel_out{is_old_pkgs} = $rel->[0];
402         $rel_out{alternatives} = [];
403
404         foreach my $rel_alt ( @$rel ) {
405             next unless ref($rel_alt);
406             my ( $p_name, $pkg_version, $arch_neg,
407                  $arch_str, $subsection, $available ) = @$rel_alt;
408
409             if ($arch_str ||= '') {
410                 if ($arch_neg) {
411                     $arch_str = _g("not")." $arch_str";
412                 } else {
413                     $arch_str = $arch_str;
414                 }
415             }
416
417             my %rel_alt_out = ( name => $p_name,
418                                 version => $pkg_version,
419                                 arch_str => $arch_str,
420                                 arch_neg => $arch_neg );
421                              
422             my @results;
423             my %entries;
424             my $entry = $entries{$p_name} ||
425                 read_entry_simple( $packages, $p_name, $opts->{h_archives}, $suite);
426             my $short_desc = $entry->[-1];
427             my $arch = $entry->[3];
428             my $archive = $entry->[1];
429             my $p_suite = $entry->[2];
430             if ( $short_desc ) {
431                 $rel_alt_out{desc} = $short_desc;
432                 $rel_alt_out{suite} = $p_suite;
433                 if ( $rel_out{is_old_pkgs} ) {
434                 } elsif (defined $entry->[1]) {
435                     $entries{$p_name} ||= $entry;
436                     $rel_alt_out{providers} = providers( $p_suite,
437                                                         $entry->[0],
438                                                         1 ) if defined $entry->[0];
439                 } elsif (defined $entry->[0]) {
440                     $rel_alt_out{desc} = undef;
441                     $rel_alt_out{providers} = providers( $p_suite,
442                                                         $entry->[0] );
443                     #FIXME: we don't handle virtual packages from
444                     # the fallback suite correctly here
445                     $rel_alt_out{suite} = $suite;
446                 }
447             } elsif ( $rel_out{is_old_pkgs} ) {
448             } else {
449                 $rel_alt_out{desc} = _g( "Package not available" );
450                 $rel_alt_out{suite} = '';
451             }
452             push @{$rel_out{alternatives}}, \%rel_alt_out;
453         }
454
455         push @{$out{terms}}, \%rel_out;
456     }
457
458     $contents->{relations} ||= [];
459     push @{$contents->{relations}}, \%out if @{$out{terms}};
460 } # end print_deps
461
462 sub pkg_list {
463     my ( $packages, $opts, $pkgs, $lang, $list ) = @_;
464     my $suite = $opts->{suite}[0];
465
466     foreach my $p ( sort @$pkgs ) {
467
468         # we don't deal with virtual packages here because for the
469         # current uses of this function this isn't needed
470         my $short_desc = (read_entry_simple( $packages, $p, $opts->{h_archives}, $suite))->[-1];
471
472         if ( $short_desc ) {
473             push @$list, { name => $p, desc => $short_desc, available => 1 };
474         } else {
475             push @$list, { name => $p, desc => _g("Not available") };
476         }
477     }
478 }
479
480
481 1;
482