Commit 2f6471b0 authored by Lukas Fleischer's avatar Lukas Fleischer
Browse files

Refactor pkgbase_set_comaintainers()



This makes the code slightly more efficient and allows for easily
determining the users that were added/removed to the co-maintainer list.
Signed-off-by: Lukas Fleischer's avatarLukas Fleischer <lfleischer@archlinux.org>
parent fd6ba764
...@@ -1023,7 +1023,7 @@ function pkgbase_set_comaintainers($base_id, $users) { ...@@ -1023,7 +1023,7 @@ function pkgbase_set_comaintainers($base_id, $users) {
$dbh = DB::connect(); $dbh = DB::connect();
$uids = array(); $uids_new = array();
foreach($users as $user) { foreach($users as $user) {
$q = "SELECT ID FROM Users "; $q = "SELECT ID FROM Users ";
$q .= "WHERE UserName = " . $dbh->quote($user); $q .= "WHERE UserName = " . $dbh->quote($user);
...@@ -1034,18 +1034,32 @@ function pkgbase_set_comaintainers($base_id, $users) { ...@@ -1034,18 +1034,32 @@ function pkgbase_set_comaintainers($base_id, $users) {
return array(false, __("Invalid user name: %s", $user)); return array(false, __("Invalid user name: %s", $user));
} }
$uids[] = $uid; $uids_new[] = $uid;
} }
$q = sprintf("DELETE FROM PackageComaintainers WHERE PackageBaseID = %d", $base_id); $q = sprintf("SELECT UsersID FROM PackageComaintainers WHERE PackageBaseID = %d", $base_id);
$dbh->exec($q); $result = $dbh->query($q);
$uids_old = $result->fetchAll(PDO::FETCH_COLUMN, 0);
$uids_add = array_diff($uids_new, $uids_old);
$uids_rem = array_diff($uids_old, $uids_new);
$i = 1; $i = 1;
foreach ($uids as $uid) { foreach ($uids_new as $uid) {
$q = sprintf("INSERT INTO PackageComaintainers (PackageBaseID, UsersID, Priority) VALUES (%d, %d, %d)", $base_id, $uid, $i); if (in_array($uid, $uids_add)) {
$q = sprintf("INSERT INTO PackageComaintainers (PackageBaseID, UsersID, Priority) VALUES (%d, %d, %d)", $base_id, $uid, $i);
} else {
$q = sprintf("UPDATE PackageComaintainers SET Priority = %d WHERE PackageBaseID = %d AND UsersID = %d", $i, $base_id, $uid);
}
$dbh->exec($q); $dbh->exec($q);
$i++; $i++;
} }
foreach ($uids_rem as $uid) {
$q = sprintf("DELETE FROM PackageComaintainers WHERE PackageBaseID = %d AND UsersID = %d", $base_id, $uid);
$dbh->exec($q);
}
return array(true, __("The package base co-maintainers have been updated.")); return array(true, __("The package base co-maintainers have been updated."));
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment