From 370d6a0f74c8076f8b49405b3ad1dcf304d9f3dd Mon Sep 17 00:00:00 2001 From: Shish Date: Fri, 18 Aug 2023 13:38:55 +0100 Subject: [PATCH] dedicated search_link() function for generating links to search results, see #936 --- core/imageboard/misc.php | 2 +- core/tests/urls.test.php | 12 ++++++++++++ core/urls.php | 14 ++++++++++++++ ext/approval/main.php | 2 +- ext/artists/theme.php | 2 +- ext/browser_search/main.php | 2 +- ext/favorites/main.php | 4 ++-- ext/home/theme.php | 2 +- ext/image/main.php | 2 +- ext/index/main.php | 11 ++--------- ext/index/theme.php | 8 +++----- ext/notes/theme.php | 2 +- ext/numeric_score/main.php | 5 ++--- ext/rating/theme.php | 3 +-- ext/relationships/theme.php | 2 +- ext/tag_edit/main.php | 2 +- ext/tag_edit/theme.php | 3 +-- ext/tag_history/theme.php | 2 +- ext/tag_list/theme.php | 3 +-- ext/trash/main.php | 2 +- ext/upload/test.php | 2 +- ext/upload/theme.php | 2 +- ext/user/main.php | 2 +- themes/danbooru/comment.theme.php | 3 +-- themes/danbooru2/comment.theme.php | 3 +-- 25 files changed, 54 insertions(+), 43 deletions(-) diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index 09def27ee..f7bc1c62a 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -197,7 +197,7 @@ function redirect_to_next_image(Image $image): void $target_image = $image->get_next($search_terms); if ($target_image === null) { - $redirect_target = referer_or(make_link("post/list"), ['post/view']); + $redirect_target = referer_or(search_link(), ['post/view']); } else { $redirect_target = make_link("post/view/{$target_image->id}", null, $query); } diff --git a/core/tests/urls.test.php b/core/tests/urls.test.php index fb34d4ab8..e607726a1 100644 --- a/core/tests/urls.test.php +++ b/core/tests/urls.test.php @@ -10,6 +10,18 @@ class UrlsTest extends TestCase { + public function test_search_link() + { + $this->assertEquals( + "/test/post/list/bar+foo/1", + search_link(["foo", "bar"]) + ); + $this->assertEquals( + "/test/post/list/cat%2A+rating%3D%5Eq/1", + search_link(["rating=?", "cat*"]) + ); + } + public function test_make_link() { // basic diff --git a/core/urls.php b/core/urls.php index 0950f4bca..40261458b 100644 --- a/core/urls.php +++ b/core/urls.php @@ -23,6 +23,20 @@ public function make_link(): string } } +/** + * Build a link to a search page for given terms, + * with all the appropriate escaping + */ +function search_link(array $terms = [], int $page = 1): string +{ + if($terms) { + $q = urlencode(Tag::caret(Tag::implode($terms))); + return make_link("post/list/$q/$page"); + } else { + return make_link("post/list/$page"); + } +} + /** * Figure out the correct way to link to a page, taking into account * things like the nice URLs setting. diff --git a/ext/approval/main.php b/ext/approval/main.php index 1f62ed5f6..8c4080e54 100644 --- a/ext/approval/main.php +++ b/ext/approval/main.php @@ -134,7 +134,7 @@ public function onUserBlockBuilding(UserBlockBuildingEvent $event) { global $user; if ($user->can(Permissions::APPROVE_IMAGE)) { - $event->add_link("Pending Approval", make_link("/post/list/approved%3Ano/1"), 60); + $event->add_link("Pending Approval", search_link(["approved:no"]), 60); } } diff --git a/ext/artists/theme.php b/ext/artists/theme.php index be5016364..ed36eb3f9 100644 --- a/ext/artists/theme.php +++ b/ext/artists/theme.php @@ -353,7 +353,7 @@ public function show_artist($artist, $aliases, $members, $urls, $images, $userIs { global $page; - $artist_link = "".str_replace("_", " ", $artist['name']).""; + $artist_link = "".str_replace("_", " ", $artist['name']).""; $html = " diff --git a/ext/browser_search/main.php b/ext/browser_search/main.php index eb71905a0..3487d78bc 100644 --- a/ext/browser_search/main.php +++ b/ext/browser_search/main.php @@ -26,7 +26,7 @@ public function onPageRequest(PageRequestEvent $event) if ($event->page_matches("browser_search.xml")) { // First, we need to build all the variables we'll need $search_title = $config->get_string(SetupConfig::TITLE); - $search_form_url = make_link('post/list/{searchTerms}'); + $search_form_url = search_link(['{searchTerms}']); $suggenton_url = make_link('browser_search/')."{searchTerms}"; $icon_b64 = base64_encode(file_get_contents("ext/static_files/static/favicon.ico")); diff --git a/ext/favorites/main.php b/ext/favorites/main.php index eb94d62cf..fb0c0f462 100644 --- a/ext/favorites/main.php +++ b/ext/favorites/main.php @@ -75,7 +75,7 @@ public function onUserPageBuilding(UserPageBuildingEvent $event) $i_favorites_count = Image::count_images(["favorited_by={$event->display_user->name}"]); $i_days_old = ((time() - strtotime($event->display_user->join_date)) / 86400) + 1; $h_favorites_rate = sprintf("%.1f", ($i_favorites_count / $i_days_old)); - $favorites_link = make_link("post/list/favorited_by={$event->display_user->name}/1"); + $favorites_link = search_link(["favorited_by={$event->display_user->name}"]); $event->add_stats("Posts favorited: $i_favorites_count, $h_favorites_rate per day"); } @@ -115,7 +115,7 @@ public function onUserBlockBuilding(UserBlockBuildingEvent $event) global $user; $username = url_escape($user->name); - $event->add_link("My Favorites", make_link("post/list/favorited_by=$username/1"), 20); + $event->add_link("My Favorites", search_link(["favorited_by=$username"]), 20); } public function onSearchTermParse(SearchTermParseEvent $event) diff --git a/ext/home/theme.php b/ext/home/theme.php index a0b37efe5..b72218cb9 100644 --- a/ext/home/theme.php +++ b/ext/home/theme.php @@ -37,7 +37,7 @@ public function build_body(string $sitename, string $main_links, string $main_te $contact_link = empty($contact_link) ? "" : "
Contact –"; $search_html = "