centralized searching as I did with the xml creation in my last commit - now all searches in accoun.pl, search.pl and video.pl use the same code. also improved the account interface

git-svn-id: http://yolanda.mister-muffin.de/svn@156 7eef14d0-6ed0-489d-bf55-20463b2d70db
This commit is contained in:
josch 2007-10-29 16:47:16 +00:00
parent 9a1de07513
commit 71b996fece
8 changed files with 173 additions and 176 deletions

View file

@ -15,26 +15,29 @@ if($userinfo->{'username'})
{ {
if($query->param('show') eq 'settings') if($query->param('show') eq 'settings')
{ {
$page->{'account'}->{'show'} = 'settings';
#results per page #results per page
#language #language
#cortado or plugin #cortado or plugin
} }
elsif($query->param('show') eq 'uploads') elsif($query->param('show') eq 'uploads')
{ {
$page->{'account'}->{'show'} = 'uploads';
$page->{'results'}->{'scriptname'} = 'account.pl'; $page->{'results'}->{'scriptname'} = 'account.pl';
$page->{'results'}->{'argument'} = 'show'; $page->{'results'}->{'argument'} = 'show';
$page->{'results'}->{'value'} = 'uploads'; $page->{'results'}->{'value'} = 'uploads';
$page->{'results'}->{'orderby'} = $query->param('orderby'); $page->{'results'}->{'orderby'} = $query->param('orderby');
$page->{'results'}->{'sort'} = $query->param('sort'); $page->{'results'}->{'sort'} = $query->param('sort');
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr;
#build mysql query #build mysql query
$dbquery = "(select v.id, v.title, u.username, from_unixtime( v.timestamp ) as timestamp, v.duration, v.viewcount $dbquery = "(select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp ) as timestamp,
v.creator, v.subject, v.contributor, v.source, v.language, v.coverage, v.rights,
v.license, filesize, duration, width, height, fps, viewcount, downloadcount
from videos as v, users as u where v.userid = u.id and u.sid = ?) from videos as v, users as u where v.userid = u.id and u.sid = ?)
union union
(select v.id, v.title, u.username, from_unixtime( v.timestamp ) as timestamp, 0, 0 (select v.id, v.title, '', u.username, from_unixtime( v.timestamp ) as timestamp,
v.creator, v.subject, v.contributor, v.source, v.language, v.coverage, v.rights,
v.license, 0, 0, 0, 0, 0, 0, 0
from uploaded as v, users as u where v.userid = u.id and u.sid = ?)"; from uploaded as v, users as u where v.userid = u.id and u.sid = ?)";
if($query->param('orderby')) if($query->param('orderby'))
@ -70,65 +73,7 @@ if($userinfo->{'username'})
} }
} }
#prepare query fill_results($session->id, $session->id);
my $sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$resultcount = $sth->execute($session->id, $session->id) or die $dbh->errstr;
$pagesize = 2;
#rediculous but funny round up, will fail with 100000000000000 results per page
#on 0.0000000000001% of all queries - this is a risk we can handle
$lastpage = int($resultcount/$pagesize+0.99999999999999);
$currentpage = $query->param('page') or $currentpage = 1;
$dbquery .= " limit ".($currentpage-1)*$pagesize.", ".$pagesize;
#prepare query
$sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$sth->execute($session->id, $session->id) or die $dbquery;
$page->{'results'}->{'lastpage'} = $lastpage;
$page->{'results'}->{'currentpage'} = $currentpage;
$page->{'results'}->{'resultcount'} = $resultcount;
$page->{'results'}->{'pagesize'} = $pagesize;
#get every returned value
while (my ($id, $title, $publisher, $timestamp, $duration, $viewcount) = $sth->fetchrow_array())
{
#before code cleanup, this was a really obfuscated array/hash creation
push @{ $page->{'results'}->{'result'} },
{
'thumbnail' => $duration == 0 ? "/images/tango/video-x-generic.png" : "/video-stills/$id",
'duration' => $duration,
'viewcount' => $viewcount,
'edit' => $userinfo->{'username'} eq $publisher ? "true" : "false",
'rdf:RDF' =>
{
'cc:Work' =>
{
'dc:title' => [$title],
'dc:date' => [$timestamp],
'dc:identifier' => ["$domain/video/$title/$id" . ($duration == 0 ? "/edit=true" : "") ],
'dc:publisher' => [$publisher]
},
'cc:License' =>
{
'rdf:about' => 'http://creativecommons.org/licenses/GPL/2.0/'
}
}
};
}
#finish query
$sth->finish() or die $dbh->errstr;
#close db
$dbh->disconnect() or die $dbh->errstr;
} }
else else
{ {

View file

@ -75,3 +75,73 @@ sub get_page_array
$page->{'xmlns:cc'} = $xmlns_cc; $page->{'xmlns:cc'} = $xmlns_cc;
$page->{'xmlns:rdf'} = $xmlns_rdf; $page->{'xmlns:rdf'} = $xmlns_rdf;
} }
sub fill_results
{
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr;
#prepare query
my $sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$resultcount = $sth->execute(@_) or die $dbh->errstr;
$pagesize = $query->param('pagesize') or $pagesize = 5;
#rediculous but funny round up, will fail with 100000000000000 results per page
#on 0.0000000000001% of all queries - this is a risk we can handle
$lastpage = int($resultcount/$pagesize+0.99999999999999);
$currentpage = $query->param('page') or $currentpage = 1;
$dbquery .= " limit ".($currentpage-1)*$pagesize.", ".$pagesize;
#prepare query
$sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$sth->execute(@_) or die $dbquery;
$page->{'results'}->{'lastpage'} = $lastpage;
$page->{'results'}->{'currentpage'} = $currentpage;
$page->{'results'}->{'resultcount'} = $resultcount eq '0E0' ? 0 : $resultcount;
$page->{'results'}->{'pagesize'} = $pagesize;
#get every returned value
while (my ($id, $title, $description, $publisher, $timestamp, $creator, $subject,
$contributor, $source, $language, $coverage, $rights, $license,
$filesize, $duration, $width, $height, $fps, $viewcount, $downloadcount) = $sth->fetchrow_array())
{
#before code cleanup, this was a really obfuscated array/hash creation
push @{ $page->{'results'}->{'result'} },
{
'thumbnail' => "./video-stills/$id",
'duration' => $duration,
'viewcount' => $viewcount,
'edit' => $userinfo->{'username'} eq $publisher ? "true" : "false",
'rdf:RDF' =>
{
'cc:Work' =>
{
'rdf:about' => "$domain/download/$id",
'dc:title' => [$title],
'dc:creator' => [$creator],
'dc:date' => [$timestamp],
'dc:identifier' => ["$domain/video/$title/$id"],
'dc:publisher' => [$publisher]
},
'cc:License' =>
{
'rdf:about' => 'http://creativecommons.org/licenses/GPL/2.0/'
}
}
};
}
#finish query
$sth->finish() or die $dbh->errstr;
#close db
$dbh->disconnect() or die $dbh->errstr;
}

View file

@ -126,6 +126,13 @@
<str id="duration">duration</str> <str id="duration">duration</str>
<str id="viewcount">viewcount</str> <str id="viewcount">viewcount</str>
<!-- account page -->
<str id="account_uploads">my uploads</str>
<str id="account_settings">edit my settings</str>
<str id="account_bookmarks">my bookmarks</str>
<str id=""></str>
</strings> </strings>

View file

@ -13,19 +13,19 @@ $session = new CGI::Session;
#check if query is set #check if query is set
if($query->param('query') or $query->param('orderby')) if($query->param('query') or $query->param('orderby'))
{ {
$page->{'search'} = [''];
$page->{'results'}->{'scriptname'} = 'search.pl'; $page->{'results'}->{'scriptname'} = 'search.pl';
$page->{'results'}->{'argument'} = 'query'; $page->{'results'}->{'argument'} = 'query';
$page->{'results'}->{'value'} = $query->param('query'); $page->{'results'}->{'value'} = $query->param('query');
$page->{'results'}->{'orderby'} = $query->param('orderby'); $page->{'results'}->{'orderby'} = $query->param('orderby');
$page->{'results'}->{'sort'} = $query->param('sort'); $page->{'results'}->{'sort'} = $query->param('sort');
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr;
my @args = (); my @args = ();
#build mysql query #build mysql query
$dbquery = "select v.id, v.title, v.creator, v.description, u.username, from_unixtime( v.timestamp ), v.duration, v.viewcount"; $dbquery = "select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp ),
v.creator, v.subject, v.contributor, v.source, v.language, v.coverage, v.rights,
v.license, filesize, duration, width, height, fps, viewcount, downloadcount";
if($query->param('query')) if($query->param('query'))
{ {
@ -80,67 +80,7 @@ if($query->param('query') or $query->param('orderby'))
} }
} }
#prepare query fill_results(@args);
my $sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$resultcount = $sth->execute(@args) or die $dbh->errstr;
$pagesize = $query->param('pagesize') or $pagesize = 5;
#rediculous but funny round up, will fail with 100000000000000 results per page
#on 0.0000000000001% of all queries - this is a risk we can handle
$lastpage = int($resultcount/$pagesize+0.99999999999999);
$currentpage = $query->param('page') or $currentpage = 1;
$dbquery .= " limit ".($currentpage-1)*$pagesize.", ".$pagesize;
#prepare query
$sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$sth->execute(@args) or die $dbquery;
$page->{'results'}->{'lastpage'} = $lastpage;
$page->{'results'}->{'currentpage'} = $currentpage;
$page->{'results'}->{'resultcount'} = $resultcount eq '0E0' ? 0 : $resultcount;
$page->{'results'}->{'pagesize'} = $pagesize;
#get every returned value
while (my ($id, $title, $creator, $description, $publisher, $timestamp, $duration, $viewcount, $relevance) = $sth->fetchrow_array())
{
#before code cleanup, this was a really obfuscated array/hash creation
push @{ $page->{'results'}->{'result'} },
{
'thumbnail' => "./video-stills/$id",
'duration' => $duration,
'viewcount' => $viewcount,
'edit' => $userinfo->{'username'} eq $publisher ? "true" : "false",
'rdf:RDF' =>
{
'cc:Work' =>
{
'rdf:about' => "$domain/download/$id",
'dc:title' => [$title],
'dc:creator' => [$creator],
'dc:date' => [$timestamp],
'dc:identifier' => ["$domain/video/$title/$id"],
'dc:publisher' => [$publisher]
},
'cc:License' =>
{
'rdf:about' => 'http://creativecommons.org/licenses/GPL/2.0/'
}
}
};
}
#finish query
$sth->finish() or die $dbh->errstr;
#close db
$dbh->disconnect() or die $dbh->errstr;
} }
else else
{ {

View file

@ -12,6 +12,8 @@
<str id=""></str> <str id=""></str>
<str id=""></str>--> <str id=""></str>-->
<str id="page_account">/account.pl</str> <str id="page_account">/account.pl</str>
<str id="page_account_settings">/account.pl?show=settings</str>
<str id="page_account_uploads">/account.pl?show=uploads</str>
<str id="page_bookmarks"></str> <str id="page_bookmarks"></str>
<str id="page_login">/login.pl</str> <str id="page_login">/login.pl</str>
<str id="page_login-openid">/login.pl?action=openid</str> <str id="page_login-openid">/login.pl?action=openid</str>

View file

@ -4,6 +4,7 @@ require "functions.pl";
#create or resume session #create or resume session
CGI::Session->name($session_name); CGI::Session->name($session_name);
$query = new CGI;
my $session = new CGI::Session; my $session = new CGI::Session;
@userinfo = get_userinfo_from_sid($session->id); @userinfo = get_userinfo_from_sid($session->id);

View file

@ -23,35 +23,42 @@ elsif($query->url_param('title') or $query->url_param('id'))
if($query->url_param('id')) if($query->url_param('id'))
{ {
#if id is passed ignore title and check for the id #if id is passed ignore title and check for the id
$sth = $dbh->prepare(qq{select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp ), $dbquery = "select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp ),
v.creator, v.subject, v.contributor, v.source, v.language, v.coverage, v.rights, v.creator, v.subject, v.contributor, v.source, v.language, v.coverage, v.rights,
v.license, filesize, duration, width, height, fps, viewcount, v.license, filesize, duration, width, height, fps, viewcount, downloadcount
downloadcount from videos as v, users as u where v.id = ? and u.id=v.userid";
from videos as v, users as u where v.id = ? and u.id=v.userid }) or die $dbh->errstr;
$rowcount = $sth->execute($query->url_param('id')) or die $dbh->errstr; @args = ($query->url_param('id'));
} }
else else
{ {
#if no id was passed there has to be a title we search for #if no id was passed there has to be a title we search for
$sth = $dbh->prepare(qq{select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp ), $dbquery = "select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp ),
v.creator, v.subject, v.contributor, v.source, v.language, v.coverage, v.rights, v.creator, v.subject, v.contributor, v.source, v.language, v.coverage, v.rights,
v.license, filesize, duration, width, height, fps, viewcount, v.license, filesize, duration, width, height, fps, viewcount, downloadcount
downloadcount from videos as v, users as u where v.title = ? and u.id=v.userid";
from videos as v, users as u where v.title = ? and u.id=v.userid }) or die $dbh->errstr;
$rowcount = $sth->execute($query->url_param('title')) or die $dbh->errstr; @args = ($query->url_param('title'));
} }
$sth = $dbh->prepare($dbquery);
$rowcount = $sth->execute(@args) or die $dbh->errstr;
#if the args are wrong there my be zero results #if the args are wrong there my be zero results
#if there was a title passed, then perform a search #if there was a title passed, then perform a search
if($rowcount == 0 and $query->url_param('title')) if($rowcount == 0 and $query->url_param('title'))
{ {
$sth = $dbh->prepare(qq{select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp ), $dbquery = "select v.id, v.title, v.description, u.username, from_unixtime( v.timestamp ),
v.creator, v.subject, v.contributor, v.source, v.language, v.coverage, v.rights, v.creator, v.subject, v.contributor, v.source, v.language, v.coverage, v.rights,
v.license, filesize, duration, width, height, fps, viewcount, v.license, filesize, duration, width, height, fps, viewcount, downloadcount";
downloadcount $dbquery .= ", match(v.title, v.description, v.subject) against( ? in boolean mode) as relevance";
from videos as v, users as u where match(v.title, v.description, v.subject) against( ? ) $dbquery .= " from videos as v, users as u where u.id = v.userid";
and u.id=v.userid }) or die $dbh->errstr; $dbquery .= " and match(v.title, v.description, v.subject) against( ? in boolean mode)";
$rowcount = $sth->execute($query->url_param('title')) or die $dbh->errstr;
@args = ($query->url_param('title'));
$sth = $dbh->prepare($dbquery);
$rowcount = $sth->execute(@args) or die $dbh->errstr;
} }
#from this point on, do not use $query->param('id') anymore - we do not know what was given #from this point on, do not use $query->param('id') anymore - we do not know what was given
@ -65,7 +72,7 @@ elsif($query->url_param('title') or $query->url_param('id'))
elsif($rowcount == 1) elsif($rowcount == 1)
{ {
#if there was a single result, display the video #if there was a single result, display the video
my ($id, $title, $description, $username, $timestamp, $creator, $subject, my ($id, $title, $description, $publisher, $timestamp, $creator, $subject,
$contributor, $source, $language, $coverage, $rights, $license, $contributor, $source, $language, $coverage, $rights, $license,
$filesize, $duration, $width, $height, $fps, $viewcount, $downloadcount) = $sth->fetchrow_array(); $filesize, $duration, $width, $height, $fps, $viewcount, $downloadcount) = $sth->fetchrow_array();
@ -131,7 +138,7 @@ elsif($query->url_param('title') or $query->url_param('id'))
'dc:creator' => [$creator], 'dc:creator' => [$creator],
'dc:subject' => [$subject], 'dc:subject' => [$subject],
'dc:description' => [$description], 'dc:description' => [$description],
'dc:publisher' => [$username], 'dc:publisher' => [$publisher],
'dc:contributor' => [$contributor], 'dc:contributor' => [$contributor],
'dc:date' => [$timestamp], 'dc:date' => [$timestamp],
'dc:identifier' => ["$domain/video/$title/$id"], 'dc:identifier' => ["$domain/video/$title/$id"],
@ -177,33 +184,17 @@ elsif($query->url_param('title') or $query->url_param('id'))
else else
{ {
#when an ambigous title was passed there may me many results - display them like search.pl does #when an ambigous title was passed there may me many results - display them like search.pl does
$page->{results}->{query} = $query->url_param('title');
#get every returned value $page->{'search'} = [''];
while (my ($id, $title, $description, $userid, $timestamp) = $sth->fetchrow_array()) $page->{'results'}->{'scriptname'} = 'video.pl';
{ $page->{'results'}->{'argument'} = 'title';
#before code cleanup, this was a really obfuscated array/hash creation $page->{'results'}->{'value'} = $query->param('title');
push @{ $page->{'results'}->{'result'} }, $page->{'results'}->{'orderby'} = $query->param('orderby');
{ $page->{'results'}->{'sort'} = $query->param('sort');
'thumbnail' => ['./video-stills/225x150/4chan_city_mashup.png'],
'rdf:RDF' => $page->{'search'} = [''];
{
'cc:Work' => fill_results(@args);
{
'rdf:about' => "./video.pl?title=$title&id=".$id,
'dc:title' => [$title],
'dc:date' => [$timestamp],
'dc:publisher' => [get_username_from_id($userid)],
'dc:description'=> [$description]
},
'cc:License' =>
{
'rdf:about' => 'http://creativecommons.org/licenses/GPL/2.0/'
}
}
};
}
#finish query
$sth->finish() or die $dbh->errstr;
} }
#close db #close db

View file

@ -166,12 +166,15 @@
<xsl:when test="//uploadform"> <xsl:when test="//uploadform">
<xsl:call-template name="uploadform"/> <xsl:call-template name="uploadform"/>
</xsl:when> </xsl:when>
<xsl:when test="//results"> <xsl:when test="//search">
<xsl:call-template name="results"/> <xsl:call-template name="search"/>
</xsl:when> </xsl:when>
<xsl:when test="//video"> <xsl:when test="//video">
<xsl:call-template name="video"/> <xsl:call-template name="video"/>
</xsl:when> </xsl:when>
<xsl:when test="//account">
<xsl:call-template name="account"/>
</xsl:when>
</xsl:choose> </xsl:choose>
<div class="footer"> <div class="footer">
@ -246,6 +249,10 @@
</xsl:template> </xsl:template>
<xsl:template name="search">
<xsl:call-template name="results"/>
</xsl:template>
<xsl:template name="searchbar"> <xsl:template name="searchbar">
<div class="logo-small-top"> <div class="logo-small-top">
@ -503,4 +510,38 @@
</xsl:template> </xsl:template>
<xsl:template name="account">
<div>
<a>
<xsl:attribute name="href">
<xsl:value-of select="$site_strings[@id='page_account_uploads']" />
</xsl:attribute>
<xsl:value-of select="$locale_strings[@id='account_uploads']" />
</a>
<xsl:value-of select="$locale_strings[@id='separator']" />
<a>
<xsl:attribute name="href">
<xsl:value-of select="$site_strings[@id='page_account_settings']" />
</xsl:attribute>
<xsl:value-of select="$locale_strings[@id='account_settings']" />
</a>
<xsl:value-of select="$locale_strings[@id='separator']" />
<a>
<xsl:attribute name="href">
<xsl:value-of select="$site_strings[@id='page_account_bookmarks']" />
</xsl:attribute>
<xsl:value-of select="$locale_strings[@id='account_bookmarks']" />
</a>
</div>
<xsl:choose>
<xsl:when test="//account/@show='uploads'">
<xsl:call-template name="results"/>
</xsl:when>
<xsl:when test="//account/@show='settings'">
settings
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet> </xsl:stylesheet>