Don't hit the database twice per comment on package

It's performance improvement day today. For non-superusers, we were hitting
the database twice per comment on a package- once to get the UID, and once
to check the owner of the comment. The best part is we already knew the
owner of the comment, and we only need to get our own UID once.

For viewing a package like yaourt, this cuts a single pageview from over 700
queries to around 18, which is still not great but a pretty big improvement.

Signed-off-by: Dan McGee <dan@archlinux.org>
Signed-off-by: Loui Chang <louipc.ist@gmail.com>
This commit is contained in:
Dan McGee 2009-04-11 20:39:01 -05:00 committed by Loui Chang
parent f028d5c140
commit 55da4d4e0d
3 changed files with 19 additions and 6 deletions

View file

@ -35,9 +35,9 @@ if (!$_REQUEST["ID"]) {
# #
if ($_REQUEST["del_Comment"]) { if ($_REQUEST["del_Comment"]) {
if ($_REQUEST["comment_id"]) { if ($_REQUEST["comment_id"]) {
if (canDeleteComment($_REQUEST["comment_id"], $atype, $_COOKIE["AURSID"])) { $uid = uid_from_sid($_COOKIE["AURSID"]);
if (canDeleteComment($_REQUEST["comment_id"], $atype, $uid)) {
$dbh = db_connect(); $dbh = db_connect();
$uid = uid_from_sid($_COOKIE["AURSID"]);
$q = "UPDATE PackageComments "; $q = "UPDATE PackageComments ";
$q.= "SET DelUsersID = ".$uid." "; $q.= "SET DelUsersID = ".$uid." ";
$q.= "WHERE ID = ".intval($_REQUEST["comment_id"]); $q.= "WHERE ID = ".intval($_REQUEST["comment_id"]);

View file

@ -8,13 +8,11 @@ $pkgsearch_vars = array("O", "L", "C", "K", "SB", "SO", "PP", "do_Orphans", "SeB
# Make sure this visitor can delete the requested package comment # Make sure this visitor can delete the requested package comment
# They can delete if they were the comment submitter, or if they are a TU/Dev # They can delete if they were the comment submitter, or if they are a TU/Dev
# #
function canDeleteComment($comment_id=0, $atype="", $SID="") { function canDeleteComment($comment_id=0, $atype="", $uid=0) {
if ($atype == "Trusted User" || $atype == "Developer") { if ($atype == "Trusted User" || $atype == "Developer") {
# A TU/Dev can delete any comment # A TU/Dev can delete any comment
#
return TRUE; return TRUE;
} }
$uid = uid_from_sid($SID);
$dbh = db_connect(); $dbh = db_connect();
$q = "SELECT COUNT(ID) AS CNT "; $q = "SELECT COUNT(ID) AS CNT ";
$q.= "FROM PackageComments "; $q.= "FROM PackageComments ";
@ -30,6 +28,20 @@ function canDeleteComment($comment_id=0, $atype="", $SID="") {
return FALSE; return FALSE;
} }
# Make sure this visitor can delete the requested package comment
# They can delete if they were the comment submitter, or if they are a TU/Dev
#
function canDeleteCommentArray($comment, $atype="", $uid=0) {
if ($atype == "Trusted User" || $atype == "Developer") {
# A TU/Dev can delete any comment
return TRUE;
} else if ($comment['UsersID'] == $uid) {
# User's own comment
return TRUE;
}
return FALSE;
}
# see if this Users.ID can manage the package # see if this Users.ID can manage the package
# #
function canManagePackage($uid=0,$AURMUID=0, $MUID=0, $SUID=0, $managed=0) { function canManagePackage($uid=0,$AURMUID=0, $MUID=0, $SUID=0, $managed=0) {

View file

@ -1,8 +1,9 @@
<div class="pgbox"> <div class="pgbox">
<?php <?php
$uid = uid_from_sid($SID);
while (list($indx, $carr) = each($comments)) { ?> while (list($indx, $carr) = each($comments)) { ?>
<div class="comment-header"><?php <div class="comment-header"><?php
if (canDeleteComment($carr['ID'], $atype, $SID)) { if (canDeleteCommentArray($carr, $atype, $uid)) {
$durl = '<a href="pkgedit.php?del_Comment=1'; $durl = '<a href="pkgedit.php?del_Comment=1';
$durl.= '&comment_id=' . $carr['ID'] . '&ID=' . $row['ID']; $durl.= '&comment_id=' . $carr['ID'] . '&ID=' . $row['ID'];
$durl.= '"><img src="images/x.png" border="0"'; $durl.= '"><img src="images/x.png" border="0"';