[v2] RPC: Allow to search packages by "*depends" fields
diff mbox

Message ID 20180131195414.8270-1-baptiste@bitsofnetworks.org
State Accepted, archived
Headers show

Commit Message

Baptiste Jonglez Jan. 31, 2018, 7:54 p.m. UTC
From: Baptiste Jonglez <git@bitsofnetworks.org>

It is now possible to search for packages that depend on a given package,
for instance:

    /rpc/?v=5&type=search&by=depends&arg=ocaml

It is similarly possible to match on "makedepends", "checkdepends" and
"optdepends".

Signed-off-by: Baptiste Jonglez <git@bitsofnetworks.org>
---

v2:
- use a subquery instead of a conditional JOIN, to simplify the code
- avoid hard-coding the new list of allowed fields deep inside the code

 doc/rpc.txt               |  8 +++++++-
 web/lib/aurjson.class.php | 21 +++++++++++++++++++--
 2 files changed, 26 insertions(+), 3 deletions(-)

Comments

Lukas Fleischer Feb. 23, 2018, 5:57 a.m. UTC | #1
On Wed, 31 Jan 2018 at 20:54:14, Baptiste Jonglez wrote:
> From: Baptiste Jonglez <git@bitsofnetworks.org>
> 
> It is now possible to search for packages that depend on a given package,
> for instance:
> 
>     /rpc/?v=5&type=search&by=depends&arg=ocaml
> 
> It is similarly possible to match on "makedepends", "checkdepends" and
> "optdepends".
> 
> Signed-off-by: Baptiste Jonglez <git@bitsofnetworks.org>
> ---
> 
> v2:
> - use a subquery instead of a conditional JOIN, to simplify the code
> - avoid hard-coding the new list of allowed fields deep inside the code
> 
>  doc/rpc.txt               |  8 +++++++-
>  web/lib/aurjson.class.php | 21 +++++++++++++++++++--
>  2 files changed, 26 insertions(+), 3 deletions(-)
> [...]

Looks good now. Merged to pu, thanks!

Patch
diff mbox

diff --git a/doc/rpc.txt b/doc/rpc.txt
index f353ff0..3148ebe 100644
--- a/doc/rpc.txt
+++ b/doc/rpc.txt
@@ -11,6 +11,10 @@  search argument and _field_ is one of the following values:
 * `name` (search by package name only)
 * `name-desc` (search by package name and description)
 * `maintainer` (search by package maintainer)
+* `depends` (search for packages that depend on _keywords_)
+* `makedepends` (search for packages that makedepend on _keywords_)
+* `optdepends` (search for packages that optdepend on _keywords_)
+* `checkdepends` (search for packages that checkdepend on _keywords_)
 
 The _by_ parameter can be skipped and defaults to `name-desc`.
 
@@ -30,7 +34,9 @@  Examples
 `search`::
   `/rpc/?v=5&type=search&arg=foobar`
 `search` by maintainer::
-  `/rpc/?v=5&type=search&search_by=maintainer&arg=john`
+  `/rpc/?v=5&type=search&by=maintainer&arg=john`
+`search` packages that have _boost_ as `makedepends`::
+  `/rpc/?v=5&type=search&by=makedepends&arg=boost`
 `search` with callback::
   `/rpc/?v=5&type=search&arg=foobar&callback=jsonp1192244621103`
 `info`::
diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php
index 9eeaafd..8b760fb 100644
--- a/web/lib/aurjson.class.php
+++ b/web/lib/aurjson.class.php
@@ -17,7 +17,11 @@  class AurJSON {
 		'suggest-pkgbase', 'get-comment-form'
 	);
 	private static $exposed_fields = array(
-		'name', 'name-desc', 'maintainer'
+		'name', 'name-desc', 'maintainer',
+		'depends', 'makedepends', 'checkdepends', 'optdepends'
+	);
+	private static $exposed_depfields = array(
+		'depends', 'makedepends', 'checkdepends', 'optdepends'
 	);
 	private static $fields_v1 = array(
 		'Packages.ID', 'Packages.Name',
@@ -243,7 +247,7 @@  class AurJSON {
 
 	/*
 	 * Retrieve package information (used in info, multiinfo, search and
-	 * msearch requests).
+	 * depends requests).
 	 *
 	 * @param $type The request type.
 	 * @param $where_condition An SQL WHERE-condition to filter packages.
@@ -407,6 +411,19 @@  class AurJSON {
 				$keyword_string = $this->dbh->quote($keyword_string);
 				$where_condition = "Users.Username = $keyword_string ";
 			}
+		} else if (in_array($search_by, self::$exposed_depfields)) {
+			if (empty($keyword_string)) {
+				return $this->json_error('Query arg is empty.');
+			} else {
+				$keyword_string = $this->dbh->quote($keyword_string);
+				$search_by = $this->dbh->quote($search_by);
+				$subquery = "SELECT PackageDepends.DepName FROM PackageDepends ";
+				$subquery .= "LEFT JOIN DependencyTypes ";
+				$subquery .= "ON PackageDepends.DepTypeID = DependencyTypes.ID ";
+				$subquery .= "WHERE PackageDepends.PackageID = Packages.ID ";
+				$subquery .= "AND DependencyTypes.Name = $search_by";
+				$where_condition = "$keyword_string IN ($subquery)";
+			}
 		}
 
 		return $this->process_query('search', $where_condition);