[aur-dev] [PATCH 1/3] Refactor pkgbase_categories()
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de> --- web/lib/pkgbasefuncs.inc.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/web/lib/pkgbasefuncs.inc.php b/web/lib/pkgbasefuncs.inc.php index 3bb07e5..91db229 100644 --- a/web/lib/pkgbasefuncs.inc.php +++ b/web/lib/pkgbasefuncs.inc.php @@ -9,17 +9,16 @@ include_once("config.inc.php"); * @return array All package categories */ function pkgbase_categories() { - $cats = array(); $dbh = DB::connect(); $q = "SELECT * FROM PackageCategories WHERE ID != 1 "; $q.= "ORDER BY Category ASC"; $result = $dbh->query($q); - if ($result) { - while ($row = $result->fetch(PDO::FETCH_NUM)) { - $cats[$row[0]] = $row[1]; - } + if (!$result) { + return null; } - return $cats; + + return array_combine($result->fetchAll(PDO::FETCH_COLUMN, 0), + $result->fetchAll(PDO::FETCH_COLUMN, 1)); } /** -- 1.9.1
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de> --- web/lib/pkgbasefuncs.inc.php | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/web/lib/pkgbasefuncs.inc.php b/web/lib/pkgbasefuncs.inc.php index 91db229..39aed0f 100644 --- a/web/lib/pkgbasefuncs.inc.php +++ b/web/lib/pkgbasefuncs.inc.php @@ -24,28 +24,26 @@ function pkgbase_categories() { /** * Get the number of non-deleted comments for a specific package base * - * @param string $pkgid The package base ID to get comment count for + * @param string $base_id The package base ID to get comment count for * * @return string The number of comments left for a specific package */ function pkgbase_comments_count($base_id) { - $dbh = DB::connect(); - $base_id = intval($base_id); - if ($base_id > 0) { - $dbh = DB::connect(); - $q = "SELECT COUNT(*) FROM PackageComments "; - $q.= "WHERE PackageBaseID = " . $base_id; - $q.= " AND DelUsersID IS NULL"; + if (!$base_id) { + return null; } - $result = $dbh->query($q); + $dbh = DB::connect(); + $q = "SELECT COUNT(*) FROM PackageComments "; + $q.= "WHERE PackageBaseID = " . $base_id . " "; + $q.= "AND DelUsersID IS NULL"; + $result = $dbh->query($q); if (!$result) { - return; + return null; } - $row = $result->fetch(PDO::FETCH_NUM); - return $row[0]; + return $result->fetchColumn(0); } /** -- 1.9.1
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de> --- web/lib/pkgbasefuncs.inc.php | 49 +++++++++++++++++++++----------------------- web/lib/pkgfuncs.inc.php | 3 ++- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/web/lib/pkgbasefuncs.inc.php b/web/lib/pkgbasefuncs.inc.php index 39aed0f..c489bfd 100644 --- a/web/lib/pkgbasefuncs.inc.php +++ b/web/lib/pkgbasefuncs.inc.php @@ -49,37 +49,33 @@ function pkgbase_comments_count($base_id) { /** * Get all package comment information for a specific package base * - * @param int $pkgid The package base ID to get comments for + * @param int $base_id The package base ID to get comments for + * @param int $limit Maximum number of comments to return (0 means unlimited) * * @return array All package comment information for a specific package base */ -function pkgbase_comments($base_id) { +function pkgbase_comments($base_id, $limit) { $base_id = intval($base_id); - $comments = array(); - if ($base_id > 0) { - $dbh = DB::connect(); - $q = "SELECT PackageComments.ID, UserName, UsersID, Comments, CommentTS "; - $q.= "FROM PackageComments LEFT JOIN Users "; - $q.= "ON PackageComments.UsersID = Users.ID "; - $q.= "WHERE PackageBaseID = " . $base_id . " "; - $q.= "AND DelUsersID IS NULL "; - $q.= "ORDER BY CommentTS DESC"; - - if (!isset($_GET['comments'])) { - $q.= " LIMIT 10"; - } - - $result = $dbh->query($q); - - if (!$result) { - return; - } + $limit = intval($limit); + if (!$base_id) { + return null; + } - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { - $comments[] = $row; - } + $dbh = DB::connect(); + $q = "SELECT PackageComments.ID, UserName, UsersID, Comments, "; + $q.= "CommentTS FROM PackageComments LEFT JOIN Users "; + $q.= "ON PackageComments.UsersID = Users.ID "; + $q.= "WHERE PackageBaseID = " . $base_id . " "; + $q.= "AND DelUsersID IS NULL ORDER BY CommentTS DESC"; + if ($limit > 0) { + $q.=" LIMIT " . $limit; + } + $result = $dbh->query($q); + if (!$result) { + return null; } - return $comments; + + return $result->fetchAll(); } /** @@ -238,7 +234,8 @@ function pkgbase_display_details($base_id, $row, $SID="") { include('pkg_comment_form.php'); } - $comments = pkgbase_comments($base_id); + $limit = isset($_GET['comments']) ? 0 : 10; + $comments = pkgbase_comments($base_id, $limit); if (!empty($comments)) { include('pkg_comments.php'); } diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index 76fed89..c6b4a27 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -301,7 +301,8 @@ function pkg_display_details($id=0, $row, $SID="") { include('pkg_comment_form.php'); } - $comments = pkgbase_comments($base_id); + $limit = isset($_GET['comments']) ? 0 : 10; + $comments = pkgbase_comments($base_id, $limit); if (!empty($comments)) { include('pkg_comments.php'); } -- 1.9.1
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de> --- web/lib/pkgbasefuncs.inc.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/web/lib/pkgbasefuncs.inc.php b/web/lib/pkgbasefuncs.inc.php index 5be7048..c55bd89 100644 --- a/web/lib/pkgbasefuncs.inc.php +++ b/web/lib/pkgbasefuncs.inc.php @@ -9,17 +9,15 @@ include_once("config.inc.php"); * @return array All package categories */ function pkgbase_categories() { - $cats = array(); $dbh = DB::connect(); $q = "SELECT * FROM PackageCategories WHERE ID != 1 "; $q.= "ORDER BY Category ASC"; $result = $dbh->query($q); - if ($result) { - while ($row = $result->fetch(PDO::FETCH_NUM)) { - $cats[$row[0]] = $row[1]; - } + if (!$result) { + return null; } - return $cats; + + return $result->fetchAll(PDO::FETCH_KEY_PAIR); } /** -- 1.9.1
participants (1)
-
Lukas Fleischer