summaryrefslogtreecommitdiff
path: root/inc/functions.php
diff options
context:
space:
mode:
authorMarcin Łabanowski <marcin@6irc.net>2013-01-23 17:19:31 +0100
committerMarcin Łabanowski <marcin@6irc.net>2013-01-23 17:19:31 +0100
commit25268f3876ecd955f193058acf550aae2c690be1 (patch)
tree0ef646c665f3693d046a4d3d28c146bdcc9012a4 /inc/functions.php
parent18ca523b185874bed900b35dd1f28c0f78f44acd (diff)
parentcd81ae8ea586bd3a4a05b0070dcb496fa2c8bd71 (diff)
Merge branch 'master' of https://github.com/savetheinternet/Tinyboard
Diffstat (limited to 'inc/functions.php')
-rw-r--r--inc/functions.php62
1 files changed, 33 insertions, 29 deletions
diff --git a/inc/functions.php b/inc/functions.php
index 3de6994d..a7e68390 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -974,45 +974,48 @@ function index($page, $mod=false) {
$th['sticky'], $th['locked'], $th['sage'], $th['embed'], $mod ? '?/' : $config['root'], $mod
);
- // if (!$mod && $config['cache']['enabled'] && $cached_stuff = cache::get("thread_index_{$board['uri']}_{$th['id']}")) {
- // $post_count = $cached_stuff[0];
- // $thread->posts = json_decode($cached_stuff[1]);
- //} else {
+ if (!$config['cache']['enabled'] || !$replies = cache::get("thread_index_{$board['uri']}_{$th['id']}")) {
$posts = prepare(sprintf("SELECT * FROM `posts_%s` WHERE `thread` = :id ORDER BY `id` DESC LIMIT :limit", $board['uri']));
$posts->bindValue(':id', $th['id']);
$posts->bindValue(':limit', ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']), PDO::PARAM_INT);
$posts->execute() or error(db_error($posts));
-
- $num_images = 0;
- while ($po = $posts->fetch()) {
- if ($po['file'])
- $num_images++;
-
- $thread->add(new Post(
- $po['id'], $th['id'], $po['subject'], $po['email'], $po['name'], $po['trip'], $po['capcode'], $po['body'], $po['time'],
- $po['thumb'], $po['thumbwidth'], $po['thumbheight'], $po['file'], $po['filewidth'], $po['fileheight'], $po['filesize'],
- $po['filename'], $po['ip'], $po['embed'], $mod ? '?/' : $config['root'], $mod)
- );
- }
+
+ $replies = $posts->fetchAll(PDO::FETCH_ASSOC);
- $post_count = $posts->rowCount();
- //}
+ if ($config['cache']['enabled'])
+ cache::set("thread_index_{$board['uri']}_{$th['id']}", $replies);
+ }
+
+ $num_images = 0;
+ foreach ($replies as $po) {
+ if ($po['file'])
+ $num_images++;
+
+ $thread->add(new Post(
+ $po['id'], $th['id'], $po['subject'], $po['email'], $po['name'], $po['trip'], $po['capcode'], $po['body'], $po['time'],
+ $po['thumb'], $po['thumbwidth'], $po['thumbheight'], $po['file'], $po['filewidth'], $po['fileheight'], $po['filesize'],
+ $po['filename'], $po['ip'], $po['embed'], $mod ? '?/' : $config['root'], $mod)
+ );
+ }
+
+ $post_count = count($replies); // $posts->rowCount()
if ($post_count == ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) {
- $count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM `posts_%s` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM `posts_%s` WHERE `file` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri']));
- $count->bindValue(':thread', $th['id'], PDO::PARAM_INT);
- $count->execute() or error(db_error($count));
-
- $c = $count->fetch();
- $thread->omitted = $c['num'] - ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']);
+ // Yeah, using two cache objects for one thread seems a little inefficient. This code is messy and dumb; please clean it up if you can.
+ if (!$config['cache']['enabled'] || !$count = cache::get("thread_index_{$board['uri']}_{$th['id']}_omitted")) {
+ $count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM `posts_%s` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM `posts_%s` WHERE `file` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri']));
+ $count->bindValue(':thread', $th['id'], PDO::PARAM_INT);
+ $count->execute() or error(db_error($count));
+ $count = $count->fetchAll(PDO::FETCH_COLUMN);
+
+ if ($config['cache']['enabled'])
+ cache::set("thread_index_{$board['uri']}_{$th['id']}_omitted", $count);
+ }
- $c = $count->fetch();
- $thread->omitted_images = $c['num'] - $num_images;
+ $thread->omitted = $count[0] - ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']);
+ $thread->omitted_images = $count[1] - $num_images;
}
- // if ($config['cache']['enabled'])
- // cache::set("thread_index_{$board['uri']}_{$th['id']}", json_encode(array($posts->rowCount(), $thread->posts)));
-
$thread->posts = array_reverse($thread->posts);
$body .= $thread->build(true);
@@ -1503,6 +1506,7 @@ function buildThread($id, $return=false, $mod=false) {
if ($config['cache']['enabled'] && !$mod) {
// Clear cache
cache::delete("thread_index_{$board['uri']}_{$id}");
+ cache::delete("thread_index_{$board['uri']}_{$id}_omitted");
cache::delete("thread_{$board['uri']}_{$id}");
}