From 67578b5b60633e0dfc9265fe356a7071dcf7e82e Mon Sep 17 00:00:00 2001 From: Marcos Vives Del Sol Date: Sun, 13 Jan 2013 11:47:36 +0100 Subject: [PATCH 1/2] Faster index generation and customizable amount of replies in preview --- inc/database_flatfile.php | 5 ----- inc/database_mysql.php | 11 ----------- inc/database_sqlite.php | 9 --------- inc/html.php | 17 ++++++++--------- settings.default.php | 3 ++- 5 files changed, 10 insertions(+), 35 deletions(-) diff --git a/inc/database_flatfile.php b/inc/database_flatfile.php index 1d494bd..886e97f 100644 --- a/inc/database_flatfile.php +++ b/inc/database_flatfile.php @@ -150,11 +150,6 @@ function postsInThreadByID($id) { return convertPostsToSQLStyle($rows); } -function latestRepliesInThreadByID($id) { - $rows = $GLOBALS['db']->selectWhere(POSTS_FILE, new SimpleWhereClause(POST_PARENT, '=', $id, INTEGER_COMPARISON), 3, new OrderBy(POST_ID, DESCENDING, INTEGER_COMPARISON)); - return convertPostsToSQLStyle($rows); -} - function postsByHex($hex) { $rows = $GLOBALS['db']->selectWhere(POSTS_FILE, new SimpleWhereClause(POST_FILE_HEX, '=', $hex, STRING_COMPARISON), 1); return convertPostsToSQLStyle($rows); diff --git a/inc/database_mysql.php b/inc/database_mysql.php index 92d35dc..ea13084 100644 --- a/inc/database_mysql.php +++ b/inc/database_mysql.php @@ -108,17 +108,6 @@ function postsInThreadByID($id) { return $posts; } -function latestRepliesInThreadByID($id) { - $posts = array(); - $replies = mysql_query("SELECT * FROM `" . TINYIB_DBPOSTS . "` WHERE `parent` = " . $id . " ORDER BY `id` DESC LIMIT 3"); - if ($replies) { - while ($post = mysql_fetch_assoc($replies)) { - $posts[] = $post; - } - } - return $posts; -} - function postsByHex($hex) { $posts = array(); $result = mysql_query("SELECT `id`, `parent` FROM `" . TINYIB_DBPOSTS . "` WHERE `file_hex` = '" . mysql_real_escape_string($hex) . "' LIMIT 1"); diff --git a/inc/database_sqlite.php b/inc/database_sqlite.php index eaf5167..470121f 100644 --- a/inc/database_sqlite.php +++ b/inc/database_sqlite.php @@ -93,15 +93,6 @@ function postsInThreadByID($id) { return $posts; } -function latestRepliesInThreadByID($id) { - $posts = array(); - $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT * FROM " . TINYIB_DBPOSTS . " WHERE parent = " . $id . " ORDER BY id DESC LIMIT 3"), SQLITE_ASSOC); - foreach ($result as $post) { - $posts[] = $post; - } - return $posts; -} - function postsByHex($hex) { $posts = array(); $result = sqlite_fetch_all(sqlite_query($GLOBALS["db"], "SELECT id, parent FROM " . TINYIB_DBPOSTS . " WHERE file_hex = '" . sqlite_escape_string($hex) . "' LIMIT 1"), SQLITE_ASSOC); diff --git a/inc/html.php b/inc/html.php index 37a5d2f..02f37eb 100644 --- a/inc/html.php +++ b/inc/html.php @@ -274,23 +274,22 @@ EOF; function rebuildIndexes() { $page = 0; $i = 0; $htmlposts = ''; - $pages = ceil(countThreads() / 10) - 1; $threads = allThreads(); + $pages = ceil(count($threads) / 10) - 1; foreach ($threads as $thread) { - $replies = latestRepliesInThreadByID($thread['id']); + $replies = postsInThreadByID($thread['id']); $htmlreplies = array(); - foreach ($replies as $reply) { - $htmlreplies[] = buildPost($reply, TINYIB_INDEXPAGE); + for ($j = min(count($replies) - 1, TINYIB_PREVIEWREPLIES); $j > 0; $j--) { + $htmlreplies[$j] = buildPost($replies[$j], TINYIB_INDEXPAGE); } - $thread['omitted'] = (count($htmlreplies) == 3) ? (count(postsInThreadByID($thread['id'])) - 4) : 0; + $thread['omitted'] = max(count($replies) - TINYIB_PREVIEWREPLIES - 1, 0); + + $htmlposts .= buildPost($thread, TINYIB_INDEXPAGE) . implode('', $htmlreplies) . "
\n
"; - $htmlposts .= buildPost($thread, TINYIB_INDEXPAGE) . implode('', array_reverse($htmlreplies)) . "
\n
"; - - $i += 1; - if ($i == 10) { + if (++$i == 10) { $file = ($page == 0) ? 'index.html' : $page . '.html'; writePage($file, buildPage($htmlposts, 0, $pages, $page)); diff --git a/settings.default.php b/settings.default.php index 8a4fcd1..80d9431 100644 --- a/settings.default.php +++ b/settings.default.php @@ -1,4 +1,4 @@ - Date: Sun, 13 Jan 2013 14:40:34 +0100 Subject: [PATCH 2/2] Customizable amount of threads per page. Fixed a bug, too. --- inc/html.php | 22 +++++++++++----------- settings.default.php | 3 ++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/inc/html.php b/inc/html.php index 02f37eb..569960c 100644 --- a/inc/html.php +++ b/inc/html.php @@ -275,25 +275,25 @@ EOF; function rebuildIndexes() { $page = 0; $i = 0; $htmlposts = ''; $threads = allThreads(); - $pages = ceil(count($threads) / 10) - 1; + $pages = ceil(count($threads) / TINYIB_THREADSPERPAGE) - 1; foreach ($threads as $thread) { $replies = postsInThreadByID($thread['id']); - - $htmlreplies = array(); - for ($j = min(count($replies) - 1, TINYIB_PREVIEWREPLIES); $j > 0; $j--) { - $htmlreplies[$j] = buildPost($replies[$j], TINYIB_INDEXPAGE); - } - - $thread['omitted'] = max(count($replies) - TINYIB_PREVIEWREPLIES - 1, 0); + $thread['omitted'] = max(0, count($replies) - TINYIB_PREVIEWREPLIES - 1); - $htmlposts .= buildPost($thread, TINYIB_INDEXPAGE) . implode('', $htmlreplies) . "
\n
"; + $htmlposts .= buildPost($thread, TINYIB_INDEXPAGE); + + for ($j = count($replies) - 1; $j > $thread['omitted']; $j--) { + $htmlposts .= buildPost($replies[$j], TINYIB_INDEXPAGE); + } + + $htmlposts .= "
\n
"; - if (++$i == 10) { + if (++$i >= TINYIB_THREADSPERPAGE) { $file = ($page == 0) ? 'index.html' : $page . '.html'; writePage($file, buildPage($htmlposts, 0, $pages, $page)); - $page += 1; $i = 0; $htmlposts = ''; + $page++; $i = 0; $htmlposts = ''; } } diff --git a/settings.default.php b/settings.default.php index 80d9431..9b70388 100644 --- a/settings.default.php +++ b/settings.default.php @@ -13,7 +13,8 @@ define('TINYIB_TRIPSEED', ""); // Enter some random text - Used when generating define('TINYIB_ADMINPASS', ""); // Text entered at the manage prompt to gain administrator access define('TINYIB_MODPASS', ""); // Moderators only have access to delete posts ["" to disable] define('TINYIB_DBMODE', "flatfile"); // Choose: flatfile / mysql / sqlite -define('TINYIB_PREVIEWREPLIES', 0); // Max replies to show on index +define('TINYIB_PREVIEWREPLIES', 3); // Max replies to show on index +define('TINYIB_THREADSPERPAGE', 10); // Amount of threads per page on index // Note: The following only apply when TINYIB_DBMODE is set to mysql define('TINYIB_DBHOST', "localhost");