From b6cac05c7b4e554f5be0272f462c1dcc3e6a4415 Mon Sep 17 00:00:00 2001 From: josch Date: Wed, 24 Oct 2007 11:25:18 +0000 Subject: [PATCH] major mysql cleanup git-svn-id: http://yolanda.mister-muffin.de/svn@134 7eef14d0-6ed0-489d-bf55-20463b2d70db --- trunk/download.pl | 18 +++++++----------- trunk/functions.pl | 28 ---------------------------- trunk/gnutube_daemon.pl | 38 ++++++++++++-------------------------- trunk/login.pl | 16 ++++------------ trunk/register.pl | 9 ++------- trunk/search.pl | 17 +++++++++++------ trunk/upload.pl | 3 +-- trunk/uploader.pl | 13 ++++++------- trunk/video.pl | 25 +++++++++++++------------ 9 files changed, 56 insertions(+), 111 deletions(-) diff --git a/trunk/download.pl b/trunk/download.pl index 7b67b55..4592f5d 100644 --- a/trunk/download.pl +++ b/trunk/download.pl @@ -29,16 +29,14 @@ if($query->param('id')) if($rowcount > 0) { #video is in database - increase referercount - $sth = $dbh->prepare(qq{update referer set count=count+1 where videoid = ? and referer = ? }) or die $dbh->errstr; - $sth->execute($query->param('id'), $referer) or die $dbh->errstr; - $sth->finish(); + $dbh->do(qq{update referer set count=count+1 where videoid = ? and referer = ? }, + undef, $query->param('id'), $referer) or die $dbh->errstr; } else { #add new referer - $sth = $dbh->prepare(qq{insert into referer (videoid, referer) values (?, ?) }) or die $dbh->errstr; - $sth->execute($query->param('id'), $referer) or die $dbh->errstr; - $sth->finish(); + $dbh->do(qq{insert into referer (videoid, referer) values (?, ?) }, + undef, $query->param('id'), $referer) or die $dbh->errstr; } } @@ -46,16 +44,14 @@ if($query->param('id')) if($query->param('view')) { #seems we only want to watch it - update viewcount - $sth = $dbh->prepare(qq{update videos set viewcount=viewcount+1 where id = ? }); - $sth->execute($query->param('id')); + $dbh->do(qq{update videos set viewcount=viewcount+1 where id = ? }, undef, $query->param('id')) or die $dbh->errstr; print $query->header(-type=>'application/ogg'); } else { #video is being downloaded - update downloadcount - $sth = $dbh->prepare(qq{update videos set downloadcount=downloadcount+1 where id = ? }); - $sth->execute($query->param('id')); + $dbh->do(qq{update videos set downloadcount=downloadcount+1 where id = ? }, undef, $query->param('id')) or die $dbh->errstr; print $query->header(-type=>'application/x-download', -attachment=>$title.".ogv"); @@ -68,9 +64,9 @@ if($query->param('id')) } else { + #there is no video with this id %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; diff --git a/trunk/functions.pl b/trunk/functions.pl index ae12c0a..496b731 100644 --- a/trunk/functions.pl +++ b/trunk/functions.pl @@ -55,34 +55,6 @@ sub get_username_from_sid return $username; } -#return a username from passed id -sub get_username_from_id -{ - #get parameters - my ($id) = @_; - - #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 username from users where id = ?}) or die $dbh->errstr; - - #execute it - $sth->execute($id) or die $dbh->errstr; - - #save the resulting username - my ($username) = $sth->fetchrow_array(); - - #finish query - $sth->finish() or die $dbh->errstr; - - #close db - $dbh->disconnect() or die $dbh->errstr; - - #return - return $username; -} - #return a username from passed session id sub get_userid_from_sid { diff --git a/trunk/gnutube_daemon.pl b/trunk/gnutube_daemon.pl index e4a1cf6..444dcf6 100755 --- a/trunk/gnutube_daemon.pl +++ b/trunk/gnutube_daemon.pl @@ -60,9 +60,7 @@ while(1) appendlog $id, "invalid stream"; #write status 2 to uploaded table - $sth = $dbh->prepare(qq{update uploaded set status = ? where id = ?}) or interrupt $dbh->errstr; - $sth->execute(2, $id) or interrupt $dbh->errstr; - $sth->finish() or interrupt $dbh->errstr; + $dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 2, $id) or interrupt $dbh->errstr; unlink "$gnutube_root/tmp/$id"; } elsif ($info =~ /I\/O error occured/) @@ -70,9 +68,7 @@ while(1) appendlog $id, "file not found"; #write status 3 to uploaded table - $sth = $dbh->prepare(qq{update uploaded set status = ? where id = ?}) or interrupt $dbh->errstr; - $sth->execute(3, $id) or interrupt $dbh->errstr; - $sth->finish() or interrupt $dbh->errstr; + $dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 3, $id) or interrupt $dbh->errstr; unlink "$gnutube_root/tmp/$id"; } elsif ($info =~ /Unknown format/ or $info =~ /could not find codec parameters/) @@ -80,9 +76,7 @@ while(1) appendlog $id, "file is no video"; #write status 4 to uploaded table - $sth = $dbh->prepare(qq{update uploaded set status = ? where id = ?}) or interrupt $dbh->errstr; - $sth->execute(4, $id) or interrupt $dbh->errstr; - $sth->finish() or interrupt $dbh->errstr; + $dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 4, $id) or interrupt $dbh->errstr; unlink "$gnutube_root/tmp/$id"; } else @@ -103,9 +97,7 @@ while(1) appendlog "$id, video already uploaded: $resultid"; #write status 5 to uploaded table - $sth = $dbh->prepare(qq{update uploaded set status = ? where id = ?}) or interrupt $dbh->errstr; - $sth->execute(5, $id) or interrupt $dbh->errstr; - $sth->finish() or interrupt $dbh->errstr; + $dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 5, $id) or interrupt $dbh->errstr; unlink "$gnutube_root/tmp/$id"; } else @@ -121,9 +113,7 @@ while(1) appendlog $id, "a stream is missing or video is corrupt"; #write status 2 to uploaded table - $sth = $dbh->prepare(qq{update uploaded set status = ? where id = ?}) or interrupt $dbh->errstr; - $sth->execute(2, $id) or interrupt $dbh->errstr; - $sth->finish() or interrupt $dbh->errstr; + $dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 2, $id) or interrupt $dbh->errstr; unlink "$gnutube_root/tmp/$id"; } else @@ -155,12 +145,11 @@ while(1) appendlog $id, "file already is ogg-theora/vorbis"; #add video to videos table - $sth = $dbh->prepare(qq{insert into videos select id, title, description, userid, timestamp, creator, + $dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator, subject, contributor, source, language, coverage, rights, license, notice, derivativeworks, sharealike, commercialuse, ?, ?, ?, ?, ?, ?, 0, 0 - from uploaded where id = ?}) or interrupt $dbh->errstr; - $sth->execute($filesize, $duration, $width, $height, $fps, $sha, $id) or interrupt $dbh->errstr; - $sth->finish() or interrupt $dbh->errstr; + from uploaded where id = ?}, undef, $filesize, $duration, $width, + $height, $fps, $sha, $id) or interrupt $dbh->errstr; #move video move "$gnutube_root/tmp/$id", "$gnutube_root/videos/$id"; @@ -173,21 +162,18 @@ while(1) appendlog $id, $audio, $video, $width, $height, $fps, $duration, $sha; #add video to videos table - $sth = $dbh->prepare(qq{insert into videos select id, title, description, userid, timestamp, creator, + $dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator, subject, contributor, source, language, coverage, rights, license, notice, derivativeworks, sharealike, commercialuse, ?, ?, ?, ?, ?, ?, 0, 0 - from uploaded where id = ?}) or interrupt $dbh->errstr; - $sth->execute($filesize, $duration, $width, $height, $fps, $sha, $id) or interrupt $dbh->errstr; - $sth->finish() or interrupt $dbh->errstr; + from uploaded where id = ?}, undef, $filesize, $duration, $width, + $height, $fps, $sha, $id) or interrupt $dbh->errstr; #delete temp file unlink "$gnutube_root/tmp/$id"; } #write status 1 to uploaded table - $sth = $dbh->prepare(qq{update uploaded set status = ? where id = ?}) or interrupt $dbh->errstr; - $sth->execute(1, $id) or interrupt $dbh->errstr; - $sth->finish() or interrupt $dbh->errstr; + $dbh->do(qq{update uploaded set status = ? where id = ?}, undef, 1, $id) or interrupt $dbh->errstr; } } } diff --git a/trunk/login.pl b/trunk/login.pl index 30d38b0..cc8443a 100644 --- a/trunk/login.pl +++ b/trunk/login.pl @@ -27,9 +27,7 @@ if($query->param('action')) if($sth->fetchrow_array()) { #store session id in database - $sth = $dbh->prepare(qq{update users set sid = ? where username = ? }); - $sth->execute($session->id, $query->param('user')); - $sth->finish(); + $dbh->do(qq{update users set sid = ? where username = ? }, undef, $session->id, $query->param('user')) or die $dbh->errstr; print $query->redirect("index.pl?information=information_logged_in"); } else @@ -92,16 +90,12 @@ if($query->param('action')) if($sth->fetchrow_array()) { #store session id in database - $sth = $dbh->prepare(qq{update users set sid = ? where username = ? }); - $sth->execute($session->id, $verified_url); - $sth->finish(); + $dbh->do(qq{update users set sid = ? where username = ? }, undef, $session->id, $verified_url) or die $dbh->errstr; } else { #add openid user to dabase - $sth = $dbh->prepare(qq{insert into users (username, sid) values ( ?, ? ) }); - $sth->execute($verified_url, $session->id); - $sth->finish(); + $dbh->do(qq{insert into users (username, sid) values ( ?, ? ) }, undef, $verified_url, $session->id) or die $dbh->errstr; } print $query->redirect("index.pl?information=information_logged_in"); @@ -124,9 +118,7 @@ if($query->param('action')) { #if logout is requested #remove sid from database - $sth = $dbh->prepare(qq{update users set sid = '' where username = ?}); - $sth->execute(get_username_from_sid($session->id)); - $sth->finish(); + $dbh->do(qq{update users set sid = '' where username = ?}, undef, get_username_from_sid($session->id)) or die $dbh->errstr; $session->delete(); print $session->header(); print "logged out"; diff --git a/trunk/register.pl b/trunk/register.pl index d13656b..7780893 100644 --- a/trunk/register.pl +++ b/trunk/register.pl @@ -14,13 +14,8 @@ if($query->param('user') and $query->param('pass')) my $dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass) or die $dbh->errstr; #do query - $sth = $dbh->prepare(qq{insert into users (username, password) values ( ?, password( ? ))}) or die $dbh->errstr; - - #execute it - $sth->execute($query->param("user"), $query->param("pass")) or die $dbh->errstr; - - #finish query - $sth->finish() or die $dbh->errstr; + $dbh->de(qq{insert into users (username, password) values ( ?, password( ? ))}, undef, + $query->param("user"), $query->param("pass")) or die $dbh->errstr; #disconnect db $dbh->disconnect() or die $dbh->errstr; diff --git a/trunk/search.pl b/trunk/search.pl index 22421db..ae583bc 100644 --- a/trunk/search.pl +++ b/trunk/search.pl @@ -25,13 +25,16 @@ if($query->param('query')) my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr; #prepare query - my $sth = $dbh->prepare(qq{select id, title, description, userid, timestamp from videos where match(title, description, subject) against( ? ) }) or die $dbh->errstr; + 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; #execute it $sth->execute($query->param('query')) or die $dbh->errstr; #get every returned value - while (my ($id, $title, $description, $userid, $timestamp) = $sth->fetchrow_array()) + while (my ($id, $title, $description, $username, $timestamp) = $sth->fetchrow_array()) { #before code cleanup, this was a really obfuscated array/hash creation push @{ $page->{'results'}->{'result'} }, @@ -44,7 +47,7 @@ if($query->param('query')) 'rdf:about' => "./video.pl?title=$title&id=$id", 'dc:title' => [$title], 'dc:date' => [$timestamp], - 'dc:publisher' => [get_username_from_id($userid)] + 'dc:publisher' => [$username] }, 'cc:License' => { @@ -84,13 +87,15 @@ elsif($query->param('sort')) my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr; #prepare query - my $sth = $dbh->prepare(qq{select id, title, description, userid, from_unixtime( timestamp ) from videos order by timestamp desc }) or die $dbh->errstr; + 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; #execute it $sth->execute($query->param('query')) or die $dbh->errstr; #get every returned value - while (my ($id, $title, $description, $userid, $timestamp) = $sth->fetchrow_array()) + while (my ($id, $title, $description, $username, $timestamp) = $sth->fetchrow_array()) { #before code cleanup, this was a really obfuscated array/hash creation push @{ $page->{'results'}->{'result'} }, @@ -103,7 +108,7 @@ elsif($query->param('sort')) 'rdf:about' => "./video.pl?title=$title&id=$id", 'dc:title' => [$title], 'dc:date' => [$timestamp], - 'dc:publisher' => [get_username_from_id($userid)] + 'dc:publisher' => [$username] }, 'cc:License' => { diff --git a/trunk/upload.pl b/trunk/upload.pl index 0064225..84678ca 100644 --- a/trunk/upload.pl +++ b/trunk/upload.pl @@ -19,7 +19,7 @@ if($username) $page->{'xmlns:dc'} = $xmlns_dc; $page->{'xmlns:cc'} = $xmlns_cc; $page->{'xmlns:rdf'} = $xmlns_rdf; - $page->{uploadform} = ['']; + $page->{uploadform} = {'page' => '2'}; #print xml http header along with session cookie print $session->header(-type=>'text/xml'); @@ -30,7 +30,6 @@ 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; diff --git a/trunk/uploader.pl b/trunk/uploader.pl index 56e200f..8733707 100644 --- a/trunk/uploader.pl +++ b/trunk/uploader.pl @@ -27,13 +27,12 @@ if($userid) #make new entry for video into the databse #FIXME: contributor, rights - my $sth = $dbh->prepare(qq{insert into uploaded (title, description, userid, timestamp, - creator, subject, contributor, source, language, coverage, rights) - values ( ?, ?, ?, unix_timestamp(), ?, ?, ?, ?, ?, ?, ? )}) or die $dbh->errstr; - $sth->execute($query->param("DC.Title"), $query->param("DC.Description"), $userid, - $query->param("DC.Creator"), $query->param("DC.Subject"), '', $query->param("DC.Source"), - $query->param("DC.Language"), $query->param("DC.Coverage"), '') or die $dbh->errstr; - $sth->finish() or die $dbh->errstr; + $dbh->do(qq{insert into uploaded (title, description, userid, timestamp, + creator, subject, contributor, source, language, coverage, rights) + values ( ?, ?, ?, unix_timestamp(), ?, ?, ?, ?, ?, ?, ? )}, undef, + $query->param("DC.Title"), $query->param("DC.Description"), $userid, + $query->param("DC.Creator"), $query->param("DC.Subject"), '', $query->param("DC.Source"), + $query->param("DC.Language"), $query->param("DC.Coverage"), '') or die $dbh->errstr; #get the id of the inserted db entry $sth = $dbh->prepare(qq{select last_insert_id() }) or die $dbh->errstr; diff --git a/trunk/video.pl b/trunk/video.pl index f1cca39..7268fef 100644 --- a/trunk/video.pl +++ b/trunk/video.pl @@ -35,9 +35,11 @@ if($query->url_param('title') or $query->url_param('id')) else { #if no id was passed there has to be a title we search for - $sth = $dbh->prepare(qq{select id, title, description, userid, from_unixtime( timestamp ), - creator, subject, contributor, source, language, coverage, rights, license - from videos where title = ? }) or die $dbh->errstr; + $sth = $dbh->prepare(qq{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; } @@ -45,9 +47,12 @@ if($query->url_param('title') or $query->url_param('id')) #if there was a title passed, then perform a search if($rowcount == 0 and $query->url_param('title')) { - $sth = $dbh->prepare(qq{select id, title, description, userid, from_unixtime( timestamp ), - creator, subject, contributor, source, language, coverage, rights, license - from videos where match(title, description, subject) against( ? ) }) or die $dbh->errstr; + $sth = $dbh->prepare(qq{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; } @@ -97,16 +102,12 @@ if($query->url_param('title') or $query->url_param('id')) if($rowcount > 0) { #video is in database - increase referercount - $sth = $dbh->prepare(qq{update referer set count=count+1 where videoid = ? and referer = ? }) or die $dbh->errstr; - $sth->execute($id, $referer) or die $dbh->errstr; - $sth->finish(); + $dbh->do(qq{update referer set count=count+1 where videoid = ? and referer = ? }, undef, $id, $referer) or die $dbh->errstr; } else { #add new referer - $sth = $dbh->prepare(qq{insert into referer (videoid, referer) values (?, ?) }) or die $dbh->errstr; - $sth->execute($id, $referer) or die $dbh->errstr; - $sth->finish(); + $dbh->do(qq{insert into referer (videoid, referer) values (?, ?) }, undef, $id, $referer) or die $dbh->errstr; } }