Count profile stats using Index database backend.

This commit is contained in:
flash 2023-08-31 00:24:59 +00:00
parent 4d6fb64f3a
commit 29426fafc1
3 changed files with 18 additions and 31 deletions

View file

@ -1,6 +1,7 @@
<?php
namespace Misuzu;
use stdClass;
use InvalidArgumentException;
use RuntimeException;
use Index\ByteFormat;
@ -287,33 +288,11 @@ if($isEditing) {
}
}
$profileStats = DB::prepare('
SELECT (
SELECT COUNT(`topic_id`)
FROM `msz_forum_topics`
WHERE `user_id` = u.`user_id`
AND `topic_deleted` IS NULL
) AS `forum_topic_count`,
(
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `user_id` = u.`user_id`
AND `post_deleted` IS NULL
) AS `forum_post_count`,
(
SELECT COUNT(`change_id`)
FROM `msz_changelog_changes`
WHERE `user_id` = u.`user_id`
) AS `changelog_count`,
(
SELECT COUNT(`comment_id`)
FROM `msz_comments_posts`
WHERE `user_id` = u.`user_id`
AND `comment_deleted` IS NULL
) AS `comments_count`
FROM `msz_users` AS u
WHERE `user_id` = :user_id
')->bind('user_id', $userInfo->getId())->fetch();
// TODO: create user counters so these can be statically kept
$profileStats = new stdClass;
$profileStats->forum_topic_count = $forum->countTopics(userInfo: $userInfo, deleted: false);
$profileStats->forum_post_count = $forum->countPosts(userInfo: $userInfo, deleted: false);
$profileStats->comments_count = $msz->getComments()->countPosts(userInfo: $userInfo, deleted: false);
if(!$viewingAsGuest) {
Template::set('profile_warnings', $msz->getWarnings()->getWarningsWithDefaultBacklog($userInfo));

View file

@ -218,6 +218,7 @@ class Comments {
public function countPosts(
CommentsCategoryInfo|string|null $categoryInfo = null,
CommentsPostInfo|string|null $parentInfo = null,
UserInfo|string|null $userInfo = null,
?bool $replies = null,
?bool $deleted = null
): int {
@ -228,6 +229,7 @@ class Comments {
$hasCategoryInfo = $categoryInfo !== null;
$hasParentInfo = $parentInfo !== null;
$hasUserInfo = $userInfo !== null;
$hasReplies = $replies !== null;
$hasDeleted = $deleted !== null;
@ -244,6 +246,8 @@ class Comments {
$query .= sprintf(' %s comment_reply_to %s NULL', ++$args > 1 ? 'AND' : 'WHERE', $replies ? 'IS NOT' : 'IS');
if($hasDeleted)
$query .= sprintf(' %s comment_deleted %s NULL', ++$args > 1 ? 'AND' : 'WHERE', $deleted ? 'IS NOT' : 'IS');
if($hasUserInfo)
$query .= sprintf(' %s user_id = ?', ++$args > 1 ? 'AND' : 'WHERE');
$args = 0;
$stmt = $this->cache->get($query);
@ -251,6 +255,8 @@ class Comments {
$stmt->addParameter(++$args, $parentInfo);
elseif($hasCategoryInfo)
$stmt->addParameter(++$args, $categoryInfo);
if($hasUserInfo)
$stmt->addParameter(++$args, $userInfo instanceof UserInfo ? $userInfo->getId() : $userInfo);
$stmt->execute();
$result = $stmt->getResult();
@ -265,6 +271,7 @@ class Comments {
public function getPosts(
CommentsCategoryInfo|string|null $categoryInfo = null,
CommentsPostInfo|string|null $parentInfo = null,
UserInfo|string|null $userInfo = null,
?bool $replies = null,
?bool $deleted = null,
bool $includeRepliesCount = false,
@ -277,6 +284,7 @@ class Comments {
$hasCategoryInfo = $categoryInfo !== null;
$hasParentInfo = $parentInfo !== null;
$hasUserInfo = $userInfo !== null;
$hasReplies = $replies !== null;
$hasDeleted = $deleted !== null;
@ -301,6 +309,8 @@ class Comments {
$query .= sprintf(' %s comment_reply_to %s NULL', ++$args > 1 ? 'AND' : 'WHERE', $replies ? 'IS NOT' : 'IS');
if($hasDeleted)
$query .= sprintf(' %s comment_deleted %s NULL', ++$args > 1 ? 'AND' : 'WHERE', $deleted ? 'IS NOT' : 'IS');
if($hasUserInfo)
$query .= sprintf(' %s user_id = ?', ++$args > 1 ? 'AND' : 'WHERE');
// this should really not be implicit like this
if($hasParentInfo)
@ -316,6 +326,8 @@ class Comments {
$stmt->addParameter(++$args, $parentInfo);
elseif($hasCategoryInfo)
$stmt->addParameter(++$args, $categoryInfo);
if($hasUserInfo)
$stmt->addParameter(++$args, $userInfo instanceof UserInfo ? $userInfo->getId() : $userInfo);
$stmt->execute();
$posts = [];

View file

@ -33,10 +33,6 @@
'title': 'Comments',
'value': profile_stats.comments_count,
},
{
'title': 'Changes',
'value': profile_stats.changelog_count,
},
] %}
{% else %}
{% set image = url('user-avatar', {'user': 0, 'res': 240}) %}