misuzu/public-legacy/search.php

235 lines
8.9 KiB
PHP
Raw Permalink Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
2023-08-28 01:17:34 +00:00
use stdClass;
2023-07-15 23:58:17 +00:00
use RuntimeException;
2023-08-28 01:17:34 +00:00
use Index\XArray;
2023-07-15 17:02:46 +00:00
use Misuzu\Comments\CommentsCategory;
if(!$msz->isLoggedIn()) {
echo render_error(403);
return;
}
2022-09-13 13:14:49 +00:00
$searchQuery = !empty($_GET['q']) && is_string($_GET['q']) ? $_GET['q'] : '';
2023-08-28 01:17:34 +00:00
$searchQueryEvaluated = ['query' => []];
Template::addFunction('search_merge_query', function($attrs) use (&$searchQueryEvaluated) {
$existing = [];
2023-07-15 17:02:46 +00:00
2023-08-28 01:17:34 +00:00
if(!empty($attrs['type']))
$existing[] = 'type:' . $attrs['type'];
elseif(!empty($searchQueryEvaluated['type']))
$existing[] = 'type:' . $searchQueryEvaluated['type'];
if(!empty($attrs['author']))
$existing[] = 'author:' . $attrs['author'];
elseif(!empty($searchQueryEvaluated['author']))
$existing[] = 'author:' . $searchQueryEvaluated['author']->getName();
if(!empty($attrs['after']))
$existing[] = 'after:' . $attrs['after'];
elseif(!empty($searchQueryEvaluated['after']))
$existing[] = 'after:' . $searchQueryEvaluated['after'];
$existing = array_merge($existing, array_unique(array_merge(
$searchQueryEvaluated['query'],
empty($attrs['query']) ? [] : explode(' ', $attrs['query'])
)));
return rawurlencode(implode(' ', $existing));
});
if(!empty($searchQuery)) {
$users = $msz->getUsers();
2023-08-28 01:17:34 +00:00
$forum = $msz->getForum();
$news = $msz->getNews();
2023-07-15 23:58:17 +00:00
$comments = $msz->getComments();
2023-08-28 01:17:34 +00:00
$userInfos = [];
$userColours = [];
$searchQueryAttributes = ['type', 'author', 'after'];
$searchQueryParts = explode(' ', $searchQuery);
foreach($searchQueryParts as $queryPart) {
$queryPart = trim($queryPart);
if($queryPart === '')
continue;
$colonIndex = strpos($queryPart, ':');
if($colonIndex !== false && $colonIndex > 0) {
$attrName = substr($queryPart, 0, $colonIndex);
if(in_array($attrName, $searchQueryAttributes)) {
$attrValue = substr($queryPart, $colonIndex + 1);
$searchQueryEvaluated[$attrName] = $attrValue;
continue;
}
}
$searchQueryEvaluated['query'][] = $queryPart;
}
$searchQueryEvaluated['query_string'] = implode(' ', $searchQueryEvaluated['query']);
if(!empty($searchQueryEvaluated['author']))
try {
$searchQueryEvaluated['author'] = $users->getUser($searchQueryEvaluated['author'], 'search');
} catch(RuntimeException $ex) {
unset($searchQueryEvaluated['author']);
}
if(empty($searchQueryEvaluated['type']) || str_starts_with($searchQueryEvaluated['type'], 'forum')) {
$currentUser = $msz->getActiveUser();
$currentUserId = $currentUser === null ? 0 : (int)$currentUser->getId();
$forumCategoryIds = XArray::where(
$forum->getCategories(hidden: false),
fn($categoryInfo) => $categoryInfo->mayHaveTopics() && forum_perms_check_user(MSZ_FORUM_PERMS_GENERAL, $categoryInfo->getId(), $currentUserId, MSZ_FORUM_PERM_VIEW_FORUM)
);
$forumTopicInfos = $forum->getTopics(categoryInfo: $forumCategoryIds, deleted: false, searchQuery: $searchQueryEvaluated);
$forumTopics = [];
foreach($forumTopicInfos as $topicInfo) {
$forumTopics[] = $topic = new stdClass;
$topic->info = $topicInfo;
$topic->unread = $forum->checkTopicUnread($topicInfo, $currentUser);
$topic->participated = $forum->checkTopicParticipated($topicInfo, $currentUser);
$topic->lastPost = new stdClass;
if($topicInfo->hasUserId()) {
$lastTopicUserId = $topicInfo->getUserId();
if(!array_key_exists($lastTopicUserId, $userInfos)) {
$userInfo = $users->getUser($lastTopicUserId, 'id');
$userInfos[$lastTopicUserId] = $userInfo;
$userColours[$lastTopicUserId] = $users->getUserColour($userInfo);
}
$topic->user = $userInfos[$lastTopicUserId];
$topic->colour = $userColours[$lastTopicUserId];
}
try {
$topic->lastPost->info = $lastPostInfo = $forum->getPost(
topicInfo: $topicInfo,
getLast: true,
deleted: $topicInfo->isDeleted() ? null : false,
);
if($lastPostInfo->hasUserId()) {
$lastPostUserId = $lastPostInfo->getUserId();
if(!array_key_exists($lastPostUserId, $userInfos)) {
$userInfo = $users->getUser($lastPostUserId, 'id');
$userInfos[$lastPostUserId] = $userInfo;
$userColours[$lastPostUserId] = $users->getUserColour($userInfo);
}
$topic->lastPost->user = $userInfos[$lastPostUserId];
$topic->lastPost->colour = $userColours[$lastPostUserId];
}
} catch(RuntimeException $ex) {}
}
$forumPostInfos = $forum->getPosts(categoryInfo: $forumCategoryIds, deleted: false, searchQuery: $searchQueryEvaluated);
$forumPosts = [];
foreach($forumPostInfos as $postInfo) {
$forumPosts[] = $post = new stdClass;
$post->info = $postInfo;
if($postInfo->hasUserId()) {
$postUserId = $postInfo->getUserId();
if(!array_key_exists($postUserId, $userInfos)) {
$userInfo = $users->getUser($postUserId, 'id');
$userInfos[$postUserId] = $userInfo;
$userColours[$postUserId] = $users->getUserColour($userInfo);
$userPostsCounts[$postUserId] = $forum->countPosts(userInfo: $userInfo, deleted: false);
}
$post->user = $userInfos[$postUserId];
$post->colour = $userColours[$postUserId];
$post->postsCount = null;
}
// can't be bothered sorry
$post->isOriginalPost = false;
$post->isOriginalPoster = false;
}
}
2023-07-15 17:02:46 +00:00
$newsPosts = [];
2023-08-28 01:17:34 +00:00
$newsPostInfos = empty($searchQueryEvaluated['type']) || $searchQueryEvaluated['type'] === 'news' ? $news->getPosts(searchQuery: $searchQuery) : [];
2023-07-15 17:02:46 +00:00
$newsCategoryInfos = [];
foreach($newsPostInfos as $postInfo) {
$userId = $postInfo->getUserId();
$categoryId = $postInfo->getCategoryId();
2023-08-28 01:17:34 +00:00
if(array_key_exists($userId, $userInfos)) {
$userInfo = $userInfos[$userId];
2023-07-15 17:02:46 +00:00
} else {
try {
$userInfo = $users->getUser($userId, 'id');
2023-08-28 01:17:34 +00:00
$userColours[$userId] = $users->getUserColour($userInfo);
} catch(RuntimeException $ex) {
2023-07-15 17:02:46 +00:00
$userInfo = null;
}
2023-08-28 01:17:34 +00:00
$userInfos[$userId] = $userInfo;
2023-07-15 17:02:46 +00:00
}
if(array_key_exists($categoryId, $newsCategoryInfos))
$categoryInfo = $newsCategoryInfos[$categoryId];
else
$newsCategoryInfos[$categoryId] = $categoryInfo = $news->getCategory(postInfo: $postInfo);
2023-07-15 17:02:46 +00:00
2023-07-15 23:58:17 +00:00
$commentsCount = $postInfo->hasCommentsCategoryId()
? $comments->countPosts(categoryInfo: $postInfo->getCommentsCategoryId(), deleted: false) : 0;
2023-07-15 17:02:46 +00:00
$newsPosts[] = [
'post' => $postInfo,
'category' => $categoryInfo,
'user' => $userInfo,
2023-08-28 01:17:34 +00:00
'user_colour' => $userColours[$userId] ?? \Index\Colour\Colour::none(),
2023-07-15 17:02:46 +00:00
'comments_count' => $commentsCount,
];
}
2022-09-13 13:14:49 +00:00
2023-08-28 01:17:34 +00:00
if(empty($searchQueryEvaluated['type']) || $searchQueryEvaluated['type'] === 'member') {
$findUsers = DB::prepare('
SELECT u.`user_id`, u.`username`, u.`user_country`,
u.`user_created`, u.`user_active`, r.`role_id`,
COALESCE(u.`user_title`, r.`role_title`) AS `user_title`,
COALESCE(u.`user_colour`, r.`role_colour`) AS `user_colour`,
(
SELECT COUNT(`topic_id`)
FROM `msz_forum_topics`
WHERE `user_id` = u.`user_id`
AND `topic_deleted` IS NULL
) AS `user_count_topics`,
(
SELECT COUNT(`post_Id`)
FROM `msz_forum_posts`
WHERE `user_id` = u.`user_id`
AND `post_deleted` IS NULL
) AS `user_count_posts`
FROM `msz_users` AS u
LEFT JOIN `msz_roles` AS r
ON r.`role_id` = u.`display_role`
LEFT JOIN `msz_users_roles` AS ur
ON ur.`user_id` = u.`user_id`
WHERE u.`username` LIKE CONCAT("%%", :query, "%%")
GROUP BY u.`user_id`
');
$findUsers->bind('query', $searchQuery);
$userList = $findUsers->fetchAll();
}
2022-09-13 13:14:49 +00:00
}
Template::render('home.search', [
'search_query' => $searchQuery,
'forum_topics' => $forumTopics ?? [],
'forum_posts' => $forumPosts ?? [],
'users' => $userList ?? [],
2022-09-13 13:14:49 +00:00
'news_posts' => $newsPosts ?? [],
]);