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;
|
2023-08-02 22:12:47 +00:00
|
|
|
|
2024-12-02 02:28:08 +00:00
|
|
|
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
|
|
|
|
die('Script must be called through the Misuzu route dispatcher.');
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
if(!$msz->authInfo->isLoggedIn)
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(403);
|
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']))
|
2024-11-30 04:09:29 +00:00
|
|
|
$existing[] = 'author:' . $searchQueryEvaluated['author']->name;
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
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'])
|
|
|
|
)));
|
|
|
|
|
2023-10-18 10:11:21 +00:00
|
|
|
return implode(' ', $existing);
|
2023-08-28 01:17:34 +00:00
|
|
|
});
|
|
|
|
if(!empty($searchQuery)) {
|
|
|
|
$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 {
|
2024-11-30 04:09:29 +00:00
|
|
|
$searchQueryEvaluated['author'] = $msz->usersCtx->getUserInfo($searchQueryEvaluated['author'], 'search');
|
2023-08-28 01:17:34 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
|
|
|
unset($searchQueryEvaluated['author']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty($searchQueryEvaluated['type']) || str_starts_with($searchQueryEvaluated['type'], 'forum')) {
|
2024-11-30 04:09:29 +00:00
|
|
|
$currentUser = $msz->authInfo->userInfo;
|
2024-11-30 04:20:20 +00:00
|
|
|
$currentUserId = $currentUser === null ? 0 : (int)$currentUser->id;
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
$forumCategoryIds = XArray::where(
|
2024-11-30 04:09:29 +00:00
|
|
|
$msz->forumCtx->categories->getCategories(hidden: false),
|
|
|
|
fn($categoryInfo) => $categoryInfo->mayHaveTopics && $msz->authInfo->getPerms('forum', $categoryInfo)->check(Perm::F_CATEGORY_VIEW)
|
2023-08-28 01:17:34 +00:00
|
|
|
);
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
$forumTopicInfos = $msz->forumCtx->topics->getTopics(categoryInfo: $forumCategoryIds, deleted: false, searchQuery: $searchQueryEvaluated);
|
2023-09-08 13:22:46 +00:00
|
|
|
$ftopics = [];
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
foreach($forumTopicInfos as $topicInfo) {
|
2023-09-08 13:22:46 +00:00
|
|
|
$ftopics[] = $topic = new stdClass;
|
2023-08-28 01:17:34 +00:00
|
|
|
$topic->info = $topicInfo;
|
2024-11-30 04:09:29 +00:00
|
|
|
$topic->unread = $msz->forumCtx->topics->checkTopicUnread($topicInfo, $currentUser);
|
|
|
|
$topic->participated = $msz->forumCtx->topics->checkTopicParticipated($topicInfo, $currentUser);
|
2023-08-28 01:17:34 +00:00
|
|
|
$topic->lastPost = new stdClass;
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
if($topicInfo->userId !== null) {
|
|
|
|
$topic->user = $msz->usersCtx->getUserInfo($topicInfo->userId, 'id');
|
|
|
|
$topic->colour = $msz->usersCtx->getUserColour($topic->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-11-30 04:09:29 +00:00
|
|
|
$topic->lastPost->info = $lastPostInfo = $msz->forumCtx->posts->getPost(
|
2023-08-28 01:17:34 +00:00
|
|
|
topicInfo: $topicInfo,
|
|
|
|
getLast: true,
|
2024-11-30 04:09:29 +00:00
|
|
|
deleted: $topicInfo->deleted ? null : false,
|
2023-08-28 01:17:34 +00:00
|
|
|
);
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
if($lastPostInfo->userId !== null) {
|
|
|
|
$topic->lastPost->user = $msz->usersCtx->getUserInfo($lastPostInfo->userId, 'id');
|
|
|
|
$topic->lastPost->colour = $msz->usersCtx->getUserColour($topic->lastPost->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
} catch(RuntimeException $ex) {}
|
|
|
|
}
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
$forumPostInfos = $msz->forumCtx->posts->getPosts(categoryInfo: $forumCategoryIds, searchQuery: $searchQueryEvaluated);
|
2023-09-08 13:22:46 +00:00
|
|
|
$fposts = [];
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
foreach($forumPostInfos as $postInfo) {
|
2023-09-08 13:22:46 +00:00
|
|
|
$fposts[] = $post = new stdClass;
|
2023-08-28 01:17:34 +00:00
|
|
|
$post->info = $postInfo;
|
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
if($postInfo->userId !== null) {
|
|
|
|
$post->user = $msz->usersCtx->getUserInfo($postInfo->userId, 'id');
|
|
|
|
$post->colour = $msz->usersCtx->getUserColour($post->user);
|
|
|
|
$post->postsCount = $msz->forumCtx->countTotalUserPosts($post->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// can't be bothered sorry
|
|
|
|
$post->isOriginalPost = false;
|
|
|
|
$post->isOriginalPoster = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:02:46 +00:00
|
|
|
$newsPosts = [];
|
2024-11-30 04:09:29 +00:00
|
|
|
$newsPostInfos = empty($searchQueryEvaluated['type']) || $searchQueryEvaluated['type'] === 'news' ? $msz->news->getPosts(searchQuery: $searchQuery) : [];
|
2023-07-15 17:02:46 +00:00
|
|
|
$newsCategoryInfos = [];
|
|
|
|
|
|
|
|
foreach($newsPostInfos as $postInfo) {
|
2024-11-30 04:09:29 +00:00
|
|
|
$categoryId = $postInfo->categoryId;
|
|
|
|
$userInfo = $postInfo->userId !== null ? $msz->usersCtx->getUserInfo($postInfo->userId) : null;
|
|
|
|
$userColour = $msz->usersCtx->getUserColour($userInfo);
|
2023-07-15 17:02:46 +00:00
|
|
|
|
|
|
|
if(array_key_exists($categoryId, $newsCategoryInfos))
|
|
|
|
$categoryInfo = $newsCategoryInfos[$categoryId];
|
|
|
|
else
|
2024-11-30 04:09:29 +00:00
|
|
|
$newsCategoryInfos[$categoryId] = $categoryInfo = $msz->news->getCategory(postInfo: $postInfo);
|
2023-07-15 17:02:46 +00:00
|
|
|
|
2024-11-30 04:09:29 +00:00
|
|
|
$commentsCount = $postInfo->commentsSectionId !== null
|
|
|
|
? $msz->comments->countPosts(categoryInfo: $postInfo->commentsSectionId, deleted: false) : 0;
|
2023-07-15 17:02:46 +00:00
|
|
|
|
|
|
|
$newsPosts[] = [
|
|
|
|
'post' => $postInfo,
|
|
|
|
'category' => $categoryInfo,
|
|
|
|
'user' => $userInfo,
|
2023-09-06 13:50:19 +00:00
|
|
|
'user_colour' => $userColour,
|
2023-07-15 17:02:46 +00:00
|
|
|
'comments_count' => $commentsCount,
|
|
|
|
];
|
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-31 00:19:20 +00:00
|
|
|
$members = [];
|
2024-11-30 04:09:29 +00:00
|
|
|
$memberInfos = $msz->usersCtx->users->getUsers(searchQuery: $searchQueryEvaluated);
|
2023-08-31 00:19:20 +00:00
|
|
|
|
|
|
|
foreach($memberInfos as $memberInfo) {
|
|
|
|
$members[] = $member = new stdClass;
|
|
|
|
$member->info = $memberInfo;
|
2024-11-30 04:09:29 +00:00
|
|
|
$member->colour = $msz->usersCtx->getUserColour($memberInfo);
|
|
|
|
$member->ftopics = $msz->forumCtx->countTotalUserTopics($memberInfo);
|
|
|
|
$member->fposts = $msz->forumCtx->countTotalUserPosts($memberInfo);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Template::render('home.search', [
|
|
|
|
'search_query' => $searchQuery,
|
2023-09-08 13:22:46 +00:00
|
|
|
'forum_topics' => $ftopics ?? [],
|
|
|
|
'forum_posts' => $fposts ?? [],
|
2023-08-31 00:19:20 +00:00
|
|
|
'members' => $members ?? [],
|
2022-09-13 13:14:49 +00:00
|
|
|
'news_posts' => $newsPosts ?? [],
|
|
|
|
]);
|