]> git.deb.at Git - deb/packages.git/blob - lib/Packages/DoShow.pm
Merge commit 'origin/master' into debian-master
[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 @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 = lc $archive) =~ s/-//go; # non-US hack
310                         $server = $FTP_SITES{$server}
311                             || $FTP_SITES{us};
312                         my $path = "/$source_dir/$src_file_name";
313
314                         push @{$contents{srcfiles}}, { server => $server, path => $path, filename => $src_file_name,
315                                                        size => sprintf("%.1f", (floor(($src_file_size/102.4)+0.5)/10)),
316                                                        md5sum => $src_file_md5 };
317                     }
318
319                     #
320                     # more information
321                     #
322                     moreinfo( name => $pkg, data => $page, vars => \%contents,
323                               opts => $opts,
324                               env => \%FTP_SITES,
325                               bugreports => 1,
326                               changesandcopy => 1, maintainers => 1,
327                               search => 1, is_source => 1 );
328
329                 } # else (unless $opts->{source})
330             } # else (unless @results)
331         } # else (unless (@results || @non_results ))
332     }
333
334 #    use Data::Dumper;
335 #    debug( "Final page object:\n".Dumper(\%contents), 3 ) if DEBUG;
336
337     %$page_contents = %contents;
338 }
339
340 sub moreinfo {
341     my %info = @_;
342     
343     my $name = $info{name} or return;
344     my $env = $info{env} or return;
345     my $opts = $info{opts} or return;
346     my $page = $info{data} or return;
347     my $contents = $info{vars} or return;
348     my $is_source = $info{is_source};
349     my $suite = $opts->{suite}[0];
350
351     my $source = $page->get_src( 'package' );
352     my $source_version = $page->get_src( 'version' );
353     my $src_dir = $page->get_src('directory');
354     if ($info{sourcedownload}) {
355         $contents->{src}{url} = make_url($source,'',{source=>'source'});
356         $contents->{src}{pkg} = $source;
357
358         my @downloads;
359         my $files = $page->get_src( 'files' );
360         if (defined($files) and @$files) {
361             foreach( @$files ) {
362                 my ($src_file_md5, $src_file_size, $src_file_name) = split /\s/o, $_;
363                 my ($server, $path);
364                 # non-US hack
365                 ($server = lc $page->get_newest('archive')) =~ s/-//go;
366                 $server = $env->{$server}||$env->{us};
367                 $path = "/$src_dir/$src_file_name";
368                 push @downloads, { name => $src_file_name, server => $server, path => $path };
369             }
370         }
371         $contents->{src}{downloads} = \@downloads;
372     }
373
374     if ($info{changesandcopy}) {
375         if ( $src_dir ) {
376             (my $src_basename = $source_version) =~ s,^\d+:,,; # strip epoche
377             $src_basename = "${source}_$src_basename";
378             $src_dir =~ s,pool/updates,pool,o;
379             $src_dir =~ s,pool/non-US,pool,o;
380
381             $contents->{files}{changelog}{path} = "$src_dir/$src_basename/changelog";
382             $contents->{files}{copyright}{path} = "$src_dir/$src_basename/".( $is_source ? 'copyright' : "$name.copyright" );
383         }
384    }
385
386     if ($info{maintainers}) {
387         my $uploaders = $page->get_src( 'uploaders' );
388         if ($uploaders && @$uploaders) {
389             my @maintainers = map { { name => $_->[0], mail => $_->[1] } } @$uploaders;
390             $contents->{maintainers} = \@maintainers;
391         }
392     }
393
394 }
395
396 sub providers {
397     my ($suite, $entry, $also) = @_;
398     my %tmp = map { $_ => 1 } split /\s/, $entry;
399     my @provided_by = keys %tmp; # weed out duplicates
400     my %out = ( also => $also,
401                 pkgs => \@provided_by );
402     return \%out;
403 }
404
405 sub build_deps {
406     my ( $packages, $opts, $pkg, $relations, $type, $contents) = @_;
407     my %dep_type = ('depends' => 'dep', 'recommends' => 'rec', 
408                     'suggests' => 'sug', 'build-depends' => 'adep',
409                     'build-depends-indep' => 'idep' );
410     my $suite = $opts->{suite}[0];
411
412     my %out = ( id => $dep_type{$type}, terms => [] );
413
414 #    use Data::Dumper;
415 #    debug( "print_deps called:\n".Dumper( $pkg, $relations, \$type ), 3 ) if DEBUG;
416
417     foreach my $rel (@$relations) {
418         my %rel_out;
419         $rel_out{is_old_pkgs} = $rel->[0];
420         $rel_out{alternatives} = [];
421
422         foreach my $rel_alt ( @$rel ) {
423             next unless ref($rel_alt);
424             my ( $p_name, $pkg_version, $arch_neg,
425                  $arch_str, $subsection, $available ) = @$rel_alt;
426
427             if ($arch_str ||= '') {
428                 if ($arch_neg) {
429                     $arch_str = _g("not")." $arch_str";
430                 } else {
431                     $arch_str = $arch_str;
432                 }
433             }
434
435             my %rel_alt_out = ( name => $p_name,
436                                 version => $pkg_version,
437                                 arch_str => $arch_str,
438                                 arch_neg => $arch_neg );
439                              
440             my @results;
441             my %entries;
442             my $entry = $entries{$p_name} ||
443                 read_entry_simple( $packages, $p_name, $opts->{h_archives}, $suite);
444             my $short_desc = $entry->[-1];
445             my $arch = $entry->[3];
446             my $archive = $entry->[1];
447             my $p_suite = $entry->[2];
448             if ( $short_desc ) {
449                 $rel_alt_out{desc} = $short_desc;
450                 $rel_alt_out{suite} = $p_suite;
451                 if ( $rel_out{is_old_pkgs} ) {
452                 } elsif (defined $entry->[1]) {
453                     $entries{$p_name} ||= $entry;
454                     $rel_alt_out{providers} = providers( $p_suite,
455                                                         $entry->[0],
456                                                         1 ) if defined $entry->[0];
457                 } elsif (defined $entry->[0]) {
458                     $rel_alt_out{desc} = undef;
459                     $rel_alt_out{providers} = providers( $p_suite,
460                                                         $entry->[0] );
461                     #FIXME: we don't handle virtual packages from
462                     # the fallback suite correctly here
463                     $rel_alt_out{suite} = $suite;
464                 }
465             } elsif ( $rel_out{is_old_pkgs} ) {
466             } else {
467                 $rel_alt_out{desc} = _g( "Package not available" );
468                 $rel_alt_out{suite} = '';
469             }
470             push @{$rel_out{alternatives}}, \%rel_alt_out;
471         }
472
473         push @{$out{terms}}, \%rel_out;
474     }
475
476     $contents->{relations} ||= [];
477     push @{$contents->{relations}}, \%out if @{$out{terms}};
478 } # end print_deps
479
480 sub pkg_list {
481     my ( $packages, $opts, $pkgs, $lang, $list ) = @_;
482     my $suite = $opts->{suite}[0];
483
484     foreach my $p ( sort @$pkgs ) {
485
486         # we don't deal with virtual packages here because for the
487         # current uses of this function this isn't needed
488         my $short_desc = (read_entry_simple( $packages, $p, $opts->{h_archives}, $suite))->[-1];
489
490         if ( $short_desc ) {
491             push @$list, { name => $p, desc => $short_desc, available => 1 };
492         } else {
493             push @$list, { name => $p, desc => _g("Not available") };
494         }
495     }
496 }
497
498
499 1;
500