Browse Source

Initial JSON-LD web services for listing content types

Stephen Ficklin 9 years ago
parent
commit
350742450b

+ 8 - 22
tripal_chado/includes/tripal_chado.term_storage.inc

@@ -15,24 +15,6 @@ function tripal_chado_vocab_storage_info() {
 
 /**
  * Implements hook_vocab_get_term().
- *
- * This hooks is called by the tripal_entity module to retrieve information
- * about the term from the storage backend.  It must return an array with
- * a set of keys.
- *
- * @param $namespace
- *   The namespace of the vocabulary in which the term is found.
- * @param $accession
- *   The unique identifier (accession) for this term.
- *
- * @return
- *   An array with at least the following keys:
- *     namespace : The namespace of the vocabulary.
- *     accession : The name unique ID of the term.
- *     name : The name of the term.
- *     definition : The term's description.
- *   any other keys may be added as desired. Returns NULL if the term
- *   cannot be found.
  */
 function tripal_chado_vocab_get_term($namespace, $accession) {
   $match = array(
@@ -49,11 +31,15 @@ function tripal_chado_vocab_get_term($namespace, $accession) {
   }
   $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
   return array(
-    'namespace' => $cvterm->dbxref_id->db_id->name,
-    'accession' => $cvterm->dbxref_id->accession,
-    'name' => $cvterm->name,
+    'namespace'  => $cvterm->dbxref_id->db_id->name,
+    'accession'  => $cvterm->dbxref_id->accession,
+    'name'       => $cvterm->name,
     'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
-    'cvterm' => $cvterm,
+    'urlprefix'  => $cvterm->dbxref_id->db_id->urlprefix,
+    // The following are not required for the returned array but we'll
+    // add these for convenience later when we look at the TripalTerm
+    // objects and these will be there.
+    'cvterm'     => $cvterm,
   );
 }
 /**

+ 16 - 14
tripal_entities/api/tripal_entities.api.inc

@@ -311,7 +311,7 @@ function tripal_get_default_title_format($entity) {
 
   // Retrieve all available tokens.
   $tokens = tripal_get_tokens($entity);
-    
+
   // A) Check to see if more informed modules have suggested a title for this type.
   // Invoke hook_tripal_default_title_format() to get all suggestions from other modules.
   $suggestions = module_invoke_all('tripal_default_title_format', $entity, $tokens);
@@ -324,17 +324,17 @@ function tripal_get_default_title_format($entity) {
     }
     $format = $suggestions[$lightest_key]['format'];
   }
-  
+
   // B) Check to see if any fields contain "name" in the machine name and if so, use them.
   $name_fields = preg_grep('/name/', array_keys($tokens));
   if ($name_fields AND !$format) {
     $format = implode(', ', $name_fields);
   }
-  
+
   // C) Generate our own ugly title by simply comma-separating all the required fields.
   if (!$format) {
     $tmp = array();
-    
+
     // Check which tokens are required fields and join them into a default format.
     foreach($tokens as $token) {
       if ($token['required']) {
@@ -343,7 +343,7 @@ function tripal_get_default_title_format($entity) {
     }
     $format = implode(', ', $tmp);
   }
-  
+
   return $format;
 }
 
@@ -359,17 +359,17 @@ function tripal_get_default_title_format($entity) {
  *   An array of potential formats. The lightest weighted format suggested by all modules will be chosen.
  *   Each array item should consist of a 'weight' and 'format'. See the hook implementation below
  *   for examples.
- *    - weight: an integer used to determine priority of suggestions. 
+ *    - weight: an integer used to determine priority of suggestions.
  *        The smaller/lighter the number the higher the priority.
  *        Best practice is to use a weight less than 0 for extension modules.
  *        specifically, -2 is a good weight for calculated formats and -5 is a
  *        good weight for hard-coded formats specific to a given type.
- *    - format: a string including approved tokens used to determine the title 
+ *    - format: a string including approved tokens used to determine the title
  *        on Tripal content pages.
  */
 function hook_tripal_default_title_format($entity, $available_tokens) {
   $format = array();
-  
+
   // If you want to suggest a default format for a particular vocabulary term:
   //---------------------------------------------------------------------------
   // Load the term associated with this Tripal Content type.
@@ -418,7 +418,7 @@ function tripal_get_tokens($entity, $options = array()) {
   // Set default options.
   $options['required only'] = (isset($options['required only'])) ? $options['required only'] : FALSE;
   $options['include id'] = (isset($options['include id'])) ? $options['include id'] : TRUE;
-  
+
   if ($options['include id']) {
     $token = '[TripalBundle__bundle_id]';
     $tokens[$token] = array(
@@ -438,7 +438,7 @@ function tripal_get_tokens($entity, $options = array()) {
       'required' => TRUE
     );
   }
-  
+
   $fields = field_info_instances('TripalEntity', $entity->name);
   foreach ($fields as $f) {
 
@@ -451,7 +451,7 @@ function tripal_get_tokens($entity, $options = array()) {
       'field_name' => $f['field_name'],
       'required' => $f['required']
     );
-    
+
     // If the required only option is set then we only want to add
     // required fields to the token list.
     if ($options['required only'] AND $current_token['required']) {
@@ -482,7 +482,7 @@ function tripal_get_tokens($entity, $options = array()) {
  *   The string will all tokens replaced with values.
  */
 function tripal_replace_tokens($string, $entity, $bundle_entity = NULL) {
-  
+
   // Determine which tokens were used in the format string
   if (preg_match_all('/\[\w+\]/', $string, $matches)) {
     $used_tokens = $matches[0];
@@ -504,7 +504,7 @@ function tripal_replace_tokens($string, $entity, $bundle_entity = NULL) {
         }
       }
       elseif ($field === 'TripalBundle__bundle_id') {
-        
+
         // Load the bundle entity if we weren't given it.
         if (!$bundle_entity) {
           $bundle_entity = tripal_load_bundle_entity($entity->bundle);
@@ -615,8 +615,9 @@ function hook_vocab_select_term_form_validate($form, &$form_state) {
 
 }
 
+
 /**
- * Retreives information about a term from the default term storage backend.
+ * Hook used by the default term storage backend to provide details for a term.
  *
  * This hook is called by the tripal_entity module to retrieve information
  * about the term from the storage backend.  It must return an array with
@@ -631,6 +632,7 @@ function hook_vocab_select_term_form_validate($form, &$form_state) {
  *   An array with at least the following keys:
  *     namespace : The namespace of the vocabulary.
  *     accession : The name unique ID of the term.
+ *     url_prefix : The URL by which terms (accessions) can be appended.
  *     name : The name of the term.
  *     definition : The term's description.
  *   any other keys may be added as desired. Returns NULL if the term

+ 4 - 1
tripal_entities/includes/TripalTerm.inc

@@ -9,9 +9,11 @@ class TripalTerm extends Entity {
     // Get the vocabulary for this term
     $vocab = entity_load('TripalVocab', array('id' => $this->vocab_id));
     $vocab = reset($vocab);
+    $this->vocab = $vocab;
 
     // Get the term description from the storage backend
-    $this->definition = '';
+    $this->definition = NULL;
+    $this->urlprefix = NULL;
 
     // TODO: we need some sort of administrative interface that lets the user
     // switch to the desired vocabulary type. For now, we'll just use the
@@ -26,6 +28,7 @@ class TripalTerm extends Entity {
         $this->details = $term_details;
         if ($term_details and $term_details['definition']) {
           $this->definition = $term_details['definition'];
+          $this->urlprefix = $term_details['urlprefix'];
         }
       }
     }

+ 81 - 31
tripal_ws/includes/tripal_ws.rest.inc

@@ -6,58 +6,108 @@ function tripal_ws_rest() {
 
   // Set some initial variables.
   $response = array();
-  $result = array();
   $status = 'success';
   $version = 'v0.1';
   $message = '';
-  $api_url = "$base_url/ws/bio-data/$version";
+  $api_url = $base_url . '/ws/bio-data/' . $version;
   $page_limit = 25;
   $pager_id = 0;
 
+  $response['@context'] =  array(
+    'schema' => 'http://schema.org/',
+    'foaf' => 'http://xmlns.com/foaf/0.1/',
+    'dc' => 'http://purl.org/dc/elements/1.1/',
+  );
+  $response['@id'] = $api_url;
+
+  $namespace   = arg(3) ? arg(3) : '';
+  $bundle_name = arg(4) ? arg(4) : '';
+
   // Lump everything ito a try block so that if there is a problem we can
   // throw an error and have that returned in the response.
   try {
-
-    // Get the list of published terms (these are the bundle IDs)
-    $bundles = db_select('tripal_bundle', 'tb')
-      ->fields('tb')
-      ->execute();
-    $terms = array();
-    while ($bundle = $bundles->fetchObject()) {
-      $cvterm_id = preg_replace('/^bio-data_/', '', $bundle->name);
-      if ($cvterm_id) {
-        $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
-        $terms[$cvterm->name] = $cvterm;
-      }
+    if (!$namespace) {
+      tripal_ws_get_content_types($api_url, $response);
     }
-    ksort($terms, SORT_STRING|SORT_FLAG_CASE);
-    $result['content_types'] = array();
-    foreach ($terms as $name => $cvterm) {
-      $result['content_types'][$name] = array(
-        'vocabulary' => $cvterm->cv_id->name,
-        'namespace' => $cvterm->dbxref_id->db_id->name,
-      );
+    if ($namespace and !$bundle_name) {
+      tripal_ws_get_content_type($api_url, $response, $namespace);
     }
+
   }
   catch (Exception $e) {
     watchdog('tripal_ws', $e->getMessage(), array(), WATCHDOG_ERROR);
     $message = $e->getMessage();
     $status = 'error';
-    $result = array();
   }
 
-  // The responses follow the same format as the AGAVE API with a
-  // status, message, version and all data in the "result" object.
-  $response['status']  = $status;
+  // The responses follow a similar format as the AGAVE API with a
+  // status, message, version and all data in the 'result' object.
+/*   $response['status']  = $status;
   $response['message'] = $message;
-  $response['version'] = $version;
+  $response['api_version'] = $version;
   $response['source'] = array(
-    'site_name' => variable_get('site_name', "Unspecified"),
+    'site_name' => variable_get('site_name', 'Unspecified'),
     'site_url' => $base_url,
-    'site_slogan' => variable_get('site_slogan', "Unspecified"),
-    'site_email' =>  variable_get('site_mail', "Unspecified"),
-  );
-  $response['result']  = $result;
+    'site_slogan' => variable_get('site_slogan', 'Unspecified'),
+    'site_email' =>  variable_get('site_mail', 'Unspecified'),
+  ); */
   print drupal_json_output($response);
 }
+/**
+ *
+ * @param unknown $api_url
+ * @param unknown $response
+ */
+function tripal_ws_get_content_types($api_url, &$response) {
+  $response['@type'] = 'ItemList';
+  $response['schema:itemListOrder'] = 'ItemListOrderAscending';
+  $response['schema:numberOfItems'] = 0;
+  // Get the list of published terms (these are the bundle IDs)
+  $bundles = db_select('tripal_bundle', 'tb')
+    ->fields('tb')
+    ->orderBy('tb.label', 'ASC')
+    ->execute();
+  $terms = array();
+  while ($bundle = $bundles->fetchObject()) {
+    $entity =  entity_load('TripalTerm', array('id' => $bundle->term_id));
+    $terms[$bundle->label] = reset($entity);
+  }
+
+  $i = 0;
+  foreach ($terms as $name => $term) {
+    $vocab = $term->vocab;
+    if (!array_key_exists($vocab->namespace, $response['@context'])) {
+      // If there is no URL prefix then use this API's vocabulary API
+      if ($term->urlprefix) {
+        $response['@context'][$vocab->namespace] = $term->urlprefix;
+      }
+      else {
+        $response['@context'][$vocab->namespace] = $api_url . '/TODO/';
+      }
+    }
+    $response['schema:itemListElement'][] = array(
+      '@type' => 'schema:ListItem',
+      'schema:position' => $i + 1,
+      'schema:item' => array(
+        '@id' => $api_url . '/' . $term->vocab->namespace .'/' . $term->name,
+        '@type' => $vocab->namespace . ':' . $term->accession,
+        'foaf:name' => $term->name,
+        'dc:description' => $term->definition,
+      ),
+    );
+    $i++;
+  }
+  $response['schema:numberOfItems'] = $i;
+}
+
+/**
+ *
+ * @param unknown $api_url
+ * @param unknown $response
+ * @param unknown $vocab
+ * @param unknown $name
+ */
+function tripal_ws_get_content_type($api_url, &$response, $vocab, $name) {
+
+}
 

+ 1 - 1
tripal_ws/tripal_ws.info

@@ -10,5 +10,5 @@ dependencies[] = tripal_core
 dependencies[] = tripal_views
 dependencies[] = tripal_db
 dependencies[] = tripal_cv
-dependencies[] = tripal_fields
+dependencies[] = tripal_fields_layout
 dependencies[] = tripal_entities