5 # Look through a provided database to find which CD/DVD image(s)
6 # contain a specified Debian package or source file.
8 # Copyright (c) 2011 Steve McIntyre <93sam@debian.org>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
28 my $dbdir = "/home/steve/debian/debian-cd/search/search-db";
29 my $cdimage_url = "http://cdimage.debian.org/cdimage/";
42 my $title_base = "Debian CD search engine";
50 $q->address("$title_base version $version"),
51 $q->address("Steve McIntyre <93sam\@debian.org>");
54 # Borrowed from Ikiwiki.pm
56 my $re=quotemeta(shift);
64 my $text = "no description";
66 open INFILE, "<", "$filename" || return $text;
79 my $errortext = shift;
82 $q->start_html($title_base),
84 $q->p("Error: $errortext"),
85 $q->p("<a href=\"" . $q->url . "\">Search again.</a>");
95 $q->start_html($title_base),
96 $q->h1("$title_base"), "\n";
97 print $q->p("This tool searches for specified file names contained in Debian CD/DVD images."), "\n";
98 print $q->p("Select which set(s) of images you wish to search:"), "\n";
99 print $q->start_form(-method=>"GET");
100 print $q->checkbox_group(-name=>'search_area',
102 -defaults=>['release'],
105 print $q->textfield(-name=>'query',
109 print $q->submit(-name=>'Search',
111 if (length($error)) {
112 print $q->p({-style=>'color: red'}, "ERROR: $error"), "\n";
114 print $q->p("Search terms should be in the form of shell globs (i.e. \"<i>cvs*deb</i>\" will match all files that start with <i>cvs</i> and end with <i>deb</i>.");
122 my $link = $cdimage_url . shift;
127 my $link = $cdimage_url . shift;
128 $link =~ s/list-/iso-/g;
129 $link =~ s/list\.gz$/iso/g;
134 my $link = $cdimage_url . shift;
135 $link =~ s/list-/jigdo-/g;
136 $link =~ s/list\.gz$/jigdo/g;
140 sub pretty_name ($) {
143 $name =~ s/\.list\.gz$//g;
147 chdir($dbdir) || log_error(500, "Failed to cd to $dbdir: $!\n");
148 opendir(my $dh, ".") || log_error(500, "Failed to open $dbdir: $!\n");
149 while (defined($_ = readdir($dh))) {
150 m/(.*)\.db$/ and push(@AREAS, "$1");
154 foreach my $area(@AREAS) {
155 $labels{$area} = "$area (" . read_text("$area.text") . ")";
158 @chosen_areas = $q->param('search_area');
159 $query_term = $q->param('query');
161 # Check what we've been given, if anything
162 if ( (!@chosen_areas) &&
163 (!defined($query_term) || length($query_term) == 0)) {
167 if (!(@chosen_areas) && defined($query_term)) {
168 blank_form("No search areas chosen");
172 (!defined($query_term) || length($query_term) == 0)) {
173 blank_form("No search terms entered");
177 my $re_search = glob2re($query_term);
179 # If we get here, we have stuff to work with. Yay!
180 foreach my $area (@chosen_areas) {
181 my $db_file_name = "$dbdir/$area.db";
182 $l .= "Looking in area $area, file $db_file_name<br>\n";
183 dbmopen(%fileinfo, "$db_file_name", 0000) ||
184 log_error(500, "Failed to open db file: $!\n");
186 if ($query_term =~ /[\*\?]/) {
187 # Will need to search through all the keys to allow for glob
188 foreach my $file (keys %fileinfo) {
189 if ($file =~ $re_search) {
191 push(@results, "$file $fileinfo{$file}");
192 if ($count >= $max_count) {
198 # We've been given an exact name - do the exact key lookup \o/
199 if (defined($fileinfo{$query_term})) {
200 push (@results, "$query_term $fileinfo{$query_term}");
204 if ($count >= $max_count) {
211 $q->start_html("$title_base: $count results"),
212 $q->h1($title_base), "\n";
213 if ($count >= $max_count) {
214 print $q->p("More than $max_count results for \"$query_term\", showing the first $count only\n");
216 print $q->p("$count result(s) for \"$query_term\"\n");
220 foreach my $result (sort (@results)) {
221 my($found, @list) = split(' ', $result);
222 print "<li> $found appears in:\n";
224 foreach my $image (sort(@list)) {
225 print "<li>" . pretty_name($image);
226 print " (<a href=\"" . list_link($image) . "\">list.gz</a> |";
227 print " <a href=\"" . jigdo_link($image) . "\">jigdo</a> |";
228 print " <a href=\"" . iso_link($image) . "\">iso</a>)";