added pagination, fixed encoding bug

git-svn-id: http://yolanda.mister-muffin.de/svn@137 7eef14d0-6ed0-489d-bf55-20463b2d70db
main
josch 17 years ago
parent ef8cdb725b
commit a826f9b93e

@ -6,96 +6,108 @@ CGI::Session->name($session_name);
$query = new CGI;
$session = new CGI::Session;
%page = ();
#if a username is associated with session id, username is nonempty
$page->{'username'} = get_username_from_sid($session->id);
$page->{'locale'} = $locale;
$page->{'stylesheet'} = $stylesheet;
$page->{'xmlns:dc'} = $xmlns_dc;
$page->{'xmlns:cc'} = $xmlns_cc;
$page->{'xmlns:rdf'} = $xmlns_rdf;
#check if query is set
if($query->param('query'))
if($query->param('query') or $query->param('orderby'))
{
%page = ();
#if a username is associated with session id, username is nonempty
$page->{'username'} = get_username_from_sid($session->id);
$page->{'locale'} = $locale;
$page->{'stylesheet'} = $stylesheet;
$page->{'xmlns:dc'} = $xmlns_dc;
$page->{'xmlns:cc'} = $xmlns_cc;
$page->{'xmlns:rdf'} = $xmlns_rdf;
$page->{results}->{query} = decode_utf8($query->param('query'));
$page->{results}->{query} = $query->param('query');
$page->{results}->{orderby} = $query->param('orderby');
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr;
#prepare query
my $sth = $dbh->prepare(qq{select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp )
from videos as v, users as u
where match(v.title, v.description, v.subject) against( ? )
and u.id = v.userid }) or die $dbh->errstr;
my @args = ();
#execute it
$sth->execute($query->param('query')) or die $dbh->errstr;
#build mysql query
$dbquery = "select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp )";
#get every returned value
while (my ($id, $title, $description, $username, $timestamp) = $sth->fetchrow_array())
if($query->param('query'))
{
#before code cleanup, this was a really obfuscated array/hash creation
push @{ $page->{'results'}->{'result'} },
$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, $query->param('query'), $query->param('query');
}
else
{
$dbquery .= " from videos as v, users as u where u.id = v.userid";
}
if($query->param('orderby'))
{
if($query->param('orderby') eq 'filesize')
{
'thumbnail' => ["./video-stills/$id"],
'rdf:RDF' =>
{
'cc:Work' =>
{
'rdf:about' => "./video.pl?title=$title&id=$id",
'dc:title' => [$title],
'dc:date' => [$timestamp],
'dc:publisher' => [$username]
},
'cc:License' =>
{
'rdf:about' => 'http://creativecommons.org/licenses/GPL/2.0/'
}
}
};
$dbquery .= " order by v.filesize";
}
elsif($query->param('orderby') eq 'duration')
{
$dbquery .= " order by v.duration";
}
elsif($query->param('orderby') eq 'viewcount')
{
$dbquery .= " order by v.viewcount";
}
elsif($query->param('orderby') eq 'downloadcount')
{
$dbquery .= " order by v.downloadcount";
}
elsif($query->param('orderby') eq 'timestamp')
{
$dbquery .= " order by v.timestamp";
}
elsif($query->param('orderby') eq 'relevance' and $query->param('query'))
{
$dbquery .= " order by relevance";
}
else
{
$dbquery .= " order by v.id";
}
if($query->param('sort') eq "asc")
{
$dbquery .= " asc"
}
else
{
$dbquery .= " desc"
}
}
#finish query
$sth->finish() or die $dbh->errstr;
#prepare query
my $sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#close db
$dbh->disconnect() or die $dbh->errstr;
#execute it
$resultcount = $sth->execute(@args) or die $dbquery;
#print xml http header along with session cookie
print $session->header(-type=>'text/xml');
#print xml
print XMLout($page, KeyAttr => {}, XMLDecl => $XMLDecl, RootName => 'page');
}
elsif($query->param('sort'))
{
%page = ();
$rowsperpage = 2;
#if a username is associated with session id, username is nonempty
$page->{'username'} = get_username_from_sid($session->id);
$page->{'locale'} = $locale;
$page->{'stylesheet'} = $stylesheet;
$page->{'xmlns:dc'} = $xmlns_dc;
$page->{'xmlns:cc'} = $xmlns_cc;
$page->{'xmlns:rdf'} = $xmlns_rdf;
$lastpage = int($resultcount/2);
$page->{results}->{query} = decode_utf8($query->param('query'));
$currentpage = $query->param('page') or $currentpage = 1;
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr;
$dbquery .= " limit ".($currentpage-1)*$rowsperpage.", ".$rowsperpage;
#prepare query
my $sth = $dbh->prepare(qq{select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp )
from videos as v, users as u
where u.id = v.userid order by v.timestamp desc}) or die $dbh->errstr;
$sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$sth->execute($query->param('query')) or die $dbh->errstr;
$sth->execute(@args) or die $dbquery;
$page->{'results'}->{'lastpage'} = $lastpage;
$page->{'results'}->{'currentpage'} = $currentpage;
#get every returned value
while (my ($id, $title, $description, $username, $timestamp) = $sth->fetchrow_array())
while (my ($id, $title, $description, $username, $timestamp, $relevance) = $sth->fetchrow_array())
{
#before code cleanup, this was a really obfuscated array/hash creation
push @{ $page->{'results'}->{'result'} },
@ -105,10 +117,9 @@ elsif($query->param('sort'))
{
'cc:Work' =>
{
'rdf:about' => "$domain/download/$id",
'rdf:about' => "./video.pl?title=$title&id=$id",
'dc:title' => [$title],
'dc:date' => [$timestamp],
'dc:identifier' => ["$domain/video/$title/$id"],
'dc:publisher' => [$username]
},
'cc:License' =>
@ -133,15 +144,6 @@ elsif($query->param('sort'))
}
else
{
%page = ();
#if a username is associated with session id, username is nonempty
$page->{'username'} = get_username_from_sid($session->id);
$page->{'locale'} = $locale;
$page->{'stylesheet'} = $stylesheet;
$page->{'xmlns:dc'} = $xmlns_dc;
$page->{'xmlns:cc'} = $xmlns_cc;
$page->{'xmlns:rdf'} = $xmlns_rdf;
$page->{'message'}->{'type'} = "error";
$page->{'message'}->{'text'} = "error_202c";

@ -155,7 +155,7 @@ if($query->url_param('title') or $query->url_param('id'))
while (my ($commentid, $text, $username, $timestamp) = $sth->fetchrow_array())
{
push @{ $page->{'comments'}->{'comment'} }, {
'text' => [decode_utf8($text)],
'text' => [$text],
'username' => $username,
'timestamp' => $timestamp,
'id' => $commentid
@ -177,7 +177,7 @@ if($query->url_param('title') or $query->url_param('id'))
else
{
#when an ambigous title was passed there may me many results - display them like search.pl does
$page->{results}->{query} = decode_utf8($query->url_param('title'));
$page->{results}->{query} = $query->url_param('title');
#get every returned value
while (my ($id, $title, $description, $userid, $timestamp) = $sth->fetchrow_array())
{

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml version="1.0" encoding="utf8" ?>
<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
@ -283,6 +283,82 @@
<xsl:template match="results">
<xsl:call-template name="searchbar"/>
<div>
<xsl:choose>
<xsl:when test="@currentpage&lt;=1">
&lt;&lt; &lt;
</xsl:when>
<xsl:otherwise>
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('search.pl?query=', @query, '&amp;orderby=', @orderby, '&amp;page=1')" />
</xsl:attribute>
&lt;&lt;
</a>
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('search.pl?query=', @query, '&amp;orderby=', @orderby, '&amp;page=', @currentpage - 1)" />
</xsl:attribute>
&lt;
</a>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="@currentpage > 2">
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('search.pl?query=', @query, '&amp;orderby=', @orderby, '&amp;page=', @currentpage - 2)" />
</xsl:attribute>
<xsl:value-of select="@currentpage - 2" />
</a>
</xsl:if>
<xsl:if test="@currentpage > 1">
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('search.pl?query=', @query, '&amp;orderby=', @orderby, '&amp;page=', @currentpage - 1)" />
</xsl:attribute>
<xsl:value-of select="@currentpage - 1" />
</a>
</xsl:if>
<xsl:value-of select="@currentpage" />
<xsl:variable name="temp" select="@lastpage - @currentpage" />
<xsl:if test="$temp > 0">
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('search.pl?query=', @query, '&amp;orderby=', @orderby, '&amp;page=', @currentpage + 1)" />
</xsl:attribute>
<xsl:value-of select="@currentpage + 1" />
</a>
</xsl:if>
<xsl:if test="$temp > 1">
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('search.pl?query=', @query, '&amp;orderby=', @orderby, '&amp;page=', @currentpage + 2)" />
</xsl:attribute>
<xsl:value-of select="@currentpage + 2" />
</a>
</xsl:if>
<xsl:choose>
<xsl:when test="@lastpage&lt;=@currentpage">
&gt; &gt;&gt;
</xsl:when>
<xsl:otherwise>
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('search.pl?query=', @query, '&amp;orderby=', @orderby, '&amp;page=', @currentpage + 1)" />
</xsl:attribute>
&gt;
</a>
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('search.pl?query=', @query, '&amp;orderby=', @orderby, '&amp;page=', @lastpage)" />
</xsl:attribute>
&gt;&gt;
</a>
</xsl:otherwise>
</xsl:choose>
</div>
<div>_</div>
<div>
<xsl:value-of select="$locale_strings/str[@id='results_for_query']" />:
"<xsl:value-of select="@query" />"

Loading…
Cancel
Save