Make the delete function remove package bases

Deleting a single package without deleting the whole package base makes
no sense. Comments and votes are already stored on a per-package basis,
making this a straightforward extension.

Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2014-04-04 23:10:38 +02:00
parent 45bd1b34f1
commit b558572a2e
2 changed files with 58 additions and 32 deletions

View file

@ -72,13 +72,13 @@ if (check_token()) {
} elseif (current_action("do_Delete")) { } elseif (current_action("do_Delete")) {
if (isset($_POST['confirm_Delete'])) { if (isset($_POST['confirm_Delete'])) {
if (!isset($_POST['merge_Into']) || empty($_POST['merge_Into'])) { if (!isset($_POST['merge_Into']) || empty($_POST['merge_Into'])) {
list($ret, $output) = pkg_delete($atype, $ids, NULL); list($ret, $output) = pkg_delete($atype, pkgbase_from_pkgid($ids), NULL);
unset($_GET['ID']); unset($_GET['ID']);
} }
else { else {
$mergepkgid = pkgid_from_name($_POST['merge_Into']); $merge_base_id = pkgbase_from_name($_POST['merge_Into']);
if ($mergepkgid) { if ($merge_base_id) {
list($ret, $output) = pkg_delete($atype, $ids, $mergepkgid); list($ret, $output) = pkg_delete($atype, pkgbase_from_pkgid($ids), $merge_base_id);
unset($_GET['ID']); unset($_GET['ID']);
} }
else { else {

View file

@ -787,6 +787,35 @@ function pkgbase_from_pkgid($ids) {
} }
} }
/**
* Retrieve ID of a package base by name
*
* @param string $name The package base name to retrieve the ID for
*
* @return int The ID of the package base
*/
function pkgbase_from_name($name) {
$dbh = DB::connect();
$q = "SELECT ID FROM PackageBases WHERE Name = " . $dbh->quote($name);
$result = $dbh->query($q);
return $result->fetch(PDO::FETCH_COLUMN, 0);
}
/**
* Retrieve the name of a package base given its ID
*
* @param int $base_id The ID of the package base to query
*
* @return string The name of the package base
*/
function pkgbase_name_from_id($base_id) {
$dbh = DB::connect();
$q = "SELECT Name FROM PackageBases WHERE ID = " . intval($base_id);
$result = $dbh->query($q);
return $result->fetch(PDO::FETCH_COLUMN, 0);
}
/** /**
* Flag package(s) as out-of-date * Flag package(s) as out-of-date
* *
@ -884,15 +913,15 @@ function pkg_unflag($atype, $ids) {
} }
/** /**
* Delete packages * Delete package bases
* *
* @param string $atype Account type, output of account_from_sid * @param string $atype Account type, output of account_from_sid
* @param array $ids Array of package IDs to delete * @param array $base_ids Array of package base IDs to delete
* @param int $mergepkgid Package to merge the deleted ones into * @param int $merge_base_id Package base to merge the deleted ones into
* *
* @return array Tuple of success/failure indicator and error message * @return array Tuple of success/failure indicator and error message
*/ */
function pkg_delete ($atype, $ids, $mergepkgid) { function pkg_delete ($atype, $base_ids, $merge_base_id) {
if (!$atype) { if (!$atype) {
return array(false, __("You must be logged in before you can delete packages.")); return array(false, __("You must be logged in before you can delete packages."));
} }
@ -901,26 +930,24 @@ function pkg_delete ($atype, $ids, $mergepkgid) {
return array(false, __("You do not have permission to delete packages.")); return array(false, __("You do not have permission to delete packages."));
} }
$ids = sanitize_ids($ids); $base_ids = sanitize_ids($base_ids);
$base_ids = pkgbase_from_pkgid($ids); if (empty($base_ids)) {
if (empty($ids)) {
return array(false, __("You did not select any packages to delete.")); return array(false, __("You did not select any packages to delete."));
} }
$dbh = DB::connect(); $dbh = DB::connect();
if ($mergepkgid) { if ($merge_base_id) {
$mergepkgname = pkgname_from_id($mergepkgid); $merge_base_name = pkgbase_name_from_id($merge_base_id);
$mergepkgbase = pkgbase_from_pkgid($mergepkgid);
} }
/* Send e-mail notifications. */ /* Send e-mail notifications. */
foreach ($ids as $pkgid) { foreach ($base_ids as $base_id) {
$q = "SELECT CommentNotify.*, Users.Email "; $q = "SELECT CommentNotify.*, Users.Email ";
$q.= "FROM CommentNotify, Users "; $q.= "FROM CommentNotify, Users ";
$q.= "WHERE Users.ID = CommentNotify.UserID "; $q.= "WHERE Users.ID = CommentNotify.UserID ";
$q.= "AND CommentNotify.UserID != " . uid_from_sid($_COOKIE['AURSID']) . " "; $q.= "AND CommentNotify.UserID != " . uid_from_sid($_COOKIE['AURSID']) . " ";
$q.= "AND CommentNotify.PackageBaseID = " . pkgbase_from_pkgid($pkgid); $q.= "AND CommentNotify.PackageBaseID = " . $base_id;
$result = $dbh->query($q); $result = $dbh->query($q);
$bcc = array(); $bcc = array();
@ -928,7 +955,7 @@ function pkg_delete ($atype, $ids, $mergepkgid) {
array_push($bcc, $row['Email']); array_push($bcc, $row['Email']);
} }
if (!empty($bcc)) { if (!empty($bcc)) {
$pkgname = pkgname_from_id($pkgid); $pkgbase_name = pkgbase_name_from_id($base_id);
/* /*
* TODO: Add native language emails for users, based on * TODO: Add native language emails for users, based on
@ -938,11 +965,11 @@ function pkg_delete ($atype, $ids, $mergepkgid) {
* comment was in. * comment was in.
*/ */
$body = ""; $body = "";
if ($mergepkgid) { if ($merge_base_id) {
$body .= username_from_sid($_COOKIE['AURSID']) . " merged \"".$pkgname."\" into \"$mergepkgname\".\n\n"; $body .= username_from_sid($_COOKIE['AURSID']) . " merged \"".$pkgbase_name."\" into \"$merge_base_name\".\n\n";
$body .= "You will no longer receive notifications about this package, please go to https://aur.archlinux.org" . get_pkg_uri($mergepkgname) . " and click the Notify button if you wish to recieve them again."; $body .= "You will no longer receive notifications about this package, please go to https://aur.archlinux.org" . get_pkgbase_uri($merge_base_name) . " and click the Notify button if you wish to recieve them again.";
} else { } else {
$body .= username_from_sid($_COOKIE['AURSID']) . " deleted \"".$pkgname."\".\n\n"; $body .= username_from_sid($_COOKIE['AURSID']) . " deleted \"".$pkgbase_name."\".\n\n";
$body .= "You will no longer receive notifications about this package."; $body .= "You will no longer receive notifications about this package.";
} }
$body = wordwrap($body, 70); $body = wordwrap($body, 70);
@ -953,43 +980,42 @@ function pkg_delete ($atype, $ids, $mergepkgid) {
"Reply-to: nobody@archlinux.org\r\n" . "Reply-to: nobody@archlinux.org\r\n" .
"From: aur-notify@archlinux.org\r\n" . "From: aur-notify@archlinux.org\r\n" .
"X-Mailer: AUR"; "X-Mailer: AUR";
@mail('undisclosed-recipients: ;', "AUR Package deleted: " . $pkgname, $body, $headers); @mail('undisclosed-recipients: ;', "AUR Package deleted: " . $pkgbase_name, $body, $headers);
} }
} }
if ($mergepkgid) { if ($merge_base_id) {
/* Merge comments */ /* Merge comments */
$q = "UPDATE PackageComments "; $q = "UPDATE PackageComments ";
$q.= "SET PackageBaseID = " . intval($mergepkgbase) . " "; $q.= "SET PackageBaseID = " . intval($merge_base_id) . " ";
$q.= "WHERE PackageBaseID IN (" . implode(",", $base_ids) . ")"; $q.= "WHERE PackageBaseID IN (" . implode(",", $base_ids) . ")";
$dbh->exec($q); $dbh->exec($q);
/* Merge votes */ /* Merge votes */
foreach ($base_ids as $base_id) { foreach ($base_ids as $base_id) {
$q = "UPDATE PackageVotes "; $q = "UPDATE PackageVotes ";
$q.= "SET PackageBaseID = " . intval($mergepkgbase) . " "; $q.= "SET PackageBaseID = " . intval($merge_base_id) . " ";
$q.= "WHERE PackageBaseID = " . $base_id . " "; $q.= "WHERE PackageBaseID = " . $base_id . " ";
$q.= "AND UsersID NOT IN ("; $q.= "AND UsersID NOT IN (";
$q.= "SELECT * FROM (SELECT UsersID "; $q.= "SELECT * FROM (SELECT UsersID ";
$q.= "FROM PackageVotes "; $q.= "FROM PackageVotes ";
$q.= "WHERE PackageBaseID = " . intval($mergepkgbase); $q.= "WHERE PackageBaseID = " . intval($merge_base_id);
$q.= ") temp)"; $q.= ") temp)";
$dbh->exec($q); $dbh->exec($q);
} }
$q = "UPDATE PackageBases "; $q = "UPDATE PackageBases ";
$q.= "SET NumVotes = (SELECT COUNT(*) FROM PackageVotes "; $q.= "SET NumVotes = (SELECT COUNT(*) FROM PackageVotes ";
$q.= "WHERE PackageBaseID = " . intval($mergepkgbase) . ") "; $q.= "WHERE PackageBaseID = " . intval($merge_base_id) . ") ";
$q.= "WHERE ID = " . intval($mergepkgbase); $q.= "WHERE ID = " . intval($merge_base_id);
$dbh->exec($q); $dbh->exec($q);
} }
$q = "DELETE FROM Packages WHERE ID IN (" . implode(",", $ids) . ")"; $q = "DELETE FROM Packages WHERE PackageBaseID IN (" . implode(",", $base_ids) . ")";
$result = $dbh->exec($q); $dbh->exec($q);
/* Deleting a package also removes the corresponding package base. */
$q = "DELETE FROM PackageBases WHERE ID IN (" . implode(",", $base_ids) . ")"; $q = "DELETE FROM PackageBases WHERE ID IN (" . implode(",", $base_ids) . ")";
$result = $dbh->exec($q); $dbh->exec($q);
return array(true, __("The selected packages have been deleted.")); return array(true, __("The selected packages have been deleted."));
} }