#!/usr/local/bin/perl -w
#
# xhibition search.cgi (c) Marc Majcher 2002
#
# Search functionality for gallery text.
use strict;
use CGI qw(:standard);
use DB_File;
use Text::Template;
use XML::Twig;
my $conf_dir = "config";
my $xhconf_file = "$conf_dir/xhconf.xml";
my $indexdb = "$conf_dir/search_index.db";
my %config = read_config($xhconf_file);
my $search_template = "$conf_dir/$config{'search_template'}";
my $search = lc(param('search'));
my $maxurls = param('max');
my $response;
my @urls;
if (defined($search)) {
my %match_urls;
my %DB;
dbmopen(%DB,$indexdb,0755) || die "can't open $indexdb: $!";
for my $search_word (split /\W+/,$search) {
if (defined($DB{$search_word})) {
for my $url (split "\t",$DB{$search_word}) {
$match_urls{$url}++;
}
}
}
dbmclose(%DB);
if (%match_urls) {
$response = "Found ".keys(%match_urls). " matches";
@urls = sort {$match_urls{$b} <=> $match_urls{$a}} keys %match_urls;
}
else {
$response = "No matches found";
}
$response .= qq( for search: $search);
}
else {
$response = "No search term given. Try again.";
}
my $template = Text::Template->new(TYPE => 'FILE',
SOURCE => $search_template,
UNTAINT => 1 );
print "Content-type: text/html\n\n";
print $template->fill_in(HASH => {
response => $response,
urls => \@urls,
max => $maxurls,
search => $search,
});
## Ohhh, look at the False Laziness! AGAIN! Refactor, lazy!
sub read_config {
# Reads an XML configuration file, and returns a hash of values
my $file = shift || return;
my %config;
my $content;
open(CONF,$file) || die "can't open config file $file: $!";
{ local $/ = undef;
$content = ; }
close(CONF);
my $t = XML::Twig->new(PrettyPrint => 'indented');
$t->parse($content);
my $r = $t->root;
$config{'base_url'} = $r->first_child('base_url')->text;
$config{'root_dir'} = $r->first_child('root_directory')->text;
$config{'search_template'} = $r->first_child('search_template')->text;
$config{'page_extension'} = $r->first_child('page_extension')->text;
$config{'thumbnail_prefix'} = $r->first_child('thumbnail_prefix')->text;
my @galleries;
for my $gtwig ($r->first_child('galleries')->children('gallery_file')) {
push @galleries, $gtwig->text;
}
$config{'galleries'} = \@galleries;
return %config;
}