This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
Sequenzia/app/models/Post/CountMethods.php

53 lines
1.9 KiB
PHP
Raw Normal View History

2013-10-27 01:06:58 +02:00
<?php
trait PostCountMethods
{
static public function fast_count($tags = null)
{
# A small sanitation
$tags = preg_replace('/ +/', ' ', trim($tags));
2013-11-19 18:31:38 +01:00
$cache_version = (int)Rails::cache()->read('$cache_version');
2013-12-04 04:51:09 +01:00
$key = ['post_count' => $tags, 'v' => $cache_version];
2013-10-27 01:06:58 +02:00
2013-11-19 18:31:38 +01:00
$count = (int)Rails::cache()->fetch($key, function() use ($tags) {
list($sql, $params) = Post::generate_sql($tags, ['count' => true]);
array_unshift($params, $sql);
return Post::countBySql($params);
});
return $count;
# This is just too brittle, and hard to make work with other features that may
# hide posts from the index.
# if tags.blank?
# return select_value_sql("SELECT row_count FROM table_data WHERE name = 'posts'").to_i
# else
# c = select_value_sql("SELECT post_count FROM tags WHERE name = ?", tags).to_i
# if c == 0
# return Post.count_by_sql(Post.generate_sql(tags, :count => true))
# else
# return c
# end
# end
2013-10-27 01:06:58 +02:00
}
static public function recalculate_row_count()
{
self::connection()->executeSql("UPDATE table_data SET row_count = (SELECT COUNT(*) FROM posts WHERE parent_id IS NULL AND status <> 'deleted') WHERE name = 'posts'");
}
static public function row_count()
{
$sql = "SELECT COUNT(*) FROM posts WHERE status <> 'deleted' AND NOT is_held AND status <> 'pending' AND is_shown_in_index";
return current(self::connection()->executeSql($sql)->fetch());
}
public function increment_count()
{
self::connection()->executeSql("UPDATE table_data SET row_count = row_count + 1 WHERE name = 'posts'");
}
public function decrement_count()
{
self::connection()->executeSql("UPDATE table_data SET row_count = row_count - 1 WHERE name = 'posts'");
}
}