Extend pkgname_from_id() to arrays of IDs

This allows for getting the package names of multiple packages at once,
without having to iterate over them and making one DB query per package.
pkgname_from_id() now accepts both integer arrays and single integers
(backwards compatibility mode).

Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2012-03-01 16:31:20 +01:00
parent 937cda9ccb
commit 9e9820ff58

View file

@ -269,20 +269,37 @@ function pkgnotify_from_sid($sid="", $dbh=NULL) {
# get name of package based on pkgid # get name of package based on pkgid
# #
function pkgname_from_id($pkgid, $dbh=NULL) { function pkgname_from_id($pkgids, $dbh=NULL) {
$pkgid = intval($pkgid); if (is_array($pkgids)) {
$name = ""; $pkgids = sanitize_ids($pkgids);
if ($pkgid > 0) { $names = array();
if(!$dbh) { if(!$dbh) {
$dbh = db_connect(); $dbh = db_connect();
} }
$q = "SELECT Name FROM Packages WHERE ID = " . $pkgid; $q = "SELECT Name FROM Packages WHERE ID IN (" .
implode(",", $pkgids) . ")";
$result = db_query($q, $dbh);
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$names[] = $row['Name'];
}
}
return $names;
}
elseif ($pkgids > 0) {
if(!$dbh) {
$dbh = db_connect();
}
$q = "SELECT Name FROM Packages WHERE ID = " . $pkgids;
$result = db_query($q, $dbh); $result = db_query($q, $dbh);
if (mysql_num_rows($result) > 0) { if (mysql_num_rows($result) > 0) {
$name = mysql_result($result, 0); $name = mysql_result($result, 0);
} }
}
return $name; return $name;
}
else {
return NULL;
}
} }
# Check if a package name is blacklisted. # Check if a package name is blacklisted.