Make JSON search return more information.

Signed-off-by: Loui Chang <louipc.ist@gmail.com>
This commit is contained in:
Loui Chang 2008-07-09 00:30:05 -04:00
parent 5528501497
commit 1deb924674

View file

@ -22,6 +22,8 @@ if (!extension_loaded('json'))
class AurJSON { class AurJSON {
private $dbh = false; private $dbh = false;
private $exposed_methods = array('search','info'); private $exposed_methods = array('search','info');
private $fields = array('ID','Name','Version','Description',
'URL','URLPath','License','NumVotes','OutOfDate');
/** /**
* Handles post data, and routes the request. * Handles post data, and routes the request.
@ -42,7 +44,9 @@ class AurJSON {
// do the routing // do the routing
if ( in_array($http_data['type'], $this->exposed_methods) ) { if ( in_array($http_data['type'], $this->exposed_methods) ) {
// ugh. this works. I hate you php. // ugh. this works. I hate you php.
$json = call_user_func_array(array(&$this,$http_data['type']),$http_data['arg']); $json = call_user_func_array(array(&$this,$http_data['type']),
$http_data['arg']);
// allow rpc callback for XDomainAjax // allow rpc callback for XDomainAjax
if ( isset($http_data['callback']) ) { if ( isset($http_data['callback']) ) {
return $http_data['callback'] . "({$json})"; return $http_data['callback'] . "({$json})";
@ -87,22 +91,22 @@ class AurJSON {
} }
$keyword_string = mysql_real_escape_string($keyword_string, $this->dbh); $keyword_string = mysql_real_escape_string($keyword_string, $this->dbh);
$query = sprintf(
"SELECT Name,ID FROM Packages WHERE ( Name LIKE '%%%s%%' OR Description LIKE '%%%s%%' ) AND DummyPkg=0", $query = "SELECT " . implode(',', $this->fields) .
$keyword_string, $keyword_string ); " FROM Packages WHERE DummyPkg=0 AND ";
$query .= sprintf("( Name LIKE '%%%s%%' OR Description LIKE '%%%s%%' )",
$keyword_string, $keyword_string);
$result = db_query($query, $this->dbh); $result = db_query($query, $this->dbh);
if ( $result && (mysql_num_rows($result) > 0) ) { if ( $result && (mysql_num_rows($result) > 0) ) {
$search_data = array(); $search_data = array();
while ( $row = mysql_fetch_assoc($result) ) { while ( $row = mysql_fetch_assoc($result) ) {
$elem = array( array_push($search_data, $row);
'Name' => $row['Name'],
'ID' => $row['ID'] );
array_push($search_data,$elem);
} }
mysql_free_result($result); mysql_free_result($result);
return $this->json_results('search',$search_data); return $this->json_results('search', $search_data);
} }
else { else {
return $this->json_error('No results found'); return $this->json_error('No results found');
@ -115,7 +119,8 @@ class AurJSON {
* @return mixed Returns an array of value data containing the package data * @return mixed Returns an array of value data containing the package data
**/ **/
private function info($pqdata) { private function info($pqdata) {
$base_query = "SELECT ID,Name,Version,Description,URL,URLPath,License,NumVotes,OutOfDate FROM Packages WHERE DummyPkg=0 AND "; $base_query = "SELECT " . implode(',', $this->fields) .
" FROM Packages WHERE DummyPkg=0 AND ";
if ( is_numeric($pqdata) ) { if ( is_numeric($pqdata) ) {
// just using sprintf to coerce the pqd to an int // just using sprintf to coerce the pqd to an int
@ -127,7 +132,8 @@ class AurJSON {
if(get_magic_quotes_gpc()) { if(get_magic_quotes_gpc()) {
$pqdata = stripslashes($pqdata); $pqdata = stripslashes($pqdata);
} }
$query_stub = sprintf("Name=\"%s\"",mysql_real_escape_string($pqdata)); $query_stub = sprintf("Name=\"%s\"",
mysql_real_escape_string($pqdata));
} }
$result = db_query($base_query.$query_stub, $this->dbh); $result = db_query($base_query.$query_stub, $this->dbh);
@ -135,11 +141,11 @@ class AurJSON {
if ( $result && (mysql_num_rows($result) > 0) ) { if ( $result && (mysql_num_rows($result) > 0) ) {
$row = mysql_fetch_assoc($result); $row = mysql_fetch_assoc($result);
mysql_free_result($result); mysql_free_result($result);
return $this->json_results('info',$row); return $this->json_results('info', $row);
} }
else { else {
return $this->json_error('No result found'); return $this->json_error('No result found');
} }
} }
} }
?>