#!/usr/bin/perl
#
# Suche in mehreren MP3-XML-Datenbanken
#
# ----- used modules
use strict;
use CGI;
use HTML::Entities;
use MIME::QuotedPrint;
use URI::Escape;
use XML::Parser;
# ----- xml bases for searching
my @xmlfiles = (
'/wwwroot/prinzess.dyndns.org/serverdata/mp3_alina.xml',
'/wwwroot/prinzess.dyndns.org/serverdata/mp3_ruthy.xml'
);
my @searchfields = ('artist','title','filename');
# ----- parse CGI data
my $cgi = new CGI;
my $q = $cgi->param('q');
my $vk = $cgi->param('vk');
# ----- check search term
$q = uri_unescape($q);
if (!$q) {
print "Content-type: text/html\n\n";
print "
Kein Suchbegriff angegeben!\n";
exit ();
}
# ----- parse the xml file
my ($db, $xmlfile, $xfname);
my %db = ();
foreach $xmlfile (@xmlfiles) {
next if (!-e $xmlfile);
$xfname = $xmlfile;
$xfname =~ s/(.*)\///g;
$xfname = substr ($xfname, 4, length($xfname) - 8);
my %this = ();
my $textbuffer = "";
sub StartTag {
my ($expat, $element) = @_;
if ($element eq "mp3") {
$this{filename} = decode_qp ($_{filename});
$this{year} = $_{year};
$this{time} = $_{time};
}
}
sub Text {
my $expat = shift;
$textbuffer = $_;
}
sub EndTag {
my ($expat, $element) = @_;
if ($element eq "artist") {
$this{artist} = decode_qp ($textbuffer);
}
elsif ($element eq "title") {
$this{title} = decode_qp ($textbuffer);
}
if ($element eq "mp3") {
$this{location} = $xfname;
my %that = %this;
push @{$db}, \%that;
}
}
my $xml = new XML::Parser(Style => 'Stream', ErrorContext => 2);
$xml->parsefile ($xmlfile);
}
# ----- prepare the database query
my $query = $q;
# filter some characters that are not needed
$query =~ s/,/ /g;
$query =~ s/\+/ /g;
$query =~ s/\\//g;
$query =~ s/\///g;
$query =~ s/\ \ / /g;
# ----- exec database query
my $result;
# split at space positions
my %terms = split (/ /, $query);
# count terms
my $count_terms = 0;
foreach my $term (%terms) {
$count_terms++ if ($term ne "");
}
my $countall = 0;
my ($thisresult, $goals, $term);
my %goals = ();
# search in database
foreach (@{$db}) {
$countall++;
$result = 0;
foreach $term (%terms) {
next if ($term eq "");
$result = 0 if (lc $vk eq "or");
$thisresult = 0;
foreach my $field (@searchfields) {
$thisresult += ($$_{$field} =~ /(.*)$term(.*)/i);
}
$result = 1 if ( ($thisresult) and (lc $vk eq "or") );
$result++ if ( ($thisresult) and (lc $vk eq "and") );
if ((($result) and (lc $vk eq "or")) or (($result eq $count_terms) and (lc $vk eq "and"))) {
my %this = (
artist => encode_entities ($$_{artist}),
title => encode_entities ($$_{title}),
filename => encode_entities ($$_{filename}),
year => $$_{year},
time => $$_{time},
location => $$_{location}
);
push @{$goals}, \%this;
last if lc $vk eq "or";
}
}
}
# ----- count results
my $count_goals = $#{@{$goals}} + 1;
# ----- sort results alphabetically
if ($count_goals) {
@{$goals} = sort { $$a{artist} cmp $$b{artist} } @{$goals};
}
# ----- print results
my $plural = "";
$plural = "n" if ($count_goals gt 1);
print "Content-type: text/html\n\n";
print<<"...";
Gesucht wurde nach "$q" mit der Suchverknüpfung
"$vk".
Es wurde$plural $count_goals Treffer in der
Datenbank gefunden. Derzeit sind $countall MP3-Dateien indiziert.
Interpret | Titel | Server | Jahr | Länge | Dateiname |
$$_{artist} | $$_{title} | $$_{location} | "; print "$$_{year} | $$_{time} | $$_{filename} |