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 <?php
namespace Misuzu; namespace Misuzu;
use stdClass;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\ByteFormat; use Index\ByteFormat;
@ -287,33 +288,11 @@ if($isEditing) {
} }
} }
$profileStats = DB::prepare(' // TODO: create user counters so these can be statically kept
SELECT ( $profileStats = new stdClass;
SELECT COUNT(`topic_id`) $profileStats->forum_topic_count = $forum->countTopics(userInfo: $userInfo, deleted: false);
FROM `msz_forum_topics` $profileStats->forum_post_count = $forum->countPosts(userInfo: $userInfo, deleted: false);
WHERE `user_id` = u.`user_id` $profileStats->comments_count = $msz->getComments()->countPosts(userInfo: $userInfo, deleted: false);
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();
if(!$viewingAsGuest) { if(!$viewingAsGuest) {
Template::set('profile_warnings', $msz->getWarnings()->getWarningsWithDefaultBacklog($userInfo)); Template::set('profile_warnings', $msz->getWarnings()->getWarningsWithDefaultBacklog($userInfo));

View file

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

View file

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