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