Hello!

To see the file structure, click on "tree".

Note that updates take place every 10 minutes, commits may not be seen immediately.
Revised to make generically available via plantontology.org, also to add
authorpreecej <preecej@localhost>
Mon, 14 Nov 2011 20:22:05 +0000 (20:22 +0000)
committerpreecej <preecej@localhost>
Mon, 14 Nov 2011 20:22:05 +0000 (20:22 +0000)
synonym searching functionality

svn path=/; revision=214

Personnel/preecej/php_singletons/PO_web_service.php

index 9ba7afee62789f8d24d1069fd065ac317869b3ec..1317b541cb54913fb650c8ba219d7ffb09a23836 100644 (file)
 <?php
-// JSON web service for PO terms: autocomplete and search methods
+/*
+JSON web service for Plant Ontology terms
 
-/* require the user as the parameter */
-if(isset($_GET['user']) && ($_GET['user']) == 'paw') {
+Required parameters:
+    request_type
+    max
+Optional parameters:
+    app_key (for access to restricted methods)
+    format (JSON only for now)
 
-        $arr_field_names = array('name','acc');
-    
-        /* read the params and/or set our own */
-        $type = isset($_GET['type']) ? $_GET['type'] : autocomplete; // autocomplete is the default
-        
-        // security measures; helps to avoid SQL injection attacks
-        $field = isset($_GET['field']) && in_array($_GET['field'],$arr_field_names)
-            ? $_GET['field']
-            : die('"field" is a required parameter and must match an available data field.');
-            
-        $number_of_terms = isset($_GET['max']) ? intval($_GET['max']) : 10; //10 is the default
-        if ($number_of_terms > 50) { $number_of_terms = 50; } 
+Public Request Types:
+    term_search(req str search_value, opt bool inc_synonyms, 
+        opt bool inc_accessions, opt str branch_filter)
+    term_detail(req str accession_id)
 
-        $qval = $_GET['qval'];
-        
-        $qval = isset($_GET['qval']) && strlen($_GET['qval']) > 0 
-            ? strtolower($_GET['qval'])
-            : die('Please provide a searchable value');
+Restricted Request Types (requires user key):
+    wiki_autocomplete(req str search_value)
+    wiki_term_detail(req str accession_id || req str term_name)
+*/
 
-        $format = strtolower($_GET['format']) != 'json' 
-            ? strtolower($_GET['format']) 
-            : 'json'; //json is the default
-        
-        /* connect to the db */
-        $link = mysql_connect($_SERVER['mysql_host'], $_SERVER['mysql_user'], $_SERVER['mysql_pw']) or die('Cannot connect to the DB');
-        mysql_select_db($_SERVER['mysql_db'],$link) or die('Cannot select the DB');
+/* read the params and/or set our own */
+$request_type = isset($_GET['request_type'])
+                    ? $_GET['request_type'] 
+                    : die('Please provide a request type.');
 
-        switch ($type) {
-            case 'autocomplete':
-                /* grab the terms from the db */
-                $query = "SELECT t.$field FROM term t"
-                    . " LEFT JOIN term_definition d ON d.term_id = t.id"
-                    . " WHERE t.$field LIKE '%$qval%'"
-                    . " AND t.term_type in ('plant_anatomy','plant_growth_and_development_stage')"
-                    . " AND t.is_obsolete = 0"
-                    . " AND UCASE(t.name) NOT LIKE 'OBSOLETE%'"
-                    . " AND UCASE(d.term_definition) NOT LIKE 'OBSOLETE%'"
-                    . " 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('term'=>$term[$field]);
-                    }
-                }
+$number_of_terms = isset($_GET['max']) ? intval($_GET['max']) : 10; // default
+if ($number_of_terms > 50) { $number_of_terms = 50; } // ceiling
 
-                /* output in necessary format */
-                if($format == 'json') {
-                    header('Content-type: application/json');
-                    echo json_encode(array('PO_term_lookup_response'=>$terms));
-                }
-                else {
-                    die('Sorry, this request cannot be fulfilled in '.$format.' format.');
-                }
-                break;
+$format = isset($_GET['format']) && strtolower($_GET['format']) != 'json' 
+    ? strtolower($_GET['format']) 
+    : 'json'; //json is the default
 
-            case 'term_detail':
-                /* grab the ontology data from the db */
-                $query = "SELECT DISTINCT t.acc as 'acc', t.term_type as 'type', d.term_definition as 'definition', d.term_comment as 'comment'"
-                    . " FROM term t"
-                    . " LEFT JOIN term_definition d ON d.term_id = t.id"
-                    . " WHERE t.name = '$qval'"
-                    . " AND t.term_type in ('plant_anatomy','plant_growth_and_development_stage')"
-                    . " AND t.is_obsolete = 0"
-                    . " AND UCASE(t.name) NOT LIKE 'OBSOLETE%'"
-                    . " AND UCASE(d.term_definition) NOT LIKE 'OBSOLETE%'"
-                    . " ORDER BY t.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(
-                            'accession_id'=>$term['acc'],
-                            'aspect'=>$term['type'] == "plant_anatomy" ? "Plant Anatomy" : "Plant Growth and Development Stage",
-                            'definition'=>$term['definition'],
-                            'comment'=>$term['comment']);
-                    }
-                }
-                /* output in necessary format */
-                if($format == 'json') {
-                    header('Content-type: application/json');
-                    echo json_encode(array('PO_term_detail_response'=>$terms));
-                }
-                else {
-                    die('Sorry, this request cannot be fulfilled in '.$format.' format.');
+$inc_synonyms = isset($_GET['inc_synonyms']) ? 1 : 0; // default 
+
+/* connect to the db */
+$link = mysql_connect($_SERVER['dev_po_host'], $_SERVER['dev_po_user'], 
+    $_SERVER['dev_po_pw']) or die(mysql_error() . 'Cannot connect to the DB');
+mysql_select_db($_SERVER['dev_po_db'],$link) or die('Cannot select the DB');
+
+switch ($request_type) {
+
+    case 'term_search':
+        $search_value = $_GET['search_value'];
+        $search_value = isset($_GET['search_value']) && strlen($_GET['search_value']) > 0 
+            ? strtolower($_GET['search_value'])
+            : die('Please provide the "search_value" parameter.');
+
+        /* grab the terms from the db */
+        $query = "(SELECT t.name, 'term' as match_type, t.acc, null as parent_name"
+            . " FROM term t"
+            . " LEFT JOIN term_definition d ON d.term_id = t.id"
+            . " LEFT JOIN term_synonym s ON s.term_id = t.id"
+            . " WHERE t.name LIKE '%$search_value%'"
+            . " AND t.term_type in ('plant_anatomy','plant_growth_and_development_stage')"
+            . " AND t.is_obsolete = 0"
+            . " AND UCASE(t.name) NOT LIKE 'OBSOLETE%'"
+            . " AND UCASE(d.term_definition) NOT LIKE 'OBSOLETE%'"
+            . " GROUP BY t.id)";
+        if ($inc_synonyms) {
+            $query .= " UNION"
+                . " (SELECT ts.term_synonym as name, 'synonym' as match_type, t2.acc, t2.name as parent_name"
+                . " FROM term_synonym ts"
+                . " LEFT JOIN term t2 ON ts.term_id = t2.id"
+                . " LEFT JOIN term_definition d2 ON d2.term_id = t2.id"
+                . " WHERE ts.term_synonym LIKE '%$search_value%'"
+                . " AND t2.term_type in ('plant_anatomy','plant_growth_and_development_stage')"
+                . " AND t2.is_obsolete = 0"
+                . " AND UCASE(t2.name) NOT LIKE 'OBSOLETE%'"
+                . " AND UCASE(d2.term_definition) NOT LIKE 'OBSOLETE%')";
+        }
+        $query .= " ORDER BY name LIMIT $number_of_terms";
+        
+        $result = mysql_query($query,$link) or die('Errant query:  '.$query);
+    
+        /* create one master array of the records */
+        $term_matches = array();
+        if(mysql_num_rows($result)) {
+            while($term = mysql_fetch_assoc($result)) {
+                $ary_match = array(
+                    'match'=>$term['name'],
+                    'match_type'=>$term['match_type'],
+                    'accession_id'=>$term['acc']);
+                if ($term['match_type'] == 'synonym') {
+                    $ary_match['has_parent_term_name'] = $term['parent_name'];
                 }
-                break;
-            default:
-                die('Sorry, this web service method is not available.');
+                $term_matches[] = $ary_match;
+          }
+        }
+
+        /* output in necessary format */
+        if($format == 'json') {
+            header('Content-type: application/json');
+            echo json_encode(array('PO_term_search_response'=>$term_matches));
+        }
+        else {
+            die('Sorry, this request cannot be fulfilled in '.$format.' format.');
+        }
+        break;
+
+    case 'term_detail':
+        $accession_id = $_GET['accession_id'];
+        $accession_id = isset($_GET['accession_id']) && strlen($_GET['accession_id']) > 0 
+            ? $_GET['accession_id']
+            : die('Please provide the "accession_id" parameter.');
+
+        /* grab the ontology data from the db */
+        $query = "SELECT DISTINCT t.name, t.acc as 'acc', t.term_type as 'type'," 
+            . " d.term_definition as 'definition', d.term_comment as 'comment',"
+            . " group_concat(s.term_synonym separator ', ') as synonyms"
+            . " FROM term t"
+            . " LEFT JOIN term_definition d ON d.term_id = t.id"
+            . " LEFT JOIN term_synonym s ON s.term_id = t.id"
+            . " WHERE t.acc = '$accession_id'"
+            . " AND t.term_type in ('plant_anatomy','plant_growth_and_development_stage')"
+            . " AND t.is_obsolete = 0"
+            . " AND UCASE(t.name) NOT LIKE 'OBSOLETE%'"
+            . " AND UCASE(d.term_definition) NOT LIKE 'OBSOLETE%'"
+            . " GROUP BY t.id"
+            . " ORDER BY t.name LIMIT 1";
+        $result = mysql_query($query,$link) or die('Errant query:  '.$query);
+    
+        /* create one master array of the records */
+        $term_details = array();
+        if(mysql_num_rows($result)) {
+            while($term = mysql_fetch_assoc($result)) {
+                $term_details[] = array(
+                    'accession_id'=>$term['acc'],
+                    'name'=>$term['name'],
+                    'aspect'=>$term['type'] == "plant_anatomy" ? "Plant Anatomy" : "Plant Growth and Development Stage",
+                    'definition'=>$term['definition'],
+                    'comment'=>$term['comment'],
+                    'synonyms'=>preg_split("/, /",$term['synonyms'])
+                    );
+            }
+        }
+        /* output in necessary format */
+        if($format == 'json') {
+            header('Content-type: application/json');
+            echo json_encode(array('PO_term_detail_response'=>$term_details));
+        }
+        else {
+            die('Sorry, this request cannot be fulfilled in '.$format.' format.');
         }
-        /* disconnect from the db */
-        @mysql_close($link);
+        break;
+    default:
+        die('Sorry, this web service method is not available.');
 }
-else { die('Not authorized.'); }
+/* disconnect from the db */
+@mysql_close($link);
 ?>