From c4dd643f53dca4aa417920d627e9a2bd33c37615 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Thu, 3 Jun 2021 17:57:10 -0700 Subject: [PATCH] Fix thread caching --- inc/database/database.php | 18 ++++++++++++++++++ inc/database/flatfile.php | 2 +- inc/database/mysql.php | 2 +- inc/database/mysqli.php | 2 +- inc/database/pdo.php | 2 +- inc/database/sqlite.php | 2 +- inc/database/sqlite3.php | 2 +- inc/html.php | 16 ++-------------- 8 files changed, 26 insertions(+), 20 deletions(-) diff --git a/inc/database/database.php b/inc/database/database.php index 9fd76cc..fbee0fe 100644 --- a/inc/database/database.php +++ b/inc/database/database.php @@ -21,3 +21,21 @@ if (TINYIB_MODPASS != '') { insertAccount($mod); } } + +$cache_all = array(); +$cache_moderated = array(); +function postsInThreadByID($id, $moderated_only = true) { + global $cache_all, $cache_moderated; + + if ($moderated_only) { + $cache = &$cache_moderated; + } else { + $cache = &$cache_all; + } + + $id = intval($id); + if (!isset($cache[$id])) { + $cache[$id] = _postsInThreadByID($id, $moderated_only); + } + return $cache[$id]; +} diff --git a/inc/database/flatfile.php b/inc/database/flatfile.php index 663ba34..bb3865b 100644 --- a/inc/database/flatfile.php +++ b/inc/database/flatfile.php @@ -364,7 +364,7 @@ function numRepliesToThreadByID($id) { return count($rows); } -function postsInThreadByID($id, $moderated_only = true) { +function _postsInThreadByID($id, $moderated_only = true) { $compClause = new OrWhereClause(); $compClause->add(new SimpleWhereClause(POST_ID, '=', $id, INTEGER_COMPARISON)); $compClause->add(new SimpleWhereClause(POST_PARENT, '=', $id, INTEGER_COMPARISON)); diff --git a/inc/database/mysql.php b/inc/database/mysql.php index 96c80be..2cbdd7e 100644 --- a/inc/database/mysql.php +++ b/inc/database/mysql.php @@ -208,7 +208,7 @@ function numRepliesToThreadByID($id) { return mysql_result(mysql_query("SELECT COUNT(*) FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = " . $id . " AND `moderated` = 1"), 0, 0); } -function postsInThreadByID($id, $moderated_only = true) { +function _postsInThreadByID($id, $moderated_only = true) { $posts = array(); $result = mysql_query("SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE (`id` = " . $id . " OR `parent` = " . $id . ")" . ($moderated_only ? " AND `moderated` = 1" : "") . " ORDER BY `id` ASC"); if ($result) { diff --git a/inc/database/mysqli.php b/inc/database/mysqli.php index 421e13c..c65f8b8 100644 --- a/inc/database/mysqli.php +++ b/inc/database/mysqli.php @@ -241,7 +241,7 @@ function numRepliesToThreadByID($id) { return mysqli_result(mysqli_query($link, "SELECT COUNT(*) FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = " . $id . " AND `moderated` = 1"), 0, 0); } -function postsInThreadByID($id, $moderated_only = true) { +function _postsInThreadByID($id, $moderated_only = true) { global $link; $posts = array(); $result = mysqli_query($link, "SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE (`id` = " . $id . " OR `parent` = " . $id . ")" . ($moderated_only ? " AND `moderated` = 1" : "") . " ORDER BY `id` ASC"); diff --git a/inc/database/pdo.php b/inc/database/pdo.php index 7ca282c..1ce7d04 100644 --- a/inc/database/pdo.php +++ b/inc/database/pdo.php @@ -195,7 +195,7 @@ function numRepliesToThreadByID($id) { return (int)$result->fetchColumn(); } -function postsInThreadByID($id, $moderated_only = true) { +function _postsInThreadByID($id, $moderated_only = true) { $posts = array(); $results = pdoQuery("SELECT * FROM " . TINYIB_DBPOSTS . " WHERE (id = ? OR parent = ?)" . ($moderated_only ? " AND moderated > 0" : "") . " ORDER BY id ASC", array($id, $id)); while ($row = $results->fetch(PDO::FETCH_ASSOC)) { diff --git a/inc/database/sqlite.php b/inc/database/sqlite.php index 698121c..de272d7 100644 --- a/inc/database/sqlite.php +++ b/inc/database/sqlite.php @@ -184,7 +184,7 @@ function numRepliesToThreadByID($id) { return sqlite_fetch_single(sqlite_query($GLOBALS["db"], "SELECT COUNT(*) FROM " . TINYIB_DBPOSTS . " WHERE parent = " . $id)); } -function postsInThreadByID($id, $moderated_only = true) { +function _postsInThreadByID($id, $moderated_only = true) { $posts = array(); $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBPOSTS . " WHERE (id = " . $id . " OR parent = " . $id . ")" . ($moderated_only ? " AND moderated > 0" : "") . " ORDER BY id ASC"), SQLITE_ASSOC); foreach ($result as $post) { diff --git a/inc/database/sqlite3.php b/inc/database/sqlite3.php index 86e8e3d..44070cd 100644 --- a/inc/database/sqlite3.php +++ b/inc/database/sqlite3.php @@ -215,7 +215,7 @@ function numRepliesToThreadByID($id) { return $db->querySingle("SELECT COUNT(*) FROM " . TINYIB_DBPOSTS . " WHERE parent = " . $id); } -function postsInThreadByID($id, $moderated_only = true) { +function _postsInThreadByID($id, $moderated_only = true) { global $db; $posts = array(); $result = $db->query("SELECT * FROM " . TINYIB_DBPOSTS . " WHERE (id = " . $id . " OR parent = " . $id . ")" . ($moderated_only ? " AND moderated > 0" : "") . " ORDER BY id ASC"); diff --git a/inc/html.php b/inc/html.php index 6afb8ea..6bf1921 100644 --- a/inc/html.php +++ b/inc/html.php @@ -417,18 +417,11 @@ function backlinks($post) { if (!TINYIB_BACKLINKS) { return ''; } - global $thread_cache; - - $parent_id = getParent($post); - - if (!isset($thread_cache[$parent_id])) { - $thread_cache[$parent_id] = postsInThreadByID($parent_id); - } + $posts = postsInThreadByID(getParent($post)); $needle = '>>' . $post['id']; - $return = ''; - foreach ($thread_cache[$parent_id] as $reply) { + foreach ($posts as $reply) { if (strpos($reply['message'], $needle) !== false) { if ($return != '') { $return .= ', '; @@ -789,9 +782,6 @@ function rebuildIndexes() { foreach ($threads as $thread) { $replies = postsInThreadByID($thread['id']); - if (!isset($thread_cache[$thread['id']])) { - $thread_cache[$thread['id']] = $replies; - } $thread['omitted'] = max(0, count($replies) - TINYIB_PREVIEWREPLIES - 1); // Build replies for preview @@ -831,7 +821,6 @@ function rebuildIndexes() { } function rebuildThread($id) { - global $thread_cache; $id = intval($id); $post = postByID($id); @@ -841,7 +830,6 @@ function rebuildThread($id) { } $posts = postsInThreadByID($id); - $thread_cache[getParent($post)] = $posts; if (count($posts) == 0) { @unlink('res/' . $id . '.html'); return;