diff --git a/application/controllers/api.php b/application/controllers/api.php index 7ac076368..321440586 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -159,30 +159,36 @@ class API extends CI_Controller { // Execute the query, and retrieve the results $s = $this->logbook_model->api_search_query($query); - $results = $s['results']; - - // Cycle through the results, and translate between MySQL column names - // and more friendly, descriptive names $a = 0; - if($results->num_rows != 0) - { - foreach ($results->result() as $row) { - $record = (array)$row; - $r[$a]['rid'] = $a; - while (list($key, $val) = each($record)) { - $r[$a][$this->api_model->name($key)] = $val; - } - $a++; - } - // Add the result record to the main results array + + if(isset($s['results'])) { + $results = $s['results']; + + // Cycle through the results, and translate between MySQL column names + // and more friendly, descriptive names + if($results->num_rows != 0) + { + foreach ($results->result() as $row) { + $record = (array)$row; + $r[$a]['rid'] = $a; + while (list($key, $val) = each($record)) { + $r[$a][$this->api_model->name($key)] = $val; + } + $a++; + } + // Add the result record to the main results array $data['data']['search_Result']['results'] = $r; - } - else - { - // We've got no results, so make this empty for completeness - $data['data']['search_Result']['results'] = ""; - } - + } + else + { + // We've got no results, so make this empty for completeness + $data['data']['search_Result']['results'] = ""; + } + } else { + $data['data']['error'] = $s['error']; + $data['data']['search_Result']['results'] = ""; + } + // Add some debugging information to the XML output $data['data']['queryInfo']['call'] = "search"; $data['data']['queryInfo']['dbQuery'] = $s['query']; @@ -266,26 +272,26 @@ class API extends CI_Controller { $arguments = array(); // Retrieve each arguments - $query = preg_grep("/^query\[(.*)\]$/", $this->uri->segments); - $limit = preg_grep("/^limit\[(.*)\]$/", $this->uri->segments); - $order = preg_grep("/^order\[(.*)\]$/", $this->uri->segments); - $fields = preg_grep("/^fields\[(.*)\]$/", $this->uri->segments); - $format = preg_grep("/^format\[(.*)\]$/", $this->uri->segments); - $key = preg_grep("/^key\[(.*)\]$/", $this->uri->segments); + $query = preg_grep("/^query=(.*)$/", $this->uri->segments); + $limit = preg_grep("/^limit=(.*)$/", $this->uri->segments); + $order = preg_grep("/^order=(.*)$/", $this->uri->segments); + $fields = preg_grep("/^fields=(.*)$/", $this->uri->segments); + $format = preg_grep("/^format=(.*)$/", $this->uri->segments); + $key = preg_grep("/^key=(.*)$/", $this->uri->segments); // Strip each argument $arguments['query'] = substr(array_pop($query), 6); - $arguments['query'] = substr($arguments['query'], 0, strlen($arguments['query']) - 1); + $arguments['query'] = substr($arguments['query'], 0, strlen($arguments['query'])); $arguments['limit'] = substr(array_pop($limit), 6); - $arguments['limit'] = substr($arguments['limit'], 0, strlen($arguments['limit']) - 1); + $arguments['limit'] = substr($arguments['limit'], 0, strlen($arguments['limit'])); $arguments['order'] = substr(array_pop($order), 6); - $arguments['order'] = substr($arguments['order'], 0, strlen($arguments['order']) - 1); + $arguments['order'] = substr($arguments['order'], 0, strlen($arguments['order'])); $arguments['fields'] = substr(array_pop($fields), 7); - $arguments['fields'] = substr($arguments['fields'], 0, strlen($arguments['fields']) - 1); + $arguments['fields'] = substr($arguments['fields'], 0, strlen($arguments['fields'])); $arguments['format'] = substr(array_pop($format), 7); - $arguments['format'] = substr($arguments['format'], 0, strlen($arguments['format']) - 1); + $arguments['format'] = substr($arguments['format'], 0, strlen($arguments['format'])); $arguments['key'] = substr(array_pop($key), 4); - $arguments['key'] = substr($arguments['key'], 0, strlen($arguments['key']) - 1); + $arguments['key'] = substr($arguments['key'], 0, strlen($arguments['key'])); // By default, assume XML for the format if not otherwise set if($arguments['format'] == "") { diff --git a/application/models/api_model.php b/application/models/api_model.php index 0eba5f84c..352a49ad5 100644 --- a/application/models/api_model.php +++ b/application/models/api_model.php @@ -193,6 +193,8 @@ class API_Model extends CI_Model { $s[3] = '/\(asc\)/'; $s[4] = '/\(desc\)/'; $s[5] = '/,$/'; + $s[6] = '/\[/'; + $s[7] = '/\]/'; $r[0] = '('; $r[1] = ')'; @@ -200,6 +202,8 @@ class API_Model extends CI_Model { $r[3] = ' ASC '; $r[4] = ' DESC '; $r[5] = ''; + $r[6] = ''; + $r[7] = ''; $q .= preg_replace($s, $r, $arguments['order']); @@ -258,8 +262,9 @@ class API_Model extends CI_Model { $r[1] = ' OR '; $r[2] = ' < '; $r[3] = ' > '; - $r[4] = '['; - $r[5] = ']'; + // Strip out square brackets + $r[4] = ''; + $r[5] = ''; $r[6] = '++$1++ ='; $r[7] = '= \'$1\''; $r[8] = 'UNIX_TIMESTAMP(NOW())'; diff --git a/application/models/logbook_model.php b/application/models/logbook_model.php index 6dcb46905..22641ea4e 100644 --- a/application/models/logbook_model.php +++ b/application/models/logbook_model.php @@ -411,7 +411,10 @@ class Logbook_model extends CI_Model { function api_search_query($query) { $time_start = microtime(true); - $results = @$this->db->query($query); + $results = $this->db->query($query); + if(!$results) { + return array('query' => $query, 'error' => $this->db->_error_number(), 'time' => 0); + } $time_end = microtime(true); $time = round($time_end - $time_start, 4); @@ -421,6 +424,9 @@ class Logbook_model extends CI_Model { function api_insert_query($query) { $time_start = microtime(true); $results = $this->db->insert($this->config->item('table_name'), $query); + if(!$results) { + return array('query' => $query, 'error' => $this->db->_error_number(), 'time' => 0); + } $time_end = microtime(true); $time = round($time_end - $time_start, 4); diff --git a/application/views/api/help.php b/application/views/api/help.php index d775b6b9f..07f92e9ad 100644 --- a/application/views/api/help.php +++ b/application/views/api/help.php @@ -64,7 +64,7 @@ ?> - status); ?> - Test + status); ?> - Test @@ -86,10 +86,10 @@ There are a number of API calls you can make from other applications, with outpu

Description

Query the logbook, and output in XML format.

Syntax

-
  • /search/format[xml]/query[<field><=|~><value>{(and|or)...]}/limit[<num>]/fields[<field1>,{<field2>}]/order[<field>]
    +
  • /search/format=<format>/query=<field><=|~><value>{(and|or)...}/limit=<num>/fields=<field1>,{<field2>/order=<field>

    Example

    Search for entries with a call beginning with M0 and a locator beginning with I or J, show the callsign and locator fields, order it by callsign and limit the results to 10. -
  • /search/format[xml]/query[Call~M0*(and)(Locator~I*(or)Locator~J*)]/limit[10]/fields[distinct(Call),Locator]/order[Call(asc)]
    -
  • Run it! (XML) or Run it! (JSON) +
  • /search/format=xml/query=Call~M0*(and)(Locator~I*(or)Locator~J*)/limit=10/fields=distinct(Call),Locator/order=Call(asc)
    +
  • Run it! (XML) or Run it! (JSON) diff --git a/application/views/api/index.php b/application/views/api/index.php index 83c63382f..2042c3e0e 100644 --- a/application/views/api/index.php +++ b/application/views/api/index.php @@ -2,13 +2,16 @@ // Create the DOMDocument for the XML output $xmlDoc = new DOMDocument("1.0"); -// Add reference to the XSLT -$xsl = $xmlDoc->createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"/css/api.xsl\""); -$xmlDoc->appendChild($xsl); + +if($data['format'] == "xml") { + // Add reference to the XSLT + $xsl = $xmlDoc->createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"/css/api.xsl\""); + $xmlDoc->appendChild($xsl); +} // Get the method called, and build the root node $call = $data['queryInfo']['call']; -$rootNode = $xmlDoc->createElement("HRDWebLogbook-API"); +$rootNode = $xmlDoc->createElement("Cloudlog-API"); $parentNode = $xmlDoc->appendChild($rootNode); // Get the results output @@ -33,14 +36,14 @@ $queryElement->setAttribute("executionTime", $data['queryInfo']['executionTime'] $queryElement->setAttribute("logbookURL", $this->config->item('base_url')); // Add the main results node -$node = $xmlDoc->createElement("elements"); +$node = $xmlDoc->createElement("results"); $elementsNode = $parentNode->appendChild($node); // Cycle through the results and add to the results node if($output['results']) { foreach($output['results'] as $e) { - $node = $xmlDoc->createElement("element"); + $node = $xmlDoc->createElement("result"); $element = $elementsNode->appendChild($node); foreach($e as $attr) { @@ -67,12 +70,22 @@ if($output['results']) } } +if(isset($data['error'])) +{ + $node = $xmlDoc->createElement("error"); + $errorNode = $parentNode->appendChild($node); + + $errorNode->setAttribute("id", $data['error']); +} + // Output // Check whether we want XML or JSON output -if($data['format'] == "xml") { - // Set the content-type for browsers - header("Content-type: text/xml"); +if(($data['format'] == "xml") || ($data['format'] == "xmlp") || ($data['format'] == "xmlt")) { + if(($data['format'] == "xml") || ($data['format'] == "xmlp")) { + // Set the content-type for browsers + header("Content-type: text/xml"); + } echo formatXmlString($xmlDoc->saveXML()); } else if($data['format'] == "json") { // Set the content-type for browsers diff --git a/css/api.xsl b/css/api.xsl index f81aa8b2d..424ecaaf1 100644 --- a/css/api.xsl +++ b/css/api.xsl @@ -11,11 +11,11 @@

    Output of ''

    - + - +