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