]> git.deb.at Git - deb/packages.git/blob - cgi-bin/search_packages.pl
Implement source package names search (no binary package information
[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 if ($searchon eq 'sourcenames') {
181     $search_on_sources = 1;
182 }
183
184 my %suites = map { $_ => 1 } @suites;
185 my %sections = map { $_ => 1 } @sections;
186 my %archs = map { $_ => 1 } @archs;
187
188 print "DEBUG: suites=@suites, sections=@sections, archs=@archs<br>" if $debug > 2;
189
190 if ($searchon eq 'names') {
191
192     $keyword = lc $keyword unless $case_bool;
193     
194     my $obj = tie my %packages, 'DB_File', "$DBDIR/packages_small.db", O_RDONLY, 0666, $DB_BTREE
195         or die "couldn't tie DB $DBDIR/packages_small.db: $!";
196     
197     if ($exact) {
198         my $result = $packages{$keyword};
199         foreach (split /\000/, $result) {
200             my @data = split ( /\s/, $_, 7 );
201             print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
202             if ($suites{$data[0]} && ($archs{$data[1]} || $data[1] eq 'all')
203                 && $sections{$data[2]}) {
204                 print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
205                 push @results, [ $keyword, @data ];
206             }
207         }
208     } else {
209         while (my ($pkg, $result) = each %packages) {
210             #what's faster? I can't really see a difference
211             (index($pkg, $keyword) >= 0) or next;
212             #$pkg =~ /\Q$keyword\E/ or next;
213             foreach (split /\000/, $packages{$pkg}) {
214                 my @data = split ( /\s/, $_, 7 );
215                 print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
216                 if ($suites{$data[0]} && ($archs{$data[1]} || $data[1] eq 'all')
217                     && $sections{$data[2]}) {
218                     print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
219                     push @results, [ $pkg , @data ];
220                 }
221             }
222         }
223     }
224 } elsif ($searchon eq 'sourcenames') {
225  
226     $keyword = lc $keyword unless $case_bool;
227     
228     my $obj = tie my %packages, 'DB_File', "$DBDIR/sources_small.db", O_RDONLY, 0666, $DB_BTREE
229         or die "couldn't tie DB $DBDIR/sources_small.db: $!";
230     
231     if ($exact) {
232         my $result = $packages{$keyword};
233         foreach (split /\000/, $result) {
234             my @data = split ( /\s/, $_, 5 );
235             print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
236             if ($suites{$data[0]} && $sections{$data[1]}) {
237                 print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
238                 push @results, [ $keyword, @data ];
239             }
240         }
241     } else {
242         while (my ($pkg, $result) = each %packages) {
243             #what's faster? I can't really see a difference
244             (index($pkg, $keyword) >= 0) or next;
245             #$pkg =~ /\Q$keyword\E/ or next;
246             foreach (split /\000/, $packages{$pkg}) {
247                 my @data = split ( /\s/, $_, 5 );
248                 print "DEBUG: Considering entry ".join( ':', @data)."<br>" if $debug > 2;
249                 if ($suites{$data[0]} && $sections{$data[1]}) {
250                     print "DEBUG: Using entry ".join( ':', @data)."<br>" if $debug > 2;
251                     push @results, [ $pkg , @data ];
252                 }
253             }
254         }
255     }
256 }
257
258 my $st1 = new Benchmark;
259 my $std = timediff($st1, $st0);
260 print "DEBUG: Search took ".timestr($std)."<br>" if $debug;
261
262 if ($format eq 'html') {
263     my $suite_wording = $suites_enc eq "all" ? "all suites"
264         : "suite(s) <em>$suites_enc</em>";
265     my $section_wording = $sections_enc eq 'all' ? "all sections"
266         : "section(s) <em>$sections_enc</em>";
267     my $arch_wording = $archs_enc eq 'any' ? "all architectures"
268         : "architecture(s) <em>$archs_enc</em>";
269     if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
270         my $source_wording = $search_on_sources ? "source " : "";
271         my $exact_wording = $exact ? "named" : "that names contain";
272         print "<p>You have searched for ${source_wording}packages $exact_wording <em>$keyword_enc</em> in $suite_wording, $section_wording, and $arch_wording.</p>";
273     } else {
274         my $exact_wording = $exact ? "" : " (including subword matching)";
275         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>";
276     }
277 }
278
279 if (!@results) {
280     if ($format eq 'html') {
281         my $keyword_esc = uri_escape( $keyword );
282         my $printed = 0;
283         if (($searchon eq "names") || ($searchon eq 'sourcenames')) {
284             if (($suites_enc eq 'all')
285                 && ($archs_enc eq 'any')
286                 && ($sections_enc eq 'all')) {
287                 print "<p><strong>Can't find that package.</strong></p>\n";
288             } else {
289                 print "<p><strong>Can't find that package, at least not in that suite ".
290                     ( $search_on_sources ? "" : " and on that architecture" ).
291                     ".</strong></p>\n";
292             }
293             
294             if ($exact) {
295                 $printed = 1;
296                 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>";
297             }
298         } else {
299             if (($suites_enc eq 'all')
300                 && ($archs_enc eq 'any')
301                 && ($sections_enc eq 'all')) {
302                 print "<p><strong>Can't find that string.</strong></p>\n";
303             } else {
304                 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";
305             }
306             
307             unless ($subword) {
308                 $printed = 1;
309                 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>";
310             }
311         }
312         print "<p>".( $printed ? "Or you" : "You" )." can try a different search on the <a href=\"$SEARCHPAGE#search_packages\">Packages search page</a>.</p>";
313         
314         &printfooter;
315     }
316     exit;
317 }
318
319 my (%pkgs, %sect, %part, %desc, %binaries);
320
321 unless ($search_on_sources) {
322     foreach (@results) {
323         my ($pkg_t, $suite, $arch, $section, $subsection,
324             $priority, $version, $desc) = @$_;
325         
326         my ($package) = $pkg_t =~ m/^(.+)/; # untaint
327         $pkgs{$package}{$suite}{$version}{$arch} = 1;
328         $sect{$package}{$suite}{$version} = $subsection;
329         $part{$package}{$suite}{$version} = $section unless $section eq 'main';
330         
331         $desc{$package}{$suite}{$version} = $desc;
332
333     }
334
335     if ($format eq 'html') {
336         my ($start, $end) = multipageheader( scalar keys %pkgs );
337         my $count = 0;
338         
339         foreach my $pkg (sort keys %pkgs) {
340             $count++;
341             next if $count < $start or $count > $end;
342             printf "<h3>Package %s</h3>\n", $pkg;
343             print "<ul>\n";
344             foreach my $ver (@SUITES) {
345                 if (exists $pkgs{$pkg}{$ver}) {
346                     my @versions = version_sort keys %{$pkgs{$pkg}{$ver}};
347                     my $part_str = "";
348                     if ($part{$pkg}{$ver}{$versions[0]}) {
349                         $part_str = "[<span style=\"color:red\">$part{$pkg}{$ver}{$versions[0]}</span>]";
350                     }
351                     printf "<li><a href=\"$ROOT/%s/%s/%s\">%s</a> (%s): %s   %s\n",
352                     $ver, $sect{$pkg}{$ver}{$versions[0]}, $pkg, $ver, $sect{$pkg}{$ver}{$versions[0]}, $desc{$pkg}{$ver}{$versions[0]}, $part_str;
353                     
354                     foreach my $v (@versions) {
355                         printf "<br>%s: %s\n",
356                         $v, join (" ", (sort keys %{$pkgs{$pkg}{$ver}{$v}}) );
357                     }
358                     print "</li>\n";
359                 }
360             }
361             print "</ul>\n";
362         }
363     } elsif ($format eq 'xml') {
364         require RDF::Simple::Serialiser;
365         my $rdf = new RDF::Simple::Serialiser;
366         $rdf->addns( debpkg => 'http://packages.debian.org/xml/01-debian-packages-rdf' );
367         my @triples;
368         foreach my $pkg (sort keys %pkgs) {
369             foreach my $ver (@DISTS) {
370                 if (exists $pkgs{$pkg}{$ver}) {
371                     my @versions = version_sort keys %{$pkgs{$pkg}{$ver}};
372                     foreach my $version (@versions) {
373                         my $id = "$ROOT/$ver/$sect{$pkg}{$ver}{$version}/$pkg/$version";
374                         push @triples, [ $id, 'debpkg:package', $pkg ];
375                         push @triples, [ $id, 'debpkg:version', $version ];
376                         push @triples, [ $id, 'debpkg:section', $sect{$pkg}{$ver}{$version}, ];
377                         push @triples, [ $id, 'debpkg:suite', $ver ];
378                         push @triples, [ $id, 'debpkg:shortdesc', $desc{$pkg}{$ver}{$version} ];
379                         push @triples, [ $id, 'debpkg:part', $part{$pkg}{$ver}{$version} || 'main' ];
380                         foreach my $arch (sort keys %{$pkgs{$pkg}{$ver}{$version}}) {
381                             push @triples, [ $id, 'debpkg:architecture', $arch ];
382                         }
383                     }
384                 }
385             }
386         }
387         
388         print $rdf->serialise(@triples);
389     }
390 } else {
391     foreach (@results) {
392         my ($package, $suite, $section, $subsection, $priority,
393             $version, $binaries) = @$_;
394         
395         $pkgs{$package}{$suite} = $version;
396         $sect{$package}{$suite}{source} = $subsection;
397         $part{$package}{$suite}{source} = $section unless $section eq 'main';
398
399         $binaries{$package}{$suite} = [ sort split( /\s*,\s*/, $binaries ) ];
400     }
401
402     if ($format eq 'html') {
403         my ($start, $end) = multipageheader( scalar keys %pkgs );
404         my $count = 0;
405         
406         foreach my $pkg (sort keys %pkgs) {
407             $count++;
408             next if ($count < $start) or ($count > $end);
409             printf "<h3>Source package %s</h3>\n", $pkg;
410             print "<ul>\n";
411             foreach my $ver (@SUITES) {
412                 if (exists $pkgs{$pkg}{$ver}) {
413                     my $part_str = "";
414                     if ($part{$pkg}{$ver}{source}) {
415                         $part_str = "[<span style=\"color:red\">$part{$pkg}{$ver}{source}</span>]";
416                     }
417                     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;
418                     
419                     print "<br>Binary packages: ";
420                     my @bp_links;
421                     foreach my $bp (@{$binaries{$pkg}{$ver}}) {
422                         my $sect = 'section';
423                         
424                         my $bp_link;
425                         if ($sect) {
426                             $bp_link = sprintf( "<a href=\"$ROOT/%s/%s/%s\">%s</a>",
427                                                 $ver, $sect, uri_escape( $bp ),  $bp );
428                         } else {
429                             $bp_link = $bp;
430                         }
431                         push @bp_links, $bp_link;
432                     }
433                     print join( ", ", @bp_links );
434                     print "</li>\n";
435                 }
436             }
437             print "</ul>\n";
438         }
439     } elsif ($format eq 'xml') {
440         require RDF::Simple::Serialiser;
441         my $rdf = new RDF::Simple::Serialiser;
442         $rdf->addns( debpkg => 'http://packages.debian.org/xml/01-debian-packages-rdf' );
443         my @triples;
444         foreach my $pkg (sort keys %pkgs) {
445             foreach my $ver (@SUITES) {
446                 if (exists $pkgs{$pkg}{$ver}) {
447                     my $id = "$ROOT/$ver/source/$pkg";
448
449                     push @triples, [ $id, 'debpkg:package', $pkg ];
450                     push @triples, [ $id, 'debpkg:type', 'source' ];
451                     push @triples, [ $id, 'debpkg:section', $sect{$pkg}{$ver}{source} ];
452                     push @triples, [ $id, 'debpkg:version', $pkgs{$pkg}{$ver} ];
453                     push @triples, [ $id, 'debpkg:part', $part{$pkg}{$ver}{source} || 'main' ];
454                     
455                     foreach my $bp (@{$binaries{$pkg}{$ver}}) {
456                         push @triples, [ $id, 'debpkg:binary', $bp ];
457                     }
458                 }
459             }
460         }
461         print $rdf->serialise(@triples);
462     }
463 }
464
465 if ($format eq 'html') {
466     &printindexline( scalar keys %pkgs );
467     &printfooter;
468 }
469
470 exit;
471
472 sub printindexline {
473     my $no_results = shift;
474
475     my $index_line;
476     if ($no_results > $results_per_page) {
477         
478         $index_line = prevlink($input,\%params)." | ".
479             indexline( $input, \%params, $no_results)." | ".
480             nextlink($input,\%params, $no_results);
481         
482         print "<p style=\"text-align:center\">$index_line</p>";
483     }
484 }
485
486 sub multipageheader {
487     my $no_results = shift;
488
489     my ($start, $end);
490     if ($results_per_page =~ /^all$/i) {
491         $start = 1;
492         $end = $no_results;
493         $results_per_page = $no_results;
494     } else {
495         $start = Packages::Search::start( \%params );
496         $end = Packages::Search::end( \%params );
497         if ($end > $no_results) { $end = $no_results; }
498     }
499
500     print "<p>Found <em>$no_results</em> matching packages,";
501     if ($end == $start) {
502         print " displaying package $end.</p>";
503     } else {
504         print " displaying packages $start to $end.</p>";
505     }
506
507     printindexline( $no_results );
508
509     if ($no_results > 100) {
510         print "<p>Results per page: ";
511         my @resperpagelinks;
512         for (50, 100, 200) {
513             if ($results_per_page == $_) {
514                 push @resperpagelinks, $_;
515             } else {
516                 push @resperpagelinks, resperpagelink($input,\%params,$_);
517             }
518         }
519         if ($params{values}{number}{final} =~ /^all$/i) {
520             push @resperpagelinks, "all";
521         } else {
522             push @resperpagelinks, resperpagelink($input, \%params,"all");
523         }
524         print join( " | ", @resperpagelinks )."</p>";
525     }
526     return ( $start, $end );
527 }
528
529 sub printfooter {
530 print <<END;
531 </div>
532
533 <hr class="hidecss">
534 <p style="text-align:right;font-size:small;font-stlye:italic"><a href="$SEARCHPAGE">Packages search page</a></p>
535
536 </div>
537 END
538
539 print $input->end_html;
540 }