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')
{
$page->{'account'}->{'show'} = 'settings';
#results per page
#language
#cortado or plugin
}
elsif($query->param('show') eq 'uploads')
{
$page->{'account'}->{'show'} = 'uploads';
$page->{'results'}->{'scriptname'} = 'account.pl';
$page->{'results'}->{'argument'} = 'show';
$page->{'results'}->{'value'} = 'uploads';
$page->{'results'}->{'orderby'} = $query->param('orderby');
$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
$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 = ?)
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 = ?)";
if($query->param('orderby'))
@ -70,65 +73,7 @@ if($userinfo->{'username'})
}
}
#prepare query
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;
fill_results($session->id, $session->id);
}
else
{

View file

@ -75,3 +75,73 @@ sub get_page_array
$page->{'xmlns:cc'} = $xmlns_cc;
$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

@ -127,6 +127,13 @@
<str id="duration">duration</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>
</xsl:stylesheet>

View file

@ -13,19 +13,19 @@ $session = new CGI::Session;
#check if query is set
if($query->param('query') or $query->param('orderby'))
{
$page->{'search'} = [''];
$page->{'results'}->{'scriptname'} = 'search.pl';
$page->{'results'}->{'argument'} = 'query';
$page->{'results'}->{'value'} = $query->param('query');
$page->{'results'}->{'orderby'} = $query->param('orderby');
$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 = ();
#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'))
{
@ -80,67 +80,7 @@ if($query->param('query') or $query->param('orderby'))
}
}
#prepare query
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;
fill_results(@args);
}
else
{

View file

@ -12,6 +12,8 @@
<str id=""></str>
<str id=""></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_login">/login.pl</str>
<str id="page_login-openid">/login.pl?action=openid</str>

View file

@ -4,6 +4,7 @@ require "functions.pl";
#create or resume session
CGI::Session->name($session_name);
$query = new CGI;
my $session = new CGI::Session;
@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 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.license, filesize, duration, width, height, fps, viewcount,
downloadcount
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;
v.license, filesize, duration, width, height, fps, viewcount, downloadcount
from videos as v, users as u where v.id = ? and u.id=v.userid";
@args = ($query->url_param('id'));
}
else
{
#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.license, filesize, duration, width, height, fps, viewcount,
downloadcount
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;
v.license, filesize, duration, width, height, fps, viewcount, downloadcount
from videos as v, users as u where v.title = ? and u.id=v.userid";
@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 there was a title passed, then perform a search
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.license, filesize, duration, width, height, fps, viewcount,
downloadcount
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;
$rowcount = $sth->execute($query->url_param('title')) or die $dbh->errstr;
v.license, filesize, duration, width, height, fps, viewcount, downloadcount";
$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)";
@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
@ -65,7 +72,7 @@ elsif($query->url_param('title') or $query->url_param('id'))
elsif($rowcount == 1)
{
#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,
$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:subject' => [$subject],
'dc:description' => [$description],
'dc:publisher' => [$username],
'dc:publisher' => [$publisher],
'dc:contributor' => [$contributor],
'dc:date' => [$timestamp],
'dc:identifier' => ["$domain/video/$title/$id"],
@ -177,33 +184,17 @@ elsif($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} = $query->url_param('title');
#get every returned value
while (my ($id, $title, $description, $userid, $timestamp) = $sth->fetchrow_array())
{
#before code cleanup, this was a really obfuscated array/hash creation
push @{ $page->{'results'}->{'result'} },
{
'thumbnail' => ['./video-stills/225x150/4chan_city_mashup.png'],
'rdf:RDF' =>
{
'cc:Work' =>
{
'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;
$page->{'search'} = [''];
$page->{'results'}->{'scriptname'} = 'video.pl';
$page->{'results'}->{'argument'} = 'title';
$page->{'results'}->{'value'} = $query->param('title');
$page->{'results'}->{'orderby'} = $query->param('orderby');
$page->{'results'}->{'sort'} = $query->param('sort');
$page->{'search'} = [''];
fill_results(@args);
}
#close db

View file

@ -166,12 +166,15 @@
<xsl:when test="//uploadform">
<xsl:call-template name="uploadform"/>
</xsl:when>
<xsl:when test="//results">
<xsl:call-template name="results"/>
<xsl:when test="//search">
<xsl:call-template name="search"/>
</xsl:when>
<xsl:when test="//video">
<xsl:call-template name="video"/>
</xsl:when>
<xsl:when test="//account">
<xsl:call-template name="account"/>
</xsl:when>
</xsl:choose>
<div class="footer">
@ -246,6 +249,10 @@
</xsl:template>
<xsl:template name="search">
<xsl:call-template name="results"/>
</xsl:template>
<xsl:template name="searchbar">
<div class="logo-small-top">
@ -503,4 +510,38 @@
</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>