[aur-dev] [PATCH 1/3] JSON-RPC: include maintainer info in results.
This patch implements the feature requested in FS#17597. The name of the maintainer is now included in the results of the search and info queries. --- web/lib/aurjson.class.php | 8 ++++++-- 1 files changed, 6 insertions(+), 2 deletions(-) diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index a5b3f9e..369f6ae 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -96,7 +96,9 @@ class AurJSON { $keyword_string = mysql_real_escape_string($keyword_string, $this->dbh); $query = "SELECT " . implode(',', $this->fields) . - " FROM Packages WHERE DummyPkg=0 AND " . + ", Users.Username AS Maintainer" . + " FROM Packages LEFT JOIN Users ON MaintainerUID = Users.ID" . + " WHERE DummyPkg=0 AND " . " ( Name LIKE '%{$keyword_string}%' OR " . " Description LIKE '%{$keyword_string}%' )"; $result = db_query($query, $this->dbh); @@ -122,7 +124,9 @@ class AurJSON { **/ private function info($pqdata) { $base_query = "SELECT " . implode(',', $this->fields) . - " FROM Packages WHERE DummyPkg=0 AND "; + ", Users.Username AS Maintainer" . + " FROM Packages LEFT JOIN Users on Packages.MaintainerUID = Users.ID" . + " WHERE DummyPkg=0 AND "; if ( is_numeric($pqdata) ) { // just using sprintf to coerce the pqd to an int -- 1.7.3.2
The SELECT statements in aurjson.class.php share much in common. This patch moves the common bit to a constant. --- web/lib/aurjson.class.php | 35 +++++++++++++++++++---------------- 1 files changed, 19 insertions(+), 16 deletions(-) diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 369f6ae..c5bb728 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -10,6 +10,21 @@ if (!extension_loaded('json')) { include_once("aur.inc"); +/* Comma-delimited string of fields used in our queries: */ +define('JSONRPC_FIELDS', implode(',', array('Packages.ID','Name','Version', + 'CategoryID', 'Description', 'LocationID', 'URL','URLPath','License', + 'NumVotes', 'OutOfDate', 'Users.Username as Maintainer'))); + +// Base SELECT statement used in several queries. +// Note: LEFT JOIN gives us both maintained and orphaned packages. +define('JSONRPC_BASE_QUERY', 'SELECT ' . JSONRPC_FIELDS . + ' FROM Packages LEFT JOIN Users ON Packages.MaintainerUID = Users.ID '); + +// Base query for msearch. This uses a natural join, since we aren't +// interested in orphans. +define('JSONRPC_MSEARCH_QUERY', str_replace('LEFT JOIN', 'NATURAL JOIN', + JSONRPC_BASE_QUERY)); + /** * This class defines a remote interface for fetching data * from the AUR using JSON formatted elements. @@ -19,9 +34,6 @@ include_once("aur.inc"); class AurJSON { private $dbh = false; private $exposed_methods = array('search','info','msearch'); - private $fields = array('Packages.ID','Name','Version','CategoryID', - 'Description', 'LocationID', 'URL','URLPath','License','NumVotes', - 'OutOfDate'); /** * Handles post data, and routes the request. @@ -95,10 +107,7 @@ class AurJSON { $keyword_string = mysql_real_escape_string($keyword_string, $this->dbh); - $query = "SELECT " . implode(',', $this->fields) . - ", Users.Username AS Maintainer" . - " FROM Packages LEFT JOIN Users ON MaintainerUID = Users.ID" . - " WHERE DummyPkg=0 AND " . + $query = JSONRPC_BASE_QUERY . " WHERE DummyPkg=0 AND " . " ( Name LIKE '%{$keyword_string}%' OR " . " Description LIKE '%{$keyword_string}%' )"; $result = db_query($query, $this->dbh); @@ -123,10 +132,7 @@ class AurJSON { * @return mixed Returns an array of value data containing the package data **/ private function info($pqdata) { - $base_query = "SELECT " . implode(',', $this->fields) . - ", Users.Username AS Maintainer" . - " FROM Packages LEFT JOIN Users on Packages.MaintainerUID = Users.ID" . - " WHERE DummyPkg=0 AND "; + $base_query = JSONRPC_BASE_QUERY . " WHERE DummyPkg=0 AND "; if ( is_numeric($pqdata) ) { // just using sprintf to coerce the pqd to an int @@ -170,12 +176,9 @@ class AurJSON { **/ private function msearch($maintainer) { $maintainer = mysql_real_escape_string($maintainer, $this->dbh); - $fields = implode(',', $this->fields); - $query = "SELECT Users.Username as Maintainer, {$fields} " . - " FROM Packages, Users " . - " WHERE Packages.MaintainerUID = Users.ID AND " . - " Users.Username = '{$maintainer}'"; + $query = JSONRPC_MSEARCH_QUERY . + " WHERE Users.Username = '{$maintainer}'"; $result = db_query($query, $this->dbh); if ( $result && (mysql_num_rows($result) > 0) ) { -- 1.7.3.2
The SELECT statements in aurjson.class.php share much in common.
Forget 2/3 for now. I was a little too hasty in sending it. Both 1/3 and 3/3 should be good. -- Chris
When processing a query of type=info, the code in aurjson.class.php was not gracefully coping with attributes which have a value of null or the empty string. For example, if the URL field of an object was "", the JSON-encoded result would contain: {"URLPath":"[PKGBUILD error: non-UTF8 character]"} This patch insures that NULL and "" are handled properly. --- web/lib/aurjson.class.php | 14 ++++++++------ 1 files changed, 8 insertions(+), 6 deletions(-) diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index c5bb728..193122d 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -154,12 +154,14 @@ class AurJSON { $row = mysql_fetch_assoc($result); mysql_free_result($result); foreach($row as $name => $value) { - $converted = utf8_encode($value); - if ($converted != "") { - $row[$name] = $converted; - } - else { - $row[$name] = "[PKGBUILD error: non-UTF8 character]"; + if (($value != NULL) && ($value != "")) { + $converted = utf8_encode($value); + if ($converted != "") { + $row[$name] = $converted; + } + else { + $row[$name] = "[PKGBUILD error: non-UTF8 character]"; + } } } return $this->json_results('info', $row); -- 1.7.3.2
participants (2)
-
Chris Brannon
-
Christopher Brannon