From 7004f2b437acf60720957ba4c4f78cf9a7056d5d Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Mon, 11 Jan 2021 20:21:50 -0300 Subject: [PATCH 1/5] Build overboard json --- inc/api.php | 4 +++- templates/themes/catalog/theme.php | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/inc/api.php b/inc/api.php index 8e35b511..ae68d00e 100644 --- a/inc/api.php +++ b/inc/api.php @@ -35,11 +35,13 @@ class Api { 'cycle' => 'cyclical', 'bump' => 'last_modified', 'embed' => 'embed', + 'board' => 'board', ); $this->threadsPageFields = array( 'id' => 'no', - 'bump' => 'last_modified' + 'bump' => 'last_modified', + 'board' => 'board', ); $this->fileFields = array( diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index 561df2b1..15e38edb 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -458,5 +458,21 @@ 'recent_posts' => $recent_posts, 'board' => $board ))); + + if ($config['api']['enabled']) { + $api = new Api(); + + $threads = array(); + + foreach ($recent_posts as $post) { + $threads[] = new Thread($post); + } + + $json = json_encode($api->translateCatalogPage($threads)); + file_write($config['dir']['home'] . $board_name . '/catalog.json', $json); + + $json = json_encode($api->translateCatalogPage($threads, true)); + file_write($config['dir']['home'] . $board_name . '/threads.json', $json); + } } } From 8e2a6ccb9d399fd4ec039b61ef7f7ed347502a33 Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Mon, 11 Jan 2021 20:48:22 -0300 Subject: [PATCH 2/5] Wrap overboard api threads inside a page --- templates/themes/catalog/theme.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index 15e38edb..b5d04ed2 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -468,10 +468,13 @@ $threads[] = new Thread($post); } - $json = json_encode($api->translateCatalogPage($threads)); + // Page 0, the only page + $threads = array($threads); + + $json = json_encode($api->translateCatalog($threads)); file_write($config['dir']['home'] . $board_name . '/catalog.json', $json); - $json = json_encode($api->translateCatalogPage($threads, true)); + $json = json_encode($api->translateCatalog($threads, true)); file_write($config['dir']['home'] . $board_name . '/threads.json', $json); } } From 24b9b8bafdb4a80e3962ccfae7c6b688cde178e8 Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Tue, 12 Jan 2021 09:16:43 -0300 Subject: [PATCH 3/5] Only rebuild the catalog for the overboard --- templates/themes/catalog/theme.php | 43 ++++++++++++++++-------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index b5d04ed2..0785dc5a 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -348,6 +348,8 @@ * Build and save the HTML of the catalog for the overboard */ public function buildOverboardCatalog($settings, $boards) { + global $config; + $board_name = $settings['overboard_location']; if (array_key_exists($board_name, $this->threadsCache)) { @@ -371,6 +373,26 @@ $recent_posts = $this->generateRecentPosts($threads); $this->saveForBoard($board_name, $recent_posts, '/' . $settings['overboard_location']); + + // Build the overboard JSON outputs + if ($config['api']['enabled']) { + $api = new Api(); + + $threads = array(); + + foreach ($recent_posts as $post) { + $threads[] = new Thread($post); + } + + // Page 0, the only page + $threads = array($threads); + + $json = json_encode($api->translateCatalog($threads)); + file_write($config['dir']['home'] . $board_name . '/catalog.json', $json); + + $json = json_encode($api->translateCatalog($threads, true)); + file_write($config['dir']['home'] . $board_name . '/threads.json', $json); + } } private function generateRecentPosts($threads) { @@ -457,25 +479,6 @@ 'config' => $config, 'recent_posts' => $recent_posts, 'board' => $board - ))); - - if ($config['api']['enabled']) { - $api = new Api(); - - $threads = array(); - - foreach ($recent_posts as $post) { - $threads[] = new Thread($post); - } - - // Page 0, the only page - $threads = array($threads); - - $json = json_encode($api->translateCatalog($threads)); - file_write($config['dir']['home'] . $board_name . '/catalog.json', $json); - - $json = json_encode($api->translateCatalog($threads, true)); - file_write($config['dir']['home'] . $board_name . '/threads.json', $json); - } + ))); } } From eafcce12cfa93cf1b2e559e1c258754d2ebf08de Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Tue, 12 Jan 2021 09:55:08 -0300 Subject: [PATCH 4/5] Correctly page the overboard json --- templates/themes/catalog/theme.php | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index 0785dc5a..c986aeb5 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -378,19 +378,26 @@ if ($config['api']['enabled']) { $api = new Api(); - $threads = array(); - - foreach ($recent_posts as $post) { - $threads[] = new Thread($post); + // Separate the threads into pages + $pages = array(array()); + $totalThreads = count($recent_posts); + $page = 0; + for ($i = 1; $i <= $totalThreads; $i++) { + $pages[$page][] = new Thread($recent_posts[$i-1]); + + // If we have not yet visited all threads, + // and we hit the limit on the current page, + // skip to the next page + if ($i < $totalThreads && ($i % $config['threads_per_page'] == 0)) { + $page++; + $pages[$page] = array(); + } } - - // Page 0, the only page - $threads = array($threads); - - $json = json_encode($api->translateCatalog($threads)); + + $json = json_encode($api->translateCatalog($pages)); file_write($config['dir']['home'] . $board_name . '/catalog.json', $json); - $json = json_encode($api->translateCatalog($threads, true)); + $json = json_encode($api->translateCatalog($pages, true)); file_write($config['dir']['home'] . $board_name . '/threads.json', $json); } } From bdd37d3322112dff4200ca48ae3c6f7fe1298583 Mon Sep 17 00:00:00 2001 From: Pietro Carrara Date: Tue, 12 Jan 2021 15:32:08 -0300 Subject: [PATCH 5/5] Add board data on all catalogs, rename overboard 'replies' and 'images' fields to keep consistency --- inc/functions.php | 4 ++-- templates/themes/catalog/catalog.html | 4 ++-- templates/themes/catalog/theme.php | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 3a624ff1..3e9fffd0 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -1322,7 +1322,7 @@ function index($page, $mod=false, $brief = false) { $body = ''; $offset = round($page*$config['threads_per_page']-$config['threads_per_page']); - $query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC LIMIT :offset,:threads_per_page", $board['uri'])); + $query = prepare(sprintf("SELECT *,'%s' as board FROM ``posts_%s`` WHERE `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC LIMIT :offset,:threads_per_page", $board['uri'], $board['uri'])); $query->bindValue(':offset', $offset, PDO::PARAM_INT); $query->bindValue(':threads_per_page', $config['threads_per_page'], PDO::PARAM_INT); $query->execute() or error(db_error($query)); @@ -2292,7 +2292,7 @@ function buildThread($id, $return = false, $mod = false) { $action = generation_strategy('sb_thread', array($board['uri'], $id)); if ($action == 'rebuild' || $return || $mod) { - $query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id`", $board['uri'])); + $query = prepare(sprintf("SELECT *,'%s' as board FROM ``posts_%s`` WHERE (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id`", $board['uri'],$board['uri'])); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); diff --git a/templates/themes/catalog/catalog.html b/templates/themes/catalog/catalog.html index a932278d..e39debee 100644 --- a/templates/themes/catalog/catalog.html +++ b/templates/themes/catalog/catalog.html @@ -48,7 +48,7 @@
{% for post in recent_posts %}
- R: {{ post.reply_count }} / I: {{ post.image_count }}{% if post.sticky %} (sticky){% endif %}{% if post.locked %}  {% endif %} + R: {{ post.replies }} / I: {{ post.images }}{% if post.sticky %} (sticky){% endif %}{% if post.locked %}  {% endif %} {% if post.subject %}

diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index c986aeb5..e2275338 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -337,8 +337,8 @@ private function buildThreadsQuery($board) { $sql = "SELECT *, `id` AS `thread_id`, " . - "(SELECT COUNT(`id`) FROM ``posts_$board`` WHERE `thread` = `thread_id`) AS `reply_count`, " . - "(SELECT SUM(`num_files`) FROM ``posts_$board`` WHERE `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `image_count`, " . + "(SELECT COUNT(`id`) FROM ``posts_$board`` WHERE `thread` = `thread_id`) AS `replies`, " . + "(SELECT SUM(`num_files`) FROM ``posts_$board`` WHERE `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `images`, " . "'$board' AS `board` FROM ``posts_$board`` WHERE `thread` IS NULL"; return $sql; @@ -445,8 +445,8 @@ $post['file'] = $config['root'] . $config['image_deleted']; } - if (empty($post['image_count'])) - $post['image_count'] = 0; + if (empty($post['images'])) + $post['images'] = 0; $post['pubdate'] = date('r', $post['time']); $posts[] = $post;