summaryrefslogtreecommitdiffstats
path: root/web/lib/pkgfuncs.inc.php
diff options
context:
space:
mode:
authorKevin Morris <kevr.gtalk@gmail.com>2020-07-06 03:19:06 +0200
committerLukas Fleischer <lfleischer@archlinux.org>2021-02-20 17:24:30 +0100
commitefe99dc16f2a94be1d4e5917a07fee2260f3d547 (patch)
treeb4dd250673b392b2b44bb6e56770c60793ef7a40 /web/lib/pkgfuncs.inc.php
parent239988def7479cba6407901ee4671b6794d0b2ef (diff)
downloadaur-efe99dc16f2a94be1d4e5917a07fee2260f3d547.tar.gz
aur-efe99dc16f2a94be1d4e5917a07fee2260f3d547.tar.xz
Support conjunctive keyword search in RPC interface
Newly supported API Version 6 modifies `type=search` for _by_ type `name-desc`: it now behaves the same as `name-desc` search through the https://aur.archlinux.org/packages/ search page. Search for packages containing the literal keyword `blah blah` AND `haha`: https://aur.archlinux.org/rpc/?v=6&type=search&arg="blah blah"%20haha Search for packages containing the literal keyword `abc 123`: https://aur.archlinux.org/rpc/?v=6&type=search&arg="abc 123" The following example searches for packages that contain `blah` AND `abc`: https://aur.archlinux.org/rpc/?v=6&type=search&arg=blah%20abc The legacy method still searches for packages that contain `blah abc`: https://aur.archlinux.org/rpc/?v=5&type=search&arg=blah%20abc https://aur.archlinux.org/rpc/?v=5&type=search&arg=blah%20abc API Version 6 is currently only considered during a `search` of `name-desc`. Note: This change was written as a solution to https://bugs.archlinux.org/task/49133. PS: + Some spacing issues fixed in comments. Signed-off-by: Kevin Morris <kevr.gtalk@gmail.com> Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
Diffstat (limited to 'web/lib/pkgfuncs.inc.php')
-rw-r--r--web/lib/pkgfuncs.inc.php34
1 files changed, 20 insertions, 14 deletions
diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php
index 80758005..ac5c8cfe 100644
--- a/web/lib/pkgfuncs.inc.php
+++ b/web/lib/pkgfuncs.inc.php
@@ -696,8 +696,10 @@ function pkg_search_page($params, $show_headers=true, $SID="") {
$q_where .= "AND (PackageBases.Name LIKE " . $dbh->quote($K) . ") ";
}
elseif (isset($params["SeB"]) && $params["SeB"] == "k") {
- /* Search by keywords. */
- $q_where .= construct_keyword_search($dbh, $params['K'], false);
+ /* Search by name. */
+ $q_where .= "AND (";
+ $q_where .= construct_keyword_search($dbh, $params['K'], false, true);
+ $q_where .= ") ";
}
elseif (isset($params["SeB"]) && $params["SeB"] == "N") {
/* Search by name (exact match). */
@@ -709,7 +711,9 @@ function pkg_search_page($params, $show_headers=true, $SID="") {
}
else {
/* Keyword search (default). */
- $q_where .= construct_keyword_search($dbh, $params['K'], true);
+ $q_where .= "AND (";
+ $q_where .= construct_keyword_search($dbh, $params['K'], true, true);
+ $q_where .= ") ";
}
}
@@ -832,10 +836,11 @@ function pkg_search_page($params, $show_headers=true, $SID="") {
* @param handle $dbh Database handle
* @param string $keywords The search term
* @param bool $namedesc Search name and description fields
+ * @param bool $keyword Search packages with a matching PackageBases.Keyword
*
* @return string WHERE part of the SQL clause
*/
-function construct_keyword_search($dbh, $keywords, $namedesc) {
+function construct_keyword_search($dbh, $keywords, $namedesc, $keyword=false) {
$count = 0;
$where_part = "";
$q_keywords = "";
@@ -860,13 +865,18 @@ function construct_keyword_search($dbh, $keywords, $namedesc) {
$term = "%" . addcslashes($term, '%_') . "%";
$q_keywords .= $op . " (";
+ $q_keywords .= "Packages.Name LIKE " . $dbh->quote($term) . " ";
if ($namedesc) {
- $q_keywords .= "Packages.Name LIKE " . $dbh->quote($term) . " OR ";
- $q_keywords .= "Description LIKE " . $dbh->quote($term) . " OR ";
+ $q_keywords .= "OR Description LIKE " . $dbh->quote($term) . " ";
+ }
+
+ if ($keyword) {
+ $q_keywords .= "OR EXISTS (SELECT * FROM PackageKeywords WHERE ";
+ $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
+ $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
+ } else {
+ $q_keywords .= ") ";
}
- $q_keywords .= "EXISTS (SELECT * FROM PackageKeywords WHERE ";
- $q_keywords .= "PackageKeywords.PackageBaseID = Packages.PackageBaseID AND ";
- $q_keywords .= "PackageKeywords.Keyword LIKE " . $dbh->quote($term) . ")) ";
$count++;
if ($count >= 20) {
@@ -875,11 +885,7 @@ function construct_keyword_search($dbh, $keywords, $namedesc) {
$op = "AND ";
}
- if (!empty($q_keywords)) {
- $where_part = "AND (" . $q_keywords . ") ";
- }
-
- return $where_part;
+ return $q_keywords;
}
/**