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