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
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
$authInfo = $msz->getAuthInfo();
|
|
|
|
if(!$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']))
|
|
|
|
$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)) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$usersCtx = $msz->getUsersContext();
|
|
|
|
$users = $usersCtx->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
|
|
|
|
|
|
|
$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 {
|
2023-09-06 13:50:19 +00:00
|
|
|
$searchQueryEvaluated['author'] = $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')) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$currentUser = $authInfo->getUserInfo();
|
2023-08-28 01:17:34 +00:00
|
|
|
$currentUserId = $currentUser === null ? 0 : (int)$currentUser->getId();
|
|
|
|
|
|
|
|
$forumCategoryIds = XArray::where(
|
|
|
|
$forum->getCategories(hidden: false),
|
2023-09-06 20:06:07 +00:00
|
|
|
fn($categoryInfo) => $categoryInfo->mayHaveTopics() && $authInfo->getPerms('forum', $categoryInfo)->check(Perm::F_CATEGORY_VIEW)
|
2023-08-28 01:17:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$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()) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$topic->user = $usersCtx->getUserInfo($topicInfo->getUserId(), 'id');
|
|
|
|
$topic->colour = $usersCtx->getUserColour($topic->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$topic->lastPost->info = $lastPostInfo = $forum->getPost(
|
|
|
|
topicInfo: $topicInfo,
|
|
|
|
getLast: true,
|
|
|
|
deleted: $topicInfo->isDeleted() ? null : false,
|
|
|
|
);
|
|
|
|
|
|
|
|
if($lastPostInfo->hasUserId()) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$topic->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId(), 'id');
|
|
|
|
$topic->lastPost->colour = $usersCtx->getUserColour($topic->lastPost->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
} catch(RuntimeException $ex) {}
|
|
|
|
}
|
|
|
|
|
2023-08-31 00:19:20 +00:00
|
|
|
$forumPostInfos = $forum->getPosts(categoryInfo: $forumCategoryIds, searchQuery: $searchQueryEvaluated);
|
2023-08-28 01:17:34 +00:00
|
|
|
$forumPosts = [];
|
|
|
|
|
|
|
|
foreach($forumPostInfos as $postInfo) {
|
|
|
|
$forumPosts[] = $post = new stdClass;
|
|
|
|
$post->info = $postInfo;
|
|
|
|
|
|
|
|
if($postInfo->hasUserId()) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$post->user = $usersCtx->getUserInfo($postInfo->getUserId(), 'id');
|
|
|
|
$post->colour = $usersCtx->getUserColour($post->user);
|
|
|
|
$post->postsCount = $forum->countPosts(userInfo: $post->user, deleted: false);
|
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 = [];
|
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-09-06 13:50:19 +00:00
|
|
|
$userInfo = $postInfo->hasUserId() ? $usersCtx->getUserInfo($postInfo->getUserId()) : null;
|
|
|
|
$userColour = $usersCtx->getUserColour($userInfo);
|
2023-07-15 17:02:46 +00:00
|
|
|
|
|
|
|
if(array_key_exists($categoryId, $newsCategoryInfos))
|
|
|
|
$categoryInfo = $newsCategoryInfos[$categoryId];
|
|
|
|
else
|
2023-08-05 13:50:15 +00:00
|
|
|
$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()
|
2023-08-05 13:50:15 +00:00
|
|
|
? $comments->countPosts(categoryInfo: $postInfo->getCommentsCategoryId(), 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 = [];
|
|
|
|
$memberInfos = $users->getUsers(searchQuery: $searchQueryEvaluated);
|
|
|
|
|
|
|
|
foreach($memberInfos as $memberInfo) {
|
|
|
|
$members[] = $member = new stdClass;
|
|
|
|
$member->info = $memberInfo;
|
2023-09-06 13:50:19 +00:00
|
|
|
$member->colour = $usersCtx->getUserColour($memberInfo);
|
2023-08-31 00:19:20 +00:00
|
|
|
$member->ftopics = $forum->countTopics(userInfo: $memberInfo, deleted: false);
|
|
|
|
$member->fposts = $forum->countPosts(userInfo: $memberInfo, deleted: false);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Template::render('home.search', [
|
|
|
|
'search_query' => $searchQuery,
|
|
|
|
'forum_topics' => $forumTopics ?? [],
|
|
|
|
'forum_posts' => $forumPosts ?? [],
|
2023-08-31 00:19:20 +00:00
|
|
|
'members' => $members ?? [],
|
2022-09-13 13:14:49 +00:00
|
|
|
'news_posts' => $newsPosts ?? [],
|
|
|
|
]);
|