1 package Packages::DoShow;
10 use Benchmark ':hireswallclock';
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 );
19 use Packages::Search qw( :all );
20 use Packages::Page ();
21 use Packages::SrcPage ();
23 our @ISA = qw( Exporter );
24 our @EXPORT = qw( do_show );
27 my ($params, $opts, $page_contents) = @_;
29 if ($params->{errors}{package}) {
30 fatal_error( _g( "package not valid or not specified" ) );
32 if ($params->{errors}{suite}) {
33 fatal_error( _g( "suite not valid or not specified" ) );
35 if (@{$opts->{suite}} > 1) {
36 fatal_error( sprintf( _g( "more than one suite specified for show (%s)" ), "@{$opts->{suite}}" ) );
40 $contents{make_url} = sub { return &Packages::CGI::make_url(@_) };
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] ||'';
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;
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: $!";
64 unless ($opts->{source}) {
65 read_entry_all( \%packages, $pkg, \@results, \@non_results, $opts );
67 read_src_entry_all( \%sources, $pkg, \@results, \@non_results, $opts );
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)) ) );
75 foreach (@results, @non_results) {
80 $contents{suites} = [ suites_sort(keys %all_suites) ];
83 fatal_error( _g( "Package not available in this suite." ) );
85 $contents{page} = $page;
86 unless ($opts->{source}) {
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;
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;
101 $page->add_provided_by([split /\s+/, $provided_by]);
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;
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 )
119 my $st1 = new Benchmark;
120 my $std = timediff($st1, $st0);
121 debug( "Data search and merging took ".timestr($std) ) if DEBUG;
123 my @similar = find_similar( $pkg, "$DBDIR/xapian/",
125 $contents{similar} = \@similar;
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' ));
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 ];
138 $contents{tags} = \@tags;
139 $contents{debtags_voc} = \%debtags;
141 $section = $page->get_newest( 'section' );
142 $contents{section} = $section;
143 $subsection = $page->get_newest( 'subsection' );
144 $contents{subsection} = $subsection;
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;
152 # process description
154 sub process_description {
157 my $short_desc = encode_entities( $1, "<>&\"" )
158 if $desc =~ s/^(.*)$//m;
159 my $long_desc = encode_entities( $desc, "<>&\"" );
161 $long_desc =~ s,((ftp|http|https)://[\S~-]+?/?)((\>\;)?[)]?[']?[:.\,]?(\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;
167 return ($short_desc, $long_desc);
170 my $desc = $descriptions{$did};
172 ($short_desc, $long_desc) = process_description($desc);
174 $contents{desc}{en} = { short => $short_desc,
175 long => $long_desc, };
177 debug( "desc_md5=$desc_md5", 2)
179 my $trans_desc = $desctrans{$desc_md5};
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)
185 while (my ($l, $d) = each %trans_desc) {
186 my ($short_t, $long_t) = process_description($d);
188 $contents{desc}{$l} = { short => $short_t,
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 };
199 my $provided_by = $page->{provided_by};
200 $contents{providers} = [];
201 pkg_list( \%packages, $opts, $provided_by, 'en', $contents{providers} ) if $provided_by;
204 # display dependencies
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 );
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}, );
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;
234 push @downloads, \%d;
236 $contents{downloads} = \@downloads;
241 moreinfo( name => $pkg, data => $page, vars => \%contents,
244 bugreports => 1, sourcedownload => 1,
245 changesandcopy => 1, maintainers => 1,
247 } else { # unless $page->is_virtual
248 $contents{is_virtual} = 1;
249 $contents{desc}{short} = _g( "virtual package" );
250 $contents{subsection} = 'virtual';
252 my $provided_by = $page->{provided_by};
253 $contents{providers} = [];
254 pkg_list( \%packages, $opts, $provided_by, 'en', $contents{providers} );
256 } # else (unless $page->is_virtual)
257 } else { # unless $opts->{source}
258 $contents{is_source} = 1;
260 for my $entry (@results) {
261 debug( join(":", @$entry), 1 ) if DEBUG;
262 my (undef, $archive, undef, $section, $subsection,
263 $priority, $version) = @$entry;
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;
269 $version = $page->{version};
270 $contents{version} = $version;
272 my $st1 = new Benchmark;
273 my $std = timediff($st1, $st0);
274 debug( "Data search and merging took ".timestr($std) ) if DEBUG;
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;
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} );
290 # display dependencies
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 );
300 # Source package download
302 my $source_files = $page->get_src( 'files' );
303 my $source_dir = $page->get_src( 'directory' );
305 $contents{srcfiles} = [];
306 foreach( @$source_files ) {
307 my ($src_file_md5, $src_file_size, $src_file_name)
309 (my $server = lc $archive) =~ s/-//go; # non-US hack
310 $server = $FTP_SITES{$server}
312 my $path = "/$source_dir/$src_file_name";
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 };
322 moreinfo( name => $pkg, data => $page, vars => \%contents,
326 changesandcopy => 1, maintainers => 1,
327 search => 1, is_source => 1 );
329 } # else (unless $opts->{source})
330 } # else (unless @results)
331 } # else (unless (@results || @non_results ))
335 # debug( "Final page object:\n".Dumper(\%contents), 3 ) if DEBUG;
337 %$page_contents = %contents;
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];
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;
359 my $files = $page->get_src( 'files' );
360 if (defined($files) and @$files) {
362 my ($src_file_md5, $src_file_size, $src_file_name) = split /\s/o, $_;
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 };
371 $contents->{src}{downloads} = \@downloads;
374 if ($info{changesandcopy}) {
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;
381 $contents->{files}{changelog}{path} = "$src_dir/$src_basename/changelog";
382 $contents->{files}{copyright}{path} = "$src_dir/$src_basename/".( $is_source ? 'copyright' : "$name.copyright" );
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;
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 );
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];
412 my %out = ( id => $dep_type{$type}, terms => [] );
415 # debug( "print_deps called:\n".Dumper( $pkg, $relations, \$type ), 3 ) if DEBUG;
417 foreach my $rel (@$relations) {
419 $rel_out{is_old_pkgs} = $rel->[0];
420 $rel_out{alternatives} = [];
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;
427 if ($arch_str ||= '') {
429 $arch_str = _g("not")." $arch_str";
431 $arch_str = $arch_str;
435 my %rel_alt_out = ( name => $p_name,
436 version => $pkg_version,
437 arch_str => $arch_str,
438 arch_neg => $arch_neg );
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];
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,
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,
461 #FIXME: we don't handle virtual packages from
462 # the fallback suite correctly here
463 $rel_alt_out{suite} = $suite;
465 } elsif ( $rel_out{is_old_pkgs} ) {
467 $rel_alt_out{desc} = _g( "Package not available" );
468 $rel_alt_out{suite} = '';
470 push @{$rel_out{alternatives}}, \%rel_alt_out;
473 push @{$out{terms}}, \%rel_out;
476 $contents->{relations} ||= [];
477 push @{$contents->{relations}}, \%out if @{$out{terms}};
481 my ( $packages, $opts, $pkgs, $lang, $list ) = @_;
482 my $suite = $opts->{suite}[0];
484 foreach my $p ( sort @$pkgs ) {
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];
491 push @$list, { name => $p, desc => $short_desc, available => 1 };
493 push @$list, { name => $p, desc => _g("Not available") };