mirror of
https://github.com/sbrl/Pepperminty-Wiki.git
synced 2024-11-25 17:23:00 +00:00
Improve dynamic page suggestions, and add option to output OpenSearch-compatible search suggestions
This commit is contained in:
parent
eb137df2b4
commit
60ebd1340c
3 changed files with 51 additions and 11 deletions
|
@ -3353,12 +3353,13 @@ register_module([
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @api {get} ?action=suggest-pages Get search suggestions for a query
|
* @api {get} ?action=suggest-pages[&type={type}] Get search suggestions for a query
|
||||||
* @apiName OpenSearchDescription
|
* @apiName OpenSearchDescription
|
||||||
* @apiGroup Search
|
* @apiGroup Search
|
||||||
* @apiPermission Anonymous
|
* @apiPermission Anonymous
|
||||||
*
|
*
|
||||||
* @apiParam {string} text The search query string to get search suggestions for.
|
* @apiParam {string} text The search query string to get search suggestions for.
|
||||||
|
* @apiParam {string} type The type of result to return. Default value: json. Available values: json, opensearch
|
||||||
*/
|
*/
|
||||||
add_action("suggest-pages", function() {
|
add_action("suggest-pages", function() {
|
||||||
global $settings, $pageindex;
|
global $settings, $pageindex;
|
||||||
|
@ -3376,17 +3377,24 @@ register_module([
|
||||||
exit("Error: You didn't specify the 'query' GET parameter.");
|
exit("Error: You didn't specify the 'query' GET parameter.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$type = $_GET["type"] ?? "json";
|
||||||
|
|
||||||
|
if(!in_array($type, ["json", "opensearch"])) {
|
||||||
|
http_response_code(406);
|
||||||
|
exit("Error: The type '$type' is not one of the supported output types. Available values: json, opensearch. Default: json");
|
||||||
|
}
|
||||||
|
|
||||||
// Rank each page name
|
// Rank each page name
|
||||||
$results = [];
|
$results = [];
|
||||||
foreach($pageindex as $pageName => $entry) {
|
foreach($pageindex as $pageName => $entry) {
|
||||||
$results[] = [
|
$results[] = [
|
||||||
"pagename" => $pageName,
|
"pagename" => $pageName,
|
||||||
// Costs: Insert: 1, Replace: 8, Delete: 6
|
// Costs: Insert: 1, Replace: 8, Delete: 6
|
||||||
"distance" => levenshtein($_GET["query"], $pageName, 1, 8, 6)
|
"distance" => levenshtein(mb_strtolower($_GET["query"]), mb_strtolower($pageName), 1, 8, 6)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort the page names by distance form the original query
|
// Sort the page names by distance from the original query
|
||||||
usort($results, function($a, $b) {
|
usort($results, function($a, $b) {
|
||||||
if($a["distance"] == $b["distance"])
|
if($a["distance"] == $b["distance"])
|
||||||
return strcmp($a["pagename"], $b["pagename"]);
|
return strcmp($a["pagename"], $b["pagename"]);
|
||||||
|
@ -3394,8 +3402,20 @@ register_module([
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send the results to the user
|
// Send the results to the user
|
||||||
header("content-type: application/json");
|
$suggestions = array_slice($results, 0, $settings->dynamic_page_suggestion_count);
|
||||||
exit(json_encode(array_slice($results, 0, $settings->dynamic_page_suggestion_count)));
|
switch($type)
|
||||||
|
{
|
||||||
|
case "json":
|
||||||
|
header("content-type: application/json");
|
||||||
|
exit(json_encode($suggestions));
|
||||||
|
case "opensearch":
|
||||||
|
$opensearch_output = [
|
||||||
|
$_GET["query"],
|
||||||
|
array_map(function($suggestion) { return $suggestion["pagename"]; }, $suggestions)
|
||||||
|
];
|
||||||
|
header("content-type: application/x-suggestions+json");
|
||||||
|
exit(json_encode($opensearch_output));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if($settings->dynamic_page_suggestion_count > 0)
|
if($settings->dynamic_page_suggestion_count > 0)
|
||||||
|
|
|
@ -104,7 +104,7 @@
|
||||||
"author": "Starbeamrainbowlabs",
|
"author": "Starbeamrainbowlabs",
|
||||||
"description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
|
"description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
|
||||||
"id": "feature-search",
|
"id": "feature-search",
|
||||||
"lastupdate": 1498639329,
|
"lastupdate": 1498643008,
|
||||||
"optional": false
|
"optional": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -279,12 +279,13 @@ register_module([
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @api {get} ?action=suggest-pages Get search suggestions for a query
|
* @api {get} ?action=suggest-pages[&type={type}] Get search suggestions for a query
|
||||||
* @apiName OpenSearchDescription
|
* @apiName OpenSearchDescription
|
||||||
* @apiGroup Search
|
* @apiGroup Search
|
||||||
* @apiPermission Anonymous
|
* @apiPermission Anonymous
|
||||||
*
|
*
|
||||||
* @apiParam {string} text The search query string to get search suggestions for.
|
* @apiParam {string} text The search query string to get search suggestions for.
|
||||||
|
* @apiParam {string} type The type of result to return. Default value: json. Available values: json, opensearch
|
||||||
*/
|
*/
|
||||||
add_action("suggest-pages", function() {
|
add_action("suggest-pages", function() {
|
||||||
global $settings, $pageindex;
|
global $settings, $pageindex;
|
||||||
|
@ -302,17 +303,24 @@ register_module([
|
||||||
exit("Error: You didn't specify the 'query' GET parameter.");
|
exit("Error: You didn't specify the 'query' GET parameter.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$type = $_GET["type"] ?? "json";
|
||||||
|
|
||||||
|
if(!in_array($type, ["json", "opensearch"])) {
|
||||||
|
http_response_code(406);
|
||||||
|
exit("Error: The type '$type' is not one of the supported output types. Available values: json, opensearch. Default: json");
|
||||||
|
}
|
||||||
|
|
||||||
// Rank each page name
|
// Rank each page name
|
||||||
$results = [];
|
$results = [];
|
||||||
foreach($pageindex as $pageName => $entry) {
|
foreach($pageindex as $pageName => $entry) {
|
||||||
$results[] = [
|
$results[] = [
|
||||||
"pagename" => $pageName,
|
"pagename" => $pageName,
|
||||||
// Costs: Insert: 1, Replace: 8, Delete: 6
|
// Costs: Insert: 1, Replace: 8, Delete: 6
|
||||||
"distance" => levenshtein($_GET["query"], $pageName, 1, 8, 6)
|
"distance" => levenshtein(mb_strtolower($_GET["query"]), mb_strtolower($pageName), 1, 8, 6)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort the page names by distance form the original query
|
// Sort the page names by distance from the original query
|
||||||
usort($results, function($a, $b) {
|
usort($results, function($a, $b) {
|
||||||
if($a["distance"] == $b["distance"])
|
if($a["distance"] == $b["distance"])
|
||||||
return strcmp($a["pagename"], $b["pagename"]);
|
return strcmp($a["pagename"], $b["pagename"]);
|
||||||
|
@ -320,8 +328,20 @@ register_module([
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send the results to the user
|
// Send the results to the user
|
||||||
header("content-type: application/json");
|
$suggestions = array_slice($results, 0, $settings->dynamic_page_suggestion_count);
|
||||||
exit(json_encode(array_slice($results, 0, $settings->dynamic_page_suggestion_count)));
|
switch($type)
|
||||||
|
{
|
||||||
|
case "json":
|
||||||
|
header("content-type: application/json");
|
||||||
|
exit(json_encode($suggestions));
|
||||||
|
case "opensearch":
|
||||||
|
$opensearch_output = [
|
||||||
|
$_GET["query"],
|
||||||
|
array_map(function($suggestion) { return $suggestion["pagename"]; }, $suggestions)
|
||||||
|
];
|
||||||
|
header("content-type: application/x-suggestions+json");
|
||||||
|
exit(json_encode($opensearch_output));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if($settings->dynamic_page_suggestion_count > 0)
|
if($settings->dynamic_page_suggestion_count > 0)
|
||||||
|
|
Loading…
Reference in a new issue