display three custom search results on main page

git-svn-id: http://yolanda.mister-muffin.de/svn@307 7eef14d0-6ed0-489d-bf55-20463b2d70db
This commit is contained in:
josch 2008-04-16 21:07:07 +00:00
parent c0ec756d50
commit f1f175b753
4 changed files with 164 additions and 98 deletions

View file

@ -48,16 +48,18 @@ sub get_page_array
}
# called by video.pl (display ambiguous videos),
# index.pl (display custom search)
# search.pl (display search results)
# and upload.pl (display similar videos)
sub fill_results
{
my ($dbquery, @args) = @_;
#prepare query
my $sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$resultcount = $sth->execute(@_) or die $dbh->errstr;
$resultcount = $sth->execute(@args) or die $dbh->errstr;
#set pagesize by query or usersettings or default
$pagesize = $query->param('pagesize') or $pagesize = $userinfo->{'pagesize'} or $pagesize = $config->{"page_results_pagesize_default"};
@ -77,7 +79,7 @@ sub fill_results
$sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$sth->execute(@_) or die $dbquery;
$sth->execute(@args) or die $dbquery;
$page->{'results'}->{'lastpage'} = $lastpage;
$page->{'results'}->{'currentpage'} = $currentpage;
@ -108,7 +110,7 @@ sub fill_results
'dc:description' => [$description],
'dc:publisher' => [$publisher],
'dc:date' => [$timestamp],
'dc:identifier' => [$config->{"url_root"}."/video/".urlencode($title)."/$id/" . ($duration == 0 ? "/action=edit" : "")],
'dc:identifier' => [$config->{"url_root"}."/video/".urlencode($title)."/$id/"],
'dc:source' => [$source],
'dc:language' => [$language],
'dc:coverage' => [$coverage],
@ -126,6 +128,103 @@ sub fill_results
$sth->finish() or die $dbh->errstr;
}
sub get_sqlquery
{
my $strquery = @_[0];
$strquery =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
(@tags) = $strquery =~ / tag:(\w+)/gi;
($orderby) = $strquery =~ / orderby:(\w+)/i;
($sort) = $strquery =~ / sort:(\w+)/i;
#($title) = $strquery =~ /title:(\w+)/i;
#($description) = $strquery =~ /description:(\w+)/i;
#($creator) = $strquery =~ /creator:(\w+)/i;
#($language) = $strquery =~ /language:(\w+)/i;
#($coverage) = $strquery =~ /coverage:(\w+)/i;
#($rights) = $strquery =~ /rights:(\w+)/i;
#($license) = $strquery =~ /license:(\w+)/i;
#($filesize) = $strquery =~ /filesize:([<>]?\w+)/i;
#($duration) = $strquery =~ /duration:([<>]?\w+)/i;
#($timestamp) = $strquery =~ /timestamp:([<>]?\w+)/i;
$strquery =~ s/ (tag|orderby|sort):\w+//gi;
$strquery =~ s/^\s*(.*?)\s*$/$1/;
#build mysql query
my $dbquery = "select v.id, v.title, v.description, u.username,
from_unixtime( v.timestamp ), v.creator, v.subject,
v.source, v.language, v.coverage, v.rights, v.license, filesize,
duration, width, height, fps, viewcount, downloadcount";
if($strquery)
{
if($strquery eq "*")
{
$dbquery .= " from videos as v, users as u where u.id = v.userid";
}
else
{
$dbquery .= ", match(v.title, v.description, v.subject) against( ? in boolean mode) as relevance";
$dbquery .= " from videos as v, users as u where u.id = v.userid";
$dbquery .= " and match(v.title, v.description, v.subject) against( ? in boolean mode)";
push @args, $strquery, $strquery;
}
if(@tags)
{
$dbquery .= " and match(v.subject) against (? in boolean mode)";
push @args, "@tags";
}
if($publisher)
{
$dbquery .= " and match(u.username) against (? in boolean mode)";
push @args, "$publisher";
}
if($orderby)
{
if($orderby eq 'filesize')
{
$dbquery .= " order by v.filesize";
}
elsif($orderby eq 'duration')
{
$dbquery .= " order by v.duration";
}
elsif($orderby eq 'viewcount')
{
$dbquery .= " order by v.viewcount";
}
elsif($orderby eq 'downloadcount')
{
$dbquery .= " order by v.downloadcount";
}
elsif($orderby eq 'timestamp')
{
$dbquery .= " order by v.timestamp";
}
elsif($orderby eq 'relevance' and $strquery)
{
$dbquery .= " order by relevance";
}
else
{
$dbquery .= " order by v.id";
}
if($sort eq "ascending")
{
$dbquery .= " asc";
}
elsif($sort eq "descending")
{
$dbquery .= " desc";
}
}
return $dbquery, @args;
}
}
#replace chars in url according to RFC 1738 <http://www.rfc-editor.org/rfc/rfc1738.txt>
sub urlencode
{

View file

@ -47,4 +47,60 @@ while (my ($text, $count) = $sth->fetchrow_array())
#finish query
$sth->finish() or die $dbh->errstr;
#TODO: make this configureable
@querystrings = ("* orderby:timestamp sort:descending", "*", "*");
foreach $strquery (@querystrings)
{
#new results block
push @{$page->{'results'} }, { "query" => $strquery };
#get query string and args
my ($dbquery, @args) = get_sqlquery($strquery);
$dbquery .= " limit 0, 3";
#prepare query
$sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$sth->execute(@args) or die $dbquery;
#foreach result, fill appropriate results hash
while (my ($id, $title, $description, $publisher, $timestamp, $creator,
$subject, $source, $language, $coverage, $rights,
$license, $filesize, $duration, $width, $height, $fps, $viewcount,
$downloadcount) = $sth->fetchrow_array())
{
push @{$page->{'results'}[$#{$page->{'results'} }]->{'result'}},
{
'thumbnail' => $config->{"url_root"}."/video-stills/thumbnails/$id",
'preview' => $config->{"url_root"}."/video-stills/previews/$id",
'duration' => $duration,
'viewcount' => $viewcount,
'rdf:RDF' =>
{
'cc:Work' =>
{
'rdf:about' => $config->{"url_root"}."/download/$id/",
'dc:title' => [$title],
'dc:creator' => [$creator],
'dc:subject' => [$subject],
'dc:description' => [$description],
'dc:publisher' => [$publisher],
'dc:date' => [$timestamp],
'dc:identifier' => [$config->{"url_root"}."/video/".urlencode($title)."/$id/"],
'dc:source' => [$source],
'dc:language' => [$language],
'dc:coverage' => [$coverage],
'dc:rights' => [$rights]
},
'cc:License' =>
{
'rdf:about' => 'http://creativecommons.org/licenses/GPL/2.0/'
}
}
};
}
}
print output_page();

View file

@ -14,100 +14,11 @@ if($query->param('query'))
{
$page->{'results'}->{'query'} = $query->param('query');
my @args = ();
my ($dbquery, @args) = get_sqlquery($query->param('query'));
$strquery = $query->param('query');
$strquery =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
(@tags) = $strquery =~ / tag:(\w+)/gi;
($orderby) = $strquery =~ / orderby:(\w+)/i;
($sort) = $strquery =~ / sort:(\w+)/i;
#($title) = $strquery =~ /title:(\w+)/i;
#($description) = $strquery =~ /description:(\w+)/i;
#($creator) = $strquery =~ /creator:(\w+)/i;
#($language) = $strquery =~ /language:(\w+)/i;
#($coverage) = $strquery =~ /coverage:(\w+)/i;
#($rights) = $strquery =~ /rights:(\w+)/i;
#($license) = $strquery =~ /license:(\w+)/i;
#($filesize) = $strquery =~ /filesize:([<>]?\w+)/i;
#($duration) = $strquery =~ /duration:([<>]?\w+)/i;
#($timestamp) = $strquery =~ /timestamp:([<>]?\w+)/i;
$strquery =~ s/ (tag|orderby|sort):\w+//gi;
$strquery =~ s/^\s*(.*?)\s*$/$1/;
#build mysql query
$dbquery = "select v.id, v.title, v.description, u.username,
from_unixtime( v.timestamp ), v.creator, v.subject,
v.source, v.language, v.coverage, v.rights, v.license, filesize,
duration, width, height, fps, viewcount, downloadcount";
if($strquery)
if($dbquery)
{
if($strquery eq "*")
{
$dbquery .= " from videos as v, users as u where u.id = v.userid";
}
else
{
$dbquery .= ", match(v.title, v.description, v.subject) against( ? in boolean mode) as relevance";
$dbquery .= " from videos as v, users as u where u.id = v.userid";
$dbquery .= " and match(v.title, v.description, v.subject) against( ? in boolean mode)";
push @args, $strquery, $strquery;
}
if(@tags)
{
$dbquery .= " and match(v.subject) against (? in boolean mode)";
push @args, "@tags";
}
if($publisher)
{
$dbquery .= " and match(u.username) against (? in boolean mode)";
push @args, "$publisher";
}
if($orderby)
{
if($orderby eq 'filesize')
{
$dbquery .= " order by v.filesize";
}
elsif($orderby eq 'duration')
{
$dbquery .= " order by v.duration";
}
elsif($orderby eq 'viewcount')
{
$dbquery .= " order by v.viewcount";
}
elsif($orderby eq 'downloadcount')
{
$dbquery .= " order by v.downloadcount";
}
elsif($orderby eq 'timestamp')
{
$dbquery .= " order by v.timestamp";
}
elsif($orderby eq 'relevance' and $strquery)
{
$dbquery .= " order by relevance";
}
else
{
$dbquery .= " order by v.id";
}
if($sort eq "ascending")
{
$dbquery .= " asc";
}
elsif($sort eq "descending")
{
$dbquery .= " desc";
}
}
fill_results(@args);
fill_results($dbquery, @args);
if(@{$page->{'results'}->{'result'}} == 0)
{

View file

@ -43,7 +43,7 @@ if($userinfo->{'username'})
my @args = ();
#build mysql query
$dbquery = "select v.id, v.title, v.description, u.username,
my $dbquery = "select v.id, v.title, v.description, u.username,
from_unixtime( v.timestamp ), v.creator, v.subject,
v.source, v.language, v.coverage, v.rights,
v.license, filesize, duration, width, height, fps, viewcount,
@ -55,7 +55,7 @@ if($userinfo->{'username'})
against( ? in boolean mode)";
push @args, $query->param('DC.Title'), $query->param('DC.Title');
fill_results(@args);
fill_results($dbquery, @args);
$page->{'uploadform'}->{'page'} = '2';
}
else