]> git.deb.at Git - deb/packages.git/blob - cgi-bin/search_packages.pl
7aa7ffdcfa27122b7a678b81033afb89b1c02ba8
[deb/packages.git] / cgi-bin / search_packages.pl
1 #!/usr/bin/perl -wT
2 #
3 # search_packages.pl -- CGI interface to the Packages files on packages.debian.org
4 #
5 # Copyright (C) 1998 James Treacy
6 # Copyright (C) 2000, 2001 Josip Rodin
7 # Copyright (C) 2001 Adam Heath
8 # Copyright (C) 2004 Martin Schulze
9 # Copyright (C) 2004-2006 Frank Lichtenheld
10 #
11 # use is allowed under the terms of the GNU Public License (GPL)                              
12 # see http://www.fsf.org/copyleft/gpl.html for a copy of the license
13
14 use strict;
15 use CGI qw( -oldstyle_urls );
16 use CGI::Carp qw( fatalsToBrowser );
17 use POSIX;
18 use URI::Escape;
19 use HTML::Entities;
20 use DB_File;
21 use Benchmark;
22
23 use lib "../lib";
24
25 use Deb::Versions;
26 use Packages::Search qw( :all );
27 use Packages::HTML ();
28
29 my $thisscript = "search_packages.pl";
30 my $HOME = "http://www.debian.org";
31 my $ROOT = "";
32 my $SEARCHPAGE = "http://packages.debian.org/";
33 my @SUITES = qw( oldstable stable testing unstable experimental );
34 my @DISTS = @SUITES;
35 my @SECTIONS = qw( main contrib non-free );
36 my @ARCHIVES = qw( us security installer );
37 my @ARCHITECTURES = qw( alpha amd64 arm hppa hurd-i386 i386 ia64
38                         kfreebsd-i386 mips mipsel powerpc s390 sparc );
39 my %SUITES = map { $_ => 1 } @SUITES;
40 my %SECTIONS = map { $_ => 1 } @SECTIONS;
41 my %ARCHIVES = map { $_ => 1 } @ARCHIVES;
42 my %ARCHITECTURES = map { $_ => 1 } @ARCHITECTURES;
43
44 $ENV{PATH} = "/bin:/usr/bin";
45
46 # Read in all the variables set by the form
47 my $input = new CGI;
48
49 my $pet0 = new Benchmark;
50 # use this to disable debugging in production mode completly
51 my $debug_allowed = 1;
52 my $debug = $debug_allowed && $input->param("debug");
53 $Search::Param::debug = 1 if $debug > 1;
54
55 # If you want, just print out a list of all of the variables and exit.
56 print $input->header if $debug;
57 # print $input->dump;
58 # exit;
59
60 if (my $path = $input->param('path')) {
61     my @components = map { lc $_ } split /\//, $path;
62
63     foreach (@components) {
64         if ($SUITES{$_}) {
65             $input->param('suite', $_);
66         } elsif ($SECTIONS{$_}) {
67             $input->param('section', $_);
68         } elsif ($ARCHIVES{$_}) {
69             $input->param('archive', $_);
70         }elsif ($ARCHITECTURES{$_}) {
71             $input->param('arch', $_);
72         }
73     }
74 }
75
76 my %params_def = ( keywords => { default => undef, match => '^\s*([-+\@\w\/.:]+)\s*$' },
77                    suite => { default => 'stable', match => '^(\w+)$',
78                               alias => 'version', array => ',',
79                               replace => { all => \@SUITES } },
80                    case => { default => 'insensitive', match => '^(\w+)$' },
81                    official => { default => 0, match => '^(\w+)$' },
82                    use_cache => { default => 1, match => '^(\w+)$' },
83                    subword => { default => 0, match => '^(\w+)$' },
84                    exact => { default => undef, match => '^(\w+)$' },
85                    searchon => { default => 'all', match => '^(\w+)$' },
86                    section => { default => 'all', match => '^([\w-]+)$',
87                                 alias => 'release', array => ',',
88                                 replace => { all => \@SECTIONS } },
89                    arch => { default => 'any', match => '^(\w+)$',
90                              array => ',', replace =>
91                              { any => \@ARCHITECTURES } },
92                    archive => { default => 'all', match => '^(\w+)$',
93                                 array => ',', replace =>
94                                 { all => \@ARCHIVES } },
95                    format => { default => 'html', match => '^(\w+)$' },
96                    );
97 my %params = Packages::Search::parse_params( $input, \%params_def );
98
99 my $format = $params{values}{format}{final};
100 #XXX: Don't use alternative output formats yet
101 $format = 'html';
102
103 if ($format eq 'html') {
104     print $input->header;
105 } elsif ($format eq 'xml') {
106 #    print $input->header( -type=>'application/rdf+xml' );
107     print $input->header( -type=>'text/plain' );
108 }
109
110 if ($params{errors}{keywords}) {
111     print "Error: keyword not valid or missing" if $format eq 'html';
112     exit 0;
113 }
114 my $keyword = $params{values}{keywords}{final};
115 my @suites = @{$params{values}{suite}{final}};
116 my $official = $params{values}{official}{final};
117 my $use_cache = $params{values}{use_cache}{final};
118 my $case = $params{values}{case}{final};
119 my $case_bool = ( $case !~ /insensitive/ );
120 my $subword = $params{values}{subword}{final};
121 my $exact = $params{values}{exact}{final};
122 $exact = !$subword unless defined $exact;
123 my $searchon = $params{values}{searchon}{final};
124 my @sections = @{$params{values}{section}{final}};
125 my @archs = @{$params{values}{arch}{final}};
126 my $page = $params{values}{page}{final};
127 my $results_per_page = $params{values}{number}{final};
128
129 # for URL construction
130 my $suites_param = join ',', @{$params{values}{suite}{no_replace}};
131 my $sections_param = join ',', @{$params{values}{section}{no_replace}};
132 my $archs_param = join ',', @{$params{values}{arch}{no_replace}};
133
134 # for output
135 my $keyword_enc = encode_entities $keyword;
136 my $searchon_enc = encode_entities $searchon;
137 my $suites_enc = encode_entities join ', ', @{$params{values}{suite}{no_replace}};
138 my $sections_enc = encode_entities join ', ', @{$params{values}{section}{no_replace}};
139 my $archs_enc = encode_entities join ', ',  @{$params{values}{arch}{no_replace}};
140 my $pet1 = new Benchmark;
141 my $petd = timediff($pet1, $pet0);
142 print "DEBUG: Parameter evaluation took ".timestr($petd)."<br>" if $debug;
143
144 if ($format eq 'html') {
145 print Packages::HTML::header( title => 'Package Search Results' ,
146                               lang => 'en',
147                               title_tag => 'Debian Package Search Results',
148                               print_title_above => 1,
149                               print_search_field => 'packages',
150                               search_field_values => { 
151                                   keywords => $keyword_enc,
152                                   searchon => $searchon,
153                                   arch => $archs_enc,
154                                   suite => $suites_enc,
155                                   section => $sections_enc,
156                                   subword => $subword,
157                                   exact => $exact,
158                                   case => $case,
159                                   },
160                               );
161 }
162
163 # read the configuration
164 my $topdir;
165 if (!open (C, "../config.sh")) {
166     print "\nInternal Error: Cannot open configuration file.\n\n"
167 if $format eq 'html';
168     exit 0;
169 }
170 while (<C>) {
171     $topdir = $1 if (/^\s*topdir="?(.*)"?\s*$/);
172 }
173 close (C);
174
175 my $DBDIR = $topdir . "/files/db";
176 my $search_on_sources = 0;
177
178 my $st0 = new Benchmark;
179 my @results;
180 my $too_much_hits;
181 if ($searchon eq 'sourcenames') {
182     $search_on_sources = 1;
183 }
184
185 my %suites = map { $_ => 1 } @suites;
186 my %sections = map { $_ => 1 } @sections;
187 my %archs = map { $_ => 1 } @archs;
188
189 print "DEBUG: suites=@suites, sections=@sections, archs=@archs<br>" if $debug > 2;
190
191 sub read_entry {
192     my ($hash, $key, $results) = @_;
193     my $result = $hash->{$key};
194     foreach (split /\000/, $result) {
195         my @data = split ( /\s/, $_, 7 );
196         print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
197         if ($suites{$data[0]} && ($archs{$data[1]} || $data[1] eq 'all')
198             && $sections{$data[2]}) {
199             print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
200             push @$results, [ $key, @data ];
201         }
202     }
203 }
204 sub read_src_entry {
205     my ($hash, $key, $results) = @_;
206     my $result = $hash->{$key};
207
208     foreach (split /\000/, $result) {
209         my @data = split ( /\s/, $_, 5 );
210         print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
211         if ($suites{$data[0]} && $sections{$data[1]}) {
212             print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
213             push @$results, [ $key, @data ];
214         }
215     }
216 }
217
218
219 if ($searchon eq 'names') {
220
221     $keyword = lc $keyword unless $case_bool;
222     
223     my $obj = tie my %packages, 'DB_File', "$DBDIR/packages_small.db", O_RDONLY, 0666, $DB_BTREE
224         or die "couldn't tie DB $DBDIR/packages_small.db: $!";
225     
226     if ($exact) {
227         read_entry( \%packages, $keyword, \@results );
228     } else {
229         my ($key, $prefixes) = ($keyword, '');
230         my %pkgs;
231         my $p_obj = tie my %pref, 'DB_File', "$DBDIR/package_postfixes.db", O_RDONLY, 0666, $DB_BTREE
232             or die "couldn't tie postfix db $DBDIR/package_postfixes.db: $!";
233         $p_obj->seq( $key, $prefixes, R_CURSOR );
234         do {
235             if ($prefixes =~ /^\001(\d+)/o) {
236                 $too_much_hits += $1;
237             } else {
238                 print "DEBUG: add word $key<br>" if $debug > 2;
239                 $pkgs{$key}++;
240                 foreach (split /\000/o, $prefixes) {
241                     print "DEBUG: add word $_$key<br>" if $debug > 2;
242                     $pkgs{$_.$key}++;
243                 }
244             }
245         } while (($p_obj->seq( $key, $prefixes, R_NEXT ) == 0)
246                  && (index($key, $keyword) >= 0)
247                  && !$too_much_hits
248                  && (keys %pkgs < 100));
249         
250         my $no_results = keys %pkgs;
251         if ($too_much_hits || ($no_results >= 100)) {
252             $too_much_hits += $no_results;
253             %pkgs = ( $keyword => 1 );
254         }
255         foreach my $pkg (sort keys %pkgs) {
256             read_entry( \%packages, $pkg, \@results );
257         }
258     }
259 } elsif ($searchon eq 'sourcenames') {
260  
261     $keyword = lc $keyword unless $case_bool;
262     
263     my $obj = tie my %packages, 'DB_File', "$DBDIR/sources_small.db", O_RDONLY, 0666, $DB_BTREE
264         or die "couldn't tie DB $DBDIR/sources_small.db: $!";
265     
266     if ($exact) {
267         read_src_entry( \%packages, $keyword, \@results );
268     } else {
269         while (my ($pkg, $result) = each %packages) {
270             #what's faster? I can't really see a difference
271             (index($pkg, $keyword) >= 0) or next;
272             #$pkg =~ /\Q$keyword\E/ or next;
273             foreach (split /\000/, $result) {
274                 my @data = split ( /\s/, $_, 5 );
275                 print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
276                 if ($suites{$data[0]} && $sections{$data[1]}) {
277                     print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
278                     push @results, [ $pkg , @data ];
279                 }
280             }
281         }
282     }
283 }
284
285 my $st1 = new Benchmark;
286 my $std = timediff($st1, $st0);
287 print "DEBUG: Search took ".timestr($std)."<br>" if $debug;
288
289 if ($format eq 'html') {
290     my $suite_wording = $suites_enc eq "all" ? "all suites"
291         : "suite(s) <em>$suites_enc</em>";
292     my $section_wording = $sections_enc eq 'all' ? "all sections"
293         : "section(s) <em>$sections_enc</em>";
294     my $arch_wording = $archs_enc eq 'any' ? "all architectures"
295         : "architecture(s) <em>$archs_enc</em>";
296     if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
297         my $source_wording = $search_on_sources ? "source " : "";
298         my $exact_wording = $exact ? "named" : "that names contain";
299         print "<p>You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording.</p>";
300     } else {
301         my $exact_wording = $exact ? "" : " (including subword matching)";
302         print "<p>You have searched for <em>$keyword_enc</em> in packages names and descriptions in $suite_wording, $section_wording, and $arch_wording$exact_wording.</p>";
303     }
304 }
305
306 if ($too_much_hits) {
307 print "<p><strong>Your search was too wide so we will only display exact matches. At least <em>$too_much_hits</em> results have been omitted and will not be displayed. Please consider using a longer keyword or more keywords.</strong></p>";
308 }
309
310 if (!@results) {
311     if ($format eq 'html') {
312         my $keyword_esc = uri_escape( $keyword );
313         my $printed = 0;
314         if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
315             if (($suites_enc eq 'all')
316                 && ($archs_enc eq 'any')
317                 && ($sections_enc eq 'all')) {
318                 print "<p><strong>Can't find that package.</strong></p>\n";
319             } else {
320                 print "<p><strong>Can't find that package, at least not in that suite ".
321                     ( $search_on_sources ? "" : " and on that architecture" ).
322                     ".</strong></p>\n";
323             }
324             
325             if ($exact) {
326                 $printed = 1;
327                 print "<p>You have searched only for exact matches of the package name. You can try to search for <a href=\"$thisscript?exact=0&amp;searchon=$searchon&amp;suite=$suites_param&amp;case=$case&amp;section=$sections_param&amp;keywords=$keyword_esc&amp;arch=$archs_param\">package names that contain your search string</a>.</p>";
328             }
329         } else {
330             if (($suites_enc eq 'all')
331                 && ($archs_enc eq 'any')
332                 && ($sections_enc eq 'all')) {
333                 print "<p><strong>Can't find that string.</strong></p>\n";
334             } else {
335                 print "<p><strong>Can't find that string, at least not in that suite ($suites_enc, section $sections_enc) and on that architecture ($archs_enc).</strong></p>\n";
336             }
337             
338             unless ($subword) {
339                 $printed = 1;
340                 print "<p>You have searched only for words exactly matching your keywords. You can try to search <a href=\"$thisscript?subword=1&amp;searchon=$searchon&amp;suite=$suites_param&amp;case=$case&amp;section=$sections_param&amp;keywords=$keyword_esc&amp;arch=$archs_param\">allowing subword matching</a>.</p>";
341             }
342         }
343         print "<p>".( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$SEARCHPAGE#search_packages\">Packages search page</a>.</p>";
344         
345         &printfooter;
346     }
347     exit;
348 }
349
350 my (%pkgs, %sect, %part, %desc, %binaries);
351
352 unless ($search_on_sources) {
353     foreach (@results) {
354         my ($pkg_t, $suite, $arch, $section, $subsection,
355             $priority, $version, $desc) = @$_;
356         
357         my ($package) = $pkg_t =~ m/^(.+)/; # untaint
358         $pkgs{$package}{$suite}{$version}{$arch} = 1;
359         $sect{$package}{$suite}{$version} = $subsection;
360         $part{$package}{$suite}{$version} = $section unless $section eq 'main';
361         
362         $desc{$package}{$suite}{$version} = $desc;
363
364     }
365
366     if ($format eq 'html') {
367         my ($start, $end) = multipageheader( scalar keys %pkgs );
368         my $count = 0;
369         
370         foreach my $pkg (sort keys %pkgs) {
371             $count++;
372             next if $count < $start or $count > $end;
373             printf "<h3>Package %s</h3>\n", $pkg;
374             print "<ul>\n";
375             foreach my $ver (@SUITES) {
376                 if (exists $pkgs{$pkg}{$ver}) {
377                     my @versions = version_sort keys %{$pkgs{$pkg}{$ver}};
378                     my $part_str = "";
379                     if ($part{$pkg}{$ver}{$versions[0]}) {
380                         $part_str = "[<span style=\"color:red\">$part{$pkg}{$ver}{$versions[0]}</span>]";
381                     }
382                     printf "<li><a href=\"$ROOT/%s/%s/%s\">%s</a> (%s): %s   %s\n",
383                     $ver, $sect{$pkg}{$ver}{$versions[0]}, $pkg, $ver, $sect{$pkg}{$ver}{$versions[0]}, $desc{$pkg}{$ver}{$versions[0]}, $part_str;
384                     
385                     foreach my $v (@versions) {
386                         printf "<br>%s: %s\n",
387                         $v, join (" ", (sort keys %{$pkgs{$pkg}{$ver}{$v}}) );
388                     }
389                     print "</li>\n";
390                 }
391             }
392             print "</ul>\n";
393         }
394     } elsif ($format eq 'xml') {
395         require RDF::Simple::Serialiser;
396         my $rdf = new RDF::Simple::Serialiser;
397         $rdf->addns( debpkg => 'http://packages.debian.org/xml/01-debian-packages-rdf' );
398         my @triples;
399         foreach my $pkg (sort keys %pkgs) {
400             foreach my $ver (@DISTS) {
401                 if (exists $pkgs{$pkg}{$ver}) {
402                     my @versions = version_sort keys %{$pkgs{$pkg}{$ver}};
403                     foreach my $version (@versions) {
404                         my $id = "$ROOT/$ver/$sect{$pkg}{$ver}{$version}/$pkg/$version";
405                         push @triples, [ $id, 'debpkg:package', $pkg ];
406                         push @triples, [ $id, 'debpkg:version', $version ];
407                         push @triples, [ $id, 'debpkg:section', $sect{$pkg}{$ver}{$version}, ];
408                         push @triples, [ $id, 'debpkg:suite', $ver ];
409                         push @triples, [ $id, 'debpkg:shortdesc', $desc{$pkg}{$ver}{$version} ];
410                         push @triples, [ $id, 'debpkg:part', $part{$pkg}{$ver}{$version} || 'main' ];
411                         foreach my $arch (sort keys %{$pkgs{$pkg}{$ver}{$version}}) {
412                             push @triples, [ $id, 'debpkg:architecture', $arch ];
413                         }
414                     }
415                 }
416             }
417         }
418         
419         print $rdf->serialise(@triples);
420     }
421 } else {
422     foreach (@results) {
423         my ($package, $suite, $section, $subsection, $priority,
424             $version, $binaries) = @$_;
425         
426         $pkgs{$package}{$suite} = $version;
427         $sect{$package}{$suite}{source} = $subsection;
428         $part{$package}{$suite}{source} = $section unless $section eq 'main';
429
430         $binaries{$package}{$suite} = [ sort split( /\s*,\s*/, $binaries ) ];
431     }
432
433     if ($format eq 'html') {
434         my ($start, $end) = multipageheader( scalar keys %pkgs );
435         my $count = 0;
436         
437         foreach my $pkg (sort keys %pkgs) {
438             $count++;
439             next if ($count < $start) or ($count > $end);
440             printf "<h3>Source package %s</h3>\n", $pkg;
441             print "<ul>\n";
442             foreach my $ver (@SUITES) {
443                 if (exists $pkgs{$pkg}{$ver}) {
444                     my $part_str = "";
445                     if ($part{$pkg}{$ver}{source}) {
446                         $part_str = "[<span style=\"color:red\">$part{$pkg}{$ver}{source}</span>]";
447                     }
448                     printf "<li><a href=\"$ROOT/%s/source/%s\">%s</a> (%s): %s   %s", $ver, $pkg, $ver, $sect{$pkg}{$ver}{source}, $pkgs{$pkg}{$ver}, $part_str;
449                     
450                     print "<br>Binary packages: ";
451                     my @bp_links;
452                     foreach my $bp (@{$binaries{$pkg}{$ver}}) {
453                         my $sect = 'section';
454                         
455                         my $bp_link;
456                         if ($sect) {
457                             $bp_link = sprintf( "<a href=\"$ROOT/%s/%s/%s\">%s</a>",
458                                                 $ver, $sect, uri_escape( $bp ),  $bp );
459                         } else {
460                             $bp_link = $bp;
461                         }
462                         push @bp_links, $bp_link;
463                     }
464                     print join( ", ", @bp_links );
465                     print "</li>\n";
466                 }
467             }
468             print "</ul>\n";
469         }
470     } elsif ($format eq 'xml') {
471         require RDF::Simple::Serialiser;
472         my $rdf = new RDF::Simple::Serialiser;
473         $rdf->addns( debpkg => 'http://packages.debian.org/xml/01-debian-packages-rdf' );
474         my @triples;
475         foreach my $pkg (sort keys %pkgs) {
476             foreach my $ver (@SUITES) {
477                 if (exists $pkgs{$pkg}{$ver}) {
478                     my $id = "$ROOT/$ver/source/$pkg";
479
480                     push @triples, [ $id, 'debpkg:package', $pkg ];
481                     push @triples, [ $id, 'debpkg:type', 'source' ];
482                     push @triples, [ $id, 'debpkg:section', $sect{$pkg}{$ver}{source} ];
483                     push @triples, [ $id, 'debpkg:version', $pkgs{$pkg}{$ver} ];
484                     push @triples, [ $id, 'debpkg:part', $part{$pkg}{$ver}{source} || 'main' ];
485                     
486                     foreach my $bp (@{$binaries{$pkg}{$ver}}) {
487                         push @triples, [ $id, 'debpkg:binary', $bp ];
488                     }
489                 }
490             }
491         }
492         print $rdf->serialise(@triples);
493     }
494 }
495
496 if ($format eq 'html') {
497     &printindexline( scalar keys %pkgs );
498     &printfooter;
499 }
500
501 exit;
502
503 sub printindexline {
504     my $no_results = shift;
505
506     my $index_line;
507     if ($no_results > $results_per_page) {
508         
509         $index_line = prevlink($input,\%params)." | ".
510             indexline( $input, \%params, $no_results)." | ".
511             nextlink($input,\%params, $no_results);
512         
513         print "<p style=\"text-align:center\">$index_line</p>";
514     }
515 }
516
517 sub multipageheader {
518     my $no_results = shift;
519
520     my ($start, $end);
521     if ($results_per_page =~ /^all$/i) {
522         $start = 1;
523         $end = $no_results;
524         $results_per_page = $no_results;
525     } else {
526         $start = Packages::Search::start( \%params );
527         $end = Packages::Search::end( \%params );
528         if ($end > $no_results) { $end = $no_results; }
529     }
530
531     print "<p>Found <em>$no_results</em> matching packages,";
532     if ($end == $start) {
533         print " displaying package $end.</p>";
534     } else {
535         print " displaying packages $start to $end.</p>";
536     }
537
538     printindexline( $no_results );
539
540     if ($no_results > 100) {
541         print "<p>Results per page: ";
542         my @resperpagelinks;
543         for (50, 100, 200) {
544             if ($results_per_page == $_) {
545                 push @resperpagelinks, $_;
546             } else {
547                 push @resperpagelinks, resperpagelink($input,\%params,$_);
548             }
549         }
550         if ($params{values}{number}{final} =~ /^all$/i) {
551             push @resperpagelinks, "all";
552         } else {
553             push @resperpagelinks, resperpagelink($input, \%params,"all");
554         }
555         print join( " | ", @resperpagelinks )."</p>";
556     }
557     return ( $start, $end );
558 }
559
560 sub printfooter {
561 print <<END;
562 </div>
563
564 <hr class="hidecss">
565 <p style="text-align:right;font-size:small;font-stlye:italic"><a href="$SEARCHPAGE">Packages search page</a></p>
566
567 </div>
568 END
569
570 print $input->end_html;
571 }