diff options
Diffstat (limited to 'web/lib/aurjson.class.php')
-rw-r--r-- | web/lib/aurjson.class.php | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 6c7725c..c1b079a 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -18,11 +18,14 @@ class AurJSON { 'search', 'info', 'multiinfo', 'msearch' ); private static $fields = array( - 'Packages.ID', 'Name', 'Version', 'CategoryID', - 'Description', 'URL', 'License', - 'NumVotes', '(OutOfDateTS IS NOT NULL) AS OutOfDate', + 'Packages.ID', 'Name', 'Version', 'CategoryID', 'Description', 'URL', + 'License', 'NumVotes', 'OutOfDateTS AS OutOfDate', 'SubmittedTS AS FirstSubmitted', 'ModifiedTS AS LastModified' ); + private static $numeric_fields = array( + 'ID', 'CategoryID', 'NumVotes', 'OutOfDate', 'FirstSubmitted', + 'LastModified' + ); /** * Handles post data, and routes the request. @@ -100,7 +103,7 @@ class AurJSON { private function json_error($msg) { // set content type header to app/json header('content-type: application/json'); - return $this->json_results('error', $msg); + return $this->json_results('error', 0, $msg); } /** @@ -109,8 +112,8 @@ class AurJSON { * @param $data The result data to return * @return mixed A json formatted result response. **/ - private function json_results($type, $data) { - return json_encode( array('type' => $type, 'results' => $data) ); + private function json_results($type, $count, $data) { + return json_encode( array('type' => $type, 'resultcount' => $count, 'results' => $data) ); } private function process_query($type, $where_condition) { @@ -121,12 +124,21 @@ class AurJSON { "WHERE ${where_condition}"; $result = db_query($query, $this->dbh); - if ( $result && (mysql_num_rows($result) > 0) ) { + $resultcount = mysql_num_rows($result); + if ( $result && $resultcount > 0 ) { $search_data = array(); while ( $row = mysql_fetch_assoc($result) ) { $name = $row['Name']; $row['URLPath'] = URL_DIR . substr($name, 0, 2) . "/" . $name . "/" . $name . ".tar.gz"; + /* Unfortunately, mysql_fetch_assoc() returns all fields as + * strings. We need to coerce numeric values into integers to + * provide proper data types in the JSON response. + */ + foreach (self::$numeric_fields as $field) { + $row[$field] = intval($row[$field]); + } + if ($type == 'info') { $search_data = $row; break; @@ -137,10 +149,10 @@ class AurJSON { } mysql_free_result($result); - return $this->json_results($type, $search_data); + return $this->json_results($type, $resultcount, $search_data); } else { - return $this->json_error('No results found'); + return $this->json_results($type, 0, array()); } } |