From 763cbf8373e3373254ad18f5b69fd16efdc6fd5c Mon Sep 17 00:00:00 2001 From: canyonknight Date: Wed, 23 May 2012 15:30:52 -0400 Subject: pkgsubmit.php: Pull out DB code * Move DB code in pkgsubmit.php to new functions in aur.inc.php and pkgfuncs.inc.php * Centralization of DB code important in a future transition to PDO interface Signed-off-by: canyonknight Signed-off-by: Lukas Fleischer --- web/html/pkgsubmit.php | 59 ++++++----------------------- web/lib/aur.inc.php | 21 +++++++++++ web/lib/pkgfuncs.inc.php | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 48 deletions(-) (limited to 'web') diff --git a/web/html/pkgsubmit.php b/web/html/pkgsubmit.php index 2693fd6..ca3e1fd 100644 --- a/web/html/pkgsubmit.php +++ b/web/html/pkgsubmit.php @@ -299,13 +299,10 @@ if ($uid): # Update the backend database if (!$error) { - $dbh = db_connect(); - db_query("BEGIN", $dbh); + begin_atomic_commit($dbh); - $q = "SELECT * FROM Packages WHERE Name = '" . db_escape_string($new_pkgbuild['pkgname']) . "'"; - $result = db_query($q, $dbh); - $pdata = mysql_fetch_assoc($result); + $pdata = pkgdetails_by_pkgname($new_pkgbuild['pkgname'], $dbh); if (isset($new_pkgbuild['epoch']) && (int)$new_pkgbuild['epoch'] > 0) { $pkg_version = sprintf('%d:%s-%s', $new_pkgbuild['epoch'], $new_pkgbuild['pkgver'], $new_pkgbuild['pkgrel']); @@ -332,46 +329,20 @@ if ($uid): $packageID = $pdata["ID"]; # Flush out old data that will be replaced with new data - $q = "DELETE FROM PackageDepends WHERE PackageID = " . $packageID; - db_query($q, $dbh); - $q = "DELETE FROM PackageSources WHERE PackageID = " . $packageID; - db_query($q, $dbh); + remove_pkg_deps($packageID, $dbh); + remove_pkg_sources($packageID, $dbh); # If a new category was chosen, change it to that if ($category_id > 1) { - $q = sprintf( "UPDATE Packages SET CategoryID = %d WHERE ID = %d", - $category_id, - $packageID); - - db_query($q, $dbh); + update_pkg_category($packageID, $category_id); } # Update package data - $q = sprintf("UPDATE Packages SET ModifiedTS = UNIX_TIMESTAMP(), Name = '%s', Version = '%s', License = '%s', Description = '%s', URL = '%s', OutOfDateTS = NULL, MaintainerUID = %d WHERE ID = %d", - db_escape_string($new_pkgbuild['pkgname']), - db_escape_string($pkg_version), - db_escape_string($new_pkgbuild['license']), - db_escape_string($new_pkgbuild['pkgdesc']), - db_escape_string($new_pkgbuild['url']), - $uid, - $packageID); - - db_query($q, $dbh); - + update_pkgdetails($packageID, $new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, "", $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], "", $uid, $dbh); } else { # This is a brand new package - $q = sprintf("INSERT INTO Packages (Name, License, Version, CategoryID, Description, URL, SubmittedTS, ModifiedTS, SubmitterUID, MaintainerUID) VALUES ('%s', '%s', '%s', %d, '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), %d, %d)", - db_escape_string($new_pkgbuild['pkgname']), - db_escape_string($new_pkgbuild['license']), - db_escape_string($pkg_version), - $category_id, - db_escape_string($new_pkgbuild['pkgdesc']), - db_escape_string($new_pkgbuild['url']), - $uid, - $uid); - - db_query($q, $dbh); - $packageID = mysql_insert_id($dbh); + new_pkgdetails($new_pkgbuild['pkgname'], $new_pkgbuild['license'], $pkg_version, $category_id, $new_pkgbuild['pkgdesc'], $new_pkgbuild['url'], $uid, $dbh); + $packageID = last_insert_id($dbh); } @@ -388,13 +359,7 @@ if ($uid): else if ($deppkgname == "#") { break; } - - $q = sprintf("INSERT INTO PackageDepends (PackageID, DepName, DepCondition) VALUES (%d, '%s', '%s')", - $packageID, - db_escape_string($deppkgname), - db_escape_string($depcondition)); - - db_query($q, $dbh); + add_pkg_dep($packageID, $deppkgname, $depcondition, $dbh); } } @@ -402,9 +367,7 @@ if ($uid): $sources = explode(" ", $new_pkgbuild['source']); foreach ($sources as $src) { if ($src != "" ) { - $q = "INSERT INTO PackageSources (PackageID, Source) VALUES ("; - $q .= $packageID . ", '" . db_escape_string($src) . "')"; - db_query($q, $dbh); + add_pkg_src($packageID, $src, $dbh); } } @@ -415,7 +378,7 @@ if ($uid): } # Entire package creation process is atomic - db_query("COMMIT", $dbh); + end_atomic_commit($dbh); header('Location: packages.php?ID=' . $packageID); } diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php index 991fb14..1a294a9 100644 --- a/web/lib/aur.inc.php +++ b/web/lib/aur.inc.php @@ -512,3 +512,24 @@ function parse_comment($comment) { return $html; } + +function begin_atomic_commit($dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + db_query("BEGIN", $dbh); +} + +function end_atomic_commit($dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + db_query("COMMIT", $dbh); +} + +function last_insert_id($dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + return mysql_insert_id($dbh); +} diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index 40fccbc..6d2f01f 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -1240,3 +1240,100 @@ function pkg_change_category($atype, $dbh=NULL) { return __("You are not allowed to change this package category."); } } + +function pkgdetails_by_pkgname($pkgname, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "SELECT * FROM Packages WHERE Name = '" . db_escape_string($pkgname) . "'"; + $result = db_query($q, $dbh); + if ($result) { + $pdata = mysql_fetch_assoc($result); + } + return $pdata; +} + +function new_pkgdetails($pkgname, $license, $pkgver, $category_id, $pkgdesc, $pkgurl, $uid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = sprintf("INSERT INTO Packages (Name, License, Version, CategoryID, Description, URL, SubmittedTS, ModifiedTS, SubmitterUID, MaintainerUID) VALUES ('%s', '%s', '%s', %d, '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), %d, %d)", + db_escape_string($pkgname), + db_escape_string($license), + db_escape_string($pkgver), + $category_id, + db_escape_string($pkgdesc), + db_escape_string($pkgurl), + $uid, + $uid); + + db_query($q, $dbh); +} + +function update_pkgdetails($pkgname, $license, $pkgver, $pkgdesc, $pkgurl, $uid, $pkgid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + # This is an overwrite of an existing package + $q = sprintf("UPDATE Packages SET ModifiedTS = UNIX_TIMESTAMP(), Name = '%s', Version = '%s', License = '%s', Description = '%s', URL = '%s', OutOfDateTS = NULL, MaintainerUID = %d WHERE ID = %d", + db_escape_string($pkgname), + db_escape_string($pkgver), + db_escape_string($license), + db_escape_string($pkgdesc), + db_escape_string($pkgurl), + $uid, + $pkgid); + + db_query($q, $dbh); +} + +function add_pkg_dep($pkgid, $depname, $depcondition, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = sprintf("INSERT INTO PackageDepends (PackageID, DepName, DepCondition) VALUES (%d, '%s', '%s')", + $pkgid, + db_escape_string($depname), + db_escape_string($depcondition)); + + db_query($q, $dbh); +} + +function add_pkg_src($pkgid, $pkgsrc, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "INSERT INTO PackageSources (PackageID, Source) VALUES ("; + $q .= $pkgid . ", '" . db_escape_string($pkgsrc) . "')"; + + db_query($q, $dbh); +} + +function update_pkg_category($pkgid, $category_id, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = sprintf( "UPDATE Packages SET CategoryID = %d WHERE ID = %d", + $category_id, + $pkgid); + + db_query($q, $dbh); +} + +function remove_pkg_deps($pkgid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "DELETE FROM PackageDepends WHERE PackageID = " . $pkgid; + + db_query($q, $dbh); +} + +function remove_pkg_sources($pkgid, $dbh=NULL) { + if(!$dbh) { + $dbh = db_connect(); + } + $q = "DELETE FROM PackageSources WHERE PackageID = " . $pkgid; + + db_query($q, $dbh); +} -- cgit v1.2.3-70-g09d2