Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dedicated search_link() function #955

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/imageboard/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions core/tests/urls.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions core/urls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion ext/approval/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/artists/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public function show_artist($artist, $aliases, $members, $urls, $images, $userIs
{
global $page;

$artist_link = "<a href='".make_link("post/list/".$artist['name']."/1")."'>".str_replace("_", " ", $artist['name'])."</a>";
$artist_link = "<a href='".search_link([$artist['name']])."'>".str_replace("_", " ", $artist['name'])."</a>";

$html = "<table id='poolsList' class='zebra'>
<thead>
Expand Down
2 changes: 1 addition & 1 deletion ext/browser_search/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
4 changes: 2 additions & 2 deletions ext/favorites/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("<a href='$favorites_link'>Posts favorited</a>: $i_favorites_count, $h_favorites_rate per day");
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ext/home/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function build_body(string $sitename, string $main_links, string $main_te
$contact_link = empty($contact_link) ? "" : "<br><a href='$contact_link'>Contact</a> &ndash;";
$search_html = "
<div class='space' id='search'>
<form action='".make_link("post/list")."' method='GET'>
<form action='".search_link()."' method='GET'>
<input name='search' size='30' type='search' value='' class='autocomplete_tags' autofocus='autofocus' autocomplete='off' />
<input type='hidden' name='q' value='/post/list'>
<input type='submit' value='Search'/>
Expand Down
2 changes: 1 addition & 1 deletion ext/image/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function onUserPageBuilding(UserPageBuildingEvent $event)
$i_image_count = Image::count_images(["user={$event->display_user->name}"]);
$i_days_old = ((time() - strtotime($event->display_user->join_date)) / 86400) + 1;
$h_image_rate = sprintf("%.1f", ($i_image_count / $i_days_old));
$images_link = make_link("post/list/user=$u_name/1");
$images_link = search_link(["user=$u_name"]);
$event->add_stats("<a href='$images_link'>Posts uploaded</a>: $i_image_count, $h_image_rate per day");
}

Expand Down
11 changes: 2 additions & 9 deletions ext/index/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ public function onPageRequest(PageRequestEvent $event)
global $cache, $page, $user;
if ($event->page_matches("post/list")) {
if (isset($_GET['search'])) {
// implode(explode()) to resolve aliases and sanitise
$search = url_escape(Tag::caret(Tag::implode(Tag::explode($_GET['search'], false))));
if (empty($search)) {
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/list/1"));
} else {
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link('post/list/'.$search.'/1'));
}
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(search_link(Tag::explode($_GET['search'], false)));
return;
}

Expand Down
8 changes: 3 additions & 5 deletions ext/index/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,13 @@ protected function build_navigation(int $page_number, int $total_pages, array $s
$next = $page_number + 1;

$u_tags = url_escape(Tag::implode($search_terms));
$query = empty($u_tags) ? "" : '/'.$u_tags;


$h_prev = ($page_number <= 1) ? "Prev" : '<a href="'.make_link('post/list'.$query.'/'.$prev).'">Prev</a>';
$h_prev = ($page_number <= 1) ? "Prev" : '<a href="'.search_link($search_terms, $prev).'">Prev</a>';
$h_index = "<a href='".make_link()."'>Index</a>";
$h_next = ($page_number >= $total_pages) ? "Next" : '<a href="'.make_link('post/list'.$query.'/'.$next).'">Next</a>';
$h_next = ($page_number >= $total_pages) ? "Next" : '<a href="'.search_link($search_terms, $next).'">Next</a>';

$h_search_string = html_escape(Tag::implode($search_terms));
$h_search_link = make_link("post/list");
$h_search_link = search_link();
$h_search = "
<p><form action='$h_search_link' method='GET'>
<input type='search' name='search' value='$h_search_string' placeholder='Search' class='autocomplete_tags' autocomplete='off' />
Expand Down
2 changes: 1 addition & 1 deletion ext/notes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function nuke_requests_button(int $image_id): string

public function search_notes_page(Page $page): void
{ //IN DEVELOPMENT, NOT FULLY WORKING
$html = '<form method="GET" action="'.make_link("post/list/note=").'">
$html = '<form method="GET" action="'.search_link(["note="]).'">
<input placeholder="Search Notes" type="text" name="search"/>
<input type="submit" style="display: none;" value="Find"/>
</form>';
Expand Down
5 changes: 2 additions & 3 deletions ext/numeric_score/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,10 @@ public function onUserPageBuilding(UserPageBuildingEvent $event)
$this->theme->get_nuller($event->display_user);
}

$u_name = url_escape($event->display_user->name);
$n_up = Image::count_images(["upvoted_by={$event->display_user->name}"]);
$link_up = make_link("post/list/upvoted_by=$u_name/1");
$link_up = search_link(["upvoted_by={$event->display_user->name}"]);
$n_down = Image::count_images(["downvoted_by={$event->display_user->name}"]);
$link_down = make_link("post/list/downvoted_by=$u_name/1");
$link_down = search_link(["downvoted_by={$event->display_user->name}]"]);
$event->add_stats("<a href='$link_up'>$n_up Upvotes</a> / <a href='$link_down'>$n_down Downvotes</a>");
}

Expand Down
3 changes: 1 addition & 2 deletions ext/rating/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public function get_selection_rater_html(string $name = "rating", array $ratings

public function get_rater_html(int $image_id, string $rating, bool $can_rate): HTMLElement
{
$u_rating = url_escape(Tag::caret($rating));
return SHM_POST_INFO(
"Rating",
$can_rate,
A(["href"=>make_link("post/list/rating=$u_rating/1")], Ratings::rating_to_human($rating)),
A(["href"=>search_link(["rating=$rating"])], Ratings::rating_to_human($rating)),
$this->get_selection_rater_html("rating", selected_options: [$rating])
);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/relationships/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function relationship_info(Image $image)
if (bool_escape($image->has_children)) {
$ids = $database->get_col("SELECT id FROM images WHERE parent_id = :iid", ["iid"=>$image->id]);

$html = "This post has <a href='".make_link('post/list/parent='.$image->id.'/1')."'>".(count($ids) > 1 ? "child posts" : "a child post")."</a>";
$html = "This post has <a href='".search_link(['parent='.$image->id])."'>".(count($ids) > 1 ? "child posts" : "a child post")."</a>";
$html .= " (post ";
foreach ($ids as $id) {
$html .= "#<a href='".make_link('post/view/'.$id)."'>{$id}</a>, ";
Expand Down
2 changes: 1 addition & 1 deletion ext/tag_edit/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function onPageRequest(PageRequestEvent $event)
if ($user->can(Permissions::MASS_TAG_EDIT) && isset($_POST['tags']) && isset($_POST['source'])) {
$this->mass_source_edit($_POST['tags'], $_POST['source']);
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/list"));
$page->set_redirect(search_link());
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions ext/tag_edit/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public function get_tag_editor_html(Image $image): HTMLElement

$tag_links = [];
foreach ($image->get_tag_array() as $tag) {
$u_tag = url_escape($tag);
$tag_links[] = A([
"href" => make_link("post/list/$u_tag/1"),
"href" => search_link([$tag]),
"class" => "tag",
"title" => "View all posts tagged $tag"
], $tag);
Expand Down
2 changes: 1 addition & 1 deletion ext/tag_history/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected function history_entry(array $fields, bool $selected): string
$current_tags = Tag::explode($current_tags);
$taglinks = SPAN();
foreach ($current_tags as $tag) {
$taglinks->appendChild(A(["href"=>make_link("post/list/$tag/1")], $tag));
$taglinks->appendChild(A(["href"=>search_link([$tag])], $tag));
$taglinks->appendChild(" ");
}

Expand Down
3 changes: 1 addition & 2 deletions ext/tag_list/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ protected function get_subtract_link(array $tags, string $tag): string

public function tag_link(string $tag): string
{
$u_tag = url_escape(Tag::caret($tag));
return make_link("post/list/$u_tag/1");
return search_link([$tag]);
}
}
2 changes: 1 addition & 1 deletion ext/trash/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function onUserBlockBuilding(UserBlockBuildingEvent $event)
{
global $user;
if ($user->can(Permissions::VIEW_TRASH)) {
$event->add_link("Trash", make_link("/post/list/in%3Atrash/1"), 60);
$event->add_link("Trash", search_link(["in:trash"]), 60);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/upload/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testRawUpload()
];
$page = $this->post_page("upload", ["tags0"=>"foo bar"]);
$this->assert_response(302);
$this->assertStringStartsWith("/test/post/list/poster=test/1", $page->redirect);
$this->assertStringStartsWith("/test/post/list/poster%3Dtest/1", $page->redirect);

$this->assertEquals(4, $database->get_one("SELECT COUNT(*) FROM images"));
}
Expand Down
2 changes: 1 addition & 1 deletion ext/upload/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function display_upload_status(Page $page, array $image_ids): void
$page->set_redirect(make_link("post/view/{$image_ids[0]}"));
} else {
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/list/poster={$user->name}/1"));
$page->set_redirect(search_link(["poster={$user->name}"]));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ext/user/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct()

public function display(array $row): HTMLElement
{
return A(["href"=>make_link("post/list/user={$row['name']}/1")], "Posts");
return A(["href"=>search_link(["user={$row['name']}"])], "Posts");
}
}

Expand Down
3 changes: 1 addition & 2 deletions themes/danbooru/comment.theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public function display_comment_list(array $images, int $page_number, int $total
$un = $image->get_owner()->name;
$t = "";
foreach ($image->get_tag_array() as $tag) {
$u_tag = url_escape($tag);
$t .= "<a href='".make_link("post/list/$u_tag/1")."'>".html_escape($tag)."</a> ";
$t .= "<a href='".search_link([$tag])."'>".html_escape($tag)."</a> ";
}
$p = autodate($image->posted);

Expand Down
3 changes: 1 addition & 2 deletions themes/danbooru2/comment.theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public function display_comment_list(array $images, int $page_number, int $total
$un = $image->get_owner()->name;
$t = "";
foreach ($image->get_tag_array() as $tag) {
$u_tag = url_escape($tag);
$t .= "<a href='".make_link("post/list/$u_tag/1")."'>".html_escape($tag)."</a> ";
$t .= "<a href='".search_link([$tag])."'>".html_escape($tag)."</a> ";
}
$p = autodate($image->posted);

Expand Down