[aur-dev] [PATCH] Expose name-only search through the RPC interface
Fixes FS#37317. Signed-off-by: Johannes Löthberg <johannes@kyriasis.com> --- web/lib/aurjson.class.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 7b77da4..6c90ebd 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -16,6 +16,9 @@ class AurJSON { 'search', 'info', 'multiinfo', 'msearch', 'suggest', 'suggest-pkgbase' ); + private static $exposed_fields = array( + 'name', 'name_and_desc' + ); private static $fields_v1 = array( 'Packages.ID', 'Packages.Name', 'PackageBases.ID AS PackageBaseID', @@ -80,11 +83,14 @@ class AurJSON { if (!in_array($http_data['type'], self::$exposed_methods)) { return $this->json_error('Incorrect request type specified.'); } + if ($http_data['search_by'] !== null && !in_array($http_data['search_by'], self::$exposed_fields)) { + return $this->json_error('Incorrect search_by field specified.'); + } $this->dbh = DB::connect(); $type = str_replace('-', '_', $http_data['type']); - $json = call_user_func(array(&$this, $type), $http_data['arg']); + $json = call_user_func(array(&$this, $type), $http_data['arg'], $http_data['search_by']); $etag = md5($json); header("Etag: \"$etag\""); @@ -319,15 +325,19 @@ class AurJSON { * * @return mixed Returns an array of package matches. */ - private function search($keyword_string) { + private function search($keyword_string, $search_by) { if (strlen($keyword_string) < 2) { return $this->json_error('Query arg too small'); } $keyword_string = $this->dbh->quote("%" . addcslashes($keyword_string, '%_') . "%"); - $where_condition = "(Packages.Name LIKE $keyword_string OR "; - $where_condition .= "Description LIKE $keyword_string)"; + if ($search_by === 'name') { + $where_condition = "(Packages.Name LIKE $keyword_string)"; + } else if ($search_by === 'name_and_desc' || $search_by === null) { + $where_condition = "(Packages.Name LIKE $keyword_string OR "; + $where_condition .= "Description LIKE $keyword_string)"; + } return $this->process_query('search', $where_condition); } -- 2.4.4
This patchset implements FS#37317. Johannes Löthberg (2): aurjson: Pass http_data array to all functions Expose name-only search through the RPC interface web/lib/aurjson.class.php | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) -- 2.4.4
This is a preparatory patch that simplifies adding more arguments to the parse functions Signed-off-by: Johannes Löthberg <johannes@kyriasis.com> --- web/lib/aurjson.class.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 7b77da4..6f95406 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -84,7 +84,7 @@ class AurJSON { $this->dbh = DB::connect(); $type = str_replace('-', '_', $http_data['type']); - $json = call_user_func(array(&$this, $type), $http_data['arg']); + $json = call_user_func(array(&$this, $type), $http_data); $etag = md5($json); header("Etag: \"$etag\""); @@ -291,7 +291,8 @@ class AurJSON { * * @return mixed An array containing 'ids' and 'names'. */ - private function parse_multiinfo_args($args) { + private function parse_multiinfo_args($http_data) { + $args = $http_data['arg']; if (!is_array($args)) { $args = array($args); } @@ -319,7 +320,9 @@ class AurJSON { * * @return mixed Returns an array of package matches. */ - private function search($keyword_string) { + private function search($http_data) { + $keyword_string = $http_data['arg']; + if (strlen($keyword_string) < 2) { return $this->json_error('Query arg too small'); } @@ -339,7 +342,8 @@ class AurJSON { * * @return mixed Returns an array of value data containing the package data */ - private function info($pqdata) { + private function info($http_data) { + $pqdata = $http_data['arg']; if (is_numeric($pqdata)) { $where_condition = "Packages.ID = $pqdata"; } else { @@ -356,7 +360,8 @@ class AurJSON { * * @return mixed Returns an array of results containing the package data */ - private function multiinfo($pqdata) { + private function multiinfo($http_data) { + $pqdata = $http_data['arg']; $args = $this->parse_multiinfo_args($pqdata); $ids = $args['ids']; $names = $args['names']; @@ -392,7 +397,8 @@ class AurJSON { * * @return mixed Returns an array of value data containing the package data */ - private function msearch($maintainer) { + private function msearch($http_data) { + $maintainer = $http_data['arg']; $maintainer = $this->dbh->quote($maintainer); $where_condition = "Users.Username = $maintainer "; @@ -407,7 +413,8 @@ class AurJSON { * * @return string The JSON formatted response data. */ - private function suggest($search) { + private function suggest($http_data) { + $search = $http_data['arg']; $query = "SELECT Packages.Name FROM Packages "; $query.= "LEFT JOIN PackageBases "; $query.= "ON PackageBases.ID = Packages.PackageBaseID "; @@ -433,7 +440,8 @@ class AurJSON { * * @return string The JSON formatted response data. */ - private function suggest_pkgbase($search) { + private function suggest_pkgbase($http_data) { + $search = $http_data['arg']; $query = "SELECT Name FROM PackageBases WHERE Name LIKE "; $query.= $this->dbh->quote(addcslashes($search, '%_') . '%'); $query.= " AND PackageBases.PackagerUID IS NOT NULL "; -- 2.4.4
Fixes FS#37317. Signed-off-by: Johannes Löthberg <johannes@kyriasis.com> --- Changes since v1: * Renamed name_and_desc to name-desc * Use isset() instead of !== null * The previous patch passes the http_data array to all functions instead of having to add another argument just to the search function for the search_by field. web/lib/aurjson.class.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 6f95406..b926d46 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -16,6 +16,9 @@ class AurJSON { 'search', 'info', 'multiinfo', 'msearch', 'suggest', 'suggest-pkgbase' ); + private static $exposed_fields = array( + 'name', 'name-desc' + ); private static $fields_v1 = array( 'Packages.ID', 'Packages.Name', 'PackageBases.ID AS PackageBaseID', @@ -80,6 +83,9 @@ class AurJSON { if (!in_array($http_data['type'], self::$exposed_methods)) { return $this->json_error('Incorrect request type specified.'); } + if (isset($http_data['search_by']) && !in_array($http_data['search_by'], self::$exposed_fields)) { + return $this->json_error('Incorrect search_by field specified.'); + } $this->dbh = DB::connect(); @@ -322,6 +328,11 @@ class AurJSON { */ private function search($http_data) { $keyword_string = $http_data['arg']; + if (isset($http_data['search_by'])) { + $search_by = $http_data['search_by']; + } else { + $search_by = 'name-desc'; + } if (strlen($keyword_string) < 2) { return $this->json_error('Query arg too small'); @@ -329,8 +340,12 @@ class AurJSON { $keyword_string = $this->dbh->quote("%" . addcslashes($keyword_string, '%_') . "%"); - $where_condition = "(Packages.Name LIKE $keyword_string OR "; - $where_condition .= "Description LIKE $keyword_string)"; + if ($search_by === 'name') { + $where_condition = "(Packages.Name LIKE $keyword_string)"; + } else if ($search_by === 'name-desc') { + $where_condition = "(Packages.Name LIKE $keyword_string OR "; + $where_condition .= "Description LIKE $keyword_string)"; + } return $this->process_query('search', $where_condition); } -- 2.4.4
participants (1)
-
Johannes Löthberg