From 522d0b3c3707099e112bdb894b210f151f575313 Mon Sep 17 00:00:00 2001 From: preecej Date: Wed, 13 Jul 2011 01:16:49 +0000 Subject: [PATCH] new RESTful web service for the PO site and for the Planteome annotation wiki svn path=/; revision=119 --- .../prototyping/TermSearch_JSON.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 preecej/semantic_wiki/prototyping/TermSearch_JSON.php diff --git a/preecej/semantic_wiki/prototyping/TermSearch_JSON.php b/preecej/semantic_wiki/prototyping/TermSearch_JSON.php new file mode 100644 index 0000000..acf7668 --- /dev/null +++ b/preecej/semantic_wiki/prototyping/TermSearch_JSON.php @@ -0,0 +1,92 @@ + 0 + ? strtolower($_GET['qval']) + : die('Please provide a searchable value'); + + // optional, for type 'lookup' + if ($type == 'lookup' && isset($_GET['ret_field']) && in_array($_GET['ret_field'],$arr_field_names)) { + $ret_field = $_GET['ret_field']; + } + + $format = strtolower($_GET['format']) != 'json' + ? strtolower($_GET['format']) + : 'json'; //json is the default + + /* connect to the db */ + $link = mysql_connect('floret.cgrb.oregonstate.edu', 'po-read-user', 'po-read-user_pw') or die('Cannot connect to the DB'); + mysql_select_db('po_beta',$link) or die('Cannot select the DB'); + + switch ($type) { + case 'autocomplete': + /* grab the terms from the db */ + $query = "select $field from term where $field like '%$qval%' ORDER BY name LIMIT $number_of_terms"; + $result = mysql_query($query,$link) or die('Errant query: '.$query); + + /* create one master array of the records */ + $terms = array(); + if(mysql_num_rows($result)) { + while($term = mysql_fetch_assoc($result)) { + $terms[] = array('title'=>$term[$field]); + } + } + + /* output in necessary format */ + if($format == 'json') { + header('Content-type: application/json'); + echo json_encode(array('sfautocomplete'=>$terms)); + } + else { + die('Sorry, this request cannot be fulfilled in '.$format.' format.'); + } + break; + + case 'lookup': + /* grab the terms from the db */ + $query = "select distinct $ret_field from term where $field = '$qval' ORDER BY name LIMIT 1"; + $result = mysql_query($query,$link) or die('Errant query: '.$query); + + /* create one master array of the records */ + $terms = array(); + if(mysql_num_rows($result)) { + while($term = mysql_fetch_assoc($result)) { + $terms[] = array('title'=>$term[$ret_field]); + } + } + // TODO: change this to the necessary output for #get_web_data wiki call + /* output in necessary format */ + if($format == 'json') { + header('Content-type: application/json'); + echo json_encode(array('sfautocomplete'=>$terms)); + } + else { + die('Sorry, this request cannot be fulfilled in '.$format.' format.'); + } + break; + default: + die('Sorry, this web service method is not available.'); + } + /* disconnect from the db */ + @mysql_close($link); +} +else { die('Not authorized.'); } +?> + -- 2.34.1