tighten up browser search

This commit is contained in:
Shish 2020-02-04 01:45:45 +00:00
parent 17c43ec7cc
commit 015a597027
2 changed files with 18 additions and 25 deletions

View file

@ -15,11 +15,11 @@ class BrowserSearch extends Extension
// Add in header code to let the browser know that the search plugin exists
// We need to build the data for the header
$search_title = $config->get_string(SetupConfig::TITLE);
$search_file_url = make_link('browser_search/please_dont_use_this_tag_as_it_would_break_stuff__search.xml');
$search_file_url = make_link('browser_search.xml');
$page->add_html_header("<link rel='search' type='application/opensearchdescription+xml' title='$search_title' href='$search_file_url'>");
// The search.xml file that is generated on the fly
if ($event->page_matches("browser_search/please_dont_use_this_tag_as_it_would_break_stuff__search.xml")) {
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}');
@ -44,36 +44,30 @@ class BrowserSearch extends Extension
$page->set_mode(PageMode::DATA);
$page->set_type("text/xml");
$page->set_data($xml);
} elseif (
$event->page_matches("browser_search") &&
!$config->get_bool("disable_search_suggestions")
) {
} elseif ($event->page_matches("browser_search")) {
$suggestions = $config->get_string("search_suggestions_results_order");
if ($suggestions == "n") {
return;
}
// We have to build some json stuff
$tag_search = $event->get_arg(0);
// Now to get DB results
if ($config->get_string("search_suggestions_results_order") == "a") {
$tags = $database->execute("SELECT tag FROM tags WHERE tag LIKE :tag AND count > 0 ORDER BY tag ASC LIMIT 30", ['tag'=>$tag_search."%"]);
if ($suggestions == "a") {
$order = "tag ASC";
} else {
$tags = $database->execute("SELECT tag FROM tags WHERE tag LIKE :tag AND count > 0 ORDER BY count DESC LIMIT 30", ['tag'=>$tag_search."%"]);
$order = "count DESC";
}
$tags = $database->get_col(
"SELECT tag FROM tags WHERE tag LIKE :tag AND count > 0 ORDER BY $order LIMIT 30",
['tag'=>$tag_search."%"]
);
// And to do stuff with it. We want our output to look like:
// ["shimmie",["shimmies","shimmy","shimmie","21 shimmies","hip shimmies","skea shimmies"],[],[]]
$json_tag_list = "";
$tags_array = [];
foreach ($tags as $tag) {
array_push($tags_array, $tag['tag']);
}
$json_tag_list .= implode("\",\"", $tags_array);
// And now for the final output
$json_string = "[\"$tag_search\",[\"$json_tag_list\"],[],[]]";
$page->set_mode(PageMode::DATA);
$page->set_data($json_string);
$page->set_data(json_encode([$tag_search, $tags, [], []]));
}
}
@ -82,10 +76,9 @@ class BrowserSearch extends Extension
$sort_by = [];
$sort_by['Alphabetical'] = 'a';
$sort_by['Tag Count'] = 't';
$sort_by['Disabled'] = 'n';
$sb = new SetupBlock("Browser Search");
$sb->add_bool_option("disable_search_suggestions", "Disable search suggestions: ");
$sb->add_label("<br>");
$sb->add_choice_option("search_suggestions_results_order", $sort_by, "Sort the suggestions by:");
$event->panel->add_block($sb);
}

View file

@ -3,7 +3,7 @@ class BrowserSearchTest extends ShimmiePHPUnitTestCase
{
public function testBasic()
{
$page = $this->get_page("browser_search/please_dont_use_this_tag_as_it_would_break_stuff__search.xml");
$page = $this->get_page("browser_search.xml");
$this->assertEquals(200, $page->code);
$page = $this->get_page("browser_search/test");