From 6448c3fe831c32b3507f54a61889dc88580f0de5 Mon Sep 17 00:00:00 2001 From: nonmakina Date: Mon, 18 Jan 2021 04:11:47 -0600 Subject: [PATCH 1/6] Adds function to count posts and images of a thread --- templates/themes/semirand/theme.php | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/templates/themes/semirand/theme.php b/templates/themes/semirand/theme.php index cd670a52..ae82bdd8 100644 --- a/templates/themes/semirand/theme.php +++ b/templates/themes/semirand/theme.php @@ -97,6 +97,17 @@ return $query->fetchAll(PDO::FETCH_ASSOC); } + /** + * Retrieve count of images and posts in a thread + */ + private function fetchThreadCount($board, $thread_id) { + $query = prepare("SELECT COUNT(NULLIF(TRIM(files), '')) as file_count, COUNT(id) as post_count FROM ``posts_$board`` WHERE `thread` = :id"); + $query->bindValue(':id', $thread_id, PDO::PARAM_INT); + $query->execute() or error(db_error($query)); + + return $query->fetchAll(PDO::FETCH_ASSOC); + } + /** * Intersperse random threads between those in bump order */ @@ -138,27 +149,15 @@ $config['threads_preview']; $replies = $this->fetchReplies($post['board'], $post['id'], $preview_count); - // Chomp the last few replies $disp_replies = $replies; - $disp_img_count = 0; foreach ($disp_replies as $reply) { - if ($reply['files'] !== '') - ++$disp_img_count; - // Append the reply to the thread as it's being built $thread->add(new Post($reply, $mod ? '?/' : $config['root'], $mod)); } - // Count the number of omitted image replies - $omitted_img_count = count(array_filter($replies, function($p) { - return $p['files'] !== ''; - })); - - // Set the corresponding omitted numbers on the thread - if (!empty($replies)) { - $thread->omitted = count($replies); - $thread->omitted_images = $omitted_img_count; - } + $threadCount = $this->fetchThreadCount($post['board'], $post['id']); + $thread->omitted = $threadCount['post_count']; + $thread->omitted_images = $threadCount['file_count']; // Board name and link $html = '

/' . From 26f8c332ef9603033760b3f6724e0f4f1c255f21 Mon Sep 17 00:00:00 2001 From: nonmakina Date: Mon, 18 Jan 2021 04:26:18 -0600 Subject: [PATCH 2/6] Fetch one, instead of all --- templates/themes/semirand/theme.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/themes/semirand/theme.php b/templates/themes/semirand/theme.php index ae82bdd8..9f5592e7 100644 --- a/templates/themes/semirand/theme.php +++ b/templates/themes/semirand/theme.php @@ -105,7 +105,7 @@ $query->bindValue(':id', $thread_id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - return $query->fetchAll(PDO::FETCH_ASSOC); + return $query->fetch(PDO::FETCH_ASSOC); } /** From f9bf65ecb74c01a01c4f75323f3f3734118c2914 Mon Sep 17 00:00:00 2001 From: nonmakina Date: Mon, 18 Jan 2021 04:31:02 -0600 Subject: [PATCH 3/6] Add offset --- templates/themes/semirand/theme.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/templates/themes/semirand/theme.php b/templates/themes/semirand/theme.php index 9f5592e7..11403bd3 100644 --- a/templates/themes/semirand/theme.php +++ b/templates/themes/semirand/theme.php @@ -100,9 +100,10 @@ /** * Retrieve count of images and posts in a thread */ - private function fetchThreadCount($board, $thread_id) { - $query = prepare("SELECT COUNT(NULLIF(TRIM(files), '')) as file_count, COUNT(id) as post_count FROM ``posts_$board`` WHERE `thread` = :id"); + private function fetchThreadCount($board, $thread_id, $preview_count) { + $query = prepare("SELECT COUNT(NULLIF(TRIM(files), '')) as file_count, COUNT(id) as post_count FROM ``posts_$board`` WHERE `thread` = :id ORDER BY `time` DESC OFFSET :offset"); $query->bindValue(':id', $thread_id, PDO::PARAM_INT); + $query->bindValue(':offset', $preview_count, PDO::PARAM_INT); $query->execute() or error(db_error($query)); return $query->fetch(PDO::FETCH_ASSOC); @@ -155,7 +156,7 @@ $thread->add(new Post($reply, $mod ? '?/' : $config['root'], $mod)); } - $threadCount = $this->fetchThreadCount($post['board'], $post['id']); + $threadCount = $this->fetchThreadCount($post['board'], $post['id'], $preview_count); $thread->omitted = $threadCount['post_count']; $thread->omitted_images = $threadCount['file_count']; From 771a1e16299ca6fbf24a174e65392073daa48176 Mon Sep 17 00:00:00 2001 From: nonmakina Date: Mon, 18 Jan 2021 04:35:34 -0600 Subject: [PATCH 4/6] Add offset --- templates/themes/semirand/theme.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/themes/semirand/theme.php b/templates/themes/semirand/theme.php index 11403bd3..7db5ef9e 100644 --- a/templates/themes/semirand/theme.php +++ b/templates/themes/semirand/theme.php @@ -101,7 +101,7 @@ * Retrieve count of images and posts in a thread */ private function fetchThreadCount($board, $thread_id, $preview_count) { - $query = prepare("SELECT COUNT(NULLIF(TRIM(files), '')) as file_count, COUNT(id) as post_count FROM ``posts_$board`` WHERE `thread` = :id ORDER BY `time` DESC OFFSET :offset"); + $query = prepare("SELECT COUNT(NULLIF(TRIM(files), '')) as file_count, COUNT(id) as post_count FROM ``posts_$board`` WHERE `thread` = :id ORDER BY `time` DESC LIMIT :offset , 18446744073709551615;"); $query->bindValue(':id', $thread_id, PDO::PARAM_INT); $query->bindValue(':offset', $preview_count, PDO::PARAM_INT); $query->execute() or error(db_error($query)); From e936312db509c5a355b4bb1131f644556aed242a Mon Sep 17 00:00:00 2001 From: nonmakina Date: Mon, 18 Jan 2021 04:49:46 -0600 Subject: [PATCH 5/6] Fix offset --- templates/themes/semirand/theme.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/themes/semirand/theme.php b/templates/themes/semirand/theme.php index 7db5ef9e..c0412962 100644 --- a/templates/themes/semirand/theme.php +++ b/templates/themes/semirand/theme.php @@ -101,7 +101,7 @@ * Retrieve count of images and posts in a thread */ private function fetchThreadCount($board, $thread_id, $preview_count) { - $query = prepare("SELECT COUNT(NULLIF(TRIM(files), '')) as file_count, COUNT(id) as post_count FROM ``posts_$board`` WHERE `thread` = :id ORDER BY `time` DESC LIMIT :offset , 18446744073709551615;"); + $query = prepare("SELECT COUNT(NULLIF(TRIM(files), '')) as file_count, COUNT(id) as post_count FROM (SELECT * FROM ``posts_$board`` WHERE `thread` = :id ORDER BY `time` DESC LIMIT :offset , 18446744073709551615);"); $query->bindValue(':id', $thread_id, PDO::PARAM_INT); $query->bindValue(':offset', $preview_count, PDO::PARAM_INT); $query->execute() or error(db_error($query)); From f40beb76407eee61f78906f2b068dc78e56b8e92 Mon Sep 17 00:00:00 2001 From: nonmakina Date: Mon, 18 Jan 2021 04:59:13 -0600 Subject: [PATCH 6/6] Fix filecount --- templates/themes/semirand/theme.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/themes/semirand/theme.php b/templates/themes/semirand/theme.php index c0412962..3897a4e4 100644 --- a/templates/themes/semirand/theme.php +++ b/templates/themes/semirand/theme.php @@ -101,7 +101,7 @@ * Retrieve count of images and posts in a thread */ private function fetchThreadCount($board, $thread_id, $preview_count) { - $query = prepare("SELECT COUNT(NULLIF(TRIM(files), '')) as file_count, COUNT(id) as post_count FROM (SELECT * FROM ``posts_$board`` WHERE `thread` = :id ORDER BY `time` DESC LIMIT :offset , 18446744073709551615);"); + $query = prepare("SELECT SUM(t.num_files) as file_count, COUNT(t.id) as post_count FROM (SELECT * FROM ``posts_$board`` WHERE `thread` = :id ORDER BY `time` DESC LIMIT :offset , 18446744073709551615) as t;"); $query->bindValue(':id', $thread_id, PDO::PARAM_INT); $query->bindValue(':offset', $preview_count, PDO::PARAM_INT); $query->execute() or error(db_error($query));