summaryrefslogtreecommitdiff
path: root/src/index.php
diff options
context:
space:
mode:
authorJonas Kohl2024-09-17 14:51:23 +0200
committerJonas Kohl2024-09-17 14:51:23 +0200
commita65d424263adfbff9629c7d91a613e4504c84613 (patch)
tree7d89c6d85168427782ae8c24625db14df90e376a /src/index.php
parentf6dd78734f86f8daed7c5d472e7a199301095ff8 (diff)
Add search
Diffstat (limited to 'src/index.php')
-rw-r--r--src/index.php59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/index.php b/src/index.php
index 1b13e97..f3a59b2 100644
--- a/src/index.php
+++ b/src/index.php
@@ -1139,6 +1139,65 @@ if ($_action === "auth") {
}
header("Location: ./?_action=viewtopic&topic=" . urlencode($topicId));
+} elseif ($_action === "search") {
+ if (RequestUtils::isRequestMethod("POST")) {
+ $query = RequestUtils::getRequiredField("query");
+ /** @var Post[] $posts */
+
+ $start_time = microtime(true);
+ $posts = $db->execCustomQuery(<<<SQL
+ SELECT posts.* FROM topics, posts
+ WHERE
+ NOT posts.deleted
+ AND to_tsvector('english', topics.title || ' ' || posts.content) @@ websearch_to_tsquery('english', $1)
+ ORDER BY posts.post_date DESC
+ ;
+ SQL, [ $query ], Post::class);
+
+ $topicLookup = [];
+ $attachmentLookup = [];
+ $userLookup = [];
+ foreach ($posts as $post) {
+ if (!isset($topicLookup[$post->topicId])) {
+ $topic = new Topic;
+ $topic->id = $post->topicId;
+ if ($db->fetch($topic))
+ $topicLookup[$topic->id] = &$topic;
+ }
+ if (!isset($attachmentLookup[$post->id])) {
+ $attachmentLookup[$post->id] = $db->fetchCustom(Attachment::class, 'WHERE post_id = $1', [ $post->id ]);
+ }
+ if (!isset($userLookup[$post->authorId])) {
+ $user = new User;
+ $user->id = $post->authorId;
+ if ($db->fetch($user))
+ $userLookup[$post->authorId] = &$user;
+ }
+ }
+ $end_time = microtime(true);
+ $search_duration = $end_time - $start_time;
+
+ _view("template_start", ["_title" => __("Search results for “%query%”", [ "query" => $query ])]);
+ _view("template_navigation_start");
+ _view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($db)]);
+ _view("template_navigation_end");
+ _view("form_search", [ "query" => $query ]);
+ _view("view_search_results", [
+ "posts" => &$posts,
+ "topics" => &$topicLookup,
+ "users" => &$userLookup,
+ "attachments" => &$attachmentLookup,
+ "search_duration" => $search_duration,
+ ]);
+ _view("template_end", [...getThemeAndLangInfo()]);
+ } else {
+ _view("template_start", ["_title" => __("Search")]);
+ _view("template_navigation_start");
+ _view("template_navigation", ["user" => RequestUtils::getAuthorizedUser($db)]);
+ _view("template_navigation_end");
+ _view("form_search");
+ _view("template_end", [...getThemeAndLangInfo()]);
+ }
} elseif ($_action === "captcha") {
$phrase = generateCaptchaText();
$builder = new CaptchaBuilder($phrase);